1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 14:42:11 +02:00

Bug 372004 - Locally declared extern variable

Change-Id: I33d634d6c63138910b2958b81f6d8df358e89e7d
Signed-off-by: Nathan Ridge <zeratul976@hotmail.com>
Reviewed-on: https://git.eclipse.org/r/23098
Tested-by: Hudson CI
Reviewed-by: Sergey Prigogin <eclipse.sprigogin@gmail.com>
IP-Clean: Sergey Prigogin <eclipse.sprigogin@gmail.com>
Tested-by: Sergey Prigogin <eclipse.sprigogin@gmail.com>
This commit is contained in:
Nathan Ridge 2014-03-10 02:31:22 -04:00 committed by Sergey Prigogin
parent c52eee2c40
commit a2a05a515a
4 changed files with 49 additions and 35 deletions

View file

@ -146,3 +146,8 @@ EMPTY int f();
//http://bugs.eclipse.org/340492
template< template<class> class U > class myClass {};
//http://bugs.eclipse.org/372004
void g() {
extern int globalVariable; // declared as global near top
}

View file

@ -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);

View file

@ -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);
}
}

View file

@ -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;
}
}
}