r323310 - Refactor RecursiveASTVisitor test for post-order traversal

Raphael Isemann via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 24 01:40:16 PST 2018


Author: teemperor
Date: Wed Jan 24 01:40:16 2018
New Revision: 323310

URL: http://llvm.org/viewvc/llvm-project?rev=323310&view=rev
Log:
Refactor RecursiveASTVisitor test for post-order traversal

Summary:
The new test is now in the right directory with the other ASTVisitor tests and uses
now the provided TestVisitor framework.

Subscribers: hintonda, v.g.vassilev, klimek, cfe-commits, mgorny

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

Added:
    cfe/trunk/unittests/Tooling/RecursiveASTVisitorTestPostOrderVisitor.cpp
Removed:
    cfe/trunk/unittests/AST/PostOrderASTVisitor.cpp
Modified:
    cfe/trunk/unittests/AST/CMakeLists.txt
    cfe/trunk/unittests/Tooling/CMakeLists.txt

Modified: cfe/trunk/unittests/AST/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/AST/CMakeLists.txt?rev=323310&r1=323309&r2=323310&view=diff
==============================================================================
--- cfe/trunk/unittests/AST/CMakeLists.txt (original)
+++ cfe/trunk/unittests/AST/CMakeLists.txt Wed Jan 24 01:40:16 2018
@@ -15,7 +15,6 @@ add_clang_unittest(ASTTests
   EvaluateAsRValueTest.cpp
   ExternalASTSourceTest.cpp
   NamedDeclPrinterTest.cpp
-  PostOrderASTVisitor.cpp
   SourceLocationTest.cpp
   StmtPrinterTest.cpp
   )

