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 8c511f1d8e0..b67f2473d7b 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 @@ -7,6 +7,7 @@ * * Contributors: * IBM Corporation - initial API and implementation + * Ed Swartz (Nokia) *******************************************************************************/ /* * Created on Nov 29, 2004 @@ -1814,18 +1815,6 @@ public class AST2CPPTests extends AST2BaseTest { 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(); - tu.accept(col); - } - // public void testFindTypeBinding_1() throws Exception { // IASTTranslationUnit tu = parse( // "int x = 5; int y(x);", ParserLanguage.CPP); //$NON-NLS-1$ @@ -2125,7 +2114,7 @@ public class AST2CPPTests extends AST2BaseTest { assertInstances(col, f2, 1); } - public void _testBug45763_4() throws Exception { + public void testBug45763_4() throws Exception { StringBuffer buffer = new StringBuffer(); buffer.append("void f( int ); \n"); //$NON-NLS-1$ buffer.append("void f( char ); \n"); //$NON-NLS-1$ @@ -2516,7 +2505,7 @@ public class AST2CPPTests extends AST2BaseTest { assertSame(decls[0], col.getName(6)); } - public void _testBug86274() throws Exception { + public void testBug86274() throws Exception { StringBuffer buffer = new StringBuffer(); buffer.append("class D {}; \n"); //$NON-NLS-1$ buffer.append("D d1; \n"); //$NON-NLS-1$ @@ -5147,4 +5136,46 @@ public class AST2CPPTests extends AST2BaseTest { parse( buffer.toString(), ParserLanguage.CPP, true, true ); } + + /** + * Test redundant class specifiers + */ + public void testBug174791() throws Exception { + StringBuffer buffer = new StringBuffer( ); + buffer.append( "class MyClass {\r\n"); //$NON-NLS-1$ + buffer.append( " int MyClass::field;\r\n"); //$NON-NLS-1$ + buffer.append( " static int MyClass::static_field;\r\n"); //$NON-NLS-1$ + buffer.append( "};\r\n" ); //$NON-NLS-1$ + buffer.append( "int MyClass::static_field;\r\n" ); //$NON-NLS-1$ + + IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.CPP, true, true ); + + // check class + IASTSimpleDeclaration sd = (IASTSimpleDeclaration) tu.getDeclarations()[0]; + ICPPASTCompositeTypeSpecifier cts = (ICPPASTCompositeTypeSpecifier) sd.getDeclSpecifier(); + + IASTSimpleDeclaration md = (IASTSimpleDeclaration) cts.getMembers()[0]; + IASTName name = md.getDeclarators()[0].getName(); + IField field = (IField) name.resolveBinding(); + // would crash and/or return wrong result + assertFalse(field.isStatic()); + assertFalse(field.isExtern()); + assertFalse(field.isAuto()); + assertFalse(field.isRegister()); + + md = (IASTSimpleDeclaration) cts.getMembers()[1]; + name = md.getDeclarators()[0].getName(); + field = (IField) name.resolveBinding(); + // would crash + assertTrue(field.isStatic()); + assertFalse(field.isExtern()); + assertFalse(field.isAuto()); + assertFalse(field.isRegister()); + + // check real static defn + sd = (IASTSimpleDeclaration) tu.getDeclarations()[1]; + name = sd.getDeclarators()[0].getName(); + field = (IField) name.resolveBinding(); + assertTrue(field.isStatic()); + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPField.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPField.java index 4acb933a600..3ec18d95b93 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPField.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPField.java @@ -24,6 +24,7 @@ import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration; import org.eclipse.cdt.core.dom.ast.ICompositeType; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTDeclSpecifier; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTVisiblityLabel; import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassScope; import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType; @@ -145,6 +146,17 @@ public class CPPField extends CPPVariable implements ICPPField, ICPPInternalBind return scope.getClassType(); } + public boolean isStatic() { + // definition of a static field doesn't necessarily say static + if (getDeclarations() == null) { + IASTNode def= getDefinition(); + if (def instanceof ICPPASTQualifiedName) { + return true; + } + } + return super.isStatic(); + } + /* (non-Javadoc) * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPVariable#isMutable() */ diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVariable.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVariable.java index 312b9191188..756ebb40b60 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVariable.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVariable.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2006 IBM Corporation and others. + * Copyright (c) 2004, 2007 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -8,6 +8,7 @@ * Contributors: * IBM Corporation - initial API and implementation * Markus Schorn (Wind River Systems) + * Ed Swartz (Nokia) *******************************************************************************/ /* * Created on Nov 29, 2004 @@ -115,16 +116,10 @@ public class CPPVariable extends PlatformObject implements ICPPVariable, ICPPInt } protected boolean isDefinition( IASTName name ){ - IASTNode node = name.getParent(); - if( node instanceof ICPPASTQualifiedName ) - node = node.getParent(); - - if( !( node instanceof IASTDeclarator ) ) - return false; - - IASTDeclarator dtor = (IASTDeclarator) node; - while( dtor.getParent() instanceof IASTDeclarator ) - dtor = (IASTDeclarator) dtor.getParent(); + IASTDeclarator dtor= findDeclarator(name); + if (dtor == null) { + return false; + } IASTSimpleDeclaration simpleDecl = (IASTSimpleDeclaration) dtor.getParent(); IASTDeclSpecifier declSpec = simpleDecl.getDeclSpecifier(); @@ -143,6 +138,21 @@ public class CPPVariable extends PlatformObject implements ICPPVariable, ICPPInt return true; } + private IASTDeclarator findDeclarator(IASTName name) { + IASTNode node = name.getParent(); + if( node instanceof ICPPASTQualifiedName ) + node = node.getParent(); + + if( !( node instanceof IASTDeclarator ) ) + return null; + + IASTDeclarator dtor = (IASTDeclarator) node; + while( dtor.getParent() instanceof IASTDeclarator ) + dtor = (IASTDeclarator) dtor.getParent(); + + return dtor; + } + public void addDeclaration( IASTNode node ) { if( !(node instanceof IASTName) ) return; @@ -249,17 +259,16 @@ public class CPPVariable extends PlatformObject implements ICPPVariable, ICPPInt */ public boolean isStatic() { IASTDeclarator dtor = null; - if( declarations != null ) - dtor = (IASTDeclarator) declarations[0].getParent(); - else { - //definition of a static field doesn't necessarily say static - if( definition.getParent() instanceof ICPPASTQualifiedName ) - return true; - dtor = (IASTDeclarator) definition.getParent(); + if( declarations != null ) { + dtor= findDeclarator(declarations[0]); + } + else { + dtor= findDeclarator(definition); + } + + if (dtor == null) { + return false; } - - while( dtor.getPropertyInParent() == IASTDeclarator.NESTED_DECLARATOR ) - dtor = (IASTDeclarator) dtor.getParent(); IASTNode node = dtor.getParent(); if( node instanceof IASTSimpleDeclaration ){