From f3141139f82703b541dd1102cc37f9c525d68c9b Mon Sep 17 00:00:00 2001 From: Nathan Ridge Date: Sat, 11 Jul 2015 22:34:57 -0400 Subject: [PATCH] Bug 472101 - Do not include variables declared in the condition of an if, while, or switch statement in the enclosing block scope Change-Id: Idd90d85f705af11c84f31ca830eb86682a046b87 Signed-off-by: Nathan Ridge --- .../cdt/core/parser/tests/ast2/AST2CPPTests.java | 10 ++++++++++ .../core/dom/parser/cpp/semantics/CPPSemantics.java | 12 ++++++------ 2 files changed, 16 insertions(+), 6 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 520c156573f..87bd065f7c2 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 @@ -10090,6 +10090,16 @@ public class AST2CPPTests extends AST2TestBase { assertNotSame(g1, g2); assertSame(g2, g3); } + + // int test() { + // extern int *e(); + // if (auto r = e()) { return *r; } + // int r = 12; + // return r; + // } + public void testVariableDeclarationInIfStatement() throws Exception { + parseAndCheckBindings(); + } // class A : A { // }; diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPSemantics.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPSemantics.java index 6071b15347b..be938c1e1b4 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPSemantics.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPSemantics.java @@ -1503,6 +1503,12 @@ public class CPPSemantics { } else { nodes= new IASTNode[] {initDeclaration}; } + } else if (parent instanceof ICPPASTSwitchStatement) { + nodes = new IASTNode[] { ((ICPPASTSwitchStatement) parent).getControllerDeclaration() }; + } else if (parent instanceof ICPPASTIfStatement) { + nodes = new IASTNode[] { ((ICPPASTIfStatement) parent).getConditionDeclaration() }; + } else if (parent instanceof ICPPASTWhileStatement) { + nodes = new IASTNode[] { ((ICPPASTWhileStatement) parent).getConditionDeclaration() }; } else if (parent instanceof ICPPASTRangeBasedForStatement) { ICPPASTRangeBasedForStatement forStatement = (ICPPASTRangeBasedForStatement) parent; final IASTDeclaration decl = forStatement.getDeclaration(); @@ -1632,12 +1638,6 @@ public class CPPSemantics { declaration = ((IASTDeclarationStatement) node).getDeclaration(); } else if (node instanceof ICPPASTCatchHandler) { declaration = ((ICPPASTCatchHandler) node).getDeclaration(); - } else if (node instanceof ICPPASTSwitchStatement) { - declaration = ((ICPPASTSwitchStatement) node).getControllerDeclaration(); - } else if (node instanceof ICPPASTIfStatement) { - declaration = ((ICPPASTIfStatement) node).getConditionDeclaration(); - } else if (node instanceof ICPPASTWhileStatement) { - declaration = ((ICPPASTWhileStatement) node).getConditionDeclaration(); } else if (node instanceof IASTParameterDeclaration) { IASTParameterDeclaration parameterDeclaration = (IASTParameterDeclaration) node; IASTDeclarator dtor = parameterDeclaration.getDeclarator();