diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java index 5bbc6deb256..2c6c5ad02d4 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java @@ -3986,4 +3986,42 @@ public class AST2CPPTests extends AST2BaseTest { assertInstances( col, n, 3 ); } + public void testDeclDefn() throws Exception { + StringBuffer buffer = new StringBuffer(); + buffer.append("int a; \n"); //$NON-NLS-1$ + buffer.append("extern int b; \n"); //$NON-NLS-1$ + buffer.append("extern int c = 1; \n"); //$NON-NLS-1$ + buffer.append("int f( ); \n"); //$NON-NLS-1$ + buffer.append("int f( int p ){} \n"); //$NON-NLS-1$ + buffer.append("struct S; \n"); //$NON-NLS-1$ + buffer.append("struct S { int d; }; \n"); //$NON-NLS-1$ + buffer.append("struct X { \n"); //$NON-NLS-1$ + buffer.append(" static int y; \n"); //$NON-NLS-1$ + buffer.append("}; \n"); //$NON-NLS-1$ + buffer.append("namespace N {} \n"); //$NON-NLS-1$ + buffer.append("int X::y = 1; \n"); //$NON-NLS-1$ + buffer.append("int ( *g(int) )(int); \n"); //$NON-NLS-1$ + buffer.append("int ( *pf)(int); \n"); //$NON-NLS-1$ + + IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.CPP ); + CPPNameCollector col = new CPPNameCollector(); + tu.accept(col); + + assertTrue( col.getName(0).isDefinition() ); //a + assertFalse( col.getName(1).isDefinition() ); //b + assertTrue( col.getName(2).isDefinition() ); //c + assertFalse( col.getName(3).isDefinition() ); //f () + assertTrue( col.getName(4).isDefinition() ); //f () {} + assertTrue( col.getName(5).isDefinition() ); //p + assertFalse( col.getName(6).isDefinition() ); //struct S; + assertTrue( col.getName(7).isDefinition() ); //struct S {} + assertTrue( col.getName(8).isDefinition() ); //d + assertTrue( col.getName(9).isDefinition() ); //X + assertFalse( col.getName(10).isDefinition() ); //y + assertTrue( col.getName(11).isDefinition() ); //N + assertTrue( col.getName(12).isDefinition() ); //X::y + assertFalse( col.getName(15).isDefinition() ); //g + assertTrue( col.getName(18).isDefinition() ); //pf + } + } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTDeclarator.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTDeclarator.java index e4a17140632..400b58060fc 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTDeclarator.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTDeclarator.java @@ -14,10 +14,12 @@ import org.eclipse.cdt.core.dom.ast.ASTVisitor; import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier; import org.eclipse.cdt.core.dom.ast.IASTDeclaration; import org.eclipse.cdt.core.dom.ast.IASTDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator; import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition; import org.eclipse.cdt.core.dom.ast.IASTInitializer; import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTNode; +import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration; import org.eclipse.cdt.core.dom.ast.IASTPointerOperator; import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration; import org.eclipse.cdt.core.dom.ast.IASTTypeId; @@ -127,19 +129,26 @@ public class CPPASTDeclarator extends CPPASTNode implements IASTDeclarator { */ public int getRoleForName(IASTName n) { IASTNode getParent = getParent(); + boolean fnDtor = ( this instanceof IASTFunctionDeclarator ); if( getParent instanceof IASTDeclaration ) { if( getParent instanceof IASTFunctionDefinition ) return r_definition; if( getParent instanceof IASTSimpleDeclaration ) { + if( getInitializer() != null ) + return r_definition; + IASTSimpleDeclaration sd = (IASTSimpleDeclaration) getParent; - if( sd.getDeclSpecifier().getStorageClass() == IASTDeclSpecifier.sc_extern ) - return r_declaration; - if( getInitializer() == null ) + int storage = sd.getDeclSpecifier().getStorageClass(); + if( storage == IASTDeclSpecifier.sc_extern || + storage == IASTDeclSpecifier.sc_typedef || + storage == IASTDeclSpecifier.sc_static ) + { return r_declaration; + } } - return r_definition; + return fnDtor ? r_declaration : r_definition; } if( getParent instanceof IASTTypeId ) return r_reference; @@ -154,17 +163,25 @@ public class CPPASTDeclarator extends CPPASTNode implements IASTDeclarator { return r_definition; if( getParent instanceof IASTSimpleDeclaration ) { - IASTSimpleDeclaration sd = (IASTSimpleDeclaration) getParent; - if( sd.getDeclSpecifier().getStorageClass() == IASTDeclSpecifier.sc_extern ) - return r_declaration; if( getInitializer() != null ) return r_definition; + IASTSimpleDeclaration sd = (IASTSimpleDeclaration) getParent; + int storage = sd.getDeclSpecifier().getStorageClass(); + if( storage == IASTDeclSpecifier.sc_extern || + storage == IASTDeclSpecifier.sc_typedef || + storage == IASTDeclSpecifier.sc_static ) + { + return r_declaration; + } } - return r_definition; + return fnDtor ? r_declaration : r_definition; } if( t instanceof IASTTypeId ) return r_reference; } + if( getParent instanceof IASTParameterDeclaration ) + return ( n.toCharArray().length > 0 ) ? r_definition : r_declaration; + return r_unclear; } }