From 6d4221b57f0210f030850fd83846d5b1b8c87b44 Mon Sep 17 00:00:00 2001 From: Markus Schorn Date: Fri, 6 Apr 2007 09:35:53 +0000 Subject: [PATCH] Fix and testcases for 181305, NPE parsing reference to 'void (f)(char)'. --- .../core/parser/tests/ast2/AST2CPPTests.java | 12 ++--- .../cdt/core/parser/tests/ast2/AST2Tests.java | 44 +++++++++++++++++++ .../core/dom/parser/cpp/CPPVisitor.java | 27 ++++++++---- 3 files changed, 70 insertions(+), 13 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 6233f9f2f04..a12a7f93b0f 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 @@ -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 diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java index 10843720b3a..4f069619f26 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java @@ -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); + } + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java index 5f323422d44..f88f0b4e3cb 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java @@ -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;