1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 22:52:11 +02:00

Fix for 174791, in cooperation with Ed Swartz, ClassCastException in CPPVariable.isStatic().

This commit is contained in:
Markus Schorn 2007-02-21 15:23:39 +00:00
parent 1565582172
commit 1578b474e2
3 changed files with 87 additions and 35 deletions

View file

@ -7,6 +7,7 @@
* *
* Contributors: * Contributors:
* IBM Corporation - initial API and implementation * IBM Corporation - initial API and implementation
* Ed Swartz (Nokia)
*******************************************************************************/ *******************************************************************************/
/* /*
* Created on Nov 29, 2004 * Created on Nov 29, 2004
@ -1814,18 +1815,6 @@ public class AST2CPPTests extends AST2BaseTest {
assertInstances(col, f, 3); 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 { // public void testFindTypeBinding_1() throws Exception {
// IASTTranslationUnit tu = parse( // IASTTranslationUnit tu = parse(
// "int x = 5; int y(x);", ParserLanguage.CPP); //$NON-NLS-1$ // "int x = 5; int y(x);", ParserLanguage.CPP); //$NON-NLS-1$
@ -2125,7 +2114,7 @@ public class AST2CPPTests extends AST2BaseTest {
assertInstances(col, f2, 1); assertInstances(col, f2, 1);
} }
public void _testBug45763_4() throws Exception { public void testBug45763_4() throws Exception {
StringBuffer buffer = new StringBuffer(); StringBuffer buffer = new StringBuffer();
buffer.append("void f( int ); \n"); //$NON-NLS-1$ buffer.append("void f( int ); \n"); //$NON-NLS-1$
buffer.append("void f( char ); \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)); assertSame(decls[0], col.getName(6));
} }
public void _testBug86274() throws Exception { public void testBug86274() throws Exception {
StringBuffer buffer = new StringBuffer(); StringBuffer buffer = new StringBuffer();
buffer.append("class D {}; \n"); //$NON-NLS-1$ buffer.append("class D {}; \n"); //$NON-NLS-1$
buffer.append("D d1; \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 ); 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());
}
} }

View file

@ -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.ICompositeType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier; 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.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.ICPPASTVisiblityLabel;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassScope; import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassScope;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType; import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
@ -145,6 +146,17 @@ public class CPPField extends CPPVariable implements ICPPField, ICPPInternalBind
return scope.getClassType(); 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) /* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.cpp.ICPPVariable#isMutable() * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPVariable#isMutable()
*/ */

View file

@ -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 * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -8,6 +8,7 @@
* Contributors: * Contributors:
* IBM Corporation - initial API and implementation * IBM Corporation - initial API and implementation
* Markus Schorn (Wind River Systems) * Markus Schorn (Wind River Systems)
* Ed Swartz (Nokia)
*******************************************************************************/ *******************************************************************************/
/* /*
* Created on Nov 29, 2004 * Created on Nov 29, 2004
@ -115,16 +116,10 @@ public class CPPVariable extends PlatformObject implements ICPPVariable, ICPPInt
} }
protected boolean isDefinition( IASTName name ){ protected boolean isDefinition( IASTName name ){
IASTNode node = name.getParent(); IASTDeclarator dtor= findDeclarator(name);
if( node instanceof ICPPASTQualifiedName ) if (dtor == null) {
node = node.getParent();
if( !( node instanceof IASTDeclarator ) )
return false; return false;
}
IASTDeclarator dtor = (IASTDeclarator) node;
while( dtor.getParent() instanceof IASTDeclarator )
dtor = (IASTDeclarator) dtor.getParent();
IASTSimpleDeclaration simpleDecl = (IASTSimpleDeclaration) dtor.getParent(); IASTSimpleDeclaration simpleDecl = (IASTSimpleDeclaration) dtor.getParent();
IASTDeclSpecifier declSpec = simpleDecl.getDeclSpecifier(); IASTDeclSpecifier declSpec = simpleDecl.getDeclSpecifier();
@ -143,6 +138,21 @@ public class CPPVariable extends PlatformObject implements ICPPVariable, ICPPInt
return true; 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 ) { public void addDeclaration( IASTNode node ) {
if( !(node instanceof IASTName) ) if( !(node instanceof IASTName) )
return; return;
@ -249,17 +259,16 @@ public class CPPVariable extends PlatformObject implements ICPPVariable, ICPPInt
*/ */
public boolean isStatic() { public boolean isStatic() {
IASTDeclarator dtor = null; IASTDeclarator dtor = null;
if( declarations != null ) if( declarations != null ) {
dtor = (IASTDeclarator) declarations[0].getParent(); dtor= findDeclarator(declarations[0]);
}
else { else {
//definition of a static field doesn't necessarily say static dtor= findDeclarator(definition);
if( definition.getParent() instanceof ICPPASTQualifiedName )
return true;
dtor = (IASTDeclarator) definition.getParent();
} }
while( dtor.getPropertyInParent() == IASTDeclarator.NESTED_DECLARATOR ) if (dtor == null) {
dtor = (IASTDeclarator) dtor.getParent(); return false;
}
IASTNode node = dtor.getParent(); IASTNode node = dtor.getParent();
if( node instanceof IASTSimpleDeclaration ){ if( node instanceof IASTSimpleDeclaration ){