1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Fixed declarations in C. Added more tests.

This commit is contained in:
Doug Schaefer 2005-04-18 18:30:27 +00:00
parent dbb59673ce
commit 03c02af46b
3 changed files with 62 additions and 14 deletions

View file

@ -11,15 +11,12 @@ import org.eclipse.cdt.core.dom.ast.ASTCompletionNode;
import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IFunction; import org.eclipse.cdt.core.dom.ast.IFunction;
import org.eclipse.cdt.core.dom.ast.ITypedef;
import org.eclipse.cdt.core.dom.ast.IVariable; import org.eclipse.cdt.core.dom.ast.IVariable;
public class BasicCompletionTest extends CompletionTestBase { public class BasicCompletionTest extends CompletionTestBase {
public void testVar() throws Exception { private void testVar(ASTCompletionNode node) throws Exception {
StringBuffer code = new StringBuffer();
code.append("int blah = 4;");
code.append("int two = bl");
ASTCompletionNode node = getGPPCompletionNode(code.toString());
IASTName[] names = node.getNames(); IASTName[] names = node.getNames();
assertEquals(1, names.length); assertEquals(1, names.length);
IBinding[] bindings = names[0].resolvePrefix(); IBinding[] bindings = names[0].resolvePrefix();
@ -27,23 +24,52 @@ public class BasicCompletionTest extends CompletionTestBase {
IVariable var = (IVariable)bindings[0]; IVariable var = (IVariable)bindings[0];
assertEquals("blah", var.getName()); assertEquals("blah", var.getName());
} }
public void testFunction() throws Exception { public void testVar() throws Exception {
StringBuffer code = new StringBuffer(); StringBuffer code = new StringBuffer();
code.append("void func(int x);"); code.append("int blah = 4;");
code.append("void func2() { fu"); code.append("int two = bl");
ASTCompletionNode node = getGPPCompletionNode(code.toString()); testVar(getGPPCompletionNode(code.toString()));
testVar(getGCCCompletionNode(code.toString()));
}
private void testFunction(ASTCompletionNode node) throws Exception {
IASTName[] names = node.getNames(); IASTName[] names = node.getNames();
// There are two names, one as an expression, one as a declaration // There are two names, one as an expression, one as a declaration
assertEquals(2, names.length); assertEquals(2, names.length);
// The declaration name is not hooked up to the TU due to backtrack // The expression points to our functions
assertNull(names[1].getTranslationUnit()); IBinding[] bindings = sortBindings(names[0].resolvePrefix());
// The points to our functions
IBinding[] bindings = names[0].resolvePrefix();
// There should be two since they both start with fu // There should be two since they both start with fu
assertEquals(2, bindings.length); assertEquals(2, bindings.length);
assertEquals("func", ((IFunction)bindings[0]).getName()); assertEquals("func", ((IFunction)bindings[0]).getName());
assertEquals("func2", ((IFunction)bindings[1]).getName()); assertEquals("func2", ((IFunction)bindings[1]).getName());
// The declaration should point to nothing since there are no types
bindings = names[1].resolvePrefix();
assertEquals(0, bindings.length);
}
public void testFunction() throws Exception {
StringBuffer code = new StringBuffer();
code.append("void func(int x) { }");
code.append("void func2() { fu");
testFunction(getGPPCompletionNode(code.toString()));
testFunction(getGCCCompletionNode(code.toString()));
} }
public void testTypedef(ASTCompletionNode node) {
IASTName[] names = node.getNames();
assertEquals(1, names.length);
IBinding[] bindings = names[0].resolvePrefix();
assertEquals(1, bindings.length);
assertEquals("blah", ((ITypedef)bindings[0]).getName());
}
public void testTypedef() throws Exception {
StringBuffer code = new StringBuffer();
code.append("typedef int blah;");
code.append("bl");
testTypedef(getGPPCompletionNode(code.toString()));
testTypedef(getGCCCompletionNode(code.toString()));
}
} }

View file

@ -7,9 +7,13 @@
**********************************************************************/ **********************************************************************/
package org.eclipse.cdt.core.parser.tests.prefix; package org.eclipse.cdt.core.parser.tests.prefix;
import java.util.Arrays;
import java.util.Comparator;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.eclipse.cdt.core.dom.ast.ASTCompletionNode; import org.eclipse.cdt.core.dom.ast.ASTCompletionNode;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.parser.CodeReader; import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.IParserLogService; import org.eclipse.cdt.core.parser.IParserLogService;
import org.eclipse.cdt.core.parser.IScanner; import org.eclipse.cdt.core.parser.IScanner;
@ -85,4 +89,18 @@ public class CompletionTestBase extends TestCase {
return getCompletionNode(code, ParserLanguage.C, true); return getCompletionNode(code, ParserLanguage.C, true);
} }
private static class BindingsComparator implements Comparator {
public int compare(Object o1, Object o2) {
IBinding b1 = (IBinding)o1;
IBinding b2 = (IBinding)o2;
return b1.getName().compareTo(b2.getName());
}
}
private static BindingsComparator bindingsComparator = new BindingsComparator();
protected IBinding[] sortBindings(IBinding[] bindings) {
Arrays.sort(bindings, bindingsComparator);
return bindings;
}
} }

View file

@ -564,6 +564,8 @@ public class GNUCSourceParser extends AbstractGNUSourceCodeParser {
int lastBacktrack = -1; int lastBacktrack = -1;
while (true) { while (true) {
try { try {
if (LT(1) == IToken.tEOC)
break;
int checkOffset = LA(1).hashCode(); int checkOffset = LA(1).hashCode();
IASTDeclaration d = declaration(); IASTDeclaration d = declaration();
d.setParent(translationUnit); d.setParent(translationUnit);
@ -1420,6 +1422,8 @@ public class GNUCSourceParser extends AbstractGNUSourceCodeParser {
break; break;
case IToken.tIDENTIFIER: case IToken.tIDENTIFIER:
case IToken.tCOMPLETION:
case IToken.tEOC:
// TODO - Kludgy way to handle constructors/destructors // TODO - Kludgy way to handle constructors/destructors
if (flags.haveEncounteredRawType()) { if (flags.haveEncounteredRawType()) {
break declSpecifiers; break declSpecifiers;