diff options
author | Markus Schorn | 2009-08-19 09:06:35 +0000 |
---|---|---|
committer | Markus Schorn | 2009-08-19 09:06:35 +0000 |
commit | 89a33b815767fc9004245777c391b3a5d1f89b62 (patch) | |
tree | dce5ecdf5b92cd2ac436ffa5f6e70746e830767f | |
parent | 5c185d3f88d958380e72a7aa7e974bccf3efe5fb (diff) | |
download | org.eclipse.cdt-89a33b815767fc9004245777c391b3a5d1f89b62.tar.gz org.eclipse.cdt-89a33b815767fc9004245777c391b3a5d1f89b62.tar.xz org.eclipse.cdt-89a33b815767fc9004245777c391b3a5d1f89b62.zip |
Declaration vs. definition for static variables, bug 286259.
2 files changed, 66 insertions, 9 deletions
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 b3f7f2f604c..17aa6293c13 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 @@ -45,6 +45,7 @@ import org.eclipse.cdt.core.dom.ast.IASTInitializerExpression; import org.eclipse.cdt.core.dom.ast.IASTLabelStatement; import org.eclipse.cdt.core.dom.ast.IASTLiteralExpression; import org.eclipse.cdt.core.dom.ast.IASTName; +import org.eclipse.cdt.core.dom.ast.IASTNameOwner; import org.eclipse.cdt.core.dom.ast.IASTNamedTypeSpecifier; import org.eclipse.cdt.core.dom.ast.IASTProblemDeclaration; import org.eclipse.cdt.core.dom.ast.IASTReturnStatement; @@ -7261,4 +7262,51 @@ public class AST2CPPTests extends AST2BaseTest { public void testLookupFromInlineFriend_284690() throws Exception { parseAndCheckBindings(getAboveComment(), ParserLanguage.CPP); } + + // int v1; + // static int v2; + // extern int v3; + // int v4= 12; + // static int v5= 1; + // class X { + // int v6; + // static const int v7; + // static const int v8= 1; + // }; + // const int X::v7= 1; + public void testVariableDefVsDecl_286259() throws Exception { + String[] declNames= {"v3"}; + String[] defNames= {"v1", "v2", "v4", "v5", "X::v7"}; + IASTTranslationUnit tu= parseAndCheckBindings(getAboveComment(), ParserLanguage.CPP); + checkDeclDef(declNames, defNames, tu.getDeclarations()); + + declNames= new String[] {"v7"}; + defNames= new String[] {"v6", "v8"}; + IASTCompositeTypeSpecifier cls= getCompositeType(tu, 5); + checkDeclDef(declNames, defNames, cls.getMembers()); + } + + private void checkDeclDef(String[] declNames, String[] defNames, IASTDeclaration[] decls) { + int i=0, j=0; + for (IASTDeclaration decl : decls) { + final IASTDeclarator[] dtors = ((IASTSimpleDeclaration) decl).getDeclarators(); + for (IASTDeclarator dtor : dtors) { + final String name = dtor.getName().toString(); + switch (dtor.getRoleForName(dtor.getName())) { + case IASTNameOwner.r_declaration: + assertTrue("Unexpected decl " + name, i < declNames.length); + assertEquals(declNames[i++], name); + break; + case IASTNameOwner.r_definition: + assertTrue("Unexpected decl " + name, i < defNames.length); + assertEquals(defNames[j++], name); + break; + default: + assertTrue(name, false); + } + } + } + assertEquals(declNames.length, i); + assertEquals(defNames.length, j); + } } 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 a651bfacf1a..7b80479a2f2 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 @@ -24,9 +24,11 @@ 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; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassScope; import org.eclipse.cdt.core.parser.util.ArrayUtil; import org.eclipse.cdt.internal.core.dom.parser.ASTNode; import org.eclipse.cdt.internal.core.dom.parser.ASTQueries; +import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor; /** * C++ specific declarator. @@ -169,15 +171,22 @@ public class CPPASTDeclarator extends ASTNode implements IASTDeclarator { if (getParent instanceof IASTDeclaration) { if (getParent instanceof IASTFunctionDefinition) return r_definition; - if (getParent instanceof IASTSimpleDeclaration) { - final int storage = ((IASTSimpleDeclaration) getParent).getDeclSpecifier().getStorageClass(); - if (getInitializer() != null || storage == IASTDeclSpecifier.sc_typedef) - return r_definition; - - if (storage == IASTDeclSpecifier.sc_extern || storage == IASTDeclSpecifier.sc_static) { - return r_declaration; - } - } + if (getParent instanceof IASTSimpleDeclaration) { + final int storage = ((IASTSimpleDeclaration) getParent).getDeclSpecifier().getStorageClass(); + + if (getInitializer() != null || storage == IASTDeclSpecifier.sc_typedef) + return r_definition; + if (storage == IASTDeclSpecifier.sc_extern) { + return r_declaration; + } + + // static member variables without initializer are declarations + if (!fnDtor && storage == IASTDeclSpecifier.sc_static) { + if (CPPVisitor.getContainingScope(getParent) instanceof ICPPClassScope) { + return r_declaration; + } + } + } return fnDtor ? r_declaration : r_definition; } if (getParent instanceof IASTTypeId) |