From a2a05a515aa3698e3e0ecdeb77a9baf241c93f10 Mon Sep 17 00:00:00 2001 From: Nathan Ridge Date: Mon, 10 Mar 2014 02:31:22 -0400 Subject: [PATCH] Bug 372004 - Locally declared extern variable Change-Id: I33d634d6c63138910b2958b81f6d8df358e89e7d Signed-off-by: Nathan Ridge Reviewed-on: https://git.eclipse.org/r/23098 Tested-by: Hudson CI Reviewed-by: Sergey Prigogin IP-Clean: Sergey Prigogin Tested-by: Sergey Prigogin --- .../resources/semanticHighlighting/SHTest.cpp | 5 ++ .../tests/text/SemanticHighlightingTest.java | 3 + .../selection/CPPSelectionTestsNoIndexer.java | 11 ++++ .../ui/editor/SemanticHighlightings.java | 65 +++++++++---------- 4 files changed, 49 insertions(+), 35 deletions(-) diff --git a/core/org.eclipse.cdt.ui.tests/resources/semanticHighlighting/SHTest.cpp b/core/org.eclipse.cdt.ui.tests/resources/semanticHighlighting/SHTest.cpp index 98f555b7f2d..31f83eaab1c 100644 --- a/core/org.eclipse.cdt.ui.tests/resources/semanticHighlighting/SHTest.cpp +++ b/core/org.eclipse.cdt.ui.tests/resources/semanticHighlighting/SHTest.cpp @@ -146,3 +146,8 @@ EMPTY int f(); //http://bugs.eclipse.org/340492 template< template class U > class myClass {}; + +//http://bugs.eclipse.org/372004 +void g() { + extern int globalVariable; // declared as global near top +} diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/SemanticHighlightingTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/SemanticHighlightingTest.java index c70d7b77511..0011a850ae1 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/SemanticHighlightingTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/SemanticHighlightingTest.java @@ -273,6 +273,7 @@ public class SemanticHighlightingTest extends AbstractSemanticHighlightingTest { createPosition(102, 8, 13), createPosition(137, 4, 1), createPosition(144, 10, 1), + createPosition(150, 5, 1), }; if (PRINT_POSITIONS) System.out.println(toString(actual)); assertEqualPositions(expected, actual); @@ -291,6 +292,7 @@ public class SemanticHighlightingTest extends AbstractSemanticHighlightingTest { createPosition(131, 4, 11), createPosition(137, 4, 1), createPosition(144, 10, 1), + createPosition(150, 5, 1), }; if (PRINT_POSITIONS) System.out.println(toString(actual)); assertEqualPositions(expected, actual); @@ -306,6 +308,7 @@ public class SemanticHighlightingTest extends AbstractSemanticHighlightingTest { createPosition(33, 15, 20), createPosition(101, 8, 12), createPosition(104, 8, 12), + createPosition(151, 15, 14), }; if (PRINT_POSITIONS) System.out.println(toString(actual)); assertEqualPositions(expected, actual); diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selection/CPPSelectionTestsNoIndexer.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selection/CPPSelectionTestsNoIndexer.java index 29c78c5e609..d7788a7c421 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selection/CPPSelectionTestsNoIndexer.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selection/CPPSelectionTestsNoIndexer.java @@ -1224,4 +1224,15 @@ public class CPPSelectionTestsNoIndexer extends BaseUITestCase { assertTrue(decl instanceof IASTName); } + // int waldo; + // void foo() { + // extern int waldo; + // } + public void testLocallyDeclaredExternVariable_372004() throws Exception { + String code = getAboveComment(); + IFile file = importFile("testWaldo.cpp", code); + + int offset = code.indexOf("extern int waldo") + 12; + assertTrue(testF3(file, offset) instanceof IASTName); + } } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/SemanticHighlightings.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/SemanticHighlightings.java index 29a8c4d9d44..c42dd2157af 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/SemanticHighlightings.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/SemanticHighlightings.java @@ -655,13 +655,8 @@ public class SemanticHighlightings { && !(binding instanceof IField) && !(binding instanceof IParameter) && !(binding instanceof IProblemBinding)) { - try { - IScope scope= binding.getScope(); - if (LocalVariableHighlighting.isLocalScope(scope)) { - return true; - } - } catch (DOMException exc) { - CUIPlugin.log(exc); + if (LocalVariableHighlighting.isLocalVariable((IVariable) binding)) { + return true; } } } @@ -715,13 +710,8 @@ public class SemanticHighlightings { && !(binding instanceof IField) && !(binding instanceof IParameter) && !(binding instanceof IProblemBinding)) { - try { - IScope scope= binding.getScope(); - if (isLocalScope(scope)) { - return true; - } - } catch (DOMException exc) { - CUIPlugin.log(exc); + if (isLocalVariable((IVariable) binding)) { + return true; } } } @@ -729,20 +719,30 @@ public class SemanticHighlightings { return false; } - public static boolean isLocalScope(IScope scope) { - while (scope != null) { - if (scope instanceof ICPPFunctionScope || - scope instanceof ICPPBlockScope || - scope instanceof ICFunctionScope) { - return true; - } - try { - scope= scope.getParent(); - } catch (DOMException e) { - scope= null; - } - } - return false; + public static boolean isLocalVariable(IVariable variable) { + // A variable marked 'extern' declares a global + // variable even if the declaration is local. + if (variable.isExtern()) { + return false; + } + try { + IScope scope= variable.getScope(); + while (scope != null) { + if (scope instanceof ICPPFunctionScope || + scope instanceof ICPPBlockScope || + scope instanceof ICFunctionScope) { + return true; + } + try { + scope= scope.getParent(); + } catch (DOMException e) { + scope= null; + } + } + } catch (DOMException exc) { + CUIPlugin.log(exc); + } + return false; } } @@ -794,13 +794,8 @@ public class SemanticHighlightings { && !(binding instanceof IParameter) && !(binding instanceof ICPPTemplateNonTypeParameter) && !(binding instanceof IProblemBinding)) { - try { - IScope scope= binding.getScope(); - if (!LocalVariableHighlighting.isLocalScope(scope)) { - return true; - } - } catch (DOMException exc) { - CUIPlugin.log(exc); + if (!LocalVariableHighlighting.isLocalVariable((IVariable) binding)) { + return true; } } }