ToolingTest.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. //===- 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. #include "clang/AST/ASTConsumer.h"
  10. #include "clang/AST/DeclCXX.h"
  11. #include "clang/AST/DeclGroup.h"
  12. #include "clang/Frontend/ASTUnit.h"
  13. #include "clang/Frontend/CompilerInstance.h"
  14. #include "clang/Frontend/FrontendAction.h"
  15. #include "clang/Frontend/FrontendActions.h"
  16. #include "clang/Tooling/CompilationDatabase.h"
  17. #include "clang/Tooling/Tooling.h"
  18. #include "llvm/ADT/STLExtras.h"
  19. #include "llvm/Support/Path.h"
  20. #include "llvm/Support/TargetRegistry.h"
  21. #include "llvm/Support/TargetSelect.h"
  22. #include "gtest/gtest.h"
  23. #include <algorithm>
  24. #include <string>
  25. namespace clang {
  26. namespace tooling {
  27. namespace {
  28. /// Takes an ast consumer and returns it from CreateASTConsumer. This only
  29. /// works with single translation unit compilations.
  30. class TestAction : public clang::ASTFrontendAction {
  31. public:
  32. /// Takes ownership of TestConsumer.
  33. explicit TestAction(std::unique_ptr<clang::ASTConsumer> TestConsumer)
  34. : TestConsumer(std::move(TestConsumer)) {}
  35. protected:
  36. std::unique_ptr<clang::ASTConsumer>
  37. CreateASTConsumer(clang::CompilerInstance &compiler,
  38. StringRef dummy) override {
  39. /// TestConsumer will be deleted by the framework calling us.
  40. return std::move(TestConsumer);
  41. }
  42. private:
  43. std::unique_ptr<clang::ASTConsumer> TestConsumer;
  44. };
  45. class FindTopLevelDeclConsumer : public clang::ASTConsumer {
  46. public:
  47. explicit FindTopLevelDeclConsumer(bool *FoundTopLevelDecl)
  48. : FoundTopLevelDecl(FoundTopLevelDecl) {}
  49. bool HandleTopLevelDecl(clang::DeclGroupRef DeclGroup) override {
  50. *FoundTopLevelDecl = true;
  51. return true;
  52. }
  53. private:
  54. bool * const FoundTopLevelDecl;
  55. };
  56. } // end namespace
  57. TEST(runToolOnCode, FindsNoTopLevelDeclOnEmptyCode) {
  58. bool FoundTopLevelDecl = false;
  59. EXPECT_TRUE(
  60. runToolOnCode(new TestAction(llvm::make_unique<FindTopLevelDeclConsumer>(
  61. &FoundTopLevelDecl)),
  62. ""));
  63. EXPECT_FALSE(FoundTopLevelDecl);
  64. }
  65. namespace {
  66. class FindClassDeclXConsumer : public clang::ASTConsumer {
  67. public:
  68. FindClassDeclXConsumer(bool *FoundClassDeclX)
  69. : FoundClassDeclX(FoundClassDeclX) {}
  70. bool HandleTopLevelDecl(clang::DeclGroupRef GroupRef) override {
  71. if (CXXRecordDecl* Record = dyn_cast<clang::CXXRecordDecl>(
  72. *GroupRef.begin())) {
  73. if (Record->getName() == "X") {
  74. *FoundClassDeclX = true;
  75. }
  76. }
  77. return true;
  78. }
  79. private:
  80. bool *FoundClassDeclX;
  81. };
  82. bool FindClassDeclX(ASTUnit *AST) {
  83. for (std::vector<Decl *>::iterator i = AST->top_level_begin(),
  84. e = AST->top_level_end();
  85. i != e; ++i) {
  86. if (CXXRecordDecl* Record = dyn_cast<clang::CXXRecordDecl>(*i)) {
  87. if (Record->getName() == "X") {
  88. return true;
  89. }
  90. }
  91. }
  92. return false;
  93. }
  94. } // end namespace
  95. TEST(runToolOnCode, FindsClassDecl) {
  96. bool FoundClassDeclX = false;
  97. EXPECT_TRUE(
  98. runToolOnCode(new TestAction(llvm::make_unique<FindClassDeclXConsumer>(
  99. &FoundClassDeclX)),
  100. "class X;"));
  101. EXPECT_TRUE(FoundClassDeclX);
  102. FoundClassDeclX = false;
  103. EXPECT_TRUE(
  104. runToolOnCode(new TestAction(llvm::make_unique<FindClassDeclXConsumer>(
  105. &FoundClassDeclX)),
  106. "class Y;"));
  107. EXPECT_FALSE(FoundClassDeclX);
  108. }
  109. TEST(buildASTFromCode, FindsClassDecl) {
  110. std::unique_ptr<ASTUnit> AST = buildASTFromCode("class X;");
  111. ASSERT_TRUE(AST.get());
  112. EXPECT_TRUE(FindClassDeclX(AST.get()));
  113. AST = buildASTFromCode("class Y;");
  114. ASSERT_TRUE(AST.get());
  115. EXPECT_FALSE(FindClassDeclX(AST.get()));
  116. }
  117. TEST(newFrontendActionFactory, CreatesFrontendActionFactoryFromType) {
  118. std::unique_ptr<FrontendActionFactory> Factory(
  119. newFrontendActionFactory<SyntaxOnlyAction>());
  120. std::unique_ptr<FrontendAction> Action(Factory->create());
  121. EXPECT_TRUE(Action.get() != nullptr);
  122. }
  123. struct IndependentFrontendActionCreator {
  124. std::unique_ptr<ASTConsumer> newASTConsumer() {
  125. return llvm::make_unique<FindTopLevelDeclConsumer>(nullptr);
  126. }
  127. };
  128. TEST(newFrontendActionFactory, CreatesFrontendActionFactoryFromFactoryType) {
  129. IndependentFrontendActionCreator Creator;
  130. std::unique_ptr<FrontendActionFactory> Factory(
  131. newFrontendActionFactory(&Creator));
  132. std::unique_ptr<FrontendAction> Action(Factory->create());
  133. EXPECT_TRUE(Action.get() != nullptr);
  134. }
  135. TEST(ToolInvocation, TestMapVirtualFile) {
  136. llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFileSystem(
  137. new llvm::vfs::OverlayFileSystem(llvm::vfs::getRealFileSystem()));
  138. llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
  139. new llvm::vfs::InMemoryFileSystem);
  140. OverlayFileSystem->pushOverlay(InMemoryFileSystem);
  141. llvm::IntrusiveRefCntPtr<FileManager> Files(
  142. new FileManager(FileSystemOptions(), OverlayFileSystem));
  143. std::vector<std::string> Args;
  144. Args.push_back("tool-executable");
  145. Args.push_back("-Idef");
  146. Args.push_back("-fsyntax-only");
  147. Args.push_back("test.cpp");
  148. clang::tooling::ToolInvocation Invocation(Args, new SyntaxOnlyAction,
  149. Files.get());
  150. InMemoryFileSystem->addFile(
  151. "test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("#include <abc>\n"));
  152. InMemoryFileSystem->addFile("def/abc", 0,
  153. llvm::MemoryBuffer::getMemBuffer("\n"));
  154. EXPECT_TRUE(Invocation.run());
  155. }
  156. TEST(ToolInvocation, TestVirtualModulesCompilation) {
  157. // FIXME: Currently, this only tests that we don't exit with an error if a
  158. // mapped module.map is found on the include path. In the future, expand this
  159. // test to run a full modules enabled compilation, so we make sure we can
  160. // rerun modules compilations with a virtual file system.
  161. llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFileSystem(
  162. new llvm::vfs::OverlayFileSystem(llvm::vfs::getRealFileSystem()));
  163. llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
  164. new llvm::vfs::InMemoryFileSystem);
  165. OverlayFileSystem->pushOverlay(InMemoryFileSystem);
  166. llvm::IntrusiveRefCntPtr<FileManager> Files(
  167. new FileManager(FileSystemOptions(), OverlayFileSystem));
  168. std::vector<std::string> Args;
  169. Args.push_back("tool-executable");
  170. Args.push_back("-Idef");
  171. Args.push_back("-fsyntax-only");
  172. Args.push_back("test.cpp");
  173. clang::tooling::ToolInvocation Invocation(Args, new SyntaxOnlyAction,
  174. Files.get());
  175. InMemoryFileSystem->addFile(
  176. "test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("#include <abc>\n"));
  177. InMemoryFileSystem->addFile("def/abc", 0,
  178. llvm::MemoryBuffer::getMemBuffer("\n"));
  179. // Add a module.map file in the include directory of our header, so we trigger
  180. // the module.map header search logic.
  181. InMemoryFileSystem->addFile("def/module.map", 0,
  182. llvm::MemoryBuffer::getMemBuffer("\n"));
  183. EXPECT_TRUE(Invocation.run());
  184. }
  185. struct VerifyEndCallback : public SourceFileCallbacks {
  186. VerifyEndCallback() : BeginCalled(0), EndCalled(0), Matched(false) {}
  187. bool handleBeginSource(CompilerInstance &CI) override {
  188. ++BeginCalled;
  189. return true;
  190. }
  191. void handleEndSource() override { ++EndCalled; }
  192. std::unique_ptr<ASTConsumer> newASTConsumer() {
  193. return llvm::make_unique<FindTopLevelDeclConsumer>(&Matched);
  194. }
  195. unsigned BeginCalled;
  196. unsigned EndCalled;
  197. bool Matched;
  198. };
  199. #if !defined(_WIN32)
  200. TEST(newFrontendActionFactory, InjectsSourceFileCallbacks) {
  201. VerifyEndCallback EndCallback;
  202. FixedCompilationDatabase Compilations("/", std::vector<std::string>());
  203. std::vector<std::string> Sources;
  204. Sources.push_back("/a.cc");
  205. Sources.push_back("/b.cc");
  206. ClangTool Tool(Compilations, Sources);
  207. Tool.mapVirtualFile("/a.cc", "void a() {}");
  208. Tool.mapVirtualFile("/b.cc", "void b() {}");
  209. std::unique_ptr<FrontendActionFactory> Action(
  210. newFrontendActionFactory(&EndCallback, &EndCallback));
  211. Tool.run(Action.get());
  212. EXPECT_TRUE(EndCallback.Matched);
  213. EXPECT_EQ(2u, EndCallback.BeginCalled);
  214. EXPECT_EQ(2u, EndCallback.EndCalled);
  215. }
  216. #endif
  217. struct SkipBodyConsumer : public clang::ASTConsumer {
  218. /// Skip the 'skipMe' function.
  219. bool shouldSkipFunctionBody(Decl *D) override {
  220. NamedDecl *F = dyn_cast<NamedDecl>(D);
  221. return F && F->getNameAsString() == "skipMe";
  222. }
  223. };
  224. struct SkipBodyAction : public clang::ASTFrontendAction {
  225. std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &Compiler,
  226. StringRef) override {
  227. Compiler.getFrontendOpts().SkipFunctionBodies = true;
  228. return llvm::make_unique<SkipBodyConsumer>();
  229. }
  230. };
  231. TEST(runToolOnCode, TestSkipFunctionBody) {
  232. std::vector<std::string> Args = {"-std=c++11"};
  233. std::vector<std::string> Args2 = {"-fno-delayed-template-parsing"};
  234. EXPECT_TRUE(runToolOnCode(new SkipBodyAction,
  235. "int skipMe() { an_error_here }"));
  236. EXPECT_FALSE(runToolOnCode(new SkipBodyAction,
  237. "int skipMeNot() { an_error_here }"));
  238. // Test constructors with initializers
  239. EXPECT_TRUE(runToolOnCodeWithArgs(
  240. new SkipBodyAction,
  241. "struct skipMe { skipMe() : an_error() { more error } };", Args));
  242. EXPECT_TRUE(runToolOnCodeWithArgs(
  243. new SkipBodyAction, "struct skipMe { skipMe(); };"
  244. "skipMe::skipMe() : an_error([](){;}) { more error }",
  245. Args));
  246. EXPECT_TRUE(runToolOnCodeWithArgs(
  247. new SkipBodyAction, "struct skipMe { skipMe(); };"
  248. "skipMe::skipMe() : an_error{[](){;}} { more error }",
  249. Args));
  250. EXPECT_TRUE(runToolOnCodeWithArgs(
  251. new SkipBodyAction,
  252. "struct skipMe { skipMe(); };"
  253. "skipMe::skipMe() : a<b<c>(e)>>(), f{}, g() { error }",
  254. Args));
  255. EXPECT_TRUE(runToolOnCodeWithArgs(
  256. new SkipBodyAction, "struct skipMe { skipMe() : bases()... { error } };",
  257. Args));
  258. EXPECT_FALSE(runToolOnCodeWithArgs(
  259. new SkipBodyAction, "struct skipMeNot { skipMeNot() : an_error() { } };",
  260. Args));
  261. EXPECT_FALSE(runToolOnCodeWithArgs(new SkipBodyAction,
  262. "struct skipMeNot { skipMeNot(); };"
  263. "skipMeNot::skipMeNot() : an_error() { }",
  264. Args));
  265. // Try/catch
  266. EXPECT_TRUE(runToolOnCode(
  267. new SkipBodyAction,
  268. "void skipMe() try { an_error() } catch(error) { error };"));
  269. EXPECT_TRUE(runToolOnCode(
  270. new SkipBodyAction,
  271. "struct S { void skipMe() try { an_error() } catch(error) { error } };"));
  272. EXPECT_TRUE(
  273. runToolOnCode(new SkipBodyAction,
  274. "void skipMe() try { an_error() } catch(error) { error; }"
  275. "catch(error) { error } catch (error) { }"));
  276. EXPECT_FALSE(runToolOnCode(
  277. new SkipBodyAction,
  278. "void skipMe() try something;")); // don't crash while parsing
  279. // Template
  280. EXPECT_TRUE(runToolOnCode(
  281. new SkipBodyAction, "template<typename T> int skipMe() { an_error_here }"
  282. "int x = skipMe<int>();"));
  283. EXPECT_FALSE(runToolOnCodeWithArgs(
  284. new SkipBodyAction,
  285. "template<typename T> int skipMeNot() { an_error_here }", Args2));
  286. }
  287. TEST(runToolOnCodeWithArgs, TestNoDepFile) {
  288. llvm::SmallString<32> DepFilePath;
  289. ASSERT_FALSE(llvm::sys::fs::getPotentiallyUniqueTempFileName("depfile", "d",
  290. DepFilePath));
  291. std::vector<std::string> Args;
  292. Args.push_back("-MMD");
  293. Args.push_back("-MT");
  294. Args.push_back(DepFilePath.str());
  295. Args.push_back("-MF");
  296. Args.push_back(DepFilePath.str());
  297. EXPECT_TRUE(runToolOnCodeWithArgs(new SkipBodyAction, "", Args));
  298. EXPECT_FALSE(llvm::sys::fs::exists(DepFilePath.str()));
  299. EXPECT_FALSE(llvm::sys::fs::remove(DepFilePath.str()));
  300. }
  301. struct CheckColoredDiagnosticsAction : public clang::ASTFrontendAction {
  302. CheckColoredDiagnosticsAction(bool ShouldShowColor)
  303. : ShouldShowColor(ShouldShowColor) {}
  304. std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &Compiler,
  305. StringRef) override {
  306. if (Compiler.getDiagnosticOpts().ShowColors != ShouldShowColor)
  307. Compiler.getDiagnostics().Report(
  308. Compiler.getDiagnostics().getCustomDiagID(
  309. DiagnosticsEngine::Fatal,
  310. "getDiagnosticOpts().ShowColors != ShouldShowColor"));
  311. return llvm::make_unique<ASTConsumer>();
  312. }
  313. private:
  314. bool ShouldShowColor = true;
  315. };
  316. TEST(runToolOnCodeWithArgs, DiagnosticsColor) {
  317. EXPECT_TRUE(runToolOnCodeWithArgs(new CheckColoredDiagnosticsAction(true), "",
  318. {"-fcolor-diagnostics"}));
  319. EXPECT_TRUE(runToolOnCodeWithArgs(new CheckColoredDiagnosticsAction(false),
  320. "", {"-fno-color-diagnostics"}));
  321. EXPECT_TRUE(
  322. runToolOnCodeWithArgs(new CheckColoredDiagnosticsAction(true), "",
  323. {"-fno-color-diagnostics", "-fcolor-diagnostics"}));
  324. EXPECT_TRUE(
  325. runToolOnCodeWithArgs(new CheckColoredDiagnosticsAction(false), "",
  326. {"-fcolor-diagnostics", "-fno-color-diagnostics"}));
  327. EXPECT_TRUE(runToolOnCodeWithArgs(
  328. new CheckColoredDiagnosticsAction(true), "",
  329. {"-fno-color-diagnostics", "-fdiagnostics-color=always"}));
  330. // Check that this test would fail if ShowColors is not what it should.
  331. EXPECT_FALSE(runToolOnCodeWithArgs(new CheckColoredDiagnosticsAction(false),
  332. "", {"-fcolor-diagnostics"}));
  333. }
  334. TEST(ClangToolTest, ArgumentAdjusters) {
  335. FixedCompilationDatabase Compilations("/", std::vector<std::string>());
  336. ClangTool Tool(Compilations, std::vector<std::string>(1, "/a.cc"));
  337. Tool.mapVirtualFile("/a.cc", "void a() {}");
  338. std::unique_ptr<FrontendActionFactory> Action(
  339. newFrontendActionFactory<SyntaxOnlyAction>());
  340. bool Found = false;
  341. bool Ran = false;
  342. ArgumentsAdjuster CheckSyntaxOnlyAdjuster =
  343. [&Found, &Ran](const CommandLineArguments &Args, StringRef /*unused*/) {
  344. Ran = true;
  345. if (std::find(Args.begin(), Args.end(), "-fsyntax-only") != Args.end())
  346. Found = true;
  347. return Args;
  348. };
  349. Tool.appendArgumentsAdjuster(CheckSyntaxOnlyAdjuster);
  350. Tool.run(Action.get());
  351. EXPECT_TRUE(Ran);
  352. EXPECT_TRUE(Found);
  353. Ran = Found = false;
  354. Tool.clearArgumentsAdjusters();
  355. Tool.appendArgumentsAdjuster(CheckSyntaxOnlyAdjuster);
  356. Tool.appendArgumentsAdjuster(getClangSyntaxOnlyAdjuster());
  357. Tool.run(Action.get());
  358. EXPECT_TRUE(Ran);
  359. EXPECT_FALSE(Found);
  360. }
  361. TEST(ClangToolTest, BaseVirtualFileSystemUsage) {
  362. FixedCompilationDatabase Compilations("/", std::vector<std::string>());
  363. llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFileSystem(
  364. new llvm::vfs::OverlayFileSystem(llvm::vfs::getRealFileSystem()));
  365. llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
  366. new llvm::vfs::InMemoryFileSystem);
  367. OverlayFileSystem->pushOverlay(InMemoryFileSystem);
  368. InMemoryFileSystem->addFile(
  369. "a.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int main() {}"));
  370. ClangTool Tool(Compilations, std::vector<std::string>(1, "a.cpp"),
  371. std::make_shared<PCHContainerOperations>(), OverlayFileSystem);
  372. std::unique_ptr<FrontendActionFactory> Action(
  373. newFrontendActionFactory<SyntaxOnlyAction>());
  374. EXPECT_EQ(0, Tool.run(Action.get()));
  375. }
  376. // Check getClangStripDependencyFileAdjuster doesn't strip args after -MD/-MMD.
  377. TEST(ClangToolTest, StripDependencyFileAdjuster) {
  378. FixedCompilationDatabase Compilations("/", {"-MD", "-c", "-MMD", "-w"});
  379. ClangTool Tool(Compilations, std::vector<std::string>(1, "/a.cc"));
  380. Tool.mapVirtualFile("/a.cc", "void a() {}");
  381. std::unique_ptr<FrontendActionFactory> Action(
  382. newFrontendActionFactory<SyntaxOnlyAction>());
  383. CommandLineArguments FinalArgs;
  384. ArgumentsAdjuster CheckFlagsAdjuster =
  385. [&FinalArgs](const CommandLineArguments &Args, StringRef /*unused*/) {
  386. FinalArgs = Args;
  387. return Args;
  388. };
  389. Tool.clearArgumentsAdjusters();
  390. Tool.appendArgumentsAdjuster(getClangStripDependencyFileAdjuster());
  391. Tool.appendArgumentsAdjuster(CheckFlagsAdjuster);
  392. Tool.run(Action.get());
  393. auto HasFlag = [&FinalArgs](const std::string &Flag) {
  394. return std::find(FinalArgs.begin(), FinalArgs.end(), Flag) !=
  395. FinalArgs.end();
  396. };
  397. EXPECT_FALSE(HasFlag("-MD"));
  398. EXPECT_FALSE(HasFlag("-MMD"));
  399. EXPECT_TRUE(HasFlag("-c"));
  400. EXPECT_TRUE(HasFlag("-w"));
  401. }
  402. namespace {
  403. /// Find a target name such that looking for it in TargetRegistry by that name
  404. /// returns the same target. We expect that there is at least one target
  405. /// configured with this property.
  406. std::string getAnyTarget() {
  407. llvm::InitializeAllTargets();
  408. for (const auto &Target : llvm::TargetRegistry::targets()) {
  409. std::string Error;
  410. StringRef TargetName(Target.getName());
  411. if (TargetName == "x86-64")
  412. TargetName = "x86_64";
  413. if (llvm::TargetRegistry::lookupTarget(TargetName, Error) == &Target) {
  414. return TargetName;
  415. }
  416. }
  417. return "";
  418. }
  419. }
  420. TEST(addTargetAndModeForProgramName, AddsTargetAndMode) {
  421. std::string Target = getAnyTarget();
  422. ASSERT_FALSE(Target.empty());
  423. std::vector<std::string> Args = {"clang", "-foo"};
  424. addTargetAndModeForProgramName(Args, "");
  425. EXPECT_EQ((std::vector<std::string>{"clang", "-foo"}), Args);
  426. addTargetAndModeForProgramName(Args, Target + "-g++");
  427. EXPECT_EQ((std::vector<std::string>{"clang", "-target", Target,
  428. "--driver-mode=g++", "-foo"}),
  429. Args);
  430. }
  431. TEST(addTargetAndModeForProgramName, PathIgnored) {
  432. std::string Target = getAnyTarget();
  433. ASSERT_FALSE(Target.empty());
  434. SmallString<32> ToolPath;
  435. llvm::sys::path::append(ToolPath, "foo", "bar", Target + "-g++");
  436. std::vector<std::string> Args = {"clang", "-foo"};
  437. addTargetAndModeForProgramName(Args, ToolPath);
  438. EXPECT_EQ((std::vector<std::string>{"clang", "-target", Target,
  439. "--driver-mode=g++", "-foo"}),
  440. Args);
  441. }
  442. TEST(addTargetAndModeForProgramName, IgnoresExistingTarget) {
  443. std::string Target = getAnyTarget();
  444. ASSERT_FALSE(Target.empty());
  445. std::vector<std::string> Args = {"clang", "-foo", "-target", "something"};
  446. addTargetAndModeForProgramName(Args, Target + "-g++");
  447. EXPECT_EQ((std::vector<std::string>{"clang", "--driver-mode=g++", "-foo",
  448. "-target", "something"}),
  449. Args);
  450. std::vector<std::string> ArgsAlt = {"clang", "-foo", "-target=something"};
  451. addTargetAndModeForProgramName(ArgsAlt, Target + "-g++");
  452. EXPECT_EQ((std::vector<std::string>{"clang", "--driver-mode=g++", "-foo",
  453. "-target=something"}),
  454. ArgsAlt);
  455. }
  456. TEST(addTargetAndModeForProgramName, IgnoresExistingMode) {
  457. std::string Target = getAnyTarget();
  458. ASSERT_FALSE(Target.empty());
  459. std::vector<std::string> Args = {"clang", "-foo", "--driver-mode=abc"};
  460. addTargetAndModeForProgramName(Args, Target + "-g++");
  461. EXPECT_EQ((std::vector<std::string>{"clang", "-target", Target, "-foo",
  462. "--driver-mode=abc"}),
  463. Args);
  464. std::vector<std::string> ArgsAlt = {"clang", "-foo", "--driver-mode", "abc"};
  465. addTargetAndModeForProgramName(ArgsAlt, Target + "-g++");
  466. EXPECT_EQ((std::vector<std::string>{"clang", "-target", Target, "-foo",
  467. "--driver-mode", "abc"}),
  468. ArgsAlt);
  469. }
  470. #ifndef _WIN32
  471. TEST(ClangToolTest, BuildASTs) {
  472. FixedCompilationDatabase Compilations("/", std::vector<std::string>());
  473. std::vector<std::string> Sources;
  474. Sources.push_back("/a.cc");
  475. Sources.push_back("/b.cc");
  476. ClangTool Tool(Compilations, Sources);
  477. Tool.mapVirtualFile("/a.cc", "void a() {}");
  478. Tool.mapVirtualFile("/b.cc", "void b() {}");
  479. std::vector<std::unique_ptr<ASTUnit>> ASTs;
  480. EXPECT_EQ(0, Tool.buildASTs(ASTs));
  481. EXPECT_EQ(2u, ASTs.size());
  482. }
  483. struct TestDiagnosticConsumer : public DiagnosticConsumer {
  484. TestDiagnosticConsumer() : NumDiagnosticsSeen(0) {}
  485. void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
  486. const Diagnostic &Info) override {
  487. ++NumDiagnosticsSeen;
  488. }
  489. unsigned NumDiagnosticsSeen;
  490. };
  491. TEST(ClangToolTest, InjectDiagnosticConsumer) {
  492. FixedCompilationDatabase Compilations("/", std::vector<std::string>());
  493. ClangTool Tool(Compilations, std::vector<std::string>(1, "/a.cc"));
  494. Tool.mapVirtualFile("/a.cc", "int x = undeclared;");
  495. TestDiagnosticConsumer Consumer;
  496. Tool.setDiagnosticConsumer(&Consumer);
  497. std::unique_ptr<FrontendActionFactory> Action(
  498. newFrontendActionFactory<SyntaxOnlyAction>());
  499. Tool.run(Action.get());
  500. EXPECT_EQ(1u, Consumer.NumDiagnosticsSeen);
  501. }
  502. TEST(ClangToolTest, InjectDiagnosticConsumerInBuildASTs) {
  503. FixedCompilationDatabase Compilations("/", std::vector<std::string>());
  504. ClangTool Tool(Compilations, std::vector<std::string>(1, "/a.cc"));
  505. Tool.mapVirtualFile("/a.cc", "int x = undeclared;");
  506. TestDiagnosticConsumer Consumer;
  507. Tool.setDiagnosticConsumer(&Consumer);
  508. std::vector<std::unique_ptr<ASTUnit>> ASTs;
  509. Tool.buildASTs(ASTs);
  510. EXPECT_EQ(1u, ASTs.size());
  511. EXPECT_EQ(1u, Consumer.NumDiagnosticsSeen);
  512. }
  513. #endif
  514. TEST(runToolOnCode, TestResetDiagnostics) {
  515. // This is a tool that resets the diagnostic during the compilation.
  516. struct ResetDiagnosticAction : public clang::ASTFrontendAction {
  517. std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &Compiler,
  518. StringRef) override {
  519. struct Consumer : public clang::ASTConsumer {
  520. bool HandleTopLevelDecl(clang::DeclGroupRef D) override {
  521. auto &Diags = (*D.begin())->getASTContext().getDiagnostics();
  522. // Ignore any error
  523. Diags.Reset();
  524. // Disable warnings because computing the CFG might crash.
  525. Diags.setIgnoreAllWarnings(true);
  526. return true;
  527. }
  528. };
  529. return llvm::make_unique<Consumer>();
  530. }
  531. };
  532. // Should not crash
  533. EXPECT_FALSE(
  534. runToolOnCode(new ResetDiagnosticAction,
  535. "struct Foo { Foo(int); ~Foo(); struct Fwd _fwd; };"
  536. "void func() { long x; Foo f(x); }"));
  537. }
  538. } // end namespace tooling
  539. } // end namespace clang