blob: 7846df3b934088223d44df10b034d76fa70db7aa [file] [log] [blame]
Manuel Klimekcb971c62012-04-04 12:07:46 +00001//===- unittest/Tooling/ToolingTest.cpp - Tooling unit 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#include "clang/AST/ASTConsumer.h"
11#include "clang/AST/DeclCXX.h"
12#include "clang/AST/DeclGroup.h"
13#include "clang/Frontend/FrontendAction.h"
14#include "clang/Frontend/FrontendActions.h"
15#include "clang/Tooling/CompilationDatabase.h"
16#include "clang/Tooling/Tooling.h"
17#include "gtest/gtest.h"
Alexander Kornienko5f60f602012-06-01 14:50:43 +000018#include <string>
Manuel Klimekcb971c62012-04-04 12:07:46 +000019
20namespace clang {
21namespace tooling {
22
23namespace {
24/// Takes an ast consumer and returns it from CreateASTConsumer. This only
25/// works with single translation unit compilations.
26class TestAction : public clang::ASTFrontendAction {
27 public:
28 /// Takes ownership of TestConsumer.
29 explicit TestAction(clang::ASTConsumer *TestConsumer)
30 : TestConsumer(TestConsumer) {}
31
32 protected:
33 virtual clang::ASTConsumer* CreateASTConsumer(
34 clang::CompilerInstance& compiler, StringRef dummy) {
35 /// TestConsumer will be deleted by the framework calling us.
36 return TestConsumer;
37 }
38
39 private:
40 clang::ASTConsumer * const TestConsumer;
41};
42
43class FindTopLevelDeclConsumer : public clang::ASTConsumer {
44 public:
45 explicit FindTopLevelDeclConsumer(bool *FoundTopLevelDecl)
46 : FoundTopLevelDecl(FoundTopLevelDecl) {}
47 virtual bool HandleTopLevelDecl(clang::DeclGroupRef DeclGroup) {
48 *FoundTopLevelDecl = true;
49 return true;
50 }
51 private:
52 bool * const FoundTopLevelDecl;
53};
54} // end namespace
55
Meador Ingec5613b22012-06-16 03:34:49 +000056TEST(runToolOnCode, FindsNoTopLevelDeclOnEmptyCode) {
Manuel Klimekcb971c62012-04-04 12:07:46 +000057 bool FoundTopLevelDecl = false;
58 EXPECT_TRUE(runToolOnCode(
59 new TestAction(new FindTopLevelDeclConsumer(&FoundTopLevelDecl)), ""));
NAKAMURA Takumi8f190372012-06-16 06:04:05 +000060#if !defined(_MSC_VER)
Meador Ingec5613b22012-06-16 03:34:49 +000061 EXPECT_FALSE(FoundTopLevelDecl);
NAKAMURA Takumi8f190372012-06-16 06:04:05 +000062#else
63 // FIXME: LangOpts.MicrosoftExt appends "class type_info;"
64 EXPECT_TRUE(FoundTopLevelDecl);
65#endif
Manuel Klimekcb971c62012-04-04 12:07:46 +000066}
67
68namespace {
69class FindClassDeclXConsumer : public clang::ASTConsumer {
70 public:
71 FindClassDeclXConsumer(bool *FoundClassDeclX)
72 : FoundClassDeclX(FoundClassDeclX) {}
73 virtual bool HandleTopLevelDecl(clang::DeclGroupRef GroupRef) {
74 if (CXXRecordDecl* Record = dyn_cast<clang::CXXRecordDecl>(
75 *GroupRef.begin())) {
76 if (Record->getName() == "X") {
77 *FoundClassDeclX = true;
78 }
79 }
80 return true;
81 }
82 private:
83 bool *FoundClassDeclX;
84};
85} // end namespace
86
87TEST(runToolOnCode, FindsClassDecl) {
88 bool FoundClassDeclX = false;
89 EXPECT_TRUE(runToolOnCode(new TestAction(
90 new FindClassDeclXConsumer(&FoundClassDeclX)), "class X;"));
91 EXPECT_TRUE(FoundClassDeclX);
92
93 FoundClassDeclX = false;
94 EXPECT_TRUE(runToolOnCode(new TestAction(
95 new FindClassDeclXConsumer(&FoundClassDeclX)), "class Y;"));
96 EXPECT_FALSE(FoundClassDeclX);
97}
98
99TEST(newFrontendActionFactory, CreatesFrontendActionFactoryFromType) {
100 llvm::OwningPtr<FrontendActionFactory> Factory(
101 newFrontendActionFactory<SyntaxOnlyAction>());
102 llvm::OwningPtr<FrontendAction> Action(Factory->create());
103 EXPECT_TRUE(Action.get() != NULL);
104}
105
106struct IndependentFrontendActionCreator {
Manuel Klimeke6df0ce2012-07-05 18:13:01 +0000107 ASTConsumer *newASTConsumer() {
108 return new FindTopLevelDeclConsumer(NULL);
109 }
Manuel Klimekcb971c62012-04-04 12:07:46 +0000110};
111
112TEST(newFrontendActionFactory, CreatesFrontendActionFactoryFromFactoryType) {
113 IndependentFrontendActionCreator Creator;
114 llvm::OwningPtr<FrontendActionFactory> Factory(
115 newFrontendActionFactory(&Creator));
116 llvm::OwningPtr<FrontendAction> Action(Factory->create());
117 EXPECT_TRUE(Action.get() != NULL);
118}
119
Alexander Kornienko5f60f602012-06-01 14:50:43 +0000120TEST(ToolInvocation, TestMapVirtualFile) {
121 clang::FileManager Files((clang::FileSystemOptions()));
122 std::vector<std::string> Args;
123 Args.push_back("tool-executable");
124 Args.push_back("-Idef");
125 Args.push_back("-fsyntax-only");
126 Args.push_back("test.cpp");
127 clang::tooling::ToolInvocation Invocation(Args, new SyntaxOnlyAction, &Files);
128 Invocation.mapVirtualFile("test.cpp", "#include <abc>\n");
129 Invocation.mapVirtualFile("def/abc", "\n");
130 EXPECT_TRUE(Invocation.run());
131}
132
Manuel Klimek9fb6b272012-10-25 08:49:11 +0000133struct VerifyEndCallback : public EndOfSourceFileCallback {
134 VerifyEndCallback() : Called(0), Matched(false) {}
135 virtual void run() {
136 ++Called;
137 }
138 ASTConsumer *newASTConsumer() {
139 return new FindTopLevelDeclConsumer(&Matched);
140 }
141 unsigned Called;
142 bool Matched;
143};
144
145TEST(newFrontendActionFactory, InjectsEndOfSourceFileCallback) {
146 VerifyEndCallback EndCallback;
147
148 FixedCompilationDatabase Compilations("/", std::vector<std::string>());
149 std::vector<std::string> Sources;
150 Sources.push_back("/a.cc");
151 Sources.push_back("/b.cc");
152 ClangTool Tool(Compilations, Sources);
153
154 Tool.mapVirtualFile("/a.cc", "void a() {}");
155 Tool.mapVirtualFile("/b.cc", "void b() {}");
156
157 Tool.run(newFrontendActionFactory(&EndCallback, &EndCallback));
158
159 EXPECT_TRUE(EndCallback.Matched);
160 EXPECT_EQ(2u, EndCallback.Called);
161}
162
Manuel Klimekcb971c62012-04-04 12:07:46 +0000163} // end namespace tooling
164} // end namespace clang