1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-01 06:05:24 +02:00

Applied patch for Andrew Niefer.

CPPSemantics#findTypeBinding() will help us resolve ambiguities.
This commit is contained in:
John Camelon 2005-02-14 20:20:39 +00:00
parent b9742041fa
commit 8564247303
2 changed files with 1548 additions and 1402 deletions

View file

@ -13,6 +13,7 @@
*/
package org.eclipse.cdt.core.parser.tests.ast2;
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
@ -26,6 +27,7 @@ import org.eclipse.cdt.core.dom.ast.IASTLiteralExpression;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNamedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTStandardFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
import org.eclipse.cdt.core.dom.ast.IArrayType;
@ -45,6 +47,7 @@ import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.ITypedef;
import org.eclipse.cdt.core.dom.ast.IVariable;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPConstructor;
@ -56,6 +59,7 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPReferenceType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier.ICPPASTBaseSpecifier;
import org.eclipse.cdt.core.parser.ParserLanguage;
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPSemantics;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVisitor;
/**
@ -68,7 +72,8 @@ public class AST2CPPTests extends AST2BaseTest {
StringBuffer buffer = new StringBuffer("class A { } a;"); //$NON-NLS-1$
IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP);
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu.getDeclarations()[0];
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu
.getDeclarations()[0];
IASTCompositeTypeSpecifier compTypeSpec = (IASTCompositeTypeSpecifier) decl
.getDeclSpecifier();
IASTName name_A = compTypeSpec.getName();
@ -89,7 +94,8 @@ public class AST2CPPTests extends AST2BaseTest {
IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP);
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu.getDeclarations()[0];
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu
.getDeclarations()[0];
assertEquals(decl.getDeclarators().length, 0);
IASTElaboratedTypeSpecifier elabSpec = (IASTElaboratedTypeSpecifier) decl
.getDeclSpecifier();
@ -112,7 +118,8 @@ public class AST2CPPTests extends AST2BaseTest {
StringBuffer buffer = new StringBuffer("class A {}; A a;"); //$NON-NLS-1$
IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP);
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu.getDeclarations()[0];
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu
.getDeclarations()[0];
assertEquals(decl.getDeclarators().length, 0);
IASTCompositeTypeSpecifier compType = (IASTCompositeTypeSpecifier) decl
.getDeclSpecifier();
@ -140,7 +147,8 @@ public class AST2CPPTests extends AST2BaseTest {
StringBuffer buffer = new StringBuffer("class A { int f; };"); //$NON-NLS-1$
IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP);
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu.getDeclarations()[0];
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu
.getDeclarations()[0];
assertEquals(decl.getDeclarators().length, 0);
ICPPASTCompositeTypeSpecifier comp = (ICPPASTCompositeTypeSpecifier) decl
.getDeclSpecifier();
@ -162,7 +170,8 @@ public class AST2CPPTests extends AST2BaseTest {
StringBuffer buffer = new StringBuffer("class A { int f(); };"); //$NON-NLS-1$
IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP);
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu.getDeclarations()[0];
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu
.getDeclarations()[0];
assertEquals(decl.getDeclarators().length, 0);
IASTCompositeTypeSpecifier comp = (IASTCompositeTypeSpecifier) decl
.getDeclSpecifier();
@ -186,7 +195,8 @@ public class AST2CPPTests extends AST2BaseTest {
buffer.append(" void A::f() { } \n"); //$NON-NLS-1$
IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP);
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu.getDeclarations()[0];
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu
.getDeclarations()[0];
assertEquals(decl.getDeclarators().length, 0);
IASTCompositeTypeSpecifier comp = (IASTCompositeTypeSpecifier) decl
.getDeclSpecifier();
@ -227,7 +237,8 @@ public class AST2CPPTests extends AST2BaseTest {
IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP);
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu.getDeclarations()[0];
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu
.getDeclarations()[0];
assertEquals(decl.getDeclarators().length, 0);
IASTCompositeTypeSpecifier comp = (IASTCompositeTypeSpecifier) decl
.getDeclSpecifier();
@ -283,7 +294,8 @@ public class AST2CPPTests extends AST2BaseTest {
IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP);
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu.getDeclarations()[0];
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu
.getDeclarations()[0];
ICPPASTCompositeTypeSpecifier comp = (ICPPASTCompositeTypeSpecifier) decl
.getDeclSpecifier();
IASTName name_A1 = comp.getName();
@ -303,8 +315,8 @@ public class AST2CPPTests extends AST2BaseTest {
IASTFunctionDefinition def = (IASTFunctionDefinition) tu
.getDeclarations()[2];
ICPPASTQualifiedName name_f2 = (ICPPASTQualifiedName) def.getDeclarator()
.getName();
ICPPASTQualifiedName name_f2 = (ICPPASTQualifiedName) def
.getDeclarator().getName();
IASTName name_B2 = name_f2.getNames()[0];
IASTName name_f3 = name_f2.getNames()[1];
@ -367,7 +379,8 @@ public class AST2CPPTests extends AST2BaseTest {
IVariable a = (IVariable) collector.getName(1).resolveBinding();
ICPPNamespace B = (ICPPNamespace) collector.getName(2).resolveBinding();
ICPPNamespace C = (ICPPNamespace) collector.getName(4).resolveBinding();
ICPPNamespace BC = (ICPPNamespace) collector.getName(6).resolveBinding();
ICPPNamespace BC = (ICPPNamespace) collector.getName(6)
.resolveBinding();
IFunction f = (IFunction) collector.getName(9).resolveBinding();
assertInstances(collector, A, 3);
assertInstances(collector, a, 3);
@ -416,9 +429,11 @@ public class AST2CPPTests extends AST2BaseTest {
CPPVisitor.visitTranslationUnit(tu, collector);
assertEquals(collector.size(), 9);
ICompositeType A = (ICompositeType) collector.getName(0).resolveBinding();
ICompositeType A = (ICompositeType) collector.getName(0)
.resolveBinding();
ICPPMethod f = (ICPPMethod) collector.getName(1).resolveBinding();
ICompositeType B = (ICompositeType) collector.getName(2).resolveBinding();
ICompositeType B = (ICompositeType) collector.getName(2)
.resolveBinding();
IVariable b = (IVariable) collector.getName(7).resolveBinding();
IVariable B2 = (IVariable) collector.getName(8).resolveBinding();
@ -488,8 +503,10 @@ public class AST2CPPTests extends AST2BaseTest {
CPPNameCollector collector = new CPPNameCollector();
CPPVisitor.visitTranslationUnit(tu, collector);
ICPPClassType A1 = (ICPPClassType) collector.getName(0).resolveBinding();
ICPPClassType A2 = (ICPPClassType) collector.getName(2).resolveBinding();
ICPPClassType A1 = (ICPPClassType) collector.getName(0)
.resolveBinding();
ICPPClassType A2 = (ICPPClassType) collector.getName(2)
.resolveBinding();
IVariable a = (IVariable) collector.getName(4).resolveBinding();
assertNotNull(a);
@ -511,8 +528,10 @@ public class AST2CPPTests extends AST2BaseTest {
CPPNameCollector collector = new CPPNameCollector();
CPPVisitor.visitTranslationUnit(tu, collector);
ICPPClassType A1 = (ICPPClassType) collector.getName(0).resolveBinding();
ICPPClassType A2 = (ICPPClassType) collector.getName(2).resolveBinding();
ICPPClassType A1 = (ICPPClassType) collector.getName(0)
.resolveBinding();
ICPPClassType A2 = (ICPPClassType) collector.getName(2)
.resolveBinding();
IVariable a = (IVariable) collector.getName(3).resolveBinding();
assertNotNull(a);
@ -535,7 +554,8 @@ public class AST2CPPTests extends AST2BaseTest {
CPPNameCollector collector = new CPPNameCollector();
CPPVisitor.visitTranslationUnit(tu, collector);
ICPPClassType A1 = (ICPPClassType) collector.getName(0).resolveBinding();
ICPPClassType A1 = (ICPPClassType) collector.getName(0)
.resolveBinding();
IVariable a = (IVariable) collector.getName(2).resolveBinding();
ICPPField i = (ICPPField) collector.getName(4).resolveBinding();
@ -674,7 +694,8 @@ public class AST2CPPTests extends AST2BaseTest {
IPointerType pt = (IPointerType) pf.getType();
assertTrue(pt.getType() instanceof IFunctionType);
tu = parse("struct A; int (*pfi)( int, struct A * );", ParserLanguage.CPP); //$NON-NLS-1$
tu = parse(
"struct A; int (*pfi)( int, struct A * );", ParserLanguage.CPP); //$NON-NLS-1$
collector = new CPPNameCollector();
CPPVisitor.visitTranslationUnit(tu, collector);
ICPPClassType A = (ICPPClassType) collector.getName(0).resolveBinding();
@ -698,7 +719,8 @@ public class AST2CPPTests extends AST2BaseTest {
IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP);
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu.getDeclarations()[0];
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu
.getDeclarations()[0];
IASTElaboratedTypeSpecifier elabSpec = (IASTElaboratedTypeSpecifier) decl
.getDeclSpecifier();
ICompositeType A = (ICompositeType) elabSpec.getName().resolveBinding();
@ -708,12 +730,13 @@ public class AST2CPPTests extends AST2BaseTest {
.resolveBinding();
decl = (IASTSimpleDeclaration) tu.getDeclarations()[2];
IVariable g = (IVariable) decl.getDeclarators()[0].getNestedDeclarator()
.getName().resolveBinding();
IVariable g = (IVariable) decl.getDeclarators()[0]
.getNestedDeclarator().getName().resolveBinding();
decl = (IASTSimpleDeclaration) tu.getDeclarations()[3];
IVariable h = (IVariable) decl.getDeclarators()[0].getNestedDeclarator()
.getNestedDeclarator().getName().resolveBinding();
IVariable h = (IVariable) decl.getDeclarators()[0]
.getNestedDeclarator().getNestedDeclarator().getName()
.resolveBinding();
IFunctionType t_f = f.getType();
IType t_f_return = t_f.getReturnType();
@ -739,7 +762,8 @@ public class AST2CPPTests extends AST2BaseTest {
assertSame(((IPointerType) t_g_func_p1).getType(), A);
//h is a pointer to a function that returns a pointer to a function
//the returned pointer to function returns void and takes 1 parameter int
//the returned pointer to function returns void and takes 1 parameter
// int
// the *h function takes 1 parameter struct A**
IType t_h = h.getType();
assertTrue(t_h instanceof IPointerType);
@ -949,7 +973,8 @@ public class AST2CPPTests extends AST2BaseTest {
IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP);
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu.getDeclarations()[0];
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu
.getDeclarations()[0];
IASTCompositeTypeSpecifier compSpec = (IASTCompositeTypeSpecifier) decl
.getDeclSpecifier();
ICPPClassType A = (ICPPClassType) compSpec.getName().resolveBinding();
@ -975,7 +1000,8 @@ public class AST2CPPTests extends AST2BaseTest {
IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP);
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu.getDeclarations()[0];
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu
.getDeclarations()[0];
IASTCompositeTypeSpecifier compSpec = (IASTCompositeTypeSpecifier) decl
.getDeclSpecifier();
ICPPClassType A = (ICPPClassType) compSpec.getName().resolveBinding();
@ -1013,10 +1039,13 @@ public class AST2CPPTests extends AST2BaseTest {
assertInstances(col, x, 3);
}
// public void testBug84250() throws Exception {
// assertTrue(((IASTDeclarationStatement) ((IASTCompoundStatement) ((IASTFunctionDefinition) parse(
// "void f() { int (*p) [2]; }", ParserLanguage.CPP).getDeclarations()[0]).getBody()).getStatements()[0]).getDeclaration() instanceof IASTSimpleDeclaration); //$NON-NLS-1$
// }
// public void testBug84250() throws Exception {
// assertTrue(((IASTDeclarationStatement) ((IASTCompoundStatement)
// ((IASTFunctionDefinition) parse(
// "void f() { int (*p) [2]; }",
// ParserLanguage.CPP).getDeclarations()[0]).getBody()).getStatements()[0]).getDeclaration()
// instanceof IASTSimpleDeclaration); //$NON-NLS-1$
// }
public void testBug84250() throws Exception {
StringBuffer buffer = new StringBuffer();
@ -1070,7 +1099,8 @@ public class AST2CPPTests extends AST2BaseTest {
assertEquals(col.size(), 7);
ICompositeType s_ref = (ICompositeType) col.getName(4).resolveBinding();
ICompositeType s_decl = (ICompositeType) col.getName(0).resolveBinding();
ICompositeType s_decl = (ICompositeType) col.getName(0)
.resolveBinding();
assertSame(s_ref, s_decl);
}
@ -1141,7 +1171,8 @@ public class AST2CPPTests extends AST2BaseTest {
ICPPField n = (ICPPField) col.getName(1).resolveBinding();
IBinding Aref = col.getName(5).resolveBinding();
IBinding nref = col.getName(6).resolveBinding();
IProblemBinding prob = (IProblemBinding) col.getName(7).resolveBinding();
IProblemBinding prob = (IProblemBinding) col.getName(7)
.resolveBinding();
assertSame(A, Aref);
assertSame(n, nref);
@ -1150,24 +1181,29 @@ public class AST2CPPTests extends AST2BaseTest {
public void testBug84371() throws Exception {
String code = "int x = ::ABC::DEF::ghi;"; //$NON-NLS-1$
IASTTranslationUnit tu = parse( code, ParserLanguage.CPP );
IASTTranslationUnit tu = parse(code, ParserLanguage.CPP);
IASTSimpleDeclaration x = (IASTSimpleDeclaration) tu.getDeclarations()[0];
IASTInitializerExpression e = (IASTInitializerExpression) x.getDeclarators()[0].getInitializer();
IASTInitializerExpression e = (IASTInitializerExpression) x
.getDeclarators()[0].getInitializer();
IASTIdExpression id = (IASTIdExpression) e.getExpression();
ICPPASTQualifiedName name = (ICPPASTQualifiedName) id.getName();
assertTrue( name.isFullyQualified() );
assertEquals( name.getNames().length, 3 );
assertEquals( name.getNames()[0].toString(), "ABC" ); //$NON-NLS-1$
assertEquals( name.getNames()[1].toString(), "DEF" ); //$NON-NLS-1$
assertEquals( name.getNames()[2].toString(), "ghi" ); //$NON-NLS-1$
assertTrue(name.isFullyQualified());
assertEquals(name.getNames().length, 3);
assertEquals(name.getNames()[0].toString(), "ABC"); //$NON-NLS-1$
assertEquals(name.getNames()[1].toString(), "DEF"); //$NON-NLS-1$
assertEquals(name.getNames()[2].toString(), "ghi"); //$NON-NLS-1$
}
public void testBug84679() throws Exception {
StringBuffer buffer = new StringBuffer();
buffer.append("namespace Y { void f(float); } \n"); //$NON-NLS-1$
buffer.append("namespace A { using namespace Y; f(int); } \n"); //$NON-NLS-1$
buffer.append("namespace B { void f(char); } \n"); //$NON-NLS-1$
buffer.append("namespace AB { using namespace A; using namespace B; } \n"); //$NON-NLS-1$
buffer
.append("namespace Y { void f(float); } \n"); //$NON-NLS-1$
buffer
.append("namespace A { using namespace Y; f(int); } \n"); //$NON-NLS-1$
buffer
.append("namespace B { void f(char); } \n"); //$NON-NLS-1$
buffer
.append("namespace AB { using namespace A; using namespace B; } \n"); //$NON-NLS-1$
buffer.append("void h(){ \n"); //$NON-NLS-1$
buffer.append(" AB::f(1); \n"); //$NON-NLS-1$
buffer.append(" AB::f(’c’); \n"); //$NON-NLS-1$
@ -1185,12 +1221,12 @@ public class AST2CPPTests extends AST2BaseTest {
IFunction f = (IFunction) col.getName(16).resolveBinding();
IFunction fdef = (IFunction) col.getName(5).resolveBinding();
IProblemBinding f2 = (IProblemBinding) col.getName(19).resolveBinding();
assertSame( f, fdef );
assertEquals( IProblemBinding.SEMANTIC_NAME_NOT_FOUND, f2.getID() );
assertInstances( col, Y, 2 );
assertInstances( col, A, 2 );
assertInstances( col, B, 2 );
assertInstances( col, AB, 3 );
assertSame(f, fdef);
assertEquals(IProblemBinding.SEMANTIC_NAME_NOT_FOUND, f2.getID());
assertInstances(col, Y, 2);
assertInstances(col, A, 2);
assertInstances(col, B, 2);
assertInstances(col, AB, 3);
}
public void testBug84692() throws Exception {
@ -1213,8 +1249,8 @@ public class AST2CPPTests extends AST2BaseTest {
ICPPClassType Node = (ICPPClassType) col.getName(1).resolveBinding();
ICPPClassType Data = (ICPPClassType) col.getName(3).resolveBinding();
assertInstances( col, Node, 3 );
assertInstances( col, Data, 2 );
assertInstances(col, Node, 3);
assertInstances(col, Data, 2);
}
public void testBug84686() throws Exception {
@ -1232,22 +1268,23 @@ public class AST2CPPTests extends AST2BaseTest {
IVariable a1 = (IVariable) col.getName(4).resolveBinding();
IVariable a2 = (IVariable) col.getName(10).resolveBinding();
assertSame( a1, a2 );
assertSame(a1, a2);
}
public void testBug84705() throws Exception {
StringBuffer buffer = new StringBuffer();
buffer.append( "struct C { \n" ); //$NON-NLS-1$
buffer.append( " void f(); \n" ); //$NON-NLS-1$
buffer.append( " const C& operator=( const C& ); \n" ); //$NON-NLS-1$
buffer.append( "}; \n" ); //$NON-NLS-1$
buffer.append( "const C& C::operator=( const C& other) { \n" ); //$NON-NLS-1$
buffer.append( " if( this != &other ) { \n" ); //$NON-NLS-1$
buffer.append( " this->~C(); \n" ); //$NON-NLS-1$
buffer.append( " new (this) C(other ); \n" ); //$NON-NLS-1$
buffer.append( " f(); \n" ); //$NON-NLS-1$
buffer.append( " } \n" ); //$NON-NLS-1$
buffer.append( " return *this; \n" ); //$NON-NLS-1$
buffer.append( "} \n" ); //$NON-NLS-1$
buffer.append("struct C { \n"); //$NON-NLS-1$
buffer.append(" void f(); \n"); //$NON-NLS-1$
buffer.append(" const C& operator=( const C& ); \n"); //$NON-NLS-1$
buffer.append("}; \n"); //$NON-NLS-1$
buffer.append("const C& C::operator=( const C& other) { \n"); //$NON-NLS-1$
buffer.append(" if( this != &other ) { \n"); //$NON-NLS-1$
buffer.append(" this->~C(); \n"); //$NON-NLS-1$
buffer.append(" new (this) C(other ); \n"); //$NON-NLS-1$
buffer.append(" f(); \n"); //$NON-NLS-1$
buffer.append(" } \n"); //$NON-NLS-1$
buffer.append(" return *this; \n"); //$NON-NLS-1$
buffer.append("} \n"); //$NON-NLS-1$
IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP);
CPPNameCollector col = new CPPNameCollector();
@ -1256,60 +1293,67 @@ public class AST2CPPTests extends AST2BaseTest {
assertEquals(col.size(), 17);
ICPPMethod f = (ICPPMethod) col.getName(1).resolveBinding();
IASTName [] refs = tu.getReferences( f );
assertEquals( 1, refs.length );
assertSame( f, refs[0].resolveBinding() );
IASTName[] refs = tu.getReferences(f);
assertEquals(1, refs.length);
assertSame(f, refs[0].resolveBinding());
ICPPClassType C = (ICPPClassType) col.getName(0).resolveBinding();
ICPPMethod op = (ICPPMethod) col.getName(3).resolveBinding();
IParameter other = (IParameter) col.getName(11).resolveBinding();
ICPPMethod dtor = (ICPPMethod) col.getName(13).resolveBinding();
assertNotNull( dtor );
assertEquals( dtor.getName(), "~C" ); //$NON-NLS-1$
assertInstances( col, C, 6 );
assertNotNull(dtor);
assertEquals(dtor.getName(), "~C"); //$NON-NLS-1$
assertInstances(col, C, 6);
assertInstances( col, op, 3 );
assertInstances( col, other, 4 );
assertInstances(col, op, 3);
assertInstances(col, other, 4);
}
public void testThis() throws Exception {
StringBuffer buffer = new StringBuffer();
buffer.append( "class A { void f(); void g() const; }; \n" ); //$NON-NLS-1$
buffer.append( "void A::f(){ this; } \n" ); //$NON-NLS-1$
buffer.append( "void A::g() const { *this; } \n" ); //$NON-NLS-1$
buffer.append("class A { void f(); void g() const; }; \n"); //$NON-NLS-1$
buffer.append("void A::f(){ this; } \n"); //$NON-NLS-1$
buffer.append("void A::g() const { *this; } \n"); //$NON-NLS-1$
IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP);
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu.getDeclarations()[0];
ICPPClassType A = (ICPPClassType) ((IASTCompositeTypeSpecifier)decl.getDeclSpecifier()).getName().resolveBinding();
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu
.getDeclarations()[0];
ICPPClassType A = (ICPPClassType) ((IASTCompositeTypeSpecifier) decl
.getDeclSpecifier()).getName().resolveBinding();
IASTFunctionDefinition def = (IASTFunctionDefinition) tu.getDeclarations()[1];
IASTExpressionStatement expStatement = (IASTExpressionStatement) ((IASTCompoundStatement)def.getBody()).getStatements()[0];
assertTrue( expStatement.getExpression() instanceof IASTLiteralExpression );
IType type = CPPVisitor.getExpressionType( expStatement.getExpression() );
IASTFunctionDefinition def = (IASTFunctionDefinition) tu
.getDeclarations()[1];
IASTExpressionStatement expStatement = (IASTExpressionStatement) ((IASTCompoundStatement) def
.getBody()).getStatements()[0];
assertTrue(expStatement.getExpression() instanceof IASTLiteralExpression);
IType type = CPPVisitor.getExpressionType(expStatement.getExpression());
assertTrue( type instanceof IPointerType );
assertSame( ((IPointerType) type).getType(), A );
assertTrue(type instanceof IPointerType);
assertSame(((IPointerType) type).getType(), A);
def = (IASTFunctionDefinition) tu.getDeclarations()[2];
expStatement = (IASTExpressionStatement) ((IASTCompoundStatement)def.getBody()).getStatements()[0];
IASTUnaryExpression ue = (IASTUnaryExpression) expStatement.getExpression();
type = CPPVisitor.getExpressionType( ue );
expStatement = (IASTExpressionStatement) ((IASTCompoundStatement) def
.getBody()).getStatements()[0];
IASTUnaryExpression ue = (IASTUnaryExpression) expStatement
.getExpression();
type = CPPVisitor.getExpressionType(ue);
//when 84749 is fixed, remove this assert and uncomment below.
assertSame( type, A );
// assertTrue( type instanceof IQualifierType );
// assertSame( ((IQualifierType) type).getType(), A );
// assertTrue( ((IQualifierType) type).isConst() );
assertSame(type, A);
// assertTrue( type instanceof IQualifierType );
// assertSame( ((IQualifierType) type).getType(), A );
// assertTrue( ((IQualifierType) type).isConst() );
}
public void testBug84710() throws Exception {
IASTTranslationUnit tu = parse( "class T { T(); };", ParserLanguage.CPP ); //$NON-NLS-1$
IASTTranslationUnit tu = parse("class T { T(); };", ParserLanguage.CPP); //$NON-NLS-1$
CPPNameCollector col = new CPPNameCollector();
CPPVisitor.visitTranslationUnit(tu, col);
ICPPConstructor T = (ICPPConstructor) col.getName(1).resolveBinding();
assertTrue( CharArrayUtils.equals( T.getNameCharArray(), "T".toCharArray() ) ) ; //$NON-NLS-1$
assertEquals( T.getName(), "T" ); //$NON-NLS-1$
assertTrue(CharArrayUtils.equals(T.getNameCharArray(),
"T".toCharArray())); //$NON-NLS-1$
assertEquals(T.getName(), "T"); //$NON-NLS-1$
}
public void testArgumentDependantLookup() throws Exception {
@ -1323,7 +1367,7 @@ public class AST2CPPTests extends AST2BaseTest {
buffer.append(" f( parm ); \n"); //$NON-NLS-1$
buffer.append("} \n"); //$NON-NLS-1$
IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.CPP ); //$NON-NLS-1$
IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP); //$NON-NLS-1$
CPPNameCollector col = new CPPNameCollector();
CPPVisitor.visitTranslationUnit(tu, col);
@ -1332,28 +1376,28 @@ public class AST2CPPTests extends AST2BaseTest {
IFunction f = (IFunction) col.getName(2).resolveBinding();
IVariable parm = (IVariable) col.getName(8).resolveBinding();
assertInstances( col, NS, 2 );
assertInstances( col, T, 4 );
assertInstances( col, f, 2 );
assertInstances( col, parm, 2 );
assertInstances(col, NS, 2);
assertInstances(col, T, 4);
assertInstances(col, f, 2);
assertInstances(col, parm, 2);
}
public void testArgumentDependantLookup_2() throws Exception {
StringBuffer buffer = new StringBuffer();
buffer.append("namespace NS1{ \n" ); //$NON-NLS-1$
buffer.append(" void f( void * ); \n" ); //$NON-NLS-1$
buffer.append("} \n" ); //$NON-NLS-1$
buffer.append("namespace NS2{ \n" ); //$NON-NLS-1$
buffer.append(" using namespace NS1; \n" ); //$NON-NLS-1$
buffer.append(" class B {}; \n" ); //$NON-NLS-1$
buffer.append(" void f( void * ); \n" ); //$NON-NLS-1$
buffer.append("} \n" ); //$NON-NLS-1$
buffer.append("class A : public NS2::B {} *a; \n" ); //$NON-NLS-1$
buffer.append("int main() { \n" ); //$NON-NLS-1$
buffer.append(" f( a ); \n" ); //$NON-NLS-1$
buffer.append("} \n" ); //$NON-NLS-1$
buffer.append("namespace NS1{ \n"); //$NON-NLS-1$
buffer.append(" void f( void * ); \n"); //$NON-NLS-1$
buffer.append("} \n"); //$NON-NLS-1$
buffer.append("namespace NS2{ \n"); //$NON-NLS-1$
buffer.append(" using namespace NS1; \n"); //$NON-NLS-1$
buffer.append(" class B {}; \n"); //$NON-NLS-1$
buffer.append(" void f( void * ); \n"); //$NON-NLS-1$
buffer.append("} \n"); //$NON-NLS-1$
buffer.append("class A : public NS2::B {} *a; \n"); //$NON-NLS-1$
buffer.append("int main() { \n"); //$NON-NLS-1$
buffer.append(" f( a ); \n"); //$NON-NLS-1$
buffer.append("} \n"); //$NON-NLS-1$
IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.CPP );
IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP);
CPPNameCollector col = new CPPNameCollector();
CPPVisitor.visitTranslationUnit(tu, col);
@ -1361,70 +1405,71 @@ public class AST2CPPTests extends AST2BaseTest {
IFunction f1 = (IFunction) col.getName(1).resolveBinding();
IFunction f2 = (IFunction) col.getName(6).resolveBinding();
assertSame( f2, fref );
assertNotNull( f1 );
assertNotNull( f2 );
assertSame(f2, fref);
assertNotNull(f1);
assertNotNull(f2);
}
public void testBug84610() throws Exception {
StringBuffer buffer = new StringBuffer();
buffer.append("namespace { int i; } //1\n" ); //$NON-NLS-1$
buffer.append("void f(){ i; } \n" ); //$NON-NLS-1$
buffer.append("namespace A { \n" ); //$NON-NLS-1$
buffer.append(" namespace { \n" ); //$NON-NLS-1$
buffer.append(" int i; //2 \n" ); //$NON-NLS-1$
buffer.append(" int j; \n" ); //$NON-NLS-1$
buffer.append(" } \n" ); //$NON-NLS-1$
buffer.append(" void g(){ i; } \n" ); //$NON-NLS-1$
buffer.append("} \n" ); //$NON-NLS-1$
buffer.append("using namespace A; \n" ); //$NON-NLS-1$
buffer.append("void h() { \n" ); //$NON-NLS-1$
buffer.append(" i; //ambiguous \n" ); //$NON-NLS-1$
buffer.append(" A::i; //i2 \n" ); //$NON-NLS-1$
buffer.append(" j; \n" ); //$NON-NLS-1$
buffer.append("} \n" ); //$NON-NLS-1$
buffer.append("namespace { int i; } //1\n"); //$NON-NLS-1$
buffer.append("void f(){ i; } \n"); //$NON-NLS-1$
buffer.append("namespace A { \n"); //$NON-NLS-1$
buffer.append(" namespace { \n"); //$NON-NLS-1$
buffer.append(" int i; //2 \n"); //$NON-NLS-1$
buffer.append(" int j; \n"); //$NON-NLS-1$
buffer.append(" } \n"); //$NON-NLS-1$
buffer.append(" void g(){ i; } \n"); //$NON-NLS-1$
buffer.append("} \n"); //$NON-NLS-1$
buffer.append("using namespace A; \n"); //$NON-NLS-1$
buffer.append("void h() { \n"); //$NON-NLS-1$
buffer.append(" i; //ambiguous \n"); //$NON-NLS-1$
buffer.append(" A::i; //i2 \n"); //$NON-NLS-1$
buffer.append(" j; \n"); //$NON-NLS-1$
buffer.append("} \n"); //$NON-NLS-1$
IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.CPP );
IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP);
CPPNameCollector col = new CPPNameCollector();
CPPVisitor.visitTranslationUnit(tu, col);
assertEquals( 17, col.size() );
assertEquals(17, col.size());
IVariable i1 = (IVariable) col.getName(1).resolveBinding();
IVariable i2 = (IVariable) col.getName(6).resolveBinding();
IVariable j = (IVariable) col.getName(7).resolveBinding();
assertInstances( col, i1, 2 );
assertInstances( col, i2, 4 );
assertInstances( col, j, 2 );
assertInstances(col, i1, 2);
assertInstances(col, i2, 4);
assertInstances(col, j, 2);
IProblemBinding problem = (IProblemBinding) col.getName(12).resolveBinding();
assertEquals( IProblemBinding.SEMANTIC_AMBIGUOUS_LOOKUP, problem.getID() );
IProblemBinding problem = (IProblemBinding) col.getName(12)
.resolveBinding();
assertEquals(IProblemBinding.SEMANTIC_AMBIGUOUS_LOOKUP, problem.getID());
}
public void testBug84703() throws Exception {
StringBuffer buffer = new StringBuffer();
buffer.append("struct B { \n" ); //$NON-NLS-1$
buffer.append(" void mutate(); \n" ); //$NON-NLS-1$
buffer.append("}; \n" ); //$NON-NLS-1$
buffer.append("void g() { \n" ); //$NON-NLS-1$
buffer.append(" B* pb = new B(); \n" ); //$NON-NLS-1$
buffer.append(" pb->mutate(); \n" ); //$NON-NLS-1$
buffer.append("} \n" ); //$NON-NLS-1$
buffer.append("struct B { \n"); //$NON-NLS-1$
buffer.append(" void mutate(); \n"); //$NON-NLS-1$
buffer.append("}; \n"); //$NON-NLS-1$
buffer.append("void g() { \n"); //$NON-NLS-1$
buffer.append(" B* pb = new B(); \n"); //$NON-NLS-1$
buffer.append(" pb->mutate(); \n"); //$NON-NLS-1$
buffer.append("} \n"); //$NON-NLS-1$
IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.CPP );
IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP);
CPPNameCollector col = new CPPNameCollector();
CPPVisitor.visitTranslationUnit(tu, col);
assertEquals( 8, col.size() );
assertEquals(8, col.size());
ICPPMethod mutate = (ICPPMethod) col.getName(1).resolveBinding();
ICPPClassType B = (ICPPClassType) col.getName(0).resolveBinding();
IVariable pb = (IVariable) col.getName(4).resolveBinding();
assertInstances( col, pb, 2 );
assertInstances( col, mutate, 2 );
assertInstances( col, B, 2 );
assertInstances(col, pb, 2);
assertInstances(col, mutate, 2);
assertInstances(col, B, 2);
}
public void testBug84469() throws Exception {
@ -1434,29 +1479,30 @@ public class AST2CPPTests extends AST2BaseTest {
buffer.append(" int S::* pm = &S::i; \n"); //$NON-NLS-1$
buffer.append("} \n"); //$NON-NLS-1$
IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.CPP );
IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP);
CPPNameCollector col = new CPPNameCollector();
CPPVisitor.visitTranslationUnit(tu, col);
assertEquals( 9, col.size() );
assertEquals(9, col.size());
}
public void testPointerToMemberType() throws Exception {
IASTTranslationUnit tu = parse( "struct S; int S::* pm;", ParserLanguage.CPP );
IASTTranslationUnit tu = parse("struct S; int S::* pm;", //$NON-NLS-1$
ParserLanguage.CPP);
CPPNameCollector col = new CPPNameCollector();
CPPVisitor.visitTranslationUnit(tu, col);
assertEquals( 4, col.size() );
assertEquals(4, col.size());
IVariable pm = (IVariable) col.getName(3).resolveBinding();
ICPPClassType S = (ICPPClassType) col.getName(0).resolveBinding();
IType t = pm.getType();
assertNotNull( t );
assertTrue( t instanceof ICPPPointerToMemberType );
ICPPClassType cls = ((ICPPPointerToMemberType)t).getMemberOfClass();
assertSame( S, cls );
assertTrue( ((ICPPPointerToMemberType)t).getType() instanceof IBasicType );
assertNotNull(t);
assertTrue(t instanceof ICPPPointerToMemberType);
ICPPClassType cls = ((ICPPPointerToMemberType) t).getMemberOfClass();
assertSame(S, cls);
assertTrue(((ICPPPointerToMemberType) t).getType() instanceof IBasicType);
}
public void testBug_PM_() throws Exception {
@ -1467,14 +1513,14 @@ public class AST2CPPTests extends AST2BaseTest {
buffer.append(" s->*pm = 1; \n"); //$NON-NLS-1$
buffer.append("} \n"); //$NON-NLS-1$
IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.CPP );
IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP);
CPPNameCollector col = new CPPNameCollector();
CPPVisitor.visitTranslationUnit(tu, col);
IBinding ref = col.getName(11).resolveBinding();
IVariable pm = (IVariable) col.getName(5).resolveBinding();
assertSame( pm, ref );
assertSame(pm, ref);
}
public void testBug_PM_2() throws Exception {
@ -1488,7 +1534,7 @@ public class AST2CPPTests extends AST2BaseTest {
buffer.append(" (s->*pm)()->i; \n"); //$NON-NLS-1$
buffer.append("} \n"); //$NON-NLS-1$
IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.CPP );
IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP);
CPPNameCollector col = new CPPNameCollector();
CPPVisitor.visitTranslationUnit(tu, col);
@ -1498,18 +1544,69 @@ public class AST2CPPTests extends AST2BaseTest {
ICPPMethod f = (ICPPMethod) col.getName(3).resolveBinding();
IType t = pm.getType();
assertTrue( t instanceof ICPPPointerToMemberType );
IFunctionType ft = (IFunctionType) ((ICPPPointerToMemberType)t).getType();
ICPPClassType ST = (ICPPClassType) ((ICPPPointerToMemberType)t).getMemberOfClass();
assertTrue(t instanceof ICPPPointerToMemberType);
IFunctionType ft = (IFunctionType) ((ICPPPointerToMemberType) t)
.getType();
ICPPClassType ST = ((ICPPPointerToMemberType) t)
.getMemberOfClass();
assertTrue( ft.getReturnType() instanceof IPointerType );
assertSame( ST, ((IPointerType)ft.getReturnType()).getType() );
assertSame( S, ST );
assertTrue(ft.getReturnType() instanceof IPointerType);
assertSame(ST, ((IPointerType) ft.getReturnType()).getType());
assertSame(S, ST);
assertInstances(col, S, 5);
assertInstances(col, pm, 2);
assertInstances(col, i, 2);
assertInstances(col, f, 3);
}
public void _testBug84469() throws Exception {
StringBuffer buffer = new StringBuffer();
buffer.append("struct S { int i; }; \n"); //$NON-NLS-1$
buffer.append("void f() { ; \n"); //$NON-NLS-1$
buffer.append(" int S::* pm = &S::i; \n"); //$NON-NLS-1$
buffer.append("} \n"); //$NON-NLS-1$
IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP);
CPPNameCollector col = new CPPNameCollector();
CPPVisitor.visitTranslationUnit(tu, col);
}
public void testFindTypeBinding_1() throws Exception {
IASTTranslationUnit tu = parse(
"int x = 5; int y(x);", ParserLanguage.CPP); //$NON-NLS-1$
IASTStandardFunctionDeclarator fdtor = (IASTStandardFunctionDeclarator) ((IASTSimpleDeclaration) tu
.getDeclarations()[1]).getDeclarators()[0];
IASTName name = fdtor.getParameters()[0].getDeclarator().getName();
IBinding binding = CPPSemantics.findTypeBinding(tu, name);
assertNull(binding);
tu = parse("struct x; int y(x);", ParserLanguage.CPP); //$NON-NLS-1$
fdtor = (IASTStandardFunctionDeclarator) ((IASTSimpleDeclaration) tu
.getDeclarations()[1]).getDeclarators()[0];
name = ((ICPPASTNamedTypeSpecifier) fdtor.getParameters()[0]
.getDeclSpecifier()).getName();
binding = CPPSemantics.findTypeBinding(tu, name);
assertNotNull(binding);
assertTrue(binding instanceof ICPPClassType);
}
public void testFindTypeBinding_2() throws Exception {
IASTTranslationUnit tu = parse(
"struct B; void f() { B * bp; }", ParserLanguage.CPP); //$NON-NLS-1$
IASTCompoundStatement compound = (IASTCompoundStatement) ((IASTFunctionDefinition) tu
.getDeclarations()[1]).getBody();
IASTBinaryExpression exp = (IASTBinaryExpression) ((IASTExpressionStatement) compound
.getStatements()[0]).getExpression();
IBinding binding = CPPSemantics.findTypeBinding(compound,
((IASTIdExpression) exp.getOperand1()).getName());
assertNotNull(binding);
assertTrue(binding instanceof ICPPClassType);
}
}

View file

@ -124,8 +124,7 @@ public class CPPSemantics {
this.name = n;
}
public boolean includeBlockItem( IASTNode item ){
if( astName == null ) return false;
if( astName.getParent() instanceof IASTIdExpression ||
if( ( astName != null && astName.getParent() instanceof IASTIdExpression ) ||
item instanceof IASTNamespaceDefinition ||
(item instanceof IASTSimpleDeclaration && ((IASTSimpleDeclaration)item).getDeclSpecifier() instanceof IASTCompositeTypeSpecifier ) )
{
@ -1759,4 +1758,54 @@ public class CPPSemantics {
}
return -1;
}
/**
* Find the binding for the type for the given name, if the given name is not a type, or can not
* be resolved, null is returned.
* @param mostRelevantScope
* @param name
* @return
*/
public static IBinding findTypeBinding( IASTNode mostRelevantScope, IASTName name ){
IScope scope = null;
if( mostRelevantScope instanceof IASTCompoundStatement )
scope = ((IASTCompoundStatement) mostRelevantScope).getScope();
else if ( mostRelevantScope instanceof IASTTranslationUnit )
scope = ((IASTTranslationUnit) mostRelevantScope).getScope();
else if ( mostRelevantScope instanceof ICPPASTNamespaceDefinition )
scope = ((ICPPASTNamespaceDefinition) mostRelevantScope).getScope();
else if( mostRelevantScope instanceof ICPPASTCompositeTypeSpecifier )
scope = ((ICPPASTCompositeTypeSpecifier) mostRelevantScope).getScope();
if( scope == null )
return null;
LookupData data = new LookupData( name ){
public boolean typesOnly(){ return true; }
public boolean forUsingDeclaration(){ return false; }
public boolean forDefinition(){ return false; }
public boolean considerConstructors(){ return false; }
public boolean functionCall(){ return false; }
public boolean qualified(){
IASTNode p1 = astName.getParent();
if( p1 instanceof ICPPASTQualifiedName ){
return ((ICPPASTQualifiedName)p1).getNames()[0] != astName;
}
return false;
}
};
try {
lookup( data, scope );
} catch (DOMException e) {
return null;
}
IBinding binding = null;
try {
binding = resolveAmbiguities( data, name );
} catch ( DOMException e2 ) {
}
return binding;
}
}