From 5acb3f2352035017c355a717238cf3f1e9624c9e Mon Sep 17 00:00:00 2001 From: Vassil Vassilev Date: Fri, 1 Jul 2016 13:30:08 +0000 Subject: [PATCH] Add forgotten test to r274348. llvm-svn: 274349 --- clang/unittests/AST/PostOrderASTVisitor.cpp | 123 ++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 clang/unittests/AST/PostOrderASTVisitor.cpp diff --git a/clang/unittests/AST/PostOrderASTVisitor.cpp b/clang/unittests/AST/PostOrderASTVisitor.cpp new file mode 100644 index 000000000000..719bc08b8470 --- /dev/null +++ b/clang/unittests/AST/PostOrderASTVisitor.cpp @@ -0,0 +1,123 @@ +//===- 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 "gtest/gtest.h" +#include "clang/AST/RecursiveASTVisitor.h" +#include "clang/Tooling/Tooling.h" + +using namespace clang; + +namespace { + + class RecordingVisitor + : public RecursiveASTVisitor { + + bool VisitPostOrder; + public: + explicit RecordingVisitor(bool VisitPostOrder) + : VisitPostOrder(VisitPostOrder) { + } + + // List of visited nodes during traversal. + std::vector VisitedNodes; + + bool shouldTraversePostOrder() const { return VisitPostOrder; } + + 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(Stmt *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( + "template class A {" + " class B {" + " int foo() { while(4) { int i = 9; } 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 expected = { + "4", "9", "i", "1", "3", "+", "2", "+", "return", "A::B::foo", "A::B", "A", "A::T" + }; + // 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( + "template 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 expected = { + "A", "A::B", "A::B::foo", "return", "+", "1", "2", "A::T" + }; + // 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]); + } +}