Vassil Vassilev | 7baef470 | 2016-07-08 08:33:56 +0000 | [diff] [blame] | 1 | //===- unittests/AST/PostOrderASTVisitor.cpp - Declaration printer tests --===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains tests for the post-order traversing functionality |
| 11 | // of RecursiveASTVisitor. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/AST/RecursiveASTVisitor.h" |
| 16 | #include "clang/Tooling/Tooling.h" |
| 17 | #include "gtest/gtest.h" |
| 18 | |
| 19 | using namespace clang; |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | class RecordingVisitor |
| 24 | : public RecursiveASTVisitor<RecordingVisitor> { |
| 25 | |
| 26 | bool VisitPostOrder; |
| 27 | public: |
| 28 | explicit RecordingVisitor(bool VisitPostOrder) |
| 29 | : VisitPostOrder(VisitPostOrder) { |
| 30 | } |
| 31 | |
| 32 | // List of visited nodes during traversal. |
| 33 | std::vector<std::string> VisitedNodes; |
| 34 | |
| 35 | bool shouldTraversePostOrder() const { return VisitPostOrder; } |
| 36 | |
Malcolm Parsons | b2b0cb7 | 2016-12-07 17:39:04 +0000 | [diff] [blame] | 37 | bool VisitUnaryOperator(UnaryOperator *Op) { |
| 38 | VisitedNodes.push_back(Op->getOpcodeStr(Op->getOpcode())); |
| 39 | return true; |
| 40 | } |
| 41 | |
Vassil Vassilev | 7baef470 | 2016-07-08 08:33:56 +0000 | [diff] [blame] | 42 | bool VisitBinaryOperator(BinaryOperator *Op) { |
| 43 | VisitedNodes.push_back(Op->getOpcodeStr()); |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | bool VisitIntegerLiteral(IntegerLiteral *Lit) { |
| 48 | VisitedNodes.push_back(Lit->getValue().toString(10, false)); |
| 49 | return true; |
| 50 | } |
| 51 | |
| 52 | bool VisitVarDecl(VarDecl* D) { |
| 53 | VisitedNodes.push_back(D->getNameAsString()); |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | bool VisitCXXMethodDecl(CXXMethodDecl *D) { |
| 58 | VisitedNodes.push_back(D->getQualifiedNameAsString()); |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | bool VisitReturnStmt(ReturnStmt *S) { |
| 63 | VisitedNodes.push_back("return"); |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | bool VisitCXXRecordDecl(CXXRecordDecl *Declaration) { |
| 68 | VisitedNodes.push_back(Declaration->getQualifiedNameAsString()); |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | bool VisitTemplateTypeParmType(TemplateTypeParmType *T) { |
| 73 | VisitedNodes.push_back(T->getDecl()->getQualifiedNameAsString()); |
| 74 | return true; |
| 75 | } |
| 76 | }; |
| 77 | |
| 78 | } |
| 79 | |
| 80 | TEST(RecursiveASTVisitor, PostOrderTraversal) { |
| 81 | auto ASTUnit = tooling::buildASTFromCode( |
| 82 | "class A {" |
| 83 | " class B {" |
Malcolm Parsons | a29d98f | 2016-12-07 20:38:20 +0000 | [diff] [blame] | 84 | " int foo() { while(4) { int i = 9; int j = -5; } return (1 + 3) + 2; }" |
Vassil Vassilev | 7baef470 | 2016-07-08 08:33:56 +0000 | [diff] [blame] | 85 | " };" |
| 86 | "};" |
| 87 | ); |
| 88 | auto TU = ASTUnit->getASTContext().getTranslationUnitDecl(); |
| 89 | // We traverse the translation unit and store all |
| 90 | // visited nodes. |
| 91 | RecordingVisitor Visitor(true); |
| 92 | Visitor.TraverseTranslationUnitDecl(TU); |
| 93 | |
Malcolm Parsons | a29d98f | 2016-12-07 20:38:20 +0000 | [diff] [blame] | 94 | std::vector<std::string> expected = {"4", "9", "i", "5", "-", |
| 95 | "j", "1", "3", "+", "2", |
| 96 | "+", "return", "A::B::foo", "A::B", "A"}; |
Vassil Vassilev | 7baef470 | 2016-07-08 08:33:56 +0000 | [diff] [blame] | 97 | // Compare the list of actually visited nodes |
| 98 | // with the expected list of visited nodes. |
| 99 | ASSERT_EQ(expected.size(), Visitor.VisitedNodes.size()); |
| 100 | for (std::size_t I = 0; I < expected.size(); I++) { |
| 101 | ASSERT_EQ(expected[I], Visitor.VisitedNodes[I]); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | TEST(RecursiveASTVisitor, NoPostOrderTraversal) { |
| 106 | auto ASTUnit = tooling::buildASTFromCode( |
| 107 | "class A {" |
| 108 | " class B {" |
| 109 | " int foo() { return 1 + 2; }" |
| 110 | " };" |
| 111 | "};" |
| 112 | ); |
| 113 | auto TU = ASTUnit->getASTContext().getTranslationUnitDecl(); |
| 114 | // We traverse the translation unit and store all |
| 115 | // visited nodes. |
| 116 | RecordingVisitor Visitor(false); |
| 117 | Visitor.TraverseTranslationUnitDecl(TU); |
| 118 | |
| 119 | std::vector<std::string> expected = { |
| 120 | "A", "A::B", "A::B::foo", "return", "+", "1", "2" |
| 121 | }; |
| 122 | // Compare the list of actually visited nodes |
| 123 | // with the expected list of visited nodes. |
| 124 | ASSERT_EQ(expected.size(), Visitor.VisitedNodes.size()); |
| 125 | for (std::size_t I = 0; I < expected.size(); I++) { |
| 126 | ASSERT_EQ(expected[I], Visitor.VisitedNodes[I]); |
| 127 | } |
| 128 | } |