1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-08 08:45:44 +02:00

Bug 371839 - Highlighting of overloaded operator in macro expansion

Change-Id: Ifbf437b12f8f946c90b55bb4a0f99ce7cde9b261
This commit is contained in:
Nathan Ridge 2016-02-21 00:12:00 -05:00
parent bd887e6800
commit 4dfd64edff
2 changed files with 57 additions and 2 deletions

View file

@ -20,6 +20,7 @@ import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.parser.IToken; import org.eclipse.cdt.core.parser.IToken;
import org.eclipse.cdt.core.parser.Keywords; import org.eclipse.cdt.core.parser.Keywords;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode; import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
import org.eclipse.cdt.internal.core.dom.parser.ASTNodeSearch;
public class CPPASTImplicitName extends CPPASTName implements IASTImplicitName { public class CPPASTImplicitName extends CPPASTName implements IASTImplicitName {
@ -120,10 +121,52 @@ public class CPPASTImplicitName extends CPPASTName implements IASTImplicitName {
setOffsetAndLength(offset, 0); setOffsetAndLength(offset, 0);
} }
} catch (ExpansionOverlapsBoundaryException e) { } catch (ExpansionOverlapsBoundaryException e) {
ASTNode parent = (ASTNode) getParent(); if (!computeOperatorOffsetsFallback(relativeNode, trailing)) {
setOffsetAndLength(parent.getOffset() + parent.getLength(), 0); // Fall-back for the fall-back
ASTNode parent = (ASTNode) getParent();
setOffsetAndLength(parent.getOffset() + parent.getLength(), 0);
}
} }
} }
// Fallback algorithm to use in computeOperatorOffsets() when the operator is
// in a macro expansion.
private boolean computeOperatorOffsetsFallback(IASTNode relativeNode, boolean trailing) {
if (!(relativeNode instanceof ASTNode)) {
return false;
}
ASTNode relative = (ASTNode) relativeNode;
// Find the sequence numbers denoting the bounds of the leading or
// trailing syntax, much as IASTNode.getLeadingSyntax() or
// getTrailingSyntax() would. The code here follows the
// implementation of those functions closely.
ASTNodeSearch visitor = new ASTNodeSearch(relativeNode);
IASTNode sibling = trailing ? visitor.findRightSibling() : visitor.findLeftSibling();
IASTNode parent = sibling == null ? relativeNode.getParent() : null;
if (!((sibling == null || sibling instanceof ASTNode) &&
(parent == null || parent instanceof ASTNode))) {
return false;
}
ASTNode sib = (ASTNode) sibling;
ASTNode par = (ASTNode) parent;
int start = trailing ? relative.getOffset() + relative.getLength()
: sib != null ? sib.getOffset() + sib.getLength()
: par.getOffset();
int end = trailing ? sib != null ? sib.getOffset()
: par.getOffset() + par.getLength()
: relative.getOffset();
// If there is only one token within the bounds, it must be the
// operator token, and we have our answer.
if (end == start + 1) {
setOffsetAndLength(start, 1);
return true;
}
// Otherwise, give up.
return false;
}
public void setOperator(boolean isOperator) { public void setOperator(boolean isOperator) {
this.isOperator = isOperator; this.isOperator = isOperator;

View file

@ -534,4 +534,16 @@ public class SemanticHighlightingTest extends TestCase {
// in the argument to be colored with the keyword highlighting. // in the argument to be colored with the keyword highlighting.
makeAssertions(); makeAssertions();
} }
// #define MIRROR(arg) arg //$macroDefinition
// struct Foo { //$class
// bool operator==(const Foo&) const; //$methodDeclaration,class
// };
// int main() { //$functionDeclaration
// Foo a, b; //$class,localVariableDeclaration,localVariableDeclaration
// MIRROR(a == b); //$macroSubstitution,localVariable,overloadedOperator,localVariable
// }
public void testOverloadedOperatorInMacroExpansion_371839() throws Exception {
makeAssertions();
}
} }