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:
parent
dbb59673ce
commit
03c02af46b
3 changed files with 62 additions and 14 deletions
|
@ -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.IBinding;
|
||||
import org.eclipse.cdt.core.dom.ast.IFunction;
|
||||
import org.eclipse.cdt.core.dom.ast.ITypedef;
|
||||
import org.eclipse.cdt.core.dom.ast.IVariable;
|
||||
|
||||
public class BasicCompletionTest extends CompletionTestBase {
|
||||
|
||||
public void testVar() throws Exception {
|
||||
StringBuffer code = new StringBuffer();
|
||||
code.append("int blah = 4;");
|
||||
code.append("int two = bl");
|
||||
ASTCompletionNode node = getGPPCompletionNode(code.toString());
|
||||
private void testVar(ASTCompletionNode node) throws Exception {
|
||||
IASTName[] names = node.getNames();
|
||||
assertEquals(1, names.length);
|
||||
IBinding[] bindings = names[0].resolvePrefix();
|
||||
|
@ -28,22 +25,51 @@ public class BasicCompletionTest extends CompletionTestBase {
|
|||
assertEquals("blah", var.getName());
|
||||
}
|
||||
|
||||
public void testFunction() throws Exception {
|
||||
public void testVar() throws Exception {
|
||||
StringBuffer code = new StringBuffer();
|
||||
code.append("void func(int x);");
|
||||
code.append("void func2() { fu");
|
||||
ASTCompletionNode node = getGPPCompletionNode(code.toString());
|
||||
code.append("int blah = 4;");
|
||||
code.append("int two = bl");
|
||||
testVar(getGPPCompletionNode(code.toString()));
|
||||
testVar(getGCCCompletionNode(code.toString()));
|
||||
}
|
||||
|
||||
private void testFunction(ASTCompletionNode node) throws Exception {
|
||||
IASTName[] names = node.getNames();
|
||||
// There are two names, one as an expression, one as a declaration
|
||||
assertEquals(2, names.length);
|
||||
// The declaration name is not hooked up to the TU due to backtrack
|
||||
assertNull(names[1].getTranslationUnit());
|
||||
// The points to our functions
|
||||
IBinding[] bindings = names[0].resolvePrefix();
|
||||
// The expression points to our functions
|
||||
IBinding[] bindings = sortBindings(names[0].resolvePrefix());
|
||||
// There should be two since they both start with fu
|
||||
assertEquals(2, bindings.length);
|
||||
assertEquals("func", ((IFunction)bindings[0]).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()));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -7,9 +7,13 @@
|
|||
**********************************************************************/
|
||||
package org.eclipse.cdt.core.parser.tests.prefix;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
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.IParserLogService;
|
||||
import org.eclipse.cdt.core.parser.IScanner;
|
||||
|
@ -85,4 +89,18 @@ public class CompletionTestBase extends TestCase {
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -564,6 +564,8 @@ public class GNUCSourceParser extends AbstractGNUSourceCodeParser {
|
|||
int lastBacktrack = -1;
|
||||
while (true) {
|
||||
try {
|
||||
if (LT(1) == IToken.tEOC)
|
||||
break;
|
||||
int checkOffset = LA(1).hashCode();
|
||||
IASTDeclaration d = declaration();
|
||||
d.setParent(translationUnit);
|
||||
|
@ -1420,6 +1422,8 @@ public class GNUCSourceParser extends AbstractGNUSourceCodeParser {
|
|||
break;
|
||||
|
||||
case IToken.tIDENTIFIER:
|
||||
case IToken.tCOMPLETION:
|
||||
case IToken.tEOC:
|
||||
// TODO - Kludgy way to handle constructors/destructors
|
||||
if (flags.haveEncounteredRawType()) {
|
||||
break declSpecifiers;
|
||||
|
|
Loading…
Add table
Reference in a new issue