1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 14:42: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:
* 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());
}
}

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.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()
*/

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
* 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();
if( declarations != null ) {
dtor= findDeclarator(declarations[0]);
}
else {
//definition of a static field doesn't necessarily say static
if( definition.getParent() instanceof ICPPASTQualifiedName )
return true;
dtor = (IASTDeclarator) definition.getParent();
dtor= findDeclarator(definition);
}
while( dtor.getPropertyInParent() == IASTDeclarator.NESTED_DECLARATOR )
dtor = (IASTDeclarator) dtor.getParent();
if (dtor == null) {
return false;
}
IASTNode node = dtor.getParent();
if( node instanceof IASTSimpleDeclaration ){