Removed: cfe/trunk/unittests/AST/PostOrderASTVisitor.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/AST/PostOrderASTVisitor.cpp?rev=323309&view=auto
==============================================================================
--- cfe/trunk/unittests/AST/PostOrderASTVisitor.cpp (original)
+++ cfe/trunk/unittests/AST/PostOrderASTVisitor.cpp (removed)
@@ -1,128 +0,0 @@
-//===- unittests/AST/PostOrderASTVisitor.cpp - Declaration printer tests --===//
-//
-//                     The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-// This file contains tests for the post-order traversing functionality
-// of RecursiveASTVisitor.
-//
-//===----------------------------------------------------------------------===//
-
-#include "clang/AST/RecursiveASTVisitor.h"
-#include "clang/Tooling/Tooling.h"
-#include "gtest/gtest.h"
-
-using namespace clang;
-
-namespace {
-
-  class RecordingVisitor
-    : public RecursiveASTVisitor<RecordingVisitor> {
-
-    bool VisitPostOrder;
-  public:
-    explicit RecordingVisitor(bool VisitPostOrder)
-      : VisitPostOrder(VisitPostOrder) {
-    }
-
-    // List of visited nodes during traversal.
-    std::vector<std::string> VisitedNodes;
-
-    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;
-    }
-
-    bool VisitIntegerLiteral(IntegerLiteral *Lit) {
-      VisitedNodes.push_back(Lit->getValue().toString(10, false));
-      return true;
-    }
-
-    bool VisitVarDecl(VarDecl* D) {
-      VisitedNodes.push_back(D->getNameAsString());
-      return true;
-    }
-
-    bool VisitCXXMethodDecl(CXXMethodDecl *D) {
-      VisitedNodes.push_back(D->getQualifiedNameAsString());
-      return true;
-    }
-
-    bool VisitReturnStmt(ReturnStmt *S) {
-      VisitedNodes.push_back("return");
-      return true;
-    }
-
-    bool VisitCXXRecordDecl(CXXRecordDecl *Declaration) {
-      VisitedNodes.push_back(Declaration->getQualifiedNameAsString());
-      return true;
-    }
-
-    bool VisitTemplateTypeParmType(TemplateTypeParmType *T) {
-      VisitedNodes.push_back(T->getDecl()->getQualifiedNameAsString());
-      return true;
-    }
-  };
-
-}
-
-TEST(RecursiveASTVisitor, PostOrderTraversal) {
-  auto ASTUnit = tooling::buildASTFromCode(
-    "class A {"
-    "  class B {"
-    "    int foo() { while(4) { int i = 9; int j = -5; } return (1 + 3) + 2; }"
-    "  };"
-    "};"
-  );
-  auto TU = ASTUnit->getASTContext().getTranslationUnitDecl();
-  // We traverse the translation unit and store all
-  // visited nodes.
-  RecordingVisitor Visitor(true);
-  Visitor.TraverseTranslationUnitDecl(TU);
-
-  std::vector<std::string> expected = {"4", "9",      "i",         "5",    "-",
-                                       "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.
-  ASSERT_EQ(expected.size(), Visitor.VisitedNodes.size());
-  for (std::size_t I = 0; I < expected.size(); I++) {
-    ASSERT_EQ(expected[I], Visitor.VisitedNodes[I]);
-  }
-}
-
-TEST(RecursiveASTVisitor, NoPostOrderTraversal) {
-  auto ASTUnit = tooling::buildASTFromCode(
-    "class A {"
-    "  class B {"
-    "    int foo() { return 1 + 2; }"
-    "  };"
-    "};"
-  );
-  auto TU = ASTUnit->getASTContext().getTranslationUnitDecl();
-  // We traverse the translation unit and store all
-  // visited nodes.
-  RecordingVisitor Visitor(false);
-  Visitor.TraverseTranslationUnitDecl(TU);
-
-  std::vector<std::string> expected = {
-    "A", "A::B", "A::B::foo", "return", "+", "1", "2"
-  };
-  // Compare the list of actually visited nodes
-  // with the expected list of visited nodes.
-  ASSERT_EQ(expected.size(), Visitor.VisitedNodes.size());
-  for (std::size_t I = 0; I < expected.size(); I++) {
-    ASSERT_EQ(expected[I], Visitor.VisitedNodes[I]);
-  }
-}

Modified: cfe/trunk/unittests/Tooling/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Tooling/CMakeLists.txt?rev=323310&r1=323309&r2=323310&view=diff
==============================================================================
--- cfe/trunk/unittests/Tooling/CMakeLists.txt (original)
+++ cfe/trunk/unittests/Tooling/CMakeLists.txt Wed Jan 24 01:40:16 2018
@@ -25,6 +25,7 @@ add_clang_unittest(ToolingTests
   RecursiveASTVisitorTestCallVisitor.cpp
   RecursiveASTVisitorTestDeclVisitor.cpp
   RecursiveASTVisitorTestExprVisitor.cpp
+  RecursiveASTVisitorTestPostOrderVisitor.cpp
   RecursiveASTVisitorTestTypeLocVisitor.cpp
   RefactoringActionRulesTest.cpp
   RefactoringCallbacksTest.cpp

Added: cfe/trunk/unittests/Tooling/RecursiveASTVisitorTestPostOrderVisitor.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Tooling/RecursiveASTVisitorTestPostOrderVisitor.cpp?rev=323310&view=auto
==============================================================================
--- cfe/trunk/unittests/Tooling/RecursiveASTVisitorTestPostOrderVisitor.cpp (added)
+++ cfe/trunk/unittests/Tooling/RecursiveASTVisitorTestPostOrderVisitor.cpp Wed Jan 24 01:40:16 2018
@@ -0,0 +1,116 @@
+//===- unittests/Tooling/RecursiveASTVisitorPostOrderASTVisitor.cpp -------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file contains tests for the post-order traversing functionality
+// of RecursiveASTVisitor.
+//
+//===----------------------------------------------------------------------===//
+
+#include "TestVisitor.h"
+
+using namespace clang;
+
+namespace {
+
+class RecordingVisitor : public TestVisitor<RecordingVisitor> {
+
+  bool VisitPostOrder;
+
+public:
+  explicit RecordingVisitor(bool VisitPostOrder)
+      : VisitPostOrder(VisitPostOrder) {}
+
+  // List of visited nodes during traversal.
+  std::vector<std::string> VisitedNodes;
+
+  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;
+  }
+
+  bool VisitIntegerLiteral(IntegerLiteral *Lit) {
+    VisitedNodes.push_back(Lit->getValue().toString(10, false));
+    return true;
+  }
+
+  bool VisitVarDecl(VarDecl *D) {
+    VisitedNodes.push_back(D->getNameAsString());
+    return true;
+  }
+
+  bool VisitCXXMethodDecl(CXXMethodDecl *D) {
+    VisitedNodes.push_back(D->getQualifiedNameAsString());
+    return true;
+  }
+
+  bool VisitReturnStmt(ReturnStmt *S) {
+    VisitedNodes.push_back("return");
+    return true;
+  }
+
+  bool VisitCXXRecordDecl(CXXRecordDecl *D) {
+    if (!D->isImplicit())
+      VisitedNodes.push_back(D->getQualifiedNameAsString());
+    return true;
+  }
+
+  bool VisitTemplateTypeParmType(TemplateTypeParmType *T) {
+    VisitedNodes.push_back(T->getDecl()->getQualifiedNameAsString());
+    return true;
+  }
+};
+} // namespace
+
+TEST(RecursiveASTVisitor, PostOrderTraversal) {
+  // We traverse the translation unit and store all visited nodes.
+  RecordingVisitor Visitor(true);
+  Visitor.runOver("class A {\n"
+                  "  class B {\n"
+                  "    int foo() {\n"
+                  "      while(4) { int i = 9; int j = -5; }\n"
+                  "      return (1 + 3) + 2; }\n"
+                  "  };\n"
+                  "};\n");
+
+  std::vector<std::string> expected = {"4", "9",      "i",         "5",    "-",
+                                       "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.
+  ASSERT_EQ(expected.size(), Visitor.VisitedNodes.size());
+  for (std::size_t I = 0; I < expected.size(); I++) {
+    ASSERT_EQ(expected[I], Visitor.VisitedNodes[I]);
+  }
+}
+
+TEST(RecursiveASTVisitor, NoPostOrderTraversal) {
+  // We traverse the translation unit and store all visited nodes.
+  RecordingVisitor Visitor(false);
+  Visitor.runOver("class A {\n"
+                  "  class B {\n"
+                  "    int foo() { return 1 + 2; }\n"
+                  "  };\n"
+                  "};\n");
+
+  std::vector<std::string> expected = {"A", "A::B", "A::B::foo", "return",
+                                       "+", "1",    "2"};
+  // Compare the list of actually visited nodes with the expected list of
+  // visited nodes.
+  ASSERT_EQ(expected.size(), Visitor.VisitedNodes.size());
+  for (std::size_t I = 0; I < expected.size(); I++) {
+    ASSERT_EQ(expected[I], Visitor.VisitedNodes[I]);
+  }
+}




More information about the cfe-commits mailing list