Skip to content

Commit ac37afa

Browse files
author
Eduardo Caldas
committed
[SyntaxTree] Unbox operators into tokens for nodes generated from CXXOperatorCallExpr
For an user define `<`, `x < y` would yield the syntax tree: ``` BinaryOperatorExpression |-IdExpression | `-UnqualifiedId | `-x |-IdExpression | `-UnqualifiedId | `-< `-IdExpression `-UnqualifiedId `-y ``` But there is no syntatic difference at call site between call site or built-in `<`. As such they should generate the same syntax tree, namely: ``` BinaryOperatorExpression |-IdExpression | `-UnqualifiedId | `-x |-< `-IdExpression `-UnqualifiedId `-y ``` Differential Revision: https://reviews.llvm.org/D85750
1 parent 88bbd30 commit ac37afa

File tree

2 files changed

+23
-38
lines changed

2 files changed

+23
-38
lines changed

clang/lib/Tooling/Syntax/BuildTree.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,23 +1007,26 @@ class BuildTreeVisitor : public RecursiveASTVisitor<BuildTreeVisitor> {
10071007
}
10081008

10091009
bool TraverseCXXOperatorCallExpr(CXXOperatorCallExpr *S) {
1010-
if (getOperatorNodeKind(*S) ==
1011-
syntax::NodeKind::PostfixUnaryOperatorExpression) {
1010+
// To construct a syntax tree of the same shape for calls to built-in and
1011+
// user-defined operators, ignore the `DeclRefExpr` that refers to the
1012+
// operator and treat it as a simple token. Do that by traversing
1013+
// arguments instead of children.
1014+
for (auto *child : S->arguments()) {
10121015
// A postfix unary operator is declared as taking two operands. The
10131016
// second operand is used to distinguish from its prefix counterpart. In
10141017
// the semantic AST this "phantom" operand is represented as a
10151018
// `IntegerLiteral` with invalid `SourceLocation`. We skip visiting this
10161019
// operand because it does not correspond to anything written in source
1017-
// code
1018-
for (auto *child : S->children()) {
1019-
if (child->getSourceRange().isInvalid())
1020-
continue;
1021-
if (!TraverseStmt(child))
1022-
return false;
1020+
// code.
1021+
if (child->getSourceRange().isInvalid()) {
1022+
assert(getOperatorNodeKind(*S) ==
1023+
syntax::NodeKind::PostfixUnaryOperatorExpression);
1024+
continue;
10231025
}
1024-
return WalkUpFromCXXOperatorCallExpr(S);
1025-
} else
1026-
return RecursiveASTVisitor::TraverseCXXOperatorCallExpr(S);
1026+
if (!TraverseStmt(child))
1027+
return false;
1028+
}
1029+
return WalkUpFromCXXOperatorCallExpr(S);
10271030
}
10281031

10291032
bool WalkUpFromCXXOperatorCallExpr(CXXOperatorCallExpr *S) {

clang/unittests/Tooling/Syntax/TreeTest.cpp

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2592,9 +2592,7 @@ void test(X x, X y, X* xp, int X::* pmi) {
25922592
| | |-IdExpression
25932593
| | | `-UnqualifiedId
25942594
| | | `-x
2595-
| | |-IdExpression
2596-
| | | `-UnqualifiedId
2597-
| | | `-=
2595+
| | |-=
25982596
| | `-IdExpression
25992597
| | `-UnqualifiedId
26002598
| | `-y
@@ -2605,9 +2603,7 @@ void test(X x, X y, X* xp, int X::* pmi) {
26052603
| | | `-IdExpression
26062604
| | | `-UnqualifiedId
26072605
| | | `-x
2608-
| | |-IdExpression
2609-
| | | `-UnqualifiedId
2610-
| | | `-+
2606+
| | |-+
26112607
| | `-IdExpression
26122608
| | `-UnqualifiedId
26132609
| | `-y
@@ -2617,9 +2613,7 @@ void test(X x, X y, X* xp, int X::* pmi) {
26172613
| | |-IdExpression
26182614
| | | `-UnqualifiedId
26192615
| | | `-x
2620-
| | |-IdExpression
2621-
| | | `-UnqualifiedId
2622-
| | | `-<
2616+
| | |-<
26232617
| | `-IdExpression
26242618
| | `-UnqualifiedId
26252619
| | `-y
@@ -2629,9 +2623,7 @@ void test(X x, X y, X* xp, int X::* pmi) {
26292623
| | |-IdExpression
26302624
| | | `-UnqualifiedId
26312625
| | | `-x
2632-
| | |-IdExpression
2633-
| | | `-UnqualifiedId
2634-
| | | `-<<
2626+
| | |-<<
26352627
| | `-IdExpression
26362628
| | `-UnqualifiedId
26372629
| | `-y
@@ -2641,9 +2633,7 @@ void test(X x, X y, X* xp, int X::* pmi) {
26412633
| | |-IdExpression
26422634
| | | `-UnqualifiedId
26432635
| | | `-x
2644-
| | |-IdExpression
2645-
| | | `-UnqualifiedId
2646-
| | | `-,
2636+
| | |-,
26472637
| | `-IdExpression
26482638
| | `-UnqualifiedId
26492639
| | `-y
@@ -2730,27 +2720,21 @@ void test(X x) {
27302720
|-{
27312721
|-ExpressionStatement
27322722
| |-PrefixUnaryOperatorExpression
2733-
| | |-IdExpression
2734-
| | | `-UnqualifiedId
2735-
| | | `-++
2723+
| | |-++
27362724
| | `-IdExpression
27372725
| | `-UnqualifiedId
27382726
| | `-x
27392727
| `-;
27402728
|-ExpressionStatement
27412729
| |-PrefixUnaryOperatorExpression
2742-
| | |-IdExpression
2743-
| | | `-UnqualifiedId
2744-
| | | `-!
2730+
| | |-!
27452731
| | `-IdExpression
27462732
| | `-UnqualifiedId
27472733
| | `-x
27482734
| `-;
27492735
|-ExpressionStatement
27502736
| |-PrefixUnaryOperatorExpression
2751-
| | |-IdExpression
2752-
| | | `-UnqualifiedId
2753-
| | | `-&
2737+
| | |-&
27542738
| | `-IdExpression
27552739
| | `-UnqualifiedId
27562740
| | `-x
@@ -2809,9 +2793,7 @@ void test(X x) {
28092793
| | |-IdExpression
28102794
| | | `-UnqualifiedId
28112795
| | | `-x
2812-
| | `-IdExpression
2813-
| | `-UnqualifiedId
2814-
| | `-++
2796+
| | `-++
28152797
| `-;
28162798
`-}
28172799
)txt"));

0 commit comments

Comments
 (0)