Эх сурвалжийг харах

[RecursiveASTVisitor] Fix post-order traversal of UnaryOperator

Reviewers: aaron.ballman, klimek, doug.gregor, teemperor, rsmith

Subscribers: cfe-commits

Differential Revision: https://reviews.llvm.org/D26742

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@288923 91177308-0d34-0410-b5e6-96231b3b80d8
Malcolm Parsons 8 жил өмнө
parent
commit
ac2fcb858c

+ 2 - 1
include/clang/AST/RecursiveASTVisitor.h

@@ -357,7 +357,8 @@ public:
 #define OPERATOR(NAME)                                                         \
 #define OPERATOR(NAME)                                                         \
   bool TraverseUnary##NAME(UnaryOperator *S,                                   \
   bool TraverseUnary##NAME(UnaryOperator *S,                                   \
                            DataRecursionQueue *Queue = nullptr) {              \
                            DataRecursionQueue *Queue = nullptr) {              \
-    TRY_TO(WalkUpFromUnary##NAME(S));                                          \
+    if (!getDerived().shouldTraversePostOrder())                               \
+      TRY_TO(WalkUpFromUnary##NAME(S));                                        \
     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getSubExpr());                          \
     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getSubExpr());                          \
     return true;                                                               \
     return true;                                                               \
   }                                                                            \
   }                                                                            \

+ 7 - 2
unittests/AST/PostOrderASTVisitor.cpp

@@ -34,6 +34,11 @@ namespace {
 
 
     bool shouldTraversePostOrder() const { return VisitPostOrder; }
     bool shouldTraversePostOrder() const { return VisitPostOrder; }
 
 
+    bool VisitUnaryOperator(UnaryOperator *Op) {
+      VisitedNodes.push_back(Op->getOpcodeStr(Op->getOpcode()));
+      return true;
+    }
+
     bool VisitBinaryOperator(BinaryOperator *Op) {
     bool VisitBinaryOperator(BinaryOperator *Op) {
       VisitedNodes.push_back(Op->getOpcodeStr());
       VisitedNodes.push_back(Op->getOpcodeStr());
       return true;
       return true;
@@ -76,7 +81,7 @@ TEST(RecursiveASTVisitor, PostOrderTraversal) {
   auto ASTUnit = tooling::buildASTFromCode(
   auto ASTUnit = tooling::buildASTFromCode(
     "class A {"
     "class A {"
     "  class B {"
     "  class B {"
-    "    int foo() { while(4) { int i = 9; } return (1 + 3) + 2; }"
+    "    int foo() { while(4) { int i = 9; int j = -i; } return (1 + 3) + 2; }"
     "  };"
     "  };"
     "};"
     "};"
   );
   );
@@ -87,7 +92,7 @@ TEST(RecursiveASTVisitor, PostOrderTraversal) {
   Visitor.TraverseTranslationUnitDecl(TU);
   Visitor.TraverseTranslationUnitDecl(TU);
 
 
   std::vector<std::string> expected = {
   std::vector<std::string> expected = {
-    "4", "9", "i", "1", "3", "+", "2", "+", "return", "A::B::foo", "A::B", "A"
+    "4", "9", "i", "-", "j", "1", "3", "+", "2", "+", "return", "A::B::foo", "A::B", "A"
   };
   };
   // Compare the list of actually visited nodes
   // Compare the list of actually visited nodes
   // with the expected list of visited nodes.
   // with the expected list of visited nodes.