blob: 10ac0c33ed85d5bd111f86d226c43d45ea54b0b3 [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"
Manuel Klimek9b30e2b2015-10-06 10:45:03 +000021#include "llvm/Support/TargetSelect.h"
22#include "llvm/Support/TargetRegistry.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000023#include "gtest/gtest.h"
Alexander Kornienko74e1c462014-12-03 17:53:02 +000024#include <algorithm>
Alexander Kornienko55f2ca92012-06-01 14:50:43 +000025#include <string>
Manuel Klimek47c245a2012-04-04 12:07:46 +000026
27namespace clang {
28namespace tooling {
29
30namespace {
31/// Takes an ast consumer and returns it from CreateASTConsumer. This only
32/// works with single translation unit compilations.
33class TestAction : public clang::ASTFrontendAction {
David Blaikie6beb6aa2014-08-10 19:56:51 +000034public:
Manuel Klimek47c245a2012-04-04 12:07:46 +000035 /// Takes ownership of TestConsumer.
David Blaikie6beb6aa2014-08-10 19:56:51 +000036 explicit TestAction(std::unique_ptr<clang::ASTConsumer> TestConsumer)
37 : TestConsumer(std::move(TestConsumer)) {}
Manuel Klimek47c245a2012-04-04 12:07:46 +000038
David Blaikie6beb6aa2014-08-10 19:56:51 +000039protected:
Alexander Kornienko34eb2072015-04-11 02:00:23 +000040 std::unique_ptr<clang::ASTConsumer>
41 CreateASTConsumer(clang::CompilerInstance &compiler,
42 StringRef dummy) override {
Manuel Klimek47c245a2012-04-04 12:07:46 +000043 /// TestConsumer will be deleted by the framework calling us.
David Blaikie6beb6aa2014-08-10 19:56:51 +000044 return std::move(TestConsumer);
Manuel Klimek47c245a2012-04-04 12:07:46 +000045 }
46
David Blaikie6beb6aa2014-08-10 19:56:51 +000047private:
48 std::unique_ptr<clang::ASTConsumer> TestConsumer;
Manuel Klimek47c245a2012-04-04 12:07:46 +000049};
50
51class FindTopLevelDeclConsumer : public clang::ASTConsumer {
52 public:
53 explicit FindTopLevelDeclConsumer(bool *FoundTopLevelDecl)
54 : FoundTopLevelDecl(FoundTopLevelDecl) {}
Alexander Kornienko34eb2072015-04-11 02:00:23 +000055 bool HandleTopLevelDecl(clang::DeclGroupRef DeclGroup) override {
Manuel Klimek47c245a2012-04-04 12:07:46 +000056 *FoundTopLevelDecl = true;
57 return true;
58 }
59 private:
60 bool * const FoundTopLevelDecl;
61};
62} // end namespace
63
Meador Inge5d3fb222012-06-16 03:34:49 +000064TEST(runToolOnCode, FindsNoTopLevelDeclOnEmptyCode) {
Manuel Klimek47c245a2012-04-04 12:07:46 +000065 bool FoundTopLevelDecl = false;
David Blaikie6beb6aa2014-08-10 19:56:51 +000066 EXPECT_TRUE(
67 runToolOnCode(new TestAction(llvm::make_unique<FindTopLevelDeclConsumer>(
68 &FoundTopLevelDecl)),
69 ""));
Meador Inge5d3fb222012-06-16 03:34:49 +000070 EXPECT_FALSE(FoundTopLevelDecl);
Manuel Klimek47c245a2012-04-04 12:07:46 +000071}
72
73namespace {
74class FindClassDeclXConsumer : public clang::ASTConsumer {
75 public:
76 FindClassDeclXConsumer(bool *FoundClassDeclX)
77 : FoundClassDeclX(FoundClassDeclX) {}
Alexander Kornienko34eb2072015-04-11 02:00:23 +000078 bool HandleTopLevelDecl(clang::DeclGroupRef GroupRef) override {
Manuel Klimek47c245a2012-04-04 12:07:46 +000079 if (CXXRecordDecl* Record = dyn_cast<clang::CXXRecordDecl>(
80 *GroupRef.begin())) {
81 if (Record->getName() == "X") {
82 *FoundClassDeclX = true;
83 }
84 }
85 return true;
86 }
87 private:
88 bool *FoundClassDeclX;
89};
Peter Collingbournec689ee72013-11-06 20:12:45 +000090bool FindClassDeclX(ASTUnit *AST) {
91 for (std::vector<Decl *>::iterator i = AST->top_level_begin(),
92 e = AST->top_level_end();
93 i != e; ++i) {
94 if (CXXRecordDecl* Record = dyn_cast<clang::CXXRecordDecl>(*i)) {
95 if (Record->getName() == "X") {
96 return true;
97 }
98 }
99 }
100 return false;
101}
Manuel Klimek47c245a2012-04-04 12:07:46 +0000102} // end namespace
103
104TEST(runToolOnCode, FindsClassDecl) {
105 bool FoundClassDeclX = false;
David Blaikie6beb6aa2014-08-10 19:56:51 +0000106 EXPECT_TRUE(
107 runToolOnCode(new TestAction(llvm::make_unique<FindClassDeclXConsumer>(
108 &FoundClassDeclX)),
109 "class X;"));
Manuel Klimek47c245a2012-04-04 12:07:46 +0000110 EXPECT_TRUE(FoundClassDeclX);
111
112 FoundClassDeclX = false;
David Blaikie6beb6aa2014-08-10 19:56:51 +0000113 EXPECT_TRUE(
114 runToolOnCode(new TestAction(llvm::make_unique<FindClassDeclXConsumer>(
115 &FoundClassDeclX)),
116 "class Y;"));
Manuel Klimek47c245a2012-04-04 12:07:46 +0000117 EXPECT_FALSE(FoundClassDeclX);
118}
119
Peter Collingbournec689ee72013-11-06 20:12:45 +0000120TEST(buildASTFromCode, FindsClassDecl) {
David Blaikie103a2de2014-04-25 17:01:33 +0000121 std::unique_ptr<ASTUnit> AST = buildASTFromCode("class X;");
Peter Collingbournec689ee72013-11-06 20:12:45 +0000122 ASSERT_TRUE(AST.get());
123 EXPECT_TRUE(FindClassDeclX(AST.get()));
124
David Blaikie103a2de2014-04-25 17:01:33 +0000125 AST = buildASTFromCode("class Y;");
Peter Collingbournec689ee72013-11-06 20:12:45 +0000126 ASSERT_TRUE(AST.get());
127 EXPECT_FALSE(FindClassDeclX(AST.get()));
128}
129
Manuel Klimek47c245a2012-04-04 12:07:46 +0000130TEST(newFrontendActionFactory, CreatesFrontendActionFactoryFromType) {
Ahmed Charlesb8984322014-03-07 20:03:18 +0000131 std::unique_ptr<FrontendActionFactory> Factory(
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000132 newFrontendActionFactory<SyntaxOnlyAction>());
Ahmed Charlesb8984322014-03-07 20:03:18 +0000133 std::unique_ptr<FrontendAction> Action(Factory->create());
Craig Topper416fa342014-06-08 08:38:12 +0000134 EXPECT_TRUE(Action.get() != nullptr);
Manuel Klimek47c245a2012-04-04 12:07:46 +0000135}
136
137struct IndependentFrontendActionCreator {
David Blaikie6beb6aa2014-08-10 19:56:51 +0000138 std::unique_ptr<ASTConsumer> newASTConsumer() {
139 return llvm::make_unique<FindTopLevelDeclConsumer>(nullptr);
Manuel Klimek5da9dcb2012-07-05 18:13:01 +0000140 }
Manuel Klimek47c245a2012-04-04 12:07:46 +0000141};
142
143TEST(newFrontendActionFactory, CreatesFrontendActionFactoryFromFactoryType) {
144 IndependentFrontendActionCreator Creator;
Ahmed Charlesb8984322014-03-07 20:03:18 +0000145 std::unique_ptr<FrontendActionFactory> Factory(
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000146 newFrontendActionFactory(&Creator));
Ahmed Charlesb8984322014-03-07 20:03:18 +0000147 std::unique_ptr<FrontendAction> Action(Factory->create());
Craig Topper416fa342014-06-08 08:38:12 +0000148 EXPECT_TRUE(Action.get() != nullptr);
Manuel Klimek47c245a2012-04-04 12:07:46 +0000149}
150
Alexander Kornienko55f2ca92012-06-01 14:50:43 +0000151TEST(ToolInvocation, TestMapVirtualFile) {
Benjamin Kramerc4cb3b12015-10-09 09:54:37 +0000152 llvm::IntrusiveRefCntPtr<vfs::OverlayFileSystem> OverlayFileSystem(
153 new vfs::OverlayFileSystem(vfs::getRealFileSystem()));
154 llvm::IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem(
155 new vfs::InMemoryFileSystem);
156 OverlayFileSystem->pushOverlay(InMemoryFileSystem);
157 llvm::IntrusiveRefCntPtr<FileManager> Files(
158 new FileManager(FileSystemOptions(), OverlayFileSystem));
Alexander Kornienko55f2ca92012-06-01 14:50:43 +0000159 std::vector<std::string> Args;
160 Args.push_back("tool-executable");
161 Args.push_back("-Idef");
162 Args.push_back("-fsyntax-only");
163 Args.push_back("test.cpp");
Peter Collingbournec689ee72013-11-06 20:12:45 +0000164 clang::tooling::ToolInvocation Invocation(Args, new SyntaxOnlyAction,
Alp Tokerf994cef2014-07-05 03:08:06 +0000165 Files.get());
Benjamin Kramerc4cb3b12015-10-09 09:54:37 +0000166 InMemoryFileSystem->addFile(
167 "test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("#include <abc>\n"));
168 InMemoryFileSystem->addFile("def/abc", 0,
169 llvm::MemoryBuffer::getMemBuffer("\n"));
Alexander Kornienko55f2ca92012-06-01 14:50:43 +0000170 EXPECT_TRUE(Invocation.run());
171}
172
Manuel Klimek1f76c4e2013-10-24 07:51:24 +0000173TEST(ToolInvocation, TestVirtualModulesCompilation) {
174 // FIXME: Currently, this only tests that we don't exit with an error if a
175 // mapped module.map is found on the include path. In the future, expand this
176 // test to run a full modules enabled compilation, so we make sure we can
177 // rerun modules compilations with a virtual file system.
Benjamin Kramerc4cb3b12015-10-09 09:54:37 +0000178 llvm::IntrusiveRefCntPtr<vfs::OverlayFileSystem> OverlayFileSystem(
179 new vfs::OverlayFileSystem(vfs::getRealFileSystem()));
180 llvm::IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem(
181 new vfs::InMemoryFileSystem);
182 OverlayFileSystem->pushOverlay(InMemoryFileSystem);
183 llvm::IntrusiveRefCntPtr<FileManager> Files(
184 new FileManager(FileSystemOptions(), OverlayFileSystem));
Manuel Klimek1f76c4e2013-10-24 07:51:24 +0000185 std::vector<std::string> Args;
186 Args.push_back("tool-executable");
187 Args.push_back("-Idef");
188 Args.push_back("-fsyntax-only");
189 Args.push_back("test.cpp");
Peter Collingbournec689ee72013-11-06 20:12:45 +0000190 clang::tooling::ToolInvocation Invocation(Args, new SyntaxOnlyAction,
Alp Tokerf994cef2014-07-05 03:08:06 +0000191 Files.get());
Benjamin Kramerc4cb3b12015-10-09 09:54:37 +0000192 InMemoryFileSystem->addFile(
193 "test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("#include <abc>\n"));
194 InMemoryFileSystem->addFile("def/abc", 0,
195 llvm::MemoryBuffer::getMemBuffer("\n"));
Manuel Klimek1f76c4e2013-10-24 07:51:24 +0000196 // Add a module.map file in the include directory of our header, so we trigger
197 // the module.map header search logic.
Benjamin Kramerc4cb3b12015-10-09 09:54:37 +0000198 InMemoryFileSystem->addFile("def/module.map", 0,
199 llvm::MemoryBuffer::getMemBuffer("\n"));
Manuel Klimek1f76c4e2013-10-24 07:51:24 +0000200 EXPECT_TRUE(Invocation.run());
201}
202
Edwin Vane20c6f542013-05-29 16:01:10 +0000203struct VerifyEndCallback : public SourceFileCallbacks {
204 VerifyEndCallback() : BeginCalled(0), EndCalled(0), Matched(false) {}
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000205 bool handleBeginSource(CompilerInstance &CI, StringRef Filename) override {
Edwin Vane20c6f542013-05-29 16:01:10 +0000206 ++BeginCalled;
207 return true;
208 }
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000209 void handleEndSource() override { ++EndCalled; }
David Blaikie6beb6aa2014-08-10 19:56:51 +0000210 std::unique_ptr<ASTConsumer> newASTConsumer() {
211 return llvm::make_unique<FindTopLevelDeclConsumer>(&Matched);
Manuel Klimek8246d872012-10-25 08:49:11 +0000212 }
Edwin Vane20c6f542013-05-29 16:01:10 +0000213 unsigned BeginCalled;
214 unsigned EndCalled;
Manuel Klimek8246d872012-10-25 08:49:11 +0000215 bool Matched;
216};
217
Hans Wennborg501eadb2014-03-12 16:07:46 +0000218#if !defined(LLVM_ON_WIN32)
Edwin Vane20c6f542013-05-29 16:01:10 +0000219TEST(newFrontendActionFactory, InjectsSourceFileCallbacks) {
Manuel Klimek8246d872012-10-25 08:49:11 +0000220 VerifyEndCallback EndCallback;
221
222 FixedCompilationDatabase Compilations("/", std::vector<std::string>());
223 std::vector<std::string> Sources;
224 Sources.push_back("/a.cc");
225 Sources.push_back("/b.cc");
226 ClangTool Tool(Compilations, Sources);
227
228 Tool.mapVirtualFile("/a.cc", "void a() {}");
229 Tool.mapVirtualFile("/b.cc", "void b() {}");
230
Nico Weber52fbbb12014-04-24 03:48:09 +0000231 std::unique_ptr<FrontendActionFactory> Action(
232 newFrontendActionFactory(&EndCallback, &EndCallback));
233 Tool.run(Action.get());
Manuel Klimek8246d872012-10-25 08:49:11 +0000234
235 EXPECT_TRUE(EndCallback.Matched);
Edwin Vane20c6f542013-05-29 16:01:10 +0000236 EXPECT_EQ(2u, EndCallback.BeginCalled);
237 EXPECT_EQ(2u, EndCallback.EndCalled);
Manuel Klimek8246d872012-10-25 08:49:11 +0000238}
NAKAMURA Takumi95fd41a2012-10-25 09:38:41 +0000239#endif
Manuel Klimek8246d872012-10-25 08:49:11 +0000240
Richard Smith9219d1b2012-11-27 21:31:01 +0000241struct SkipBodyConsumer : public clang::ASTConsumer {
242 /// Skip the 'skipMe' function.
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000243 bool shouldSkipFunctionBody(Decl *D) override {
Olivier Goffartf9e890c2016-06-16 21:40:06 +0000244 NamedDecl *F = dyn_cast<NamedDecl>(D);
Richard Smith9219d1b2012-11-27 21:31:01 +0000245 return F && F->getNameAsString() == "skipMe";
246 }
247};
248
249struct SkipBodyAction : public clang::ASTFrontendAction {
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000250 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &Compiler,
251 StringRef) override {
Richard Smith9219d1b2012-11-27 21:31:01 +0000252 Compiler.getFrontendOpts().SkipFunctionBodies = true;
David Blaikie6beb6aa2014-08-10 19:56:51 +0000253 return llvm::make_unique<SkipBodyConsumer>();
Richard Smith9219d1b2012-11-27 21:31:01 +0000254 }
255};
256
Hal Finkel1d3e3d72013-01-28 04:37:38 +0000257TEST(runToolOnCode, TestSkipFunctionBody) {
Olivier Goffartf9e890c2016-06-16 21:40:06 +0000258 std::vector<std::string> Args = {"-std=c++11"};
NAKAMURA Takumic690ee02016-06-17 02:04:51 +0000259 std::vector<std::string> Args2 = {"-fno-delayed-template-parsing"};
Olivier Goffartf9e890c2016-06-16 21:40:06 +0000260
Richard Smith9219d1b2012-11-27 21:31:01 +0000261 EXPECT_TRUE(runToolOnCode(new SkipBodyAction,
262 "int skipMe() { an_error_here }"));
263 EXPECT_FALSE(runToolOnCode(new SkipBodyAction,
264 "int skipMeNot() { an_error_here }"));
Olivier Goffartf9e890c2016-06-16 21:40:06 +0000265
266 // Test constructors with initializers
267 EXPECT_TRUE(runToolOnCodeWithArgs(
268 new SkipBodyAction,
269 "struct skipMe { skipMe() : an_error() { more error } };", Args));
270 EXPECT_TRUE(runToolOnCodeWithArgs(
271 new SkipBodyAction, "struct skipMe { skipMe(); };"
272 "skipMe::skipMe() : an_error([](){;}) { more error }",
273 Args));
274 EXPECT_TRUE(runToolOnCodeWithArgs(
275 new SkipBodyAction, "struct skipMe { skipMe(); };"
276 "skipMe::skipMe() : an_error{[](){;}} { more error }",
277 Args));
278 EXPECT_TRUE(runToolOnCodeWithArgs(
279 new SkipBodyAction,
280 "struct skipMe { skipMe(); };"
281 "skipMe::skipMe() : a<b<c>(e)>>(), f{}, g() { error }",
282 Args));
283 EXPECT_TRUE(runToolOnCodeWithArgs(
284 new SkipBodyAction, "struct skipMe { skipMe() : bases()... { error } };",
285 Args));
286
287 EXPECT_FALSE(runToolOnCodeWithArgs(
288 new SkipBodyAction, "struct skipMeNot { skipMeNot() : an_error() { } };",
289 Args));
290 EXPECT_FALSE(runToolOnCodeWithArgs(new SkipBodyAction,
291 "struct skipMeNot { skipMeNot(); };"
292 "skipMeNot::skipMeNot() : an_error() { }",
293 Args));
294
295 // Try/catch
296 EXPECT_TRUE(runToolOnCode(
297 new SkipBodyAction,
298 "void skipMe() try { an_error() } catch(error) { error };"));
299 EXPECT_TRUE(runToolOnCode(
300 new SkipBodyAction,
301 "struct S { void skipMe() try { an_error() } catch(error) { error } };"));
302 EXPECT_TRUE(
303 runToolOnCode(new SkipBodyAction,
304 "void skipMe() try { an_error() } catch(error) { error; }"
305 "catch(error) { error } catch (error) { }"));
306 EXPECT_FALSE(runToolOnCode(
307 new SkipBodyAction,
308 "void skipMe() try something;")); // don't crash while parsing
309
310 // Template
311 EXPECT_TRUE(runToolOnCode(
312 new SkipBodyAction, "template<typename T> int skipMe() { an_error_here }"
313 "int x = skipMe<int>();"));
NAKAMURA Takumic690ee02016-06-17 02:04:51 +0000314 EXPECT_FALSE(runToolOnCodeWithArgs(
315 new SkipBodyAction,
316 "template<typename T> int skipMeNot() { an_error_here }", Args2));
Richard Smith9219d1b2012-11-27 21:31:01 +0000317}
318
Peter Collingbournec0423b32014-03-02 23:37:26 +0000319TEST(runToolOnCodeWithArgs, TestNoDepFile) {
320 llvm::SmallString<32> DepFilePath;
321 ASSERT_FALSE(
322 llvm::sys::fs::createTemporaryFile("depfile", "d", DepFilePath));
Peter Collingbournef9372542014-03-03 08:13:06 +0000323 std::vector<std::string> Args;
324 Args.push_back("-MMD");
325 Args.push_back("-MT");
326 Args.push_back(DepFilePath.str());
327 Args.push_back("-MF");
328 Args.push_back(DepFilePath.str());
Peter Collingbourne2bbb0292014-03-03 07:49:35 +0000329 EXPECT_TRUE(runToolOnCodeWithArgs(new SkipBodyAction, "", Args));
Peter Collingbournec0423b32014-03-02 23:37:26 +0000330 EXPECT_FALSE(llvm::sys::fs::exists(DepFilePath.str()));
331 EXPECT_FALSE(llvm::sys::fs::remove(DepFilePath.str()));
332}
333
Manuel Klimekd91ac932013-06-04 14:44:44 +0000334TEST(ClangToolTest, ArgumentAdjusters) {
335 FixedCompilationDatabase Compilations("/", std::vector<std::string>());
336
337 ClangTool Tool(Compilations, std::vector<std::string>(1, "/a.cc"));
338 Tool.mapVirtualFile("/a.cc", "void a() {}");
339
Nico Weber52fbbb12014-04-24 03:48:09 +0000340 std::unique_ptr<FrontendActionFactory> Action(
341 newFrontendActionFactory<SyntaxOnlyAction>());
342
Manuel Klimekd91ac932013-06-04 14:44:44 +0000343 bool Found = false;
344 bool Ran = false;
Alexander Kornienko74e1c462014-12-03 17:53:02 +0000345 ArgumentsAdjuster CheckSyntaxOnlyAdjuster =
Alexander Kornienko857b10f2015-11-05 02:19:53 +0000346 [&Found, &Ran](const CommandLineArguments &Args, StringRef /*unused*/) {
Alexander Kornienko74e1c462014-12-03 17:53:02 +0000347 Ran = true;
348 if (std::find(Args.begin(), Args.end(), "-fsyntax-only") != Args.end())
349 Found = true;
350 return Args;
351 };
352 Tool.appendArgumentsAdjuster(CheckSyntaxOnlyAdjuster);
Nico Weber52fbbb12014-04-24 03:48:09 +0000353 Tool.run(Action.get());
Manuel Klimekd91ac932013-06-04 14:44:44 +0000354 EXPECT_TRUE(Ran);
355 EXPECT_TRUE(Found);
356
357 Ran = Found = false;
358 Tool.clearArgumentsAdjusters();
Alexander Kornienko74e1c462014-12-03 17:53:02 +0000359 Tool.appendArgumentsAdjuster(CheckSyntaxOnlyAdjuster);
360 Tool.appendArgumentsAdjuster(getClangSyntaxOnlyAdjuster());
Nico Weber52fbbb12014-04-24 03:48:09 +0000361 Tool.run(Action.get());
Manuel Klimekd91ac932013-06-04 14:44:44 +0000362 EXPECT_TRUE(Ran);
363 EXPECT_FALSE(Found);
364}
365
Manuel Klimek9b30e2b2015-10-06 10:45:03 +0000366namespace {
367/// Find a target name such that looking for it in TargetRegistry by that name
368/// returns the same target. We expect that there is at least one target
369/// configured with this property.
370std::string getAnyTarget() {
371 llvm::InitializeAllTargets();
372 for (const auto &Target : llvm::TargetRegistry::targets()) {
373 std::string Error;
NAKAMURA Takumife2e6422015-10-06 13:58:13 +0000374 StringRef TargetName(Target.getName());
375 if (TargetName == "x86-64")
376 TargetName = "x86_64";
377 if (llvm::TargetRegistry::lookupTarget(TargetName, Error) == &Target) {
378 return TargetName;
Manuel Klimek9b30e2b2015-10-06 10:45:03 +0000379 }
380 }
381 return "";
382}
383}
384
385TEST(addTargetAndModeForProgramName, AddsTargetAndMode) {
386 std::string Target = getAnyTarget();
387 ASSERT_FALSE(Target.empty());
388
389 std::vector<std::string> Args = {"clang", "-foo"};
390 addTargetAndModeForProgramName(Args, "");
391 EXPECT_EQ((std::vector<std::string>{"clang", "-foo"}), Args);
392 addTargetAndModeForProgramName(Args, Target + "-g++");
393 EXPECT_EQ((std::vector<std::string>{"clang", "-target", Target,
394 "--driver-mode=g++", "-foo"}),
395 Args);
396}
397
398TEST(addTargetAndModeForProgramName, PathIgnored) {
399 std::string Target = getAnyTarget();
400 ASSERT_FALSE(Target.empty());
401
402 SmallString<32> ToolPath;
403 llvm::sys::path::append(ToolPath, "foo", "bar", Target + "-g++");
404
405 std::vector<std::string> Args = {"clang", "-foo"};
406 addTargetAndModeForProgramName(Args, ToolPath);
407 EXPECT_EQ((std::vector<std::string>{"clang", "-target", Target,
408 "--driver-mode=g++", "-foo"}),
409 Args);
410}
411
412TEST(addTargetAndModeForProgramName, IgnoresExistingTarget) {
413 std::string Target = getAnyTarget();
414 ASSERT_FALSE(Target.empty());
415
416 std::vector<std::string> Args = {"clang", "-foo", "-target", "something"};
417 addTargetAndModeForProgramName(Args, Target + "-g++");
418 EXPECT_EQ((std::vector<std::string>{"clang", "--driver-mode=g++", "-foo",
419 "-target", "something"}),
420 Args);
421
422 std::vector<std::string> ArgsAlt = {"clang", "-foo", "-target=something"};
423 addTargetAndModeForProgramName(ArgsAlt, Target + "-g++");
424 EXPECT_EQ((std::vector<std::string>{"clang", "--driver-mode=g++", "-foo",
425 "-target=something"}),
426 ArgsAlt);
427}
428
429TEST(addTargetAndModeForProgramName, IgnoresExistingMode) {
430 std::string Target = getAnyTarget();
431 ASSERT_FALSE(Target.empty());
432
433 std::vector<std::string> Args = {"clang", "-foo", "--driver-mode=abc"};
434 addTargetAndModeForProgramName(Args, Target + "-g++");
435 EXPECT_EQ((std::vector<std::string>{"clang", "-target", Target, "-foo",
436 "--driver-mode=abc"}),
437 Args);
438
439 std::vector<std::string> ArgsAlt = {"clang", "-foo", "--driver-mode", "abc"};
440 addTargetAndModeForProgramName(ArgsAlt, Target + "-g++");
441 EXPECT_EQ((std::vector<std::string>{"clang", "-target", Target, "-foo",
442 "--driver-mode", "abc"}),
443 ArgsAlt);
444}
445
Hans Wennborg501eadb2014-03-12 16:07:46 +0000446#ifndef LLVM_ON_WIN32
Peter Collingbournec689ee72013-11-06 20:12:45 +0000447TEST(ClangToolTest, BuildASTs) {
448 FixedCompilationDatabase Compilations("/", std::vector<std::string>());
449
450 std::vector<std::string> Sources;
451 Sources.push_back("/a.cc");
452 Sources.push_back("/b.cc");
453 ClangTool Tool(Compilations, Sources);
454
455 Tool.mapVirtualFile("/a.cc", "void a() {}");
456 Tool.mapVirtualFile("/b.cc", "void b() {}");
457
David Blaikie39808ff2014-04-25 14:49:37 +0000458 std::vector<std::unique_ptr<ASTUnit>> ASTs;
Peter Collingbournec689ee72013-11-06 20:12:45 +0000459 EXPECT_EQ(0, Tool.buildASTs(ASTs));
460 EXPECT_EQ(2u, ASTs.size());
Peter Collingbournec689ee72013-11-06 20:12:45 +0000461}
462
Manuel Klimek64083012013-11-07 23:18:05 +0000463struct TestDiagnosticConsumer : public DiagnosticConsumer {
464 TestDiagnosticConsumer() : NumDiagnosticsSeen(0) {}
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000465 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
466 const Diagnostic &Info) override {
Manuel Klimek64083012013-11-07 23:18:05 +0000467 ++NumDiagnosticsSeen;
468 }
469 unsigned NumDiagnosticsSeen;
470};
471
472TEST(ClangToolTest, InjectDiagnosticConsumer) {
473 FixedCompilationDatabase Compilations("/", std::vector<std::string>());
474 ClangTool Tool(Compilations, std::vector<std::string>(1, "/a.cc"));
475 Tool.mapVirtualFile("/a.cc", "int x = undeclared;");
476 TestDiagnosticConsumer Consumer;
477 Tool.setDiagnosticConsumer(&Consumer);
Nico Weber52fbbb12014-04-24 03:48:09 +0000478 std::unique_ptr<FrontendActionFactory> Action(
479 newFrontendActionFactory<SyntaxOnlyAction>());
480 Tool.run(Action.get());
Manuel Klimek64083012013-11-07 23:18:05 +0000481 EXPECT_EQ(1u, Consumer.NumDiagnosticsSeen);
482}
483
Manuel Klimek31cd3fc2013-11-12 17:53:18 +0000484TEST(ClangToolTest, InjectDiagnosticConsumerInBuildASTs) {
485 FixedCompilationDatabase Compilations("/", std::vector<std::string>());
486 ClangTool Tool(Compilations, std::vector<std::string>(1, "/a.cc"));
487 Tool.mapVirtualFile("/a.cc", "int x = undeclared;");
488 TestDiagnosticConsumer Consumer;
489 Tool.setDiagnosticConsumer(&Consumer);
David Blaikie39808ff2014-04-25 14:49:37 +0000490 std::vector<std::unique_ptr<ASTUnit>> ASTs;
Manuel Klimek31cd3fc2013-11-12 17:53:18 +0000491 Tool.buildASTs(ASTs);
492 EXPECT_EQ(1u, ASTs.size());
493 EXPECT_EQ(1u, Consumer.NumDiagnosticsSeen);
494}
NAKAMURA Takumid3b07c62013-11-13 00:18:50 +0000495#endif
Manuel Klimek31cd3fc2013-11-12 17:53:18 +0000496
Manuel Klimek47c245a2012-04-04 12:07:46 +0000497} // end namespace tooling
498} // end namespace clang