From b7dffd1646a8a95d119cf54c06c50e2063a2f11a Mon Sep 17 00:00:00 2001 From: John Camelon Date: Tue, 1 Mar 2005 21:21:54 +0000 Subject: [PATCH] Patch for Devin Steffler. Fixed 86698 - [Selection] selecting from constructor chain initializer fails to find IASTNode due to stop condition --- .../parser/tests/ast2/DOMLocationTests.java | 41 +++++++++++++++++++ .../dom/parser/cpp/GNUCPPSourceParser.java | 10 ++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationTests.java index 821c09e1446..324c00491b0 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationTests.java @@ -47,6 +47,9 @@ import org.eclipse.cdt.core.dom.ast.IASTTypeId; import org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression; import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression; 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.ICPPASTFunctionDeclarator; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionTryBlockDeclarator; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLinkageSpecification; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamedTypeSpecifier; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceDefinition; @@ -459,4 +462,42 @@ public class DOMLocationTests extends AST2BaseTest { assertNull( for_stmt.getInitDeclaration() ); } } + + public void testBug86698_1() throws Exception { + StringBuffer buffer = new StringBuffer(); + buffer.append( "struct C;\n"); //$NON-NLS-1$ + buffer.append( "void no_opt(C*);\n"); //$NON-NLS-1$ + buffer.append( "struct C {\n"); //$NON-NLS-1$ + buffer.append( "int c;\n"); //$NON-NLS-1$ + buffer.append( "C() : c(0) { no_opt(this); }\n"); //$NON-NLS-1$ + buffer.append( "};\n"); //$NON-NLS-1$ + + IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP); + ICPPASTFunctionDeclarator funC = (ICPPASTFunctionDeclarator)((IASTFunctionDefinition)((ICPPASTCompositeTypeSpecifier)((IASTSimpleDeclaration)tu.getDeclarations()[2]).getDeclSpecifier()).getMembers()[1]).getDeclarator(); + assertSoleLocation( funC, buffer.toString().indexOf("C() : c(0)"), "C() : c(0)".length() ); //$NON-NLS-1$//$NON-NLS-2$ + } + + public void testBug86698_2() throws Exception { + StringBuffer buffer = new StringBuffer(); + buffer.append( "void foo() {\n"); //$NON-NLS-1$ + buffer.append( "int f(int);\n"); //$NON-NLS-1$ + buffer.append( "class C {\n"); //$NON-NLS-1$ + buffer.append( "int i;\n"); //$NON-NLS-1$ + buffer.append( "double d;\n"); //$NON-NLS-1$ + buffer.append( "public:\n"); //$NON-NLS-1$ + buffer.append( "C(int, double);\n"); //$NON-NLS-1$ + buffer.append( "};\n"); //$NON-NLS-1$ + buffer.append( "C::C(int ii, double id)\n"); //$NON-NLS-1$ + buffer.append( "try\n"); //$NON-NLS-1$ + buffer.append( ": i(f(ii)), d(id)\n"); //$NON-NLS-1$ + buffer.append( "{\n }\n"); //$NON-NLS-1$ + buffer.append( "catch (...)\n"); //$NON-NLS-1$ + buffer.append( "{\n }\n"); //$NON-NLS-1$ + buffer.append( "}\n"); //$NON-NLS-1$ + + IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP); + ICPPASTFunctionTryBlockDeclarator funC = (ICPPASTFunctionTryBlockDeclarator)((IASTFunctionDefinition)((IASTDeclarationStatement)((IASTCompoundStatement)((IASTFunctionDefinition)tu.getDeclarations()[0]).getBody()).getStatements()[2]).getDeclaration()).getDeclarator(); + assertSoleLocation( funC, buffer.toString().indexOf("C::C(int ii, double id)\ntry\n: i(f(ii)), d(id)"), "C::C(int ii, double id)\ntry\n: i(f(ii)), d(id)".length() ); //$NON-NLS-1$//$NON-NLS-2$ + } + } \ No newline at end of file diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/GNUCPPSourceParser.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/GNUCPPSourceParser.java index c61eb956ff2..db57cd48040 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/GNUCPPSourceParser.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/GNUCPPSourceParser.java @@ -2708,7 +2708,9 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser { if (!constructorChain.isEmpty() && declarator instanceof ICPPASTFunctionDeclarator) { ICPPASTFunctionDeclarator fd = (ICPPASTFunctionDeclarator) declarator; - for (int i = 0; i < constructorChain.size(); ++i) { + + int size = constructorChain.size(); + for (int i = 0; i < size; ++i) { ICPPASTConstructorChainInitializer initializer = (ICPPASTConstructorChainInitializer) constructorChain .get(i); fd.addConstructorToChain(initializer); @@ -2716,6 +2718,12 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser { initializer .setPropertyInParent(ICPPASTFunctionDeclarator.CONSTRUCTOR_CHAIN_MEMBER); } + + // fix for 86698, now that the constructorChain is established, update the declarator's length + if (fd instanceof ASTNode && constructorChain.get(size-1) instanceof ASTNode) { + ASTNode init = (ASTNode) constructorChain.get(size-1); + ((ASTNode)fd).setLength(init.getOffset() + init.getLength() - ((ASTNode)fd).getOffset()); + } } IASTFunctionDefinition funcDefinition = createFunctionDefinition();