Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 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 | |
| 10 | #include "clang/AST/ASTConsumer.h" |
| 11 | #include "clang/AST/DeclCXX.h" |
| 12 | #include "clang/AST/DeclGroup.h" |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 13 | #include "clang/Frontend/ASTUnit.h" |
Richard Smith | 9219d1b | 2012-11-27 21:31:01 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/CompilerInstance.h" |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 15 | #include "clang/Frontend/FrontendAction.h" |
| 16 | #include "clang/Frontend/FrontendActions.h" |
| 17 | #include "clang/Tooling/CompilationDatabase.h" |
| 18 | #include "clang/Tooling/Tooling.h" |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/STLExtras.h" |
Alp Toker | 1d257e1 | 2014-06-04 03:28:55 +0000 | [diff] [blame] | 20 | #include "llvm/Config/llvm-config.h" |
Manuel Klimek | 9b30e2b | 2015-10-06 10:45:03 +0000 | [diff] [blame] | 21 | #include "llvm/Support/TargetSelect.h" |
| 22 | #include "llvm/Support/TargetRegistry.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 23 | #include "gtest/gtest.h" |
Alexander Kornienko | 74e1c46 | 2014-12-03 17:53:02 +0000 | [diff] [blame] | 24 | #include <algorithm> |
Alexander Kornienko | 55f2ca9 | 2012-06-01 14:50:43 +0000 | [diff] [blame] | 25 | #include <string> |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 26 | |
| 27 | namespace clang { |
| 28 | namespace tooling { |
| 29 | |
| 30 | namespace { |
| 31 | /// Takes an ast consumer and returns it from CreateASTConsumer. This only |
| 32 | /// works with single translation unit compilations. |
| 33 | class TestAction : public clang::ASTFrontendAction { |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 34 | public: |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 35 | /// Takes ownership of TestConsumer. |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 36 | explicit TestAction(std::unique_ptr<clang::ASTConsumer> TestConsumer) |
| 37 | : TestConsumer(std::move(TestConsumer)) {} |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 38 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 39 | protected: |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 40 | std::unique_ptr<clang::ASTConsumer> |
| 41 | CreateASTConsumer(clang::CompilerInstance &compiler, |
| 42 | StringRef dummy) override { |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 43 | /// TestConsumer will be deleted by the framework calling us. |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 44 | return std::move(TestConsumer); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 45 | } |
| 46 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 47 | private: |
| 48 | std::unique_ptr<clang::ASTConsumer> TestConsumer; |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | class FindTopLevelDeclConsumer : public clang::ASTConsumer { |
| 52 | public: |
| 53 | explicit FindTopLevelDeclConsumer(bool *FoundTopLevelDecl) |
| 54 | : FoundTopLevelDecl(FoundTopLevelDecl) {} |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 55 | bool HandleTopLevelDecl(clang::DeclGroupRef DeclGroup) override { |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 56 | *FoundTopLevelDecl = true; |
| 57 | return true; |
| 58 | } |
| 59 | private: |
| 60 | bool * const FoundTopLevelDecl; |
| 61 | }; |
| 62 | } // end namespace |
| 63 | |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 64 | TEST(runToolOnCode, FindsNoTopLevelDeclOnEmptyCode) { |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 65 | bool FoundTopLevelDecl = false; |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 66 | EXPECT_TRUE( |
| 67 | runToolOnCode(new TestAction(llvm::make_unique<FindTopLevelDeclConsumer>( |
| 68 | &FoundTopLevelDecl)), |
| 69 | "")); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 70 | EXPECT_FALSE(FoundTopLevelDecl); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | namespace { |
| 74 | class FindClassDeclXConsumer : public clang::ASTConsumer { |
| 75 | public: |
| 76 | FindClassDeclXConsumer(bool *FoundClassDeclX) |
| 77 | : FoundClassDeclX(FoundClassDeclX) {} |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 78 | bool HandleTopLevelDecl(clang::DeclGroupRef GroupRef) override { |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 79 | 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 Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 90 | bool 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 Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 102 | } // end namespace |
| 103 | |
| 104 | TEST(runToolOnCode, FindsClassDecl) { |
| 105 | bool FoundClassDeclX = false; |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 106 | EXPECT_TRUE( |
| 107 | runToolOnCode(new TestAction(llvm::make_unique<FindClassDeclXConsumer>( |
| 108 | &FoundClassDeclX)), |
| 109 | "class X;")); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 110 | EXPECT_TRUE(FoundClassDeclX); |
| 111 | |
| 112 | FoundClassDeclX = false; |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 113 | EXPECT_TRUE( |
| 114 | runToolOnCode(new TestAction(llvm::make_unique<FindClassDeclXConsumer>( |
| 115 | &FoundClassDeclX)), |
| 116 | "class Y;")); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 117 | EXPECT_FALSE(FoundClassDeclX); |
| 118 | } |
| 119 | |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 120 | TEST(buildASTFromCode, FindsClassDecl) { |
David Blaikie | 103a2de | 2014-04-25 17:01:33 +0000 | [diff] [blame] | 121 | std::unique_ptr<ASTUnit> AST = buildASTFromCode("class X;"); |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 122 | ASSERT_TRUE(AST.get()); |
| 123 | EXPECT_TRUE(FindClassDeclX(AST.get())); |
| 124 | |
David Blaikie | 103a2de | 2014-04-25 17:01:33 +0000 | [diff] [blame] | 125 | AST = buildASTFromCode("class Y;"); |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 126 | ASSERT_TRUE(AST.get()); |
| 127 | EXPECT_FALSE(FindClassDeclX(AST.get())); |
| 128 | } |
| 129 | |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 130 | TEST(newFrontendActionFactory, CreatesFrontendActionFactoryFromType) { |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 131 | std::unique_ptr<FrontendActionFactory> Factory( |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 132 | newFrontendActionFactory<SyntaxOnlyAction>()); |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 133 | std::unique_ptr<FrontendAction> Action(Factory->create()); |
Craig Topper | 416fa34 | 2014-06-08 08:38:12 +0000 | [diff] [blame] | 134 | EXPECT_TRUE(Action.get() != nullptr); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | struct IndependentFrontendActionCreator { |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 138 | std::unique_ptr<ASTConsumer> newASTConsumer() { |
| 139 | return llvm::make_unique<FindTopLevelDeclConsumer>(nullptr); |
Manuel Klimek | 5da9dcb | 2012-07-05 18:13:01 +0000 | [diff] [blame] | 140 | } |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | TEST(newFrontendActionFactory, CreatesFrontendActionFactoryFromFactoryType) { |
| 144 | IndependentFrontendActionCreator Creator; |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 145 | std::unique_ptr<FrontendActionFactory> Factory( |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 146 | newFrontendActionFactory(&Creator)); |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 147 | std::unique_ptr<FrontendAction> Action(Factory->create()); |
Craig Topper | 416fa34 | 2014-06-08 08:38:12 +0000 | [diff] [blame] | 148 | EXPECT_TRUE(Action.get() != nullptr); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Alexander Kornienko | 55f2ca9 | 2012-06-01 14:50:43 +0000 | [diff] [blame] | 151 | TEST(ToolInvocation, TestMapVirtualFile) { |
Benjamin Kramer | c4cb3b1 | 2015-10-09 09:54:37 +0000 | [diff] [blame] | 152 | 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 Kornienko | 55f2ca9 | 2012-06-01 14:50:43 +0000 | [diff] [blame] | 159 | 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 Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 164 | clang::tooling::ToolInvocation Invocation(Args, new SyntaxOnlyAction, |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 165 | Files.get()); |
Benjamin Kramer | c4cb3b1 | 2015-10-09 09:54:37 +0000 | [diff] [blame] | 166 | InMemoryFileSystem->addFile( |
| 167 | "test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("#include <abc>\n")); |
| 168 | InMemoryFileSystem->addFile("def/abc", 0, |
| 169 | llvm::MemoryBuffer::getMemBuffer("\n")); |
Alexander Kornienko | 55f2ca9 | 2012-06-01 14:50:43 +0000 | [diff] [blame] | 170 | EXPECT_TRUE(Invocation.run()); |
| 171 | } |
| 172 | |
Manuel Klimek | 1f76c4e | 2013-10-24 07:51:24 +0000 | [diff] [blame] | 173 | TEST(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 Kramer | c4cb3b1 | 2015-10-09 09:54:37 +0000 | [diff] [blame] | 178 | 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 Klimek | 1f76c4e | 2013-10-24 07:51:24 +0000 | [diff] [blame] | 185 | 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 Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 190 | clang::tooling::ToolInvocation Invocation(Args, new SyntaxOnlyAction, |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 191 | Files.get()); |
Benjamin Kramer | c4cb3b1 | 2015-10-09 09:54:37 +0000 | [diff] [blame] | 192 | InMemoryFileSystem->addFile( |
| 193 | "test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("#include <abc>\n")); |
| 194 | InMemoryFileSystem->addFile("def/abc", 0, |
| 195 | llvm::MemoryBuffer::getMemBuffer("\n")); |
Manuel Klimek | 1f76c4e | 2013-10-24 07:51:24 +0000 | [diff] [blame] | 196 | // Add a module.map file in the include directory of our header, so we trigger |
| 197 | // the module.map header search logic. |
Benjamin Kramer | c4cb3b1 | 2015-10-09 09:54:37 +0000 | [diff] [blame] | 198 | InMemoryFileSystem->addFile("def/module.map", 0, |
| 199 | llvm::MemoryBuffer::getMemBuffer("\n")); |
Manuel Klimek | 1f76c4e | 2013-10-24 07:51:24 +0000 | [diff] [blame] | 200 | EXPECT_TRUE(Invocation.run()); |
| 201 | } |
| 202 | |
Edwin Vane | 20c6f54 | 2013-05-29 16:01:10 +0000 | [diff] [blame] | 203 | struct VerifyEndCallback : public SourceFileCallbacks { |
| 204 | VerifyEndCallback() : BeginCalled(0), EndCalled(0), Matched(false) {} |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 205 | bool handleBeginSource(CompilerInstance &CI, StringRef Filename) override { |
Edwin Vane | 20c6f54 | 2013-05-29 16:01:10 +0000 | [diff] [blame] | 206 | ++BeginCalled; |
| 207 | return true; |
| 208 | } |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 209 | void handleEndSource() override { ++EndCalled; } |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 210 | std::unique_ptr<ASTConsumer> newASTConsumer() { |
| 211 | return llvm::make_unique<FindTopLevelDeclConsumer>(&Matched); |
Manuel Klimek | 8246d87 | 2012-10-25 08:49:11 +0000 | [diff] [blame] | 212 | } |
Edwin Vane | 20c6f54 | 2013-05-29 16:01:10 +0000 | [diff] [blame] | 213 | unsigned BeginCalled; |
| 214 | unsigned EndCalled; |
Manuel Klimek | 8246d87 | 2012-10-25 08:49:11 +0000 | [diff] [blame] | 215 | bool Matched; |
| 216 | }; |
| 217 | |
Hans Wennborg | 501eadb | 2014-03-12 16:07:46 +0000 | [diff] [blame] | 218 | #if !defined(LLVM_ON_WIN32) |
Edwin Vane | 20c6f54 | 2013-05-29 16:01:10 +0000 | [diff] [blame] | 219 | TEST(newFrontendActionFactory, InjectsSourceFileCallbacks) { |
Manuel Klimek | 8246d87 | 2012-10-25 08:49:11 +0000 | [diff] [blame] | 220 | 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 Weber | 52fbbb1 | 2014-04-24 03:48:09 +0000 | [diff] [blame] | 231 | std::unique_ptr<FrontendActionFactory> Action( |
| 232 | newFrontendActionFactory(&EndCallback, &EndCallback)); |
| 233 | Tool.run(Action.get()); |
Manuel Klimek | 8246d87 | 2012-10-25 08:49:11 +0000 | [diff] [blame] | 234 | |
| 235 | EXPECT_TRUE(EndCallback.Matched); |
Edwin Vane | 20c6f54 | 2013-05-29 16:01:10 +0000 | [diff] [blame] | 236 | EXPECT_EQ(2u, EndCallback.BeginCalled); |
| 237 | EXPECT_EQ(2u, EndCallback.EndCalled); |
Manuel Klimek | 8246d87 | 2012-10-25 08:49:11 +0000 | [diff] [blame] | 238 | } |
NAKAMURA Takumi | 95fd41a | 2012-10-25 09:38:41 +0000 | [diff] [blame] | 239 | #endif |
Manuel Klimek | 8246d87 | 2012-10-25 08:49:11 +0000 | [diff] [blame] | 240 | |
Richard Smith | 9219d1b | 2012-11-27 21:31:01 +0000 | [diff] [blame] | 241 | struct SkipBodyConsumer : public clang::ASTConsumer { |
| 242 | /// Skip the 'skipMe' function. |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 243 | bool shouldSkipFunctionBody(Decl *D) override { |
Richard Smith | 9219d1b | 2012-11-27 21:31:01 +0000 | [diff] [blame] | 244 | FunctionDecl *F = dyn_cast<FunctionDecl>(D); |
| 245 | return F && F->getNameAsString() == "skipMe"; |
| 246 | } |
| 247 | }; |
| 248 | |
| 249 | struct SkipBodyAction : public clang::ASTFrontendAction { |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 250 | std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &Compiler, |
| 251 | StringRef) override { |
Richard Smith | 9219d1b | 2012-11-27 21:31:01 +0000 | [diff] [blame] | 252 | Compiler.getFrontendOpts().SkipFunctionBodies = true; |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 253 | return llvm::make_unique<SkipBodyConsumer>(); |
Richard Smith | 9219d1b | 2012-11-27 21:31:01 +0000 | [diff] [blame] | 254 | } |
| 255 | }; |
| 256 | |
Hal Finkel | 1d3e3d7 | 2013-01-28 04:37:38 +0000 | [diff] [blame] | 257 | TEST(runToolOnCode, TestSkipFunctionBody) { |
Richard Smith | 9219d1b | 2012-11-27 21:31:01 +0000 | [diff] [blame] | 258 | EXPECT_TRUE(runToolOnCode(new SkipBodyAction, |
| 259 | "int skipMe() { an_error_here }")); |
| 260 | EXPECT_FALSE(runToolOnCode(new SkipBodyAction, |
| 261 | "int skipMeNot() { an_error_here }")); |
| 262 | } |
| 263 | |
Peter Collingbourne | c0423b3 | 2014-03-02 23:37:26 +0000 | [diff] [blame] | 264 | TEST(runToolOnCodeWithArgs, TestNoDepFile) { |
| 265 | llvm::SmallString<32> DepFilePath; |
| 266 | ASSERT_FALSE( |
| 267 | llvm::sys::fs::createTemporaryFile("depfile", "d", DepFilePath)); |
Peter Collingbourne | f937254 | 2014-03-03 08:13:06 +0000 | [diff] [blame] | 268 | std::vector<std::string> Args; |
| 269 | Args.push_back("-MMD"); |
| 270 | Args.push_back("-MT"); |
| 271 | Args.push_back(DepFilePath.str()); |
| 272 | Args.push_back("-MF"); |
| 273 | Args.push_back(DepFilePath.str()); |
Peter Collingbourne | 2bbb029 | 2014-03-03 07:49:35 +0000 | [diff] [blame] | 274 | EXPECT_TRUE(runToolOnCodeWithArgs(new SkipBodyAction, "", Args)); |
Peter Collingbourne | c0423b3 | 2014-03-02 23:37:26 +0000 | [diff] [blame] | 275 | EXPECT_FALSE(llvm::sys::fs::exists(DepFilePath.str())); |
| 276 | EXPECT_FALSE(llvm::sys::fs::remove(DepFilePath.str())); |
| 277 | } |
| 278 | |
Manuel Klimek | d91ac93 | 2013-06-04 14:44:44 +0000 | [diff] [blame] | 279 | TEST(ClangToolTest, ArgumentAdjusters) { |
| 280 | FixedCompilationDatabase Compilations("/", std::vector<std::string>()); |
| 281 | |
| 282 | ClangTool Tool(Compilations, std::vector<std::string>(1, "/a.cc")); |
| 283 | Tool.mapVirtualFile("/a.cc", "void a() {}"); |
| 284 | |
Nico Weber | 52fbbb1 | 2014-04-24 03:48:09 +0000 | [diff] [blame] | 285 | std::unique_ptr<FrontendActionFactory> Action( |
| 286 | newFrontendActionFactory<SyntaxOnlyAction>()); |
| 287 | |
Manuel Klimek | d91ac93 | 2013-06-04 14:44:44 +0000 | [diff] [blame] | 288 | bool Found = false; |
| 289 | bool Ran = false; |
Alexander Kornienko | 74e1c46 | 2014-12-03 17:53:02 +0000 | [diff] [blame] | 290 | ArgumentsAdjuster CheckSyntaxOnlyAdjuster = |
Alexander Kornienko | 857b10f | 2015-11-05 02:19:53 +0000 | [diff] [blame] | 291 | [&Found, &Ran](const CommandLineArguments &Args, StringRef /*unused*/) { |
Alexander Kornienko | 74e1c46 | 2014-12-03 17:53:02 +0000 | [diff] [blame] | 292 | Ran = true; |
| 293 | if (std::find(Args.begin(), Args.end(), "-fsyntax-only") != Args.end()) |
| 294 | Found = true; |
| 295 | return Args; |
| 296 | }; |
| 297 | Tool.appendArgumentsAdjuster(CheckSyntaxOnlyAdjuster); |
Nico Weber | 52fbbb1 | 2014-04-24 03:48:09 +0000 | [diff] [blame] | 298 | Tool.run(Action.get()); |
Manuel Klimek | d91ac93 | 2013-06-04 14:44:44 +0000 | [diff] [blame] | 299 | EXPECT_TRUE(Ran); |
| 300 | EXPECT_TRUE(Found); |
| 301 | |
| 302 | Ran = Found = false; |
| 303 | Tool.clearArgumentsAdjusters(); |
Alexander Kornienko | 74e1c46 | 2014-12-03 17:53:02 +0000 | [diff] [blame] | 304 | Tool.appendArgumentsAdjuster(CheckSyntaxOnlyAdjuster); |
| 305 | Tool.appendArgumentsAdjuster(getClangSyntaxOnlyAdjuster()); |
Nico Weber | 52fbbb1 | 2014-04-24 03:48:09 +0000 | [diff] [blame] | 306 | Tool.run(Action.get()); |
Manuel Klimek | d91ac93 | 2013-06-04 14:44:44 +0000 | [diff] [blame] | 307 | EXPECT_TRUE(Ran); |
| 308 | EXPECT_FALSE(Found); |
| 309 | } |
| 310 | |
Manuel Klimek | 9b30e2b | 2015-10-06 10:45:03 +0000 | [diff] [blame] | 311 | namespace { |
| 312 | /// Find a target name such that looking for it in TargetRegistry by that name |
| 313 | /// returns the same target. We expect that there is at least one target |
| 314 | /// configured with this property. |
| 315 | std::string getAnyTarget() { |
| 316 | llvm::InitializeAllTargets(); |
| 317 | for (const auto &Target : llvm::TargetRegistry::targets()) { |
| 318 | std::string Error; |
NAKAMURA Takumi | fe2e642 | 2015-10-06 13:58:13 +0000 | [diff] [blame] | 319 | StringRef TargetName(Target.getName()); |
| 320 | if (TargetName == "x86-64") |
| 321 | TargetName = "x86_64"; |
| 322 | if (llvm::TargetRegistry::lookupTarget(TargetName, Error) == &Target) { |
| 323 | return TargetName; |
Manuel Klimek | 9b30e2b | 2015-10-06 10:45:03 +0000 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | return ""; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | TEST(addTargetAndModeForProgramName, AddsTargetAndMode) { |
| 331 | std::string Target = getAnyTarget(); |
| 332 | ASSERT_FALSE(Target.empty()); |
| 333 | |
| 334 | std::vector<std::string> Args = {"clang", "-foo"}; |
| 335 | addTargetAndModeForProgramName(Args, ""); |
| 336 | EXPECT_EQ((std::vector<std::string>{"clang", "-foo"}), Args); |
| 337 | addTargetAndModeForProgramName(Args, Target + "-g++"); |
| 338 | EXPECT_EQ((std::vector<std::string>{"clang", "-target", Target, |
| 339 | "--driver-mode=g++", "-foo"}), |
| 340 | Args); |
| 341 | } |
| 342 | |
| 343 | TEST(addTargetAndModeForProgramName, PathIgnored) { |
| 344 | std::string Target = getAnyTarget(); |
| 345 | ASSERT_FALSE(Target.empty()); |
| 346 | |
| 347 | SmallString<32> ToolPath; |
| 348 | llvm::sys::path::append(ToolPath, "foo", "bar", Target + "-g++"); |
| 349 | |
| 350 | std::vector<std::string> Args = {"clang", "-foo"}; |
| 351 | addTargetAndModeForProgramName(Args, ToolPath); |
| 352 | EXPECT_EQ((std::vector<std::string>{"clang", "-target", Target, |
| 353 | "--driver-mode=g++", "-foo"}), |
| 354 | Args); |
| 355 | } |
| 356 | |
| 357 | TEST(addTargetAndModeForProgramName, IgnoresExistingTarget) { |
| 358 | std::string Target = getAnyTarget(); |
| 359 | ASSERT_FALSE(Target.empty()); |
| 360 | |
| 361 | std::vector<std::string> Args = {"clang", "-foo", "-target", "something"}; |
| 362 | addTargetAndModeForProgramName(Args, Target + "-g++"); |
| 363 | EXPECT_EQ((std::vector<std::string>{"clang", "--driver-mode=g++", "-foo", |
| 364 | "-target", "something"}), |
| 365 | Args); |
| 366 | |
| 367 | std::vector<std::string> ArgsAlt = {"clang", "-foo", "-target=something"}; |
| 368 | addTargetAndModeForProgramName(ArgsAlt, Target + "-g++"); |
| 369 | EXPECT_EQ((std::vector<std::string>{"clang", "--driver-mode=g++", "-foo", |
| 370 | "-target=something"}), |
| 371 | ArgsAlt); |
| 372 | } |
| 373 | |
| 374 | TEST(addTargetAndModeForProgramName, IgnoresExistingMode) { |
| 375 | std::string Target = getAnyTarget(); |
| 376 | ASSERT_FALSE(Target.empty()); |
| 377 | |
| 378 | std::vector<std::string> Args = {"clang", "-foo", "--driver-mode=abc"}; |
| 379 | addTargetAndModeForProgramName(Args, Target + "-g++"); |
| 380 | EXPECT_EQ((std::vector<std::string>{"clang", "-target", Target, "-foo", |
| 381 | "--driver-mode=abc"}), |
| 382 | Args); |
| 383 | |
| 384 | std::vector<std::string> ArgsAlt = {"clang", "-foo", "--driver-mode", "abc"}; |
| 385 | addTargetAndModeForProgramName(ArgsAlt, Target + "-g++"); |
| 386 | EXPECT_EQ((std::vector<std::string>{"clang", "-target", Target, "-foo", |
| 387 | "--driver-mode", "abc"}), |
| 388 | ArgsAlt); |
| 389 | } |
| 390 | |
Hans Wennborg | 501eadb | 2014-03-12 16:07:46 +0000 | [diff] [blame] | 391 | #ifndef LLVM_ON_WIN32 |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 392 | TEST(ClangToolTest, BuildASTs) { |
| 393 | FixedCompilationDatabase Compilations("/", std::vector<std::string>()); |
| 394 | |
| 395 | std::vector<std::string> Sources; |
| 396 | Sources.push_back("/a.cc"); |
| 397 | Sources.push_back("/b.cc"); |
| 398 | ClangTool Tool(Compilations, Sources); |
| 399 | |
| 400 | Tool.mapVirtualFile("/a.cc", "void a() {}"); |
| 401 | Tool.mapVirtualFile("/b.cc", "void b() {}"); |
| 402 | |
David Blaikie | 39808ff | 2014-04-25 14:49:37 +0000 | [diff] [blame] | 403 | std::vector<std::unique_ptr<ASTUnit>> ASTs; |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 404 | EXPECT_EQ(0, Tool.buildASTs(ASTs)); |
| 405 | EXPECT_EQ(2u, ASTs.size()); |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Manuel Klimek | 6408301 | 2013-11-07 23:18:05 +0000 | [diff] [blame] | 408 | struct TestDiagnosticConsumer : public DiagnosticConsumer { |
| 409 | TestDiagnosticConsumer() : NumDiagnosticsSeen(0) {} |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 410 | void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, |
| 411 | const Diagnostic &Info) override { |
Manuel Klimek | 6408301 | 2013-11-07 23:18:05 +0000 | [diff] [blame] | 412 | ++NumDiagnosticsSeen; |
| 413 | } |
| 414 | unsigned NumDiagnosticsSeen; |
| 415 | }; |
| 416 | |
| 417 | TEST(ClangToolTest, InjectDiagnosticConsumer) { |
| 418 | FixedCompilationDatabase Compilations("/", std::vector<std::string>()); |
| 419 | ClangTool Tool(Compilations, std::vector<std::string>(1, "/a.cc")); |
| 420 | Tool.mapVirtualFile("/a.cc", "int x = undeclared;"); |
| 421 | TestDiagnosticConsumer Consumer; |
| 422 | Tool.setDiagnosticConsumer(&Consumer); |
Nico Weber | 52fbbb1 | 2014-04-24 03:48:09 +0000 | [diff] [blame] | 423 | std::unique_ptr<FrontendActionFactory> Action( |
| 424 | newFrontendActionFactory<SyntaxOnlyAction>()); |
| 425 | Tool.run(Action.get()); |
Manuel Klimek | 6408301 | 2013-11-07 23:18:05 +0000 | [diff] [blame] | 426 | EXPECT_EQ(1u, Consumer.NumDiagnosticsSeen); |
| 427 | } |
| 428 | |
Manuel Klimek | 31cd3fc | 2013-11-12 17:53:18 +0000 | [diff] [blame] | 429 | TEST(ClangToolTest, InjectDiagnosticConsumerInBuildASTs) { |
| 430 | FixedCompilationDatabase Compilations("/", std::vector<std::string>()); |
| 431 | ClangTool Tool(Compilations, std::vector<std::string>(1, "/a.cc")); |
| 432 | Tool.mapVirtualFile("/a.cc", "int x = undeclared;"); |
| 433 | TestDiagnosticConsumer Consumer; |
| 434 | Tool.setDiagnosticConsumer(&Consumer); |
David Blaikie | 39808ff | 2014-04-25 14:49:37 +0000 | [diff] [blame] | 435 | std::vector<std::unique_ptr<ASTUnit>> ASTs; |
Manuel Klimek | 31cd3fc | 2013-11-12 17:53:18 +0000 | [diff] [blame] | 436 | Tool.buildASTs(ASTs); |
| 437 | EXPECT_EQ(1u, ASTs.size()); |
| 438 | EXPECT_EQ(1u, Consumer.NumDiagnosticsSeen); |
| 439 | } |
NAKAMURA Takumi | d3b07c6 | 2013-11-13 00:18:50 +0000 | [diff] [blame] | 440 | #endif |
Manuel Klimek | 31cd3fc | 2013-11-12 17:53:18 +0000 | [diff] [blame] | 441 | |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 442 | } // end namespace tooling |
| 443 | } // end namespace clang |