blob: 2921f3ead50e41c3e0c93d8846a942771bd145be [file] [log] [blame]
Vassil Vassilev7baef4702016-07-08 08:33:56 +00001//===- 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
19using namespace clang;
20
21namespace {
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 Parsonsb2b0cb72016-12-07 17:39:04 +000037 bool VisitUnaryOperator(UnaryOperator *Op) {
38 VisitedNodes.push_back(Op->getOpcodeStr(Op->getOpcode()));
39 return true;
40 }
41
Vassil Vassilev7baef4702016-07-08 08:33:56 +000042 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
80TEST(RecursiveASTVisitor, PostOrderTraversal) {
81 auto ASTUnit = tooling::buildASTFromCode(
82 "class A {"
83 " class B {"
Malcolm Parsonsa29d98f2016-12-07 20:38:20 +000084 " int foo() { while(4) { int i = 9; int j = -5; } return (1 + 3) + 2; }"
Vassil Vassilev7baef4702016-07-08 08:33:56 +000085 " };"
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 Parsonsa29d98f2016-12-07 20:38:20 +000094 std::vector<std::string> expected = {"4", "9", "i", "5", "-",
95 "j", "1", "3", "+", "2",
96 "+", "return", "A::B::foo", "A::B", "A"};
Vassil Vassilev7baef4702016-07-08 08:33:56 +000097 // 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
105TEST(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}