r288923 - [RecursiveASTVisitor] Fix post-order traversal of UnaryOperator

Malcolm Parsons via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 7 09:39:04 PST 2016


Author: malcolm.parsons
Date: Wed Dec  7 11:39:04 2016
New Revision: 288923

URL: http://llvm.org/viewvc/llvm-project?rev=288923&view=rev
Log:
[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

Modified:
    cfe/trunk/include/clang/AST/RecursiveASTVisitor.h
    cfe/trunk/unittests/AST/PostOrderASTVisitor.cpp

Modified: cfe/trunk/include/clang/AST/RecursiveASTVisitor.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/RecursiveASTVisitor.h?rev=288923&r1=288922&r2=288923&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/RecursiveASTVisitor.h (original)
+++ cfe/trunk/include/clang/AST/RecursiveASTVisitor.h Wed Dec  7 11:39:04 2016
@@ -357,7 +357,8 @@ public:
 #define OPERATOR(NAME)                                                         \
   bool TraverseUnary##NAME(UnaryOperator *S,                                   \
                            DataRecursionQueue *Queue = nullptr) {              \
-    TRY_TO(WalkUpFromUnary##NAME(S));                                          \
+    if (!getDerived().shouldTraversePostOrder())                               \
+      TRY_TO(WalkUpFromUnary##NAME(S));                                        \
     TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getSubExpr());                          \
     return true;                                                               \
   }                                                                            \

Modified: cfe/trunk/unittests/AST/PostOrderASTVisitor.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/AST/PostOrderASTVisitor.cpp?rev=288923&r1=288922&r2=288923&view=diff
==============================================================================
--- cfe/trunk/unittests/AST/PostOrderASTVisitor.cpp (original)
+++ cfe/trunk/unittests/AST/PostOrderASTVisitor.cpp Wed Dec  7 11:39:04 2016
@@ -34,6 +34,11 @@ namespace {
 
     bool shouldTraversePostOrder() const { return VisitPostOrder; }
 
+    bool VisitUnaryOperator(UnaryOperator *Op) {
+      VisitedNodes.push_back(Op->getOpcodeStr(Op->getOpcode()));
+      return true;
+    }
+
     bool VisitBinaryOperator(BinaryOperator *Op) {
       VisitedNodes.push_back(Op->getOpcodeStr());
       return true;
@@ -76,7 +81,7 @@ TEST(RecursiveASTVisitor, PostOrderTrave
   auto ASTUnit = tooling::buildASTFromCode(
     "class A {"
     "  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, PostOrderTrave
   Visitor.TraverseTranslationUnitDecl(TU);
 
   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
   // with the expected list of visited nodes.




More information about the cfe-commits mailing list