1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-28 11:25:35 +02:00

Fix and testcases for 181305, NPE parsing reference to 'void (f)(char)'.

This commit is contained in:
Markus Schorn 2007-04-06 09:35:53 +00:00
parent 56523f3c6a
commit 6d4221b57f
3 changed files with 70 additions and 13 deletions

View file

@ -5194,12 +5194,14 @@ public class AST2CPPTests extends AST2BaseTest {
assertTrue(field.isStatic());
}
// namespace nsSplit {}
// namespace nsSplit {
// void a();
// }
// void nsSplit::a() {
// }
public void testBug180979() throws Exception {
StringBuffer buffer = new StringBuffer( );
buffer.append( "namespace nsSplit {}\r\n"); //$NON-NLS-1$
buffer.append( "namespace nsSplit {void a();}\r\n"); //$NON-NLS-1$
buffer.append( "void nsSplit::a() {}\r\n"); //$NON-NLS-1$
StringBuffer buffer = getContents(1)[0];
IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.CPP, true, true );
// check class

View file

@ -97,6 +97,7 @@ import org.eclipse.cdt.internal.core.parser.ParserException;
* @author Doug Schaefer
*/
public class AST2Tests extends AST2BaseTest {
private static ParserLanguage[] LANGUAGES= {ParserLanguage.C, ParserLanguage.CPP};
public static TestSuite suite() {
return suite(AST2Tests.class);
@ -3579,4 +3580,47 @@ public class AST2Tests extends AST2BaseTest {
// TODO: exhaustive macro testing
}
// void (decl)(char);
// void foo() {
// decl('a');
// }
public void testBug181305_1() throws Exception {
StringBuffer buffer = getContents(1)[0];
for (int i = 0; i < LANGUAGES.length; i++) {
final ParserLanguage lang= LANGUAGES[i];
IASTTranslationUnit tu = parse( buffer.toString(), lang, true, true );
// check class
IASTFunctionDefinition fd = (IASTFunctionDefinition) tu.getDeclarations()[1];
IASTCompoundStatement comp_stmt= (IASTCompoundStatement) fd.getBody();
IASTExpressionStatement expr_stmt= (IASTExpressionStatement) comp_stmt.getStatements()[0];
IASTFunctionCallExpression expr= (IASTFunctionCallExpression) expr_stmt.getExpression();
IASTIdExpression idExpr= (IASTIdExpression) expr.getFunctionNameExpression();
IBinding binding= idExpr.getName().resolveBinding();
assertTrue(lang.toString(), binding instanceof IFunction);
assertFalse(lang.toString(), binding instanceof IProblemBinding);
}
}
// void (*decl)(char);
// void foo() {
// decl('a');
// }
public void testBug181305_2() throws Exception {
StringBuffer buffer = getContents(1)[0];
for (int i = 0; i < LANGUAGES.length; i++) {
final ParserLanguage lang= LANGUAGES[i];
IASTTranslationUnit tu = parse( buffer.toString(), lang, true, true );
// check class
IASTFunctionDefinition fd = (IASTFunctionDefinition) tu.getDeclarations()[1];
IASTCompoundStatement comp_stmt= (IASTCompoundStatement) fd.getBody();
IASTExpressionStatement expr_stmt= (IASTExpressionStatement) comp_stmt.getStatements()[0];
IASTFunctionCallExpression expr= (IASTFunctionCallExpression) expr_stmt.getExpression();
IASTIdExpression idExpr= (IASTIdExpression) expr.getFunctionNameExpression();
IBinding binding= idExpr.getName().resolveBinding();
assertTrue(lang.toString(), binding instanceof IVariable);
assertFalse(lang.toString(), binding instanceof IProblemBinding);
}
}
}

View file

@ -470,19 +470,19 @@ public class CPPVisitor {
declarator = declarator.getNestedDeclarator();
IASTFunctionDeclarator funcDeclarator= null;
IASTNode node= declarator;
IASTNode tmpNode= declarator;
do {
if (node instanceof IASTFunctionDeclarator) {
funcDeclarator= (IASTFunctionDeclarator) node;
if (tmpNode instanceof IASTFunctionDeclarator) {
funcDeclarator= (IASTFunctionDeclarator) tmpNode;
break;
}
if (((IASTDeclarator) node).getPointerOperators().length > 0 ||
node.getPropertyInParent() != IASTDeclarator.NESTED_DECLARATOR) {
if (((IASTDeclarator) tmpNode).getPointerOperators().length > 0 ||
tmpNode.getPropertyInParent() != IASTDeclarator.NESTED_DECLARATOR) {
break;
}
node= node.getParent();
tmpNode= tmpNode.getParent();
}
while (node instanceof IASTDeclarator);
while (tmpNode instanceof IASTDeclarator);
IASTName name = declarator.getName();
if( name instanceof ICPPASTQualifiedName ){
@ -534,7 +534,7 @@ public class CPPVisitor {
parent = param.getParent();
if( parent instanceof IASTStandardFunctionDeclarator ) {
IASTStandardFunctionDeclarator fDtor = (IASTStandardFunctionDeclarator) param.getParent();
if( /*fDtor.getParent() instanceof IASTDeclarator ||*/ fDtor.getNestedDeclarator() != null )
if( hasNestedPointerOperator(fDtor) )
return null;
IBinding temp = fDtor.getName().resolveBinding();
if( temp instanceof ICPPInternalFunction ){
@ -645,6 +645,17 @@ public class CPPVisitor {
return binding;
}
private static boolean hasNestedPointerOperator(IASTDeclarator decl) {
decl= decl.getNestedDeclarator();
while (decl != null) {
if (decl.getPointerOperators().length > 0) {
return true;
}
decl= decl.getNestedDeclarator();
}
return false;
}
public static boolean isConstructor( IScope containingScope, IASTDeclarator declarator ){
if( containingScope == null || !(containingScope instanceof ICPPClassScope) )
return false;