mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-31 12:55:40 +02:00
Bug 486682 - Syntax coloring of macro arguments that occur in reverse order in the AST
This patch also fixes a couple of other bugs related to syntax coloring of macro expansions, which are exposed by this change (bug 490415, bug 496696). Change-Id: I3c0030ff61e721e099dc50afc109dd44e37276a3
This commit is contained in:
parent
82f4fac87b
commit
e9c6ca09e8
3 changed files with 61 additions and 17 deletions
|
@ -30,7 +30,7 @@ import org.eclipse.core.runtime.IAdaptable;
|
||||||
* Models IASTNames as needed for the preprocessor statements and macro expansions.
|
* Models IASTNames as needed for the preprocessor statements and macro expansions.
|
||||||
* @since 5.0
|
* @since 5.0
|
||||||
*/
|
*/
|
||||||
class ASTPreprocessorName extends ASTPreprocessorNode implements IASTName {
|
public class ASTPreprocessorName extends ASTPreprocessorNode implements IASTName {
|
||||||
private final char[] fName;
|
private final char[] fName;
|
||||||
private final IBinding fBinding;
|
private final IBinding fBinding;
|
||||||
|
|
||||||
|
@ -236,6 +236,16 @@ class ASTMacroReferenceName extends ASTPreprocessorName {
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return super.getImageLocation();
|
// ASTNode.getImageLocation() computes an image location based on the node location.
|
||||||
|
// Macro reference names which are nested references rather than the name of the
|
||||||
|
// macro being expanded itself, have their node location set to the entire macro
|
||||||
|
// expansion (see LocationMap.pushMacroExpansion()), which doesn't produce a
|
||||||
|
// useful image location.
|
||||||
|
if (getParent() instanceof ASTMacroExpansion) {
|
||||||
|
if (((ASTMacroExpansion) getParent()).getContext().getMacroReference() == this) {
|
||||||
|
return super.getImageLocation();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -569,4 +569,37 @@ public class SemanticHighlightingTest extends TestCase {
|
||||||
public void testVariableTemplates_486672() throws Exception {
|
public void testVariableTemplates_486672() throws Exception {
|
||||||
makeAssertions();
|
makeAssertions();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// #define MACRO(Name, Type) Type Name(); //$macroDefinition
|
||||||
|
// typedef int Int; //$typedef
|
||||||
|
// class S { //$class
|
||||||
|
// MACRO(foo, Int) //$macroSubstitution,methodDeclaration,typedef
|
||||||
|
// };
|
||||||
|
public void testMethodNameInsideMacro_486682() throws Exception {
|
||||||
|
makeAssertions();
|
||||||
|
}
|
||||||
|
|
||||||
|
// #define IF_0(t, f) f //$macroDefinition
|
||||||
|
// #define IF(bit, t, f) IF_ ## bit(t, f) //$macroDefinition
|
||||||
|
// #define WALDO //$macroDefinition
|
||||||
|
// #define MAIN(...) int main() { __VA_ARGS__ } //$macroDefinition
|
||||||
|
//
|
||||||
|
// MAIN //$macroSubstitution
|
||||||
|
// (
|
||||||
|
// int x; //$localVariableDeclaration
|
||||||
|
// IF(0, WALDO, WALDO) //$macroSubstitution,macroSubstitution,macroSubstitution
|
||||||
|
// )
|
||||||
|
public void testLexicalColoringInsideMacroExpansion_490415() throws Exception {
|
||||||
|
makeAssertions();
|
||||||
|
}
|
||||||
|
|
||||||
|
// #define N1(x) x
|
||||||
|
// #define M1(x) N1(x)
|
||||||
|
|
||||||
|
// int main() { //$functionDeclaration
|
||||||
|
// M1(0); //$macroSubstitution
|
||||||
|
// }
|
||||||
|
public void testLexicalColoringInsideMacroExpansion_496696() throws Exception {
|
||||||
|
makeAssertions();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,6 +49,7 @@ import org.eclipse.cdt.ui.CUIPlugin;
|
||||||
|
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
|
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
|
||||||
import org.eclipse.cdt.internal.core.model.ASTCache;
|
import org.eclipse.cdt.internal.core.model.ASTCache;
|
||||||
|
import org.eclipse.cdt.internal.core.parser.scanner.ASTPreprocessorName;
|
||||||
|
|
||||||
import org.eclipse.cdt.internal.ui.editor.SemanticHighlightingManager.HighlightedPosition;
|
import org.eclipse.cdt.internal.ui.editor.SemanticHighlightingManager.HighlightedPosition;
|
||||||
import org.eclipse.cdt.internal.ui.editor.SemanticHighlightingManager.HighlightingStyle;
|
import org.eclipse.cdt.internal.ui.editor.SemanticHighlightingManager.HighlightingStyle;
|
||||||
|
@ -67,10 +68,8 @@ public class SemanticHighlightingReconciler implements ICReconcilingListener {
|
||||||
private class PositionCollector extends ASTVisitor {
|
private class PositionCollector extends ASTVisitor {
|
||||||
/** The semantic token */
|
/** The semantic token */
|
||||||
private SemanticToken fToken= new SemanticToken();
|
private SemanticToken fToken= new SemanticToken();
|
||||||
private int fMinLocation;
|
|
||||||
|
|
||||||
public PositionCollector(boolean visitImplicitNames) {
|
public PositionCollector(boolean visitImplicitNames) {
|
||||||
fMinLocation= -1;
|
|
||||||
shouldVisitTranslationUnit= true;
|
shouldVisitTranslationUnit= true;
|
||||||
shouldVisitNames= true;
|
shouldVisitNames= true;
|
||||||
shouldVisitDeclarations= true;
|
shouldVisitDeclarations= true;
|
||||||
|
@ -92,7 +91,6 @@ public class SemanticHighlightingReconciler implements ICReconcilingListener {
|
||||||
visitNode(macroDef.getName());
|
visitNode(macroDef.getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fMinLocation= -1;
|
|
||||||
|
|
||||||
// Visit macro expansions.
|
// Visit macro expansions.
|
||||||
IASTPreprocessorMacroExpansion[] macroExps= tu.getMacroExpansions();
|
IASTPreprocessorMacroExpansion[] macroExps= tu.getMacroExpansions();
|
||||||
|
@ -106,7 +104,6 @@ public class SemanticHighlightingReconciler implements ICReconcilingListener {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fMinLocation= -1;
|
|
||||||
|
|
||||||
// Visit ordinary code.
|
// Visit ordinary code.
|
||||||
return super.visit(tu);
|
return super.visit(tu);
|
||||||
|
@ -215,11 +212,18 @@ public class SemanticHighlightingReconciler implements ICReconcilingListener {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Fallback in case no image location available.
|
// Fallback in case no image location available.
|
||||||
IASTNodeLocation[] nodeLocations= node.getNodeLocations();
|
// Only use the fallback for nodes that are not preprocessor nodes,
|
||||||
if (nodeLocations.length == 1) {
|
// because in the case of nested macro expansions, a preprocessor node
|
||||||
IASTNodeLocation nodeLocation = nodeLocations[0];
|
// can have a node location that is not representative of its actual
|
||||||
if (!(nodeLocation instanceof IASTMacroExpansionLocation)) {
|
// image; such nodes should have an image location (accessed via
|
||||||
return nodeLocation;
|
// getImageLocation(), above) where appropriate.
|
||||||
|
if (!(node instanceof ASTPreprocessorName)) {
|
||||||
|
IASTNodeLocation[] nodeLocations= node.getNodeLocations();
|
||||||
|
if (nodeLocations.length == 1) {
|
||||||
|
IASTNodeLocation nodeLocation = nodeLocations[0];
|
||||||
|
if (!(nodeLocation instanceof IASTMacroExpansionLocation)) {
|
||||||
|
return nodeLocation;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -234,12 +238,9 @@ public class SemanticHighlightingReconciler implements ICReconcilingListener {
|
||||||
*/
|
*/
|
||||||
private void highlightLocation(IASTNodeLocation nodeLocation, HighlightingStyle highlightingStyle) {
|
private void highlightLocation(IASTNodeLocation nodeLocation, HighlightingStyle highlightingStyle) {
|
||||||
int offset= nodeLocation.getNodeOffset();
|
int offset= nodeLocation.getNodeOffset();
|
||||||
if (offset >= fMinLocation) {
|
int length= nodeLocation.getNodeLength();
|
||||||
int length= nodeLocation.getNodeLength();
|
if (offset > -1 && length > 0) {
|
||||||
if (offset > -1 && length > 0) {
|
addPosition(offset, length, highlightingStyle);
|
||||||
fMinLocation= offset + length;
|
|
||||||
addPosition(offset, length, highlightingStyle);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue