1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Use new node selection API and fix for 220392: empty macro is not highlighted as a macro reference

This commit is contained in:
Anton Leherbauer 2008-03-04 13:45:54 +00:00
parent 83a13921ac
commit d860addc6d
8 changed files with 229 additions and 541 deletions

View file

@ -91,7 +91,6 @@ import org.eclipse.core.runtime.OperationCanceledException;
public class CModelBuilder2 implements IContributedModelBuilder {
private final TranslationUnit fTranslationUnit;
private String fTranslationUnitFileName;
private ASTAccessVisibility fCurrentVisibility;
private Stack<ASTAccessVisibility> fVisibilityStack;
private IProgressMonitor fProgressMonitor;
@ -182,7 +181,6 @@ public class CModelBuilder2 implements IContributedModelBuilder {
* @throws DOMException
*/
private void buildModel(IASTTranslationUnit ast) throws CModelException, DOMException {
fTranslationUnitFileName= ast.getFilePath();
fVisibilityStack= new Stack<ASTAccessVisibility>();
// includes
@ -256,7 +254,7 @@ public class CModelBuilder2 implements IContributedModelBuilder {
}
private boolean isLocalToFile(IASTNode node) {
return fTranslationUnitFileName.equals(node.getContainingFilename());
return node.isPartOfTranslationUnitFile();
}
private Include createInclusion(Parent parent, IASTPreprocessorIncludeStatement inclusion, Set<Include> allIncludes) throws CModelException{

View file

@ -56,7 +56,7 @@ import org.eclipse.cdt.core.dom.ast.IASTInitializerExpression;
import org.eclipse.cdt.core.dom.ast.IASTInitializerList;
import org.eclipse.cdt.core.dom.ast.IASTLabelStatement;
import org.eclipse.cdt.core.dom.ast.IASTLiteralExpression;
import org.eclipse.cdt.core.dom.ast.IASTMacroExpansion;
import org.eclipse.cdt.core.dom.ast.IASTMacroExpansionLocation;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNamedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTNode;
@ -1466,7 +1466,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
}
private int visit(ICPPASTVisiblityLabel node) {
if (node.getNodeLocations()[0] instanceof IASTMacroExpansion) {
if (node.getNodeLocations()[0] instanceof IASTMacroExpansionLocation) {
skipNode(node);
} else {
switch (node.getVisibility()) {
@ -1911,7 +1911,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
formatLeftCurlyBrace(line, brace_position);
formatOpeningBrace(brace_position, preferences.insert_space_before_opening_brace_in_initializer_list);
if (preferences.insert_new_line_after_opening_brace_in_initializer_list) {
scribe.printNewLine();
scribe.startNewLine();
}
if (preferences.insert_space_after_opening_brace_in_initializer_list) {
scribe.space();
@ -2751,7 +2751,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
}
IASTNodeLocation[] locations= node.getNodeLocations();
if (locations.length == 0) {
} else if (locations[0] instanceof IASTMacroExpansion) {
} else if (locations[0] instanceof IASTMacroExpansionLocation) {
IASTFileLocation expansionLocation= locations[0].asFileLocation();
int startOffset= expansionLocation.getNodeOffset();
int endOffset= startOffset + expansionLocation.getNodeLength();
@ -2803,7 +2803,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
IASTNodeLocation[] locations= node.getNodeLocations();
for (int i= 0; i < locations.length; i++) {
IASTNodeLocation nodeLocation= locations[i];
if (nodeLocation instanceof IASTMacroExpansion) {
if (nodeLocation instanceof IASTMacroExpansionLocation) {
IASTFileLocation expansionLocation= nodeLocation.asFileLocation();
int startOffset= expansionLocation.getNodeOffset();
int endOffset= startOffset + expansionLocation.getNodeLength();
@ -2908,7 +2908,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
IASTNodeLocation[] locations= node.getNodeLocations();
if (locations.length == 0) {
} else if (node instanceof IASTProblemHolder) {
} else if (locations[0] instanceof IASTMacroExpansion) {
} else if (locations[0] instanceof IASTMacroExpansionLocation) {
IASTFileLocation expansionLocation= locations[0].asFileLocation();
IASTFileLocation fileLocation= node.getFileLocation();
return expansionLocation.getNodeOffset() == fileLocation.getNodeOffset();

View file

@ -1,5 +1,6 @@
#define INT int
#define FUNCTION_MACRO(arg) globalFunc(arg)
#define EMPTY_MACRO(arg)
enum Enumeration {
enumerator
@ -11,6 +12,7 @@ static int globalStaticVariable = 0;
void globalFunc(int a);
static void globalStaticFunc() {
EMPTY_MACRO(n);
};
class Base1 {};
@ -134,3 +136,7 @@ int f()
{
return n;
}
//http://bugs.eclipse.org/220392
#define EMPTY
EMPTY int f();

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2007 IBM Corporation and others.
* Copyright (c) 2000, 2008 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@ -39,13 +39,13 @@ public class SemanticHighlightingTest extends AbstractSemanticHighlightingTest {
setUpSemanticHighlighting(SemanticHighlightings.STATIC_FIELD);
Position[] actual= getSemanticHighlightingPositions();
Position[] expected= new Position[] {
createPosition(23, 15, 14),
createPosition(25, 21, 19),
createPosition(42, 21, 20),
createPosition(43, 15, 15),
createPosition(57, 21, 20),
createPosition(58, 15, 15),
createPosition(118, 20, 15),
createPosition(25, 15, 14),
createPosition(27, 21, 19),
createPosition(44, 21, 20),
createPosition(45, 15, 15),
createPosition(59, 21, 20),
createPosition(60, 15, 15),
createPosition(120, 20, 15),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
@ -55,31 +55,31 @@ public class SemanticHighlightingTest extends AbstractSemanticHighlightingTest {
setUpSemanticHighlighting(SemanticHighlightings.FIELD);
Position[] actual= getSemanticHighlightingPositions();
Position[] expected= new Position[] {
createPosition(23, 15, 14),
createPosition(24, 14, 13),
createPosition(25, 21, 19),
createPosition(26, 8, 8),
createPosition(42, 21, 20),
createPosition(43, 15, 15),
createPosition(44, 15, 14),
createPosition(45, 8, 9),
createPosition(57, 21, 20),
createPosition(58, 15, 15),
createPosition(59, 15, 14),
createPosition(60, 8, 9),
createPosition(75, 7, 5),
createPosition(76, 7, 5),
createPosition(78, 8, 5),
createPosition(79, 8, 5),
createPosition(87, 8, 11),
createPosition(91, 8, 10),
createPosition(105, 11, 9),
createPosition(110, 11, 8),
createPosition(115, 8, 11),
createPosition(117, 7, 10),
createPosition(118, 20, 15),
createPosition(121, 11, 10),
createPosition(121, 28, 11),
createPosition(25, 15, 14),
createPosition(26, 14, 13),
createPosition(27, 21, 19),
createPosition(28, 8, 8),
createPosition(44, 21, 20),
createPosition(45, 15, 15),
createPosition(46, 15, 14),
createPosition(47, 8, 9),
createPosition(59, 21, 20),
createPosition(60, 15, 15),
createPosition(61, 15, 14),
createPosition(62, 8, 9),
createPosition(77, 7, 5),
createPosition(78, 7, 5),
createPosition(80, 8, 5),
createPosition(81, 8, 5),
createPosition(89, 8, 11),
createPosition(93, 8, 10),
createPosition(107, 11, 9),
createPosition(112, 11, 8),
createPosition(117, 8, 11),
createPosition(119, 7, 10),
createPosition(120, 20, 15),
createPosition(123, 11, 10),
createPosition(123, 28, 11),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
@ -89,16 +89,16 @@ public class SemanticHighlightingTest extends AbstractSemanticHighlightingTest {
setUpSemanticHighlighting(SemanticHighlightings.METHOD_DECLARATION);
Position[] actual= getSemanticHighlightingPositions();
Position[] expected= new Position[] {
createPosition(28, 15, 15),
createPosition(33, 8, 9),
createPosition(47, 15, 16),
createPosition(48, 8, 10),
createPosition(62, 15, 16),
createPosition(63, 8, 10),
createPosition(77, 4, 13),
createPosition(104, 4, 26),
createPosition(108, 4, 25),
createPosition(113, 4, 32),
createPosition(30, 15, 15),
createPosition(35, 8, 9),
createPosition(49, 15, 16),
createPosition(50, 8, 10),
createPosition(64, 15, 16),
createPosition(65, 8, 10),
createPosition(79, 4, 13),
createPosition(106, 4, 26),
createPosition(110, 4, 25),
createPosition(115, 4, 32),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
@ -107,19 +107,19 @@ public class SemanticHighlightingTest extends AbstractSemanticHighlightingTest {
public void testMethodHighlighting() throws Exception {
setUpSemanticHighlighting(SemanticHighlightings.METHOD);
Position[] expected= new Position[] {
createPosition(28, 15, 15),
createPosition(33, 8, 9),
createPosition(47, 15, 16),
createPosition(48, 8, 10),
createPosition(62, 15, 16),
createPosition(63, 8, 10),
createPosition(77, 4, 13),
createPosition(104, 4, 26),
createPosition(108, 4, 25),
createPosition(113, 4, 32),
createPosition(114, 23, 9),
createPosition(118, 4, 15),
createPosition(125, 13, 9),
createPosition(30, 15, 15),
createPosition(35, 8, 9),
createPosition(49, 15, 16),
createPosition(50, 8, 10),
createPosition(64, 15, 16),
createPosition(65, 8, 10),
createPosition(79, 4, 13),
createPosition(106, 4, 26),
createPosition(110, 4, 25),
createPosition(115, 4, 32),
createPosition(116, 23, 9),
createPosition(120, 4, 15),
createPosition(127, 13, 9),
};
Position[] actual= getSemanticHighlightingPositions();
if (PRINT_POSITIONS) System.out.println(toString(actual));
@ -129,7 +129,7 @@ public class SemanticHighlightingTest extends AbstractSemanticHighlightingTest {
public void testStaticMethodInvocationHighlighting() throws Exception {
setUpSemanticHighlighting(SemanticHighlightings.STATIC_METHOD_INVOCATION);
Position[] expected= new Position[] {
createPosition(118, 4, 15),
createPosition(120, 4, 15),
};
Position[] actual= getSemanticHighlightingPositions();
if (PRINT_POSITIONS) System.out.println(toString(actual));
@ -139,10 +139,10 @@ public class SemanticHighlightingTest extends AbstractSemanticHighlightingTest {
public void testLocalVariableDeclarationHighlighting() throws Exception {
setUpSemanticHighlighting(SemanticHighlightings.LOCAL_VARIABLE_DECLARATION);
Position[] expected= new Position[] {
createPosition(109, 8, 8),
createPosition(114, 15, 2),
createPosition(116, 13, 2),
createPosition(124, 13, 8),
createPosition(111, 8, 8),
createPosition(116, 15, 2),
createPosition(118, 13, 2),
createPosition(126, 13, 8),
};
Position[] actual= getSemanticHighlightingPositions();
if (PRINT_POSITIONS) System.out.println(toString(actual));
@ -152,12 +152,12 @@ public class SemanticHighlightingTest extends AbstractSemanticHighlightingTest {
public void testLocalVariableReferencesHighlighting() throws Exception {
setUpSemanticHighlighting(SemanticHighlightings.LOCAL_VARIABLE);
Position[] expected= new Position[] {
createPosition(110, 22, 8),
createPosition(115, 4, 2),
createPosition(112, 22, 8),
createPosition(117, 4, 2),
createPosition(121, 8, 2),
createPosition(121, 24, 2),
createPosition(125, 4, 8),
createPosition(119, 4, 2),
createPosition(123, 8, 2),
createPosition(123, 24, 2),
createPosition(127, 4, 8),
};
Position[] actual= getSemanticHighlightingPositions();
if (PRINT_POSITIONS) System.out.println(toString(actual));
@ -168,14 +168,14 @@ public class SemanticHighlightingTest extends AbstractSemanticHighlightingTest {
setUpSemanticHighlighting(SemanticHighlightings.PARAMETER_VARIABLE);
Position[] actual= getSemanticHighlightingPositions();
Position[] expected= new Position[] {
createPosition(11, 20, 1),
createPosition(28, 35, 3),
createPosition(29, 23, 3),
createPosition(30, 19, 3),
createPosition(77, 21, 4),
createPosition(77, 30, 4),
createPosition(78, 16, 4),
createPosition(79, 16, 4),
createPosition(12, 20, 1),
createPosition(30, 35, 3),
createPosition(31, 23, 3),
createPosition(32, 19, 3),
createPosition(79, 21, 4),
createPosition(79, 30, 4),
createPosition(80, 16, 4),
createPosition(81, 16, 4),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
@ -185,16 +185,16 @@ public class SemanticHighlightingTest extends AbstractSemanticHighlightingTest {
setUpSemanticHighlighting(SemanticHighlightings.TEMPLATE_PARAMETER);
Position[] actual= getSemanticHighlightingPositions();
Position[] expected= new Position[] {
createPosition(74, 15, 2),
createPosition(74, 25, 2),
createPosition(75, 4, 2),
createPosition(76, 4, 2),
createPosition(77, 18, 2),
createPosition(77, 27, 2),
createPosition(83, 15, 2),
createPosition(83, 66, 2),
createPosition(131, 14, 1),
createPosition(134, 9, 1),
createPosition(76, 15, 2),
createPosition(76, 25, 2),
createPosition(77, 4, 2),
createPosition(78, 4, 2),
createPosition(79, 18, 2),
createPosition(79, 27, 2),
createPosition(85, 15, 2),
createPosition(85, 66, 2),
createPosition(133, 14, 1),
createPosition(136, 9, 1),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
@ -204,10 +204,10 @@ public class SemanticHighlightingTest extends AbstractSemanticHighlightingTest {
setUpSemanticHighlighting(SemanticHighlightings.ENUM);
Position[] actual= getSemanticHighlightingPositions();
Position[] expected= new Position[] {
createPosition(3, 5, 11),
createPosition(35, 9, 14),
createPosition(50, 9, 15),
createPosition(65, 9, 15),
createPosition(4, 5, 11),
createPosition(37, 9, 14),
createPosition(52, 9, 15),
createPosition(67, 9, 15),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
@ -217,37 +217,37 @@ public class SemanticHighlightingTest extends AbstractSemanticHighlightingTest {
setUpSemanticHighlighting(SemanticHighlightings.CLASS);
Position[] actual= getSemanticHighlightingPositions();
Position[] expected= new Position[] {
createPosition(15, 6, 5),
createPosition(16, 6, 5),
createPosition(18, 6, 14),
createPosition(18, 23, 5),
createPosition(18, 30, 5),
createPosition(20, 17, 11),
createPosition(36, 10, 8),
createPosition(37, 10, 9),
createPosition(17, 6, 5),
createPosition(18, 6, 5),
createPosition(20, 6, 14),
createPosition(20, 23, 5),
createPosition(20, 30, 5),
createPosition(22, 17, 11),
createPosition(38, 10, 8),
createPosition(39, 12, 8),
createPosition(51, 10, 9),
createPosition(52, 10, 10),
createPosition(39, 10, 9),
createPosition(40, 10, 8),
createPosition(41, 12, 8),
createPosition(53, 10, 9),
createPosition(54, 12, 9),
createPosition(66, 10, 9),
createPosition(67, 10, 10),
createPosition(54, 10, 10),
createPosition(55, 10, 9),
createPosition(56, 12, 9),
createPosition(68, 10, 9),
createPosition(69, 12, 9),
createPosition(74, 35, 13),
createPosition(83, 25, 24),
createPosition(83, 52, 13),
createPosition(83, 70, 5),
createPosition(86, 7, 9),
createPosition(90, 6, 8),
createPosition(94, 8, 8),
createPosition(104, 4, 14),
createPosition(108, 4, 14),
createPosition(113, 4, 14),
createPosition(114, 4, 9),
createPosition(116, 4, 8),
createPosition(124, 4, 8),
createPosition(69, 10, 10),
createPosition(70, 10, 9),
createPosition(71, 12, 9),
createPosition(76, 35, 13),
createPosition(85, 25, 24),
createPosition(85, 52, 13),
createPosition(85, 70, 5),
createPosition(88, 7, 9),
createPosition(92, 6, 8),
createPosition(96, 8, 8),
createPosition(106, 4, 14),
createPosition(110, 4, 14),
createPosition(115, 4, 14),
createPosition(116, 4, 9),
createPosition(118, 4, 8),
createPosition(126, 4, 8),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
@ -257,11 +257,12 @@ public class SemanticHighlightingTest extends AbstractSemanticHighlightingTest {
setUpSemanticHighlighting(SemanticHighlightings.FUNCTION_DECLARATION);
Position[] actual= getSemanticHighlightingPositions();
Position[] expected= new Position[] {
createPosition(11, 5, 10),
createPosition(12, 12, 16),
createPosition(19, 16, 10),
createPosition(98, 8, 13),
createPosition(132, 4, 1),
createPosition(12, 5, 10),
createPosition(13, 12, 16),
createPosition(21, 16, 10),
createPosition(100, 8, 13),
createPosition(134, 4, 1),
createPosition(141, 10, 1),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
@ -271,14 +272,15 @@ public class SemanticHighlightingTest extends AbstractSemanticHighlightingTest {
setUpSemanticHighlighting(SemanticHighlightings.FUNCTION);
Position[] actual= getSemanticHighlightingPositions();
Position[] expected= new Position[] {
createPosition(11, 5, 10),
createPosition(12, 12, 16),
createPosition(19, 16, 10),
createPosition(30, 8, 10),
createPosition(98, 8, 13),
createPosition(99, 1, 16),
createPosition(126, 4, 11),
createPosition(132, 4, 1),
createPosition(12, 5, 10),
createPosition(13, 12, 16),
createPosition(21, 16, 10),
createPosition(32, 8, 10),
createPosition(100, 8, 13),
createPosition(101, 1, 16),
createPosition(128, 4, 11),
createPosition(134, 4, 1),
createPosition(141, 10, 1),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
@ -289,12 +291,12 @@ public class SemanticHighlightingTest extends AbstractSemanticHighlightingTest {
setUpSemanticHighlighting(SemanticHighlightings.GLOBAL_VARIABLE);
Position[] actual= getSemanticHighlightingPositions();
Position[] expected= new Position[] {
createPosition(7, 10, 14),
createPosition(8, 4, 14),
createPosition(9, 11, 20),
createPosition(31, 15, 20),
createPosition(97, 8, 12),
createPosition(100, 8, 12),
createPosition(8, 10, 14),
createPosition(9, 4, 14),
createPosition(10, 11, 20),
createPosition(33, 15, 20),
createPosition(99, 8, 12),
createPosition(102, 8, 12),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
@ -306,6 +308,8 @@ public class SemanticHighlightingTest extends AbstractSemanticHighlightingTest {
Position[] expected= new Position[] {
createPosition(0, 8, 3),
createPosition(1, 8, 14),
createPosition(2, 8, 11),
createPosition(140, 8, 5),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
@ -315,11 +319,13 @@ public class SemanticHighlightingTest extends AbstractSemanticHighlightingTest {
setUpSemanticHighlighting(SemanticHighlightings.MACRO_REFERENCE);
Position[] actual= getSemanticHighlightingPositions();
Position[] expected= new Position[] {
createPosition(29, 8, 14),
createPosition(104, 0, 3),
createPosition(108, 0, 3),
createPosition(113, 0, 3),
createPosition(120, 4, 14),
createPosition(14, 4, 11),
createPosition(31, 8, 14),
createPosition(106, 0, 3),
createPosition(110, 0, 3),
createPosition(115, 0, 3),
createPosition(122, 4, 14),
createPosition(141, 0, 5),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
@ -329,10 +335,10 @@ public class SemanticHighlightingTest extends AbstractSemanticHighlightingTest {
setUpSemanticHighlighting(SemanticHighlightings.TYPEDEF);
Position[] actual= getSemanticHighlightingPositions();
Position[] expected= new Position[] {
createPosition(39, 21, 10),
createPosition(54, 22, 11),
createPosition(69, 22, 11),
createPosition(94, 17, 6),
createPosition(41, 21, 10),
createPosition(56, 22, 11),
createPosition(71, 22, 11),
createPosition(96, 17, 6),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
@ -342,7 +348,7 @@ public class SemanticHighlightingTest extends AbstractSemanticHighlightingTest {
setUpSemanticHighlighting(SemanticHighlightings.NAMESPACE);
Position[] actual= getSemanticHighlightingPositions();
Position[] expected= new Position[] {
createPosition(96, 10, 2),
createPosition(98, 10, 2),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
@ -352,8 +358,8 @@ public class SemanticHighlightingTest extends AbstractSemanticHighlightingTest {
setUpSemanticHighlighting(SemanticHighlightings.LABEL);
Position[] actual= getSemanticHighlightingPositions();
Position[] expected= new Position[] {
createPosition(119, 0, 5),
createPosition(121, 46, 5),
createPosition(121, 0, 5),
createPosition(123, 46, 5),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
@ -363,10 +369,10 @@ public class SemanticHighlightingTest extends AbstractSemanticHighlightingTest {
setUpSemanticHighlighting(SemanticHighlightings.ENUMERATOR);
Position[] actual= getSemanticHighlightingPositions();
Position[] expected= new Position[] {
createPosition(4, 4, 10),
createPosition(35, 25, 13),
createPosition(50, 26, 14),
createPosition(65, 26, 14),
createPosition(5, 4, 10),
createPosition(37, 25, 13),
createPosition(52, 26, 14),
createPosition(67, 26, 14),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
@ -376,7 +382,7 @@ public class SemanticHighlightingTest extends AbstractSemanticHighlightingTest {
setUpSemanticHighlighting(SemanticHighlightings.PROBLEM);
Position[] actual= getSemanticHighlightingPositions();
Position[] expected= new Position[] {
createPosition(122, 4, 13),
createPosition(124, 4, 13),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
@ -386,8 +392,8 @@ public class SemanticHighlightingTest extends AbstractSemanticHighlightingTest {
setUpSemanticHighlighting(SemanticHighlightings.EXTERNAL_SDK);
Position[] actual= getSemanticHighlightingPositions();
Position[] expected= new Position[] {
createPosition(125, 13, 9),
createPosition(126, 4, 11),
createPosition(127, 13, 9),
createPosition(128, 4, 11),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);

View file

@ -158,12 +158,10 @@ import com.ibm.icu.text.BreakIterator;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.CCorePreferenceConstants;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTNodeSelector;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.gnu.cpp.GPPLanguage;
import org.eclipse.cdt.core.formatter.DefaultCodeFormatterConstants;
import org.eclipse.cdt.core.model.CModelException;
@ -3079,48 +3077,15 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IC
OccurrenceLocation[] locations= null;
IASTNode selectedNode= astRoot.selectNodeForLocation(astRoot.getFilePath(), wordRegion.getOffset(), wordRegion.getLength());
if (selectedNode == null && astRoot instanceof ICPPASTTranslationUnit && wordRegion.getOffset() > 0) {
// destructor?
try {
if (document.getChar(wordRegion.getOffset() - 1) == '~') {
selectedNode= astRoot.selectNodeForLocation(astRoot.getFilePath(), wordRegion.getOffset() - 1, wordRegion.getLength() + 1);
}
} catch (BadLocationException exc) {
// should not happen
}
}
final class NameFinder extends ASTVisitor {
private IASTName fName;
IASTNodeSelector selector= astRoot.getNodeSelector(astRoot.getFilePath());
IASTName name= selector.findSurroundingName(wordRegion.getOffset(), wordRegion.getLength());
public NameFinder() {
shouldVisitNames= true;
}
public int visit(IASTName name) {
fName= name;
return PROCESS_ABORT;
}
public IASTName getName() {
return fName;
}
}
if (selectedNode != null) {
IASTName name;
if (selectedNode instanceof IASTName) {
name= (IASTName) selectedNode;
} else {
NameFinder nameFinder= new NameFinder();
selectedNode.accept(nameFinder);
name= nameFinder.getName();
}
if (name != null) {
IBinding binding= name.resolveBinding();
if (binding != null) {
OccurrencesFinder occurrencesFinder= new OccurrencesFinder();
if (occurrencesFinder.initialize(astRoot, name) == null) {
locations= occurrencesFinder.getOccurrences();
}
if (name != null) {
IBinding binding= name.resolveBinding();
if (binding != null) {
OccurrencesFinder occurrencesFinder= new OccurrencesFinder();
if (occurrencesFinder.initialize(astRoot, name) == null) {
locations= occurrencesFinder.getOccurrences();
}
}
}

View file

@ -29,32 +29,22 @@ import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchPartSite;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTImageLocation;
import org.eclipse.cdt.core.dom.ast.IASTInitializer;
import org.eclipse.cdt.core.dom.ast.IASTMacroExpansion;
import org.eclipse.cdt.core.dom.ast.IASTMacroExpansionLocation;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTNodeLocation;
import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorMacroDefinition;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorMacroExpansion;
import org.eclipse.cdt.core.dom.ast.IASTProblem;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IASTTypeId;
import org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier.IASTEnumerator;
import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCatchHandler;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionTryBlockDeclarator;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceDefinition;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateParameter;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier.ICPPASTBaseSpecifier;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ILanguage;
import org.eclipse.cdt.ui.CUIPlugin;
@ -74,108 +64,6 @@ import org.eclipse.cdt.internal.ui.text.ICReconcilingListener;
*/
public class SemanticHighlightingReconciler implements ICReconcilingListener {
/**
* AST visitor to test whether a node is a leaf node.
*/
public static final class ChildNodeFinder extends CPPASTVisitor {
{
shouldVisitNames= true;
shouldVisitDeclarations= true;
shouldVisitInitializers= true;
shouldVisitParameterDeclarations= true;
shouldVisitDeclarators= true;
shouldVisitDeclSpecifiers= true;
shouldVisitExpressions= true;
shouldVisitStatements= true;
shouldVisitTypeIds= true;
shouldVisitEnumerators= true;
shouldVisitTranslationUnit= false;
shouldVisitProblems= true;
shouldVisitBaseSpecifiers= true;
shouldVisitNamespaces= true;
shouldVisitTemplateParameters= true;
}
private int fVisits;
private int fFirstChildOffset;
private int processNode(IASTNode node) {
if (++fVisits > 1) {
final IASTFileLocation fileLocation = node.getFileLocation();
if (fileLocation != null) {
fFirstChildOffset= fileLocation.getNodeOffset();
return PROCESS_ABORT;
}
}
return PROCESS_CONTINUE;
}
@Override
public int visit(ICPPASTBaseSpecifier specifier) {
return processNode(specifier);
}
@Override
public int visit(ICPPASTNamespaceDefinition namespace) {
return processNode(namespace);
}
@Override
public int visit(ICPPASTTemplateParameter parameter) {
return processNode(parameter);
}
@Override
public int visit(IASTDeclaration declaration) {
return processNode(declaration);
}
@Override
public int visit(IASTDeclarator declarator) {
return processNode(declarator);
}
@Override
public int visit(IASTDeclSpecifier declSpec) {
return processNode(declSpec);
}
@Override
public int visit(IASTEnumerator enumerator) {
return processNode(enumerator);
}
@Override
public int visit(IASTExpression expression) {
return processNode(expression);
}
@Override
public int visit(IASTInitializer initializer) {
return processNode(initializer);
}
@Override
public int visit(IASTName name) {
return processNode(name);
}
@Override
public int visit(IASTParameterDeclaration parameterDeclaration) {
return processNode(parameterDeclaration);
}
@Override
public int visit(IASTProblem problem) {
return processNode(problem);
}
@Override
public int visit(IASTStatement statement) {
return processNode(statement);
}
@Override
public int visit(IASTTranslationUnit tu) {
return processNode(tu);
}
@Override
public int visit(IASTTypeId typeId) {
return processNode(typeId);
}
public int getFirstChildOffset(IASTNode node) {
fVisits= 0;
fFirstChildOffset= Integer.MAX_VALUE;
node.accept(this);
return fFirstChildOffset;
}
}
/**
* Collects positions from the AST.
*/
@ -186,7 +74,6 @@ public class SemanticHighlightingReconciler implements ICReconcilingListener {
shouldVisitDeclarations= true;
shouldVisitExpressions= true;
shouldVisitStatements= true;
shouldVisitDeclSpecifiers= true;
shouldVisitDeclarators= true;
shouldVisitNamespaces= true;
}
@ -194,9 +81,7 @@ public class SemanticHighlightingReconciler implements ICReconcilingListener {
/** The semantic token */
private SemanticToken fToken= new SemanticToken();
private String fFilePath;
private int fMinLocation;
private final ChildNodeFinder fgChildNodeFinder= new ChildNodeFinder();
public PositionCollector() {
fMinLocation= -1;
@ -207,7 +92,6 @@ public class SemanticHighlightingReconciler implements ICReconcilingListener {
*/
@Override
public int visit(IASTTranslationUnit tu) {
fFilePath= tu.getFilePath();
// visit macro definitions
IASTPreprocessorMacroDefinition[] macroDefs= tu.getMacroDefinitions();
@ -217,14 +101,25 @@ public class SemanticHighlightingReconciler implements ICReconcilingListener {
visitNode(macroDef.getName());
}
}
// TODO visit macro expansions
// IASTName[] macroExps= tu.getMacroExpansions();
// for (int i= 0; i < macroExps.length; i++) {
// IASTName macroExp= macroExps[i];
// if (macroExp.isPartOfTranslationUnitFile()) {
// visitMacroExpansion(macroExp);
// }
// }
fMinLocation= -1;
// visit macro expansions
IASTPreprocessorMacroExpansion[] macroExps= tu.getMacroExpansions();
for (int i= 0; i < macroExps.length; i++) {
IASTPreprocessorMacroExpansion macroExp= macroExps[i];
if (macroExp.isPartOfTranslationUnitFile()) {
IASTName macroRef= macroExp.getMacroReference();
visitNode(macroRef);
IASTName[] nestedMacroRefs= macroExp.getNestedMacroReferences();
for (int j= 0; j < nestedMacroRefs.length; j++) {
IASTName nestedMacroRef= nestedMacroRefs[j];
visitNode(nestedMacroRef);
}
}
}
fMinLocation= -1;
// visit ordinary code
return super.visit(tu);
}
@ -236,9 +131,6 @@ public class SemanticHighlightingReconciler implements ICReconcilingListener {
if (!declaration.isPartOfTranslationUnitFile()) {
return PROCESS_SKIP;
}
if (checkForMacro(declaration)) {
return PROCESS_SKIP;
}
return PROCESS_CONTINUE;
}
@ -267,20 +159,6 @@ public class SemanticHighlightingReconciler implements ICReconcilingListener {
if (!namespace.isPartOfTranslationUnitFile()) {
return PROCESS_SKIP;
}
if (checkForMacro(namespace)) {
return PROCESS_SKIP;
}
return PROCESS_CONTINUE;
}
/*
* @see org.eclipse.cdt.core.dom.ast.ASTVisitor#visit(org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier)
*/
@Override
public int visit(IASTDeclSpecifier declSpec) {
if (checkForMacro(declSpec)) {
return PROCESS_SKIP;
}
return PROCESS_CONTINUE;
}
@ -289,26 +167,12 @@ public class SemanticHighlightingReconciler implements ICReconcilingListener {
*/
@Override
public int visit(IASTDeclarator declarator) {
if (checkForMacro(declarator)) {
return PROCESS_SKIP;
}
if (declarator instanceof ICPPASTFunctionTryBlockDeclarator) {
shouldVisitCatchHandlers= false;
}
return PROCESS_CONTINUE;
}
/*
* @see org.eclipse.cdt.core.dom.ast.ASTVisitor#visit(org.eclipse.cdt.core.dom.ast.IASTExpression)
*/
@Override
public int visit(IASTExpression expression) {
if (checkForMacro(expression)) {
return PROCESS_SKIP;
}
return PROCESS_CONTINUE;
}
/*
* @see org.eclipse.cdt.core.dom.ast.ASTVisitor#visit(org.eclipse.cdt.core.dom.ast.IASTStatement)
*/
@ -317,9 +181,6 @@ public class SemanticHighlightingReconciler implements ICReconcilingListener {
if (!shouldVisitCatchHandlers && statement instanceof ICPPASTCatchHandler) {
return PROCESS_SKIP;
}
if (checkForMacro(statement)) {
return PROCESS_SKIP;
}
return PROCESS_CONTINUE;
}
@ -328,69 +189,12 @@ public class SemanticHighlightingReconciler implements ICReconcilingListener {
*/
@Override
public int visit(IASTName name) {
if (checkForMacro(name)) {
return PROCESS_SKIP;
}
if (visitNode(name)) {
return PROCESS_SKIP;
}
return PROCESS_CONTINUE;
}
private boolean checkForMacro(IASTNode node) {
int firstChildOffset= fgChildNodeFinder.getFirstChildOffset(node);
IASTNodeLocation[] nodeLocations= node.getNodeLocations();
for (int i= 0; i < nodeLocations.length; i++) {
IASTNodeLocation nodeLocation= nodeLocations[i];
if (nodeLocation instanceof IASTMacroExpansion) {
IASTNodeLocation useLocation= nodeLocation.asFileLocation();
if (useLocation != null) {
final int useOffset = useLocation.getNodeOffset();
if (useOffset > firstChildOffset) {
break;
}
if (useOffset > fMinLocation) {
fMinLocation= useOffset;
IASTPreprocessorMacroDefinition macroDef= ((IASTMacroExpansion)nodeLocation).getMacroDefinition();
final int macroLength;
IASTNodeLocation defLocation= macroDef.getName().getFileLocation();
if (defLocation != null) {
macroLength= defLocation.getNodeLength();
} else {
macroLength= macroDef.getName().toCharArray().length;
}
IASTNode macroNode= node.getTranslationUnit().selectNodeForLocation(fFilePath, useOffset, macroLength);
if (macroNode instanceof IASTPreprocessorMacroExpansion) {
macroNode= ((IASTPreprocessorMacroExpansion) macroNode).getMacroReference();
}
if (macroNode != null && visitMacro(macroNode, macroLength)) {
fMinLocation= useOffset + macroLength;
}
}
}
}
}
return false;
}
private boolean visitMacro(IASTNode node, int macroLength) {
fToken.update(node);
for (int i= 0, n= fJobSemanticHighlightings.length; i < n; ++i) {
SemanticHighlighting semanticHighlighting= fJobSemanticHighlightings[i];
if (fJobHighlightings[i].isEnabled() && semanticHighlighting.consumes(fToken)) {
if (node instanceof IASTName) {
addNameLocation((IASTName)node, fJobHighlightings[i]);
} else {
addMacroLocation(node.getFileLocation(), macroLength, fJobHighlightings[i]);
}
break;
}
}
fToken.clear();
// always consume this node
return true;
}
private boolean visitNode(IASTNode node) {
boolean consumed= false;
fToken.update(node);
@ -418,16 +222,23 @@ public class SemanticHighlightingReconciler implements ICReconcilingListener {
*/
private void addNameLocation(IASTName name, HighlightingStyle highlightingStyle) {
IASTImageLocation imageLocation= name.getImageLocation();
if (imageLocation == null || !fFilePath.equals(imageLocation.getFileName())) {
addNodeLocation(name.getFileLocation(), highlightingStyle);
} else {
int offset= imageLocation.getNodeOffset();
if (offset >= fMinLocation) {
int length= imageLocation.getNodeLength();
if (offset > -1 && length > 0) {
addPosition(offset, length, highlightingStyle);
if (imageLocation != null) {
if (imageLocation.getLocationKind() != IASTImageLocation.MACRO_DEFINITION) {
int offset= imageLocation.getNodeOffset();
if (offset >= fMinLocation) {
int length= imageLocation.getNodeLength();
if (offset > -1 && length > 0) {
fMinLocation= offset + length;
addPosition(offset, length, highlightingStyle);
}
}
}
} else {
// fallback in case no image location available
IASTNodeLocation[] nodeLocations= name.getNodeLocations();
if (nodeLocations.length == 1 && !(nodeLocations[0] instanceof IASTMacroExpansionLocation)) {
addNodeLocation(nodeLocations[0], highlightingStyle);
}
}
}
@ -445,29 +256,12 @@ public class SemanticHighlightingReconciler implements ICReconcilingListener {
if (offset >= fMinLocation) {
int length= nodeLocation.getNodeLength();
if (offset > -1 && length > 0) {
fMinLocation= offset + length;
addPosition(offset, length, highlighting);
}
}
}
/**
* Add the a location range for the given macro highlighting.
*
* @param macroUseLocaton The location of the macro occurrence
* @param macroLength the length of the macro name
* @param highlighting The highlighting
*/
private void addMacroLocation(IASTNodeLocation macroUseLocation, int macroLength, HighlightingStyle highlighting) {
if (macroUseLocation == null) {
return;
}
int offset= macroUseLocation.getNodeOffset();
int length= macroLength;
if (offset > -1 && length > 0) {
addPosition(offset, length, highlighting);
}
}
/**
* Add a position with the given range and highlighting iff it does not exist already.
*

View file

@ -30,7 +30,7 @@ public class OccurrencesFinder implements IOccurrencesFinder {
private IASTName fSelectedNode;
private IBinding fTarget;
private List/*<OccurrenceLocation>*/fResult;
private List<OccurrenceLocation> fResult;
private String fDescription;
public OccurrencesFinder() {
@ -52,7 +52,7 @@ public class OccurrencesFinder implements IOccurrencesFinder {
private void performSearch() {
if (fResult == null) {
fResult= new ArrayList/*<OccurrenceLocation>*/();
fResult= new ArrayList<OccurrenceLocation>();
IASTName[] names= fRoot.getDeclarationsInAST(fTarget);
for (int i= 0; i < names.length; i++) {
IASTName candidate= names[i];
@ -74,7 +74,7 @@ public class OccurrencesFinder implements IOccurrencesFinder {
performSearch();
if (fResult.isEmpty())
return null;
return (OccurrenceLocation[]) fResult.toArray(new OccurrenceLocation[fResult.size()]);
return fResult.toArray(new OccurrenceLocation[fResult.size()]);
}
public IASTTranslationUnit getASTRoot() {

View file

@ -28,16 +28,13 @@ import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTMacroExpansion;
import org.eclipse.cdt.core.dom.ast.IASTMacroExpansionLocation;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTNodeLocation;
import org.eclipse.cdt.core.dom.ast.IASTNodeSelector;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorMacroExpansion;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IMacroBinding;
@ -61,81 +58,6 @@ import org.eclipse.cdt.internal.ui.text.CHeuristicScanner;
*/
public class CMacroExpansionInput {
/**
* AST visitor to find a node (declaration, statement or expression) enclosing a given source range.
*/
private static class FindEnclosingNodeAction extends ASTVisitor {
{
shouldVisitTranslationUnit= true;
shouldVisitDeclarations = true;
shouldVisitStatements = true;
shouldVisitExpressions = true;
}
private final int fOffset;
private final int fEndOffset;
private final String fFilePath;
private IASTNode fBestMatch= null;
private int fBestOffset= -1;
private int fBestEndOffset= Integer.MAX_VALUE;
public FindEnclosingNodeAction(String filePath, int offset, int length) {
fFilePath= filePath;
fOffset= offset;
fEndOffset= offset + length;
}
private int processNode(IASTNode node) {
IASTFileLocation location= node.getFileLocation();
if (location != null && fFilePath.equals(location.getFileName())) {
final int startOffset = location.getNodeOffset();
if (startOffset <= fOffset) {
int endOffset= startOffset + location.getNodeLength();
if (endOffset >= fEndOffset || node instanceof IASTTranslationUnit) {
if (startOffset > fBestOffset || endOffset < fBestEndOffset) {
fBestMatch= node;
fBestOffset= startOffset;
fBestEndOffset= endOffset;
boolean isPerfectMatch= startOffset == fOffset || endOffset == fEndOffset;
if (isPerfectMatch) {
return PROCESS_ABORT;
}
}
} else {
return PROCESS_SKIP;
}
} else {
return PROCESS_ABORT;
}
}
return PROCESS_CONTINUE;
}
@Override
public int visit(IASTTranslationUnit tu) {
return processNode(tu);
}
@Override
public int visit(IASTDeclaration declaration) {
return processNode(declaration);
}
@Override
public int visit(IASTExpression expression) {
return processNode(expression);
}
@Override
public int visit(IASTStatement statement) {
return processNode(statement);
}
public IASTNode getNode() {
return fBestMatch;
}
}
/**
* Computes the expansion region for a selection.
*/
@ -157,8 +79,9 @@ public class CMacroExpansionInput {
*/
public IStatus runOnAST(ILanguage lang, IASTTranslationUnit ast) {
if (ast != null) {
// try macro name match first
IASTNode node= ast.selectNodeForLocation(ast.getFilePath(), fTextRegion.getOffset(), fTextRegion.getLength());
final IASTNodeSelector nodeSelector = ast.getNodeSelector(ast.getFilePath());
// try exact macro name match first
IASTNode node= nodeSelector.findName(fTextRegion.getOffset(), fTextRegion.getLength());
if (node instanceof IASTName) {
IASTName macroName= (IASTName) node;
IBinding binding= macroName.getBinding();
@ -172,21 +95,17 @@ public class CMacroExpansionInput {
}
if (fAllowSelection) {
// selection
FindEnclosingNodeAction nodeFinder= new FindEnclosingNodeAction(ast.getFilePath(), fTextRegion.getOffset(), fTextRegion.getLength());
ast.accept(nodeFinder);
fEnclosingNode= nodeFinder.getNode();
fEnclosingNode= nodeSelector.findSurroundingNode(fTextRegion.getOffset(), fTextRegion.getLength());
if (fEnclosingNode != null) {
boolean macroOccurrence= false;
IASTNodeLocation[] locations= fEnclosingNode.getNodeLocations();
for (int i = 0; i < locations.length; i++) {
IASTNodeLocation location= locations[i];
if (location instanceof IASTMacroExpansion) {
if (location instanceof IASTMacroExpansionLocation) {
IASTFileLocation fileLocation= location.asFileLocation();
if (fileLocation != null && ast.getFilePath().equals(fileLocation.getFileName())) {
if (fTextRegion.overlapsWith(fileLocation.getNodeOffset(), fileLocation.getNodeLength())) {
nodeFinder= new FindEnclosingNodeAction(ast.getFilePath(), fileLocation.getNodeOffset(), fileLocation.getNodeLength());
ast.accept(nodeFinder);
addExpansionNode(nodeFinder.getNode());
addExpansionNode(nodeSelector.findSurroundingNode(fileLocation.getNodeOffset(), fileLocation.getNodeLength()));
macroOccurrence= true;
}
}