blob: 85ab942387b3dd92c03f0ec1effb2f24c5eaa332 [file] [log] [blame]
Manuel Klimek47c245a2012-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"
Peter Collingbournec689ee72013-11-06 20:12:45 +000013#include "clang/Frontend/ASTUnit.h"
Richard Smith9219d1b2012-11-27 21:31:01 +000014#include "clang/Frontend/CompilerInstance.h"
Manuel Klimek47c245a2012-04-04 12:07:46 +000015#include "clang/Frontend/FrontendAction.h"
16#include "clang/Frontend/FrontendActions.h"
17#include "clang/Tooling/CompilationDatabase.h"
18#include "clang/Tooling/Tooling.h"
Peter Collingbournec689ee72013-11-06 20:12:45 +000019#include "llvm/ADT/STLExtras.h"
Alp Toker1d257e12014-06-04 03:28:55 +000020#include "llvm/Config/llvm-config.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000021#include "gtest/gtest.h"
Alexander Kornienko55f2ca92012-06-01 14:50:43 +000022#include <string>
Manuel Klimek47c245a2012-04-04 12:07:46 +000023
24namespace clang {
25namespace tooling {
26
27namespace {
28/// Takes an ast consumer and returns it from CreateASTConsumer. This only
29/// works with single translation unit compilations.
30class TestAction : public clang::ASTFrontendAction {
David Blaikie6beb6aa2014-08-10 19:56:51 +000031public:
Manuel Klimek47c245a2012-04-04 12:07:46 +000032 /// Takes ownership of TestConsumer.
David Blaikie6beb6aa2014-08-10 19:56:51 +000033 explicit TestAction(std::unique_ptr<clang::ASTConsumer> TestConsumer)
34 : TestConsumer(std::move(TestConsumer)) {}
Manuel Klimek47c245a2012-04-04 12:07:46 +000035
David Blaikie6beb6aa2014-08-10 19:56:51 +000036protected:
37 virtual std::unique_ptr<clang::ASTConsumer>
38 CreateASTConsumer(clang::CompilerInstance &compiler, StringRef dummy) {
Manuel Klimek47c245a2012-04-04 12:07:46 +000039 /// TestConsumer will be deleted by the framework calling us.
David Blaikie6beb6aa2014-08-10 19:56:51 +000040 return std::move(TestConsumer);
Manuel Klimek47c245a2012-04-04 12:07:46 +000041 }
42
David Blaikie6beb6aa2014-08-10 19:56:51 +000043private:
44 std::unique_ptr<clang::ASTConsumer> TestConsumer;
Manuel Klimek47c245a2012-04-04 12:07:46 +000045};
46
47class FindTopLevelDeclConsumer : public clang::ASTConsumer {
48 public:
49 explicit FindTopLevelDeclConsumer(bool *FoundTopLevelDecl)
50 : FoundTopLevelDecl(FoundTopLevelDecl) {}
51 virtual bool HandleTopLevelDecl(clang::DeclGroupRef DeclGroup) {
52 *FoundTopLevelDecl = true;
53 return true;
54 }
55 private:
56 bool * const FoundTopLevelDecl;
57};
58} // end namespace
59
Meador Inge5d3fb222012-06-16 03:34:49 +000060TEST(runToolOnCode, FindsNoTopLevelDeclOnEmptyCode) {
Manuel Klimek47c245a2012-04-04 12:07:46 +000061 bool FoundTopLevelDecl = false;
David Blaikie6beb6aa2014-08-10 19:56:51 +000062 EXPECT_TRUE(
63 runToolOnCode(new TestAction(llvm::make_unique<FindTopLevelDeclConsumer>(
64 &FoundTopLevelDecl)),
65 ""));
Meador Inge5d3fb222012-06-16 03:34:49 +000066 EXPECT_FALSE(FoundTopLevelDecl);
Manuel Klimek47c245a2012-04-04 12:07:46 +000067}
68
69namespace {
70class FindClassDeclXConsumer : public clang::ASTConsumer {
71 public:
72 FindClassDeclXConsumer(bool *FoundClassDeclX)
73 : FoundClassDeclX(FoundClassDeclX) {}
74 virtual bool HandleTopLevelDecl(clang::DeclGroupRef GroupRef) {
75 if (CXXRecordDecl* Record = dyn_cast<clang::CXXRecordDecl>(
76 *GroupRef.begin())) {
77 if (Record->getName() == "X") {
78 *FoundClassDeclX = true;
79 }
80 }
81 return true;
82 }
83 private:
84 bool *FoundClassDeclX;
85};
Peter Collingbournec689ee72013-11-06 20:12:45 +000086bool FindClassDeclX(ASTUnit *AST) {
87 for (std::vector<Decl *>::iterator i = AST->top_level_begin(),
88 e = AST->top_level_end();
89 i != e; ++i) {
90 if (CXXRecordDecl* Record = dyn_cast<clang::CXXRecordDecl>(*i)) {
91 if (Record->getName() == "X") {
92 return true;
93 }
94 }
95 }
96 return false;
97}
Manuel Klimek47c245a2012-04-04 12:07:46 +000098} // end namespace
99
100TEST(runToolOnCode, FindsClassDecl) {
101 bool FoundClassDeclX = false;
David Blaikie6beb6aa2014-08-10 19:56:51 +0000102 EXPECT_TRUE(
103 runToolOnCode(new TestAction(llvm::make_unique<FindClassDeclXConsumer>(
104 &FoundClassDeclX)),
105 "class X;"));
Manuel Klimek47c245a2012-04-04 12:07:46 +0000106 EXPECT_TRUE(FoundClassDeclX);
107
108 FoundClassDeclX = false;
David Blaikie6beb6aa2014-08-10 19:56:51 +0000109 EXPECT_TRUE(
110 runToolOnCode(new TestAction(llvm::make_unique<FindClassDeclXConsumer>(
111 &FoundClassDeclX)),
112 "class Y;"));
Manuel Klimek47c245a2012-04-04 12:07:46 +0000113 EXPECT_FALSE(FoundClassDeclX);
114}
115
Peter Collingbournec689ee72013-11-06 20:12:45 +0000116TEST(buildASTFromCode, FindsClassDecl) {
David Blaikie103a2de2014-04-25 17:01:33 +0000117 std::unique_ptr<ASTUnit> AST = buildASTFromCode("class X;");
Peter Collingbournec689ee72013-11-06 20:12:45 +0000118 ASSERT_TRUE(AST.get());
119 EXPECT_TRUE(FindClassDeclX(AST.get()));
120
David Blaikie103a2de2014-04-25 17:01:33 +0000121 AST = buildASTFromCode("class Y;");
Peter Collingbournec689ee72013-11-06 20:12:45 +0000122 ASSERT_TRUE(AST.get());
123 EXPECT_FALSE(FindClassDeclX(AST.get()));
124}
125
Manuel Klimek47c245a2012-04-04 12:07:46 +0000126TEST(newFrontendActionFactory, CreatesFrontendActionFactoryFromType) {
Ahmed Charlesb8984322014-03-07 20:03:18 +0000127 std::unique_ptr<FrontendActionFactory> Factory(
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000128 newFrontendActionFactory<SyntaxOnlyAction>());
Ahmed Charlesb8984322014-03-07 20:03:18 +0000129 std::unique_ptr<FrontendAction> Action(Factory->create());
Craig Topper416fa342014-06-08 08:38:12 +0000130 EXPECT_TRUE(Action.get() != nullptr);
Manuel Klimek47c245a2012-04-04 12:07:46 +0000131}
132
133struct IndependentFrontendActionCreator {
David Blaikie6beb6aa2014-08-10 19:56:51 +0000134 std::unique_ptr<ASTConsumer> newASTConsumer() {
135 return llvm::make_unique<FindTopLevelDeclConsumer>(nullptr);
Manuel Klimek5da9dcb2012-07-05 18:13:01 +0000136 }
Manuel Klimek47c245a2012-04-04 12:07:46 +0000137};
138
139TEST(newFrontendActionFactory, CreatesFrontendActionFactoryFromFactoryType) {
140 IndependentFrontendActionCreator Creator;
Ahmed Charlesb8984322014-03-07 20:03:18 +0000141 std::unique_ptr<FrontendActionFactory> Factory(
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000142 newFrontendActionFactory(&Creator));
Ahmed Charlesb8984322014-03-07 20:03:18 +0000143 std::unique_ptr<FrontendAction> Action(Factory->create());
Craig Topper416fa342014-06-08 08:38:12 +0000144 EXPECT_TRUE(Action.get() != nullptr);
Manuel Klimek47c245a2012-04-04 12:07:46 +0000145}
146
Alexander Kornienko55f2ca92012-06-01 14:50:43 +0000147TEST(ToolInvocation, TestMapVirtualFile) {
Peter Collingbournec689ee72013-11-06 20:12:45 +0000148 IntrusiveRefCntPtr<clang::FileManager> Files(
149 new clang::FileManager(clang::FileSystemOptions()));
Alexander Kornienko55f2ca92012-06-01 14:50:43 +0000150 std::vector<std::string> Args;
151 Args.push_back("tool-executable");
152 Args.push_back("-Idef");
153 Args.push_back("-fsyntax-only");
154 Args.push_back("test.cpp");
Peter Collingbournec689ee72013-11-06 20:12:45 +0000155 clang::tooling::ToolInvocation Invocation(Args, new SyntaxOnlyAction,
Alp Tokerf994cef2014-07-05 03:08:06 +0000156 Files.get());
Alexander Kornienko55f2ca92012-06-01 14:50:43 +0000157 Invocation.mapVirtualFile("test.cpp", "#include <abc>\n");
158 Invocation.mapVirtualFile("def/abc", "\n");
159 EXPECT_TRUE(Invocation.run());
160}
161
Manuel Klimek1f76c4e2013-10-24 07:51:24 +0000162TEST(ToolInvocation, TestVirtualModulesCompilation) {
163 // FIXME: Currently, this only tests that we don't exit with an error if a
164 // mapped module.map is found on the include path. In the future, expand this
165 // test to run a full modules enabled compilation, so we make sure we can
166 // rerun modules compilations with a virtual file system.
Peter Collingbournec689ee72013-11-06 20:12:45 +0000167 IntrusiveRefCntPtr<clang::FileManager> Files(
168 new clang::FileManager(clang::FileSystemOptions()));
Manuel Klimek1f76c4e2013-10-24 07:51:24 +0000169 std::vector<std::string> Args;
170 Args.push_back("tool-executable");
171 Args.push_back("-Idef");
172 Args.push_back("-fsyntax-only");
173 Args.push_back("test.cpp");
Peter Collingbournec689ee72013-11-06 20:12:45 +0000174 clang::tooling::ToolInvocation Invocation(Args, new SyntaxOnlyAction,
Alp Tokerf994cef2014-07-05 03:08:06 +0000175 Files.get());
Manuel Klimek1f76c4e2013-10-24 07:51:24 +0000176 Invocation.mapVirtualFile("test.cpp", "#include <abc>\n");
177 Invocation.mapVirtualFile("def/abc", "\n");
178 // Add a module.map file in the include directory of our header, so we trigger
179 // the module.map header search logic.
180 Invocation.mapVirtualFile("def/module.map", "\n");
181 EXPECT_TRUE(Invocation.run());
182}
183
Edwin Vane20c6f542013-05-29 16:01:10 +0000184struct VerifyEndCallback : public SourceFileCallbacks {
185 VerifyEndCallback() : BeginCalled(0), EndCalled(0), Matched(false) {}
Edwin Vane3a331f32013-05-30 13:59:44 +0000186 virtual bool handleBeginSource(CompilerInstance &CI,
Craig Toppera798a9d2014-03-02 09:32:10 +0000187 StringRef Filename) override {
Edwin Vane20c6f542013-05-29 16:01:10 +0000188 ++BeginCalled;
189 return true;
190 }
Edwin Vane3a331f32013-05-30 13:59:44 +0000191 virtual void handleEndSource() {
Edwin Vane20c6f542013-05-29 16:01:10 +0000192 ++EndCalled;
Manuel Klimek8246d872012-10-25 08:49:11 +0000193 }
David Blaikie6beb6aa2014-08-10 19:56:51 +0000194 std::unique_ptr<ASTConsumer> newASTConsumer() {
195 return llvm::make_unique<FindTopLevelDeclConsumer>(&Matched);
Manuel Klimek8246d872012-10-25 08:49:11 +0000196 }
Edwin Vane20c6f542013-05-29 16:01:10 +0000197 unsigned BeginCalled;
198 unsigned EndCalled;
Manuel Klimek8246d872012-10-25 08:49:11 +0000199 bool Matched;
200};
201
Hans Wennborg501eadb2014-03-12 16:07:46 +0000202#if !defined(LLVM_ON_WIN32)
Edwin Vane20c6f542013-05-29 16:01:10 +0000203TEST(newFrontendActionFactory, InjectsSourceFileCallbacks) {
Manuel Klimek8246d872012-10-25 08:49:11 +0000204 VerifyEndCallback EndCallback;
205
206 FixedCompilationDatabase Compilations("/", std::vector<std::string>());
207 std::vector<std::string> Sources;
208 Sources.push_back("/a.cc");
209 Sources.push_back("/b.cc");
210 ClangTool Tool(Compilations, Sources);
211
212 Tool.mapVirtualFile("/a.cc", "void a() {}");
213 Tool.mapVirtualFile("/b.cc", "void b() {}");
214
Nico Weber52fbbb12014-04-24 03:48:09 +0000215 std::unique_ptr<FrontendActionFactory> Action(
216 newFrontendActionFactory(&EndCallback, &EndCallback));
217 Tool.run(Action.get());
Manuel Klimek8246d872012-10-25 08:49:11 +0000218
219 EXPECT_TRUE(EndCallback.Matched);
Edwin Vane20c6f542013-05-29 16:01:10 +0000220 EXPECT_EQ(2u, EndCallback.BeginCalled);
221 EXPECT_EQ(2u, EndCallback.EndCalled);
Manuel Klimek8246d872012-10-25 08:49:11 +0000222}
NAKAMURA Takumi95fd41a2012-10-25 09:38:41 +0000223#endif
Manuel Klimek8246d872012-10-25 08:49:11 +0000224
Richard Smith9219d1b2012-11-27 21:31:01 +0000225struct SkipBodyConsumer : public clang::ASTConsumer {
226 /// Skip the 'skipMe' function.
227 virtual bool shouldSkipFunctionBody(Decl *D) {
228 FunctionDecl *F = dyn_cast<FunctionDecl>(D);
229 return F && F->getNameAsString() == "skipMe";
230 }
231};
232
233struct SkipBodyAction : public clang::ASTFrontendAction {
David Blaikie6beb6aa2014-08-10 19:56:51 +0000234 virtual std::unique_ptr<ASTConsumer>
235 CreateASTConsumer(CompilerInstance &Compiler, StringRef) {
Richard Smith9219d1b2012-11-27 21:31:01 +0000236 Compiler.getFrontendOpts().SkipFunctionBodies = true;
David Blaikie6beb6aa2014-08-10 19:56:51 +0000237 return llvm::make_unique<SkipBodyConsumer>();
Richard Smith9219d1b2012-11-27 21:31:01 +0000238 }
239};
240
Hal Finkel1d3e3d72013-01-28 04:37:38 +0000241TEST(runToolOnCode, TestSkipFunctionBody) {
Richard Smith9219d1b2012-11-27 21:31:01 +0000242 EXPECT_TRUE(runToolOnCode(new SkipBodyAction,
243 "int skipMe() { an_error_here }"));
244 EXPECT_FALSE(runToolOnCode(new SkipBodyAction,
245 "int skipMeNot() { an_error_here }"));
246}
247
Peter Collingbournec0423b32014-03-02 23:37:26 +0000248TEST(runToolOnCodeWithArgs, TestNoDepFile) {
249 llvm::SmallString<32> DepFilePath;
250 ASSERT_FALSE(
251 llvm::sys::fs::createTemporaryFile("depfile", "d", DepFilePath));
Peter Collingbournef9372542014-03-03 08:13:06 +0000252 std::vector<std::string> Args;
253 Args.push_back("-MMD");
254 Args.push_back("-MT");
255 Args.push_back(DepFilePath.str());
256 Args.push_back("-MF");
257 Args.push_back(DepFilePath.str());
Peter Collingbourne2bbb0292014-03-03 07:49:35 +0000258 EXPECT_TRUE(runToolOnCodeWithArgs(new SkipBodyAction, "", Args));
Peter Collingbournec0423b32014-03-02 23:37:26 +0000259 EXPECT_FALSE(llvm::sys::fs::exists(DepFilePath.str()));
260 EXPECT_FALSE(llvm::sys::fs::remove(DepFilePath.str()));
261}
262
Manuel Klimekd91ac932013-06-04 14:44:44 +0000263struct CheckSyntaxOnlyAdjuster: public ArgumentsAdjuster {
264 bool &Found;
265 bool &Ran;
266
267 CheckSyntaxOnlyAdjuster(bool &Found, bool &Ran) : Found(Found), Ran(Ran) { }
268
269 virtual CommandLineArguments
Craig Toppera798a9d2014-03-02 09:32:10 +0000270 Adjust(const CommandLineArguments &Args) override {
Manuel Klimekd91ac932013-06-04 14:44:44 +0000271 Ran = true;
272 for (unsigned I = 0, E = Args.size(); I != E; ++I) {
273 if (Args[I] == "-fsyntax-only") {
274 Found = true;
275 break;
276 }
277 }
278 return Args;
279 }
280};
281
282TEST(ClangToolTest, ArgumentAdjusters) {
283 FixedCompilationDatabase Compilations("/", std::vector<std::string>());
284
285 ClangTool Tool(Compilations, std::vector<std::string>(1, "/a.cc"));
286 Tool.mapVirtualFile("/a.cc", "void a() {}");
287
Nico Weber52fbbb12014-04-24 03:48:09 +0000288 std::unique_ptr<FrontendActionFactory> Action(
289 newFrontendActionFactory<SyntaxOnlyAction>());
290
Manuel Klimekd91ac932013-06-04 14:44:44 +0000291 bool Found = false;
292 bool Ran = false;
293 Tool.appendArgumentsAdjuster(new CheckSyntaxOnlyAdjuster(Found, Ran));
Nico Weber52fbbb12014-04-24 03:48:09 +0000294 Tool.run(Action.get());
Manuel Klimekd91ac932013-06-04 14:44:44 +0000295 EXPECT_TRUE(Ran);
296 EXPECT_TRUE(Found);
297
298 Ran = Found = false;
299 Tool.clearArgumentsAdjusters();
300 Tool.appendArgumentsAdjuster(new CheckSyntaxOnlyAdjuster(Found, Ran));
301 Tool.appendArgumentsAdjuster(new ClangSyntaxOnlyAdjuster());
Nico Weber52fbbb12014-04-24 03:48:09 +0000302 Tool.run(Action.get());
Manuel Klimekd91ac932013-06-04 14:44:44 +0000303 EXPECT_TRUE(Ran);
304 EXPECT_FALSE(Found);
305}
306
Hans Wennborg501eadb2014-03-12 16:07:46 +0000307#ifndef LLVM_ON_WIN32
Peter Collingbournec689ee72013-11-06 20:12:45 +0000308TEST(ClangToolTest, BuildASTs) {
309 FixedCompilationDatabase Compilations("/", std::vector<std::string>());
310
311 std::vector<std::string> Sources;
312 Sources.push_back("/a.cc");
313 Sources.push_back("/b.cc");
314 ClangTool Tool(Compilations, Sources);
315
316 Tool.mapVirtualFile("/a.cc", "void a() {}");
317 Tool.mapVirtualFile("/b.cc", "void b() {}");
318
David Blaikie39808ff2014-04-25 14:49:37 +0000319 std::vector<std::unique_ptr<ASTUnit>> ASTs;
Peter Collingbournec689ee72013-11-06 20:12:45 +0000320 EXPECT_EQ(0, Tool.buildASTs(ASTs));
321 EXPECT_EQ(2u, ASTs.size());
Peter Collingbournec689ee72013-11-06 20:12:45 +0000322}
323
Manuel Klimek64083012013-11-07 23:18:05 +0000324struct TestDiagnosticConsumer : public DiagnosticConsumer {
325 TestDiagnosticConsumer() : NumDiagnosticsSeen(0) {}
326 virtual void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
327 const Diagnostic &Info) {
328 ++NumDiagnosticsSeen;
329 }
330 unsigned NumDiagnosticsSeen;
331};
332
333TEST(ClangToolTest, InjectDiagnosticConsumer) {
334 FixedCompilationDatabase Compilations("/", std::vector<std::string>());
335 ClangTool Tool(Compilations, std::vector<std::string>(1, "/a.cc"));
336 Tool.mapVirtualFile("/a.cc", "int x = undeclared;");
337 TestDiagnosticConsumer Consumer;
338 Tool.setDiagnosticConsumer(&Consumer);
Nico Weber52fbbb12014-04-24 03:48:09 +0000339 std::unique_ptr<FrontendActionFactory> Action(
340 newFrontendActionFactory<SyntaxOnlyAction>());
341 Tool.run(Action.get());
Manuel Klimek64083012013-11-07 23:18:05 +0000342 EXPECT_EQ(1u, Consumer.NumDiagnosticsSeen);
343}
344
Manuel Klimek31cd3fc2013-11-12 17:53:18 +0000345TEST(ClangToolTest, InjectDiagnosticConsumerInBuildASTs) {
346 FixedCompilationDatabase Compilations("/", std::vector<std::string>());
347 ClangTool Tool(Compilations, std::vector<std::string>(1, "/a.cc"));
348 Tool.mapVirtualFile("/a.cc", "int x = undeclared;");
349 TestDiagnosticConsumer Consumer;
350 Tool.setDiagnosticConsumer(&Consumer);
David Blaikie39808ff2014-04-25 14:49:37 +0000351 std::vector<std::unique_ptr<ASTUnit>> ASTs;
Manuel Klimek31cd3fc2013-11-12 17:53:18 +0000352 Tool.buildASTs(ASTs);
353 EXPECT_EQ(1u, ASTs.size());
354 EXPECT_EQ(1u, Consumer.NumDiagnosticsSeen);
355}
NAKAMURA Takumid3b07c62013-11-13 00:18:50 +0000356#endif
Manuel Klimek31cd3fc2013-11-12 17:53:18 +0000357
Manuel Klimek47c245a2012-04-04 12:07:46 +0000358} // end namespace tooling
359} // end namespace clang