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" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 21 | #include "gtest/gtest.h" |
Alexander Kornienko | 74e1c46 | 2014-12-03 17:53:02 +0000 | [diff] [blame] | 22 | #include <algorithm> |
Alexander Kornienko | 55f2ca9 | 2012-06-01 14:50:43 +0000 | [diff] [blame] | 23 | #include <string> |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 24 | |
| 25 | namespace clang { |
| 26 | namespace tooling { |
| 27 | |
| 28 | namespace { |
| 29 | /// Takes an ast consumer and returns it from CreateASTConsumer. This only |
| 30 | /// works with single translation unit compilations. |
| 31 | class TestAction : public clang::ASTFrontendAction { |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 32 | public: |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 33 | /// Takes ownership of TestConsumer. |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 34 | explicit TestAction(std::unique_ptr<clang::ASTConsumer> TestConsumer) |
| 35 | : TestConsumer(std::move(TestConsumer)) {} |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 36 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 37 | protected: |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 38 | std::unique_ptr<clang::ASTConsumer> |
| 39 | CreateASTConsumer(clang::CompilerInstance &compiler, |
| 40 | StringRef dummy) override { |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 41 | /// TestConsumer will be deleted by the framework calling us. |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 42 | return std::move(TestConsumer); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 43 | } |
| 44 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 45 | private: |
| 46 | std::unique_ptr<clang::ASTConsumer> TestConsumer; |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | class FindTopLevelDeclConsumer : public clang::ASTConsumer { |
| 50 | public: |
| 51 | explicit FindTopLevelDeclConsumer(bool *FoundTopLevelDecl) |
| 52 | : FoundTopLevelDecl(FoundTopLevelDecl) {} |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 53 | bool HandleTopLevelDecl(clang::DeclGroupRef DeclGroup) override { |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 54 | *FoundTopLevelDecl = true; |
| 55 | return true; |
| 56 | } |
| 57 | private: |
| 58 | bool * const FoundTopLevelDecl; |
| 59 | }; |
| 60 | } // end namespace |
| 61 | |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 62 | TEST(runToolOnCode, FindsNoTopLevelDeclOnEmptyCode) { |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 63 | bool FoundTopLevelDecl = false; |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 64 | EXPECT_TRUE( |
| 65 | runToolOnCode(new TestAction(llvm::make_unique<FindTopLevelDeclConsumer>( |
| 66 | &FoundTopLevelDecl)), |
| 67 | "")); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 68 | EXPECT_FALSE(FoundTopLevelDecl); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | namespace { |
| 72 | class FindClassDeclXConsumer : public clang::ASTConsumer { |
| 73 | public: |
| 74 | FindClassDeclXConsumer(bool *FoundClassDeclX) |
| 75 | : FoundClassDeclX(FoundClassDeclX) {} |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 76 | bool HandleTopLevelDecl(clang::DeclGroupRef GroupRef) override { |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 77 | if (CXXRecordDecl* Record = dyn_cast<clang::CXXRecordDecl>( |
| 78 | *GroupRef.begin())) { |
| 79 | if (Record->getName() == "X") { |
| 80 | *FoundClassDeclX = true; |
| 81 | } |
| 82 | } |
| 83 | return true; |
| 84 | } |
| 85 | private: |
| 86 | bool *FoundClassDeclX; |
| 87 | }; |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 88 | bool FindClassDeclX(ASTUnit *AST) { |
| 89 | for (std::vector<Decl *>::iterator i = AST->top_level_begin(), |
| 90 | e = AST->top_level_end(); |
| 91 | i != e; ++i) { |
| 92 | if (CXXRecordDecl* Record = dyn_cast<clang::CXXRecordDecl>(*i)) { |
| 93 | if (Record->getName() == "X") { |
| 94 | return true; |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | return false; |
| 99 | } |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 100 | } // end namespace |
| 101 | |
| 102 | TEST(runToolOnCode, FindsClassDecl) { |
| 103 | bool FoundClassDeclX = false; |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 104 | EXPECT_TRUE( |
| 105 | runToolOnCode(new TestAction(llvm::make_unique<FindClassDeclXConsumer>( |
| 106 | &FoundClassDeclX)), |
| 107 | "class X;")); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 108 | EXPECT_TRUE(FoundClassDeclX); |
| 109 | |
| 110 | FoundClassDeclX = false; |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 111 | EXPECT_TRUE( |
| 112 | runToolOnCode(new TestAction(llvm::make_unique<FindClassDeclXConsumer>( |
| 113 | &FoundClassDeclX)), |
| 114 | "class Y;")); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 115 | EXPECT_FALSE(FoundClassDeclX); |
| 116 | } |
| 117 | |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 118 | TEST(buildASTFromCode, FindsClassDecl) { |
David Blaikie | 103a2de | 2014-04-25 17:01:33 +0000 | [diff] [blame] | 119 | std::unique_ptr<ASTUnit> AST = buildASTFromCode("class X;"); |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 120 | ASSERT_TRUE(AST.get()); |
| 121 | EXPECT_TRUE(FindClassDeclX(AST.get())); |
| 122 | |
David Blaikie | 103a2de | 2014-04-25 17:01:33 +0000 | [diff] [blame] | 123 | AST = buildASTFromCode("class Y;"); |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 124 | ASSERT_TRUE(AST.get()); |
| 125 | EXPECT_FALSE(FindClassDeclX(AST.get())); |
| 126 | } |
| 127 | |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 128 | TEST(newFrontendActionFactory, CreatesFrontendActionFactoryFromType) { |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 129 | std::unique_ptr<FrontendActionFactory> Factory( |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 130 | newFrontendActionFactory<SyntaxOnlyAction>()); |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 131 | std::unique_ptr<FrontendAction> Action(Factory->create()); |
Craig Topper | 416fa34 | 2014-06-08 08:38:12 +0000 | [diff] [blame] | 132 | EXPECT_TRUE(Action.get() != nullptr); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | struct IndependentFrontendActionCreator { |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 136 | std::unique_ptr<ASTConsumer> newASTConsumer() { |
| 137 | return llvm::make_unique<FindTopLevelDeclConsumer>(nullptr); |
Manuel Klimek | 5da9dcb | 2012-07-05 18:13:01 +0000 | [diff] [blame] | 138 | } |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | TEST(newFrontendActionFactory, CreatesFrontendActionFactoryFromFactoryType) { |
| 142 | IndependentFrontendActionCreator Creator; |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 143 | std::unique_ptr<FrontendActionFactory> Factory( |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 144 | newFrontendActionFactory(&Creator)); |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 145 | std::unique_ptr<FrontendAction> Action(Factory->create()); |
Craig Topper | 416fa34 | 2014-06-08 08:38:12 +0000 | [diff] [blame] | 146 | EXPECT_TRUE(Action.get() != nullptr); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Alexander Kornienko | 55f2ca9 | 2012-06-01 14:50:43 +0000 | [diff] [blame] | 149 | TEST(ToolInvocation, TestMapVirtualFile) { |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 150 | IntrusiveRefCntPtr<clang::FileManager> Files( |
| 151 | new clang::FileManager(clang::FileSystemOptions())); |
Alexander Kornienko | 55f2ca9 | 2012-06-01 14:50:43 +0000 | [diff] [blame] | 152 | std::vector<std::string> Args; |
| 153 | Args.push_back("tool-executable"); |
| 154 | Args.push_back("-Idef"); |
| 155 | Args.push_back("-fsyntax-only"); |
| 156 | Args.push_back("test.cpp"); |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 157 | clang::tooling::ToolInvocation Invocation(Args, new SyntaxOnlyAction, |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 158 | Files.get()); |
Alexander Kornienko | 55f2ca9 | 2012-06-01 14:50:43 +0000 | [diff] [blame] | 159 | Invocation.mapVirtualFile("test.cpp", "#include <abc>\n"); |
| 160 | Invocation.mapVirtualFile("def/abc", "\n"); |
| 161 | EXPECT_TRUE(Invocation.run()); |
| 162 | } |
| 163 | |
Manuel Klimek | 1f76c4e | 2013-10-24 07:51:24 +0000 | [diff] [blame] | 164 | TEST(ToolInvocation, TestVirtualModulesCompilation) { |
| 165 | // FIXME: Currently, this only tests that we don't exit with an error if a |
| 166 | // mapped module.map is found on the include path. In the future, expand this |
| 167 | // test to run a full modules enabled compilation, so we make sure we can |
| 168 | // rerun modules compilations with a virtual file system. |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 169 | IntrusiveRefCntPtr<clang::FileManager> Files( |
| 170 | new clang::FileManager(clang::FileSystemOptions())); |
Manuel Klimek | 1f76c4e | 2013-10-24 07:51:24 +0000 | [diff] [blame] | 171 | std::vector<std::string> Args; |
| 172 | Args.push_back("tool-executable"); |
| 173 | Args.push_back("-Idef"); |
| 174 | Args.push_back("-fsyntax-only"); |
| 175 | Args.push_back("test.cpp"); |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 176 | clang::tooling::ToolInvocation Invocation(Args, new SyntaxOnlyAction, |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 177 | Files.get()); |
Manuel Klimek | 1f76c4e | 2013-10-24 07:51:24 +0000 | [diff] [blame] | 178 | Invocation.mapVirtualFile("test.cpp", "#include <abc>\n"); |
| 179 | Invocation.mapVirtualFile("def/abc", "\n"); |
| 180 | // Add a module.map file in the include directory of our header, so we trigger |
| 181 | // the module.map header search logic. |
| 182 | Invocation.mapVirtualFile("def/module.map", "\n"); |
| 183 | EXPECT_TRUE(Invocation.run()); |
| 184 | } |
| 185 | |
Edwin Vane | 20c6f54 | 2013-05-29 16:01:10 +0000 | [diff] [blame] | 186 | struct VerifyEndCallback : public SourceFileCallbacks { |
| 187 | VerifyEndCallback() : BeginCalled(0), EndCalled(0), Matched(false) {} |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 188 | bool handleBeginSource(CompilerInstance &CI, StringRef Filename) override { |
Edwin Vane | 20c6f54 | 2013-05-29 16:01:10 +0000 | [diff] [blame] | 189 | ++BeginCalled; |
| 190 | return true; |
| 191 | } |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 192 | void handleEndSource() override { ++EndCalled; } |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 193 | std::unique_ptr<ASTConsumer> newASTConsumer() { |
| 194 | return llvm::make_unique<FindTopLevelDeclConsumer>(&Matched); |
Manuel Klimek | 8246d87 | 2012-10-25 08:49:11 +0000 | [diff] [blame] | 195 | } |
Edwin Vane | 20c6f54 | 2013-05-29 16:01:10 +0000 | [diff] [blame] | 196 | unsigned BeginCalled; |
| 197 | unsigned EndCalled; |
Manuel Klimek | 8246d87 | 2012-10-25 08:49:11 +0000 | [diff] [blame] | 198 | bool Matched; |
| 199 | }; |
| 200 | |
Hans Wennborg | 501eadb | 2014-03-12 16:07:46 +0000 | [diff] [blame] | 201 | #if !defined(LLVM_ON_WIN32) |
Edwin Vane | 20c6f54 | 2013-05-29 16:01:10 +0000 | [diff] [blame] | 202 | TEST(newFrontendActionFactory, InjectsSourceFileCallbacks) { |
Manuel Klimek | 8246d87 | 2012-10-25 08:49:11 +0000 | [diff] [blame] | 203 | VerifyEndCallback EndCallback; |
| 204 | |
| 205 | FixedCompilationDatabase Compilations("/", std::vector<std::string>()); |
| 206 | std::vector<std::string> Sources; |
| 207 | Sources.push_back("/a.cc"); |
| 208 | Sources.push_back("/b.cc"); |
| 209 | ClangTool Tool(Compilations, Sources); |
| 210 | |
| 211 | Tool.mapVirtualFile("/a.cc", "void a() {}"); |
| 212 | Tool.mapVirtualFile("/b.cc", "void b() {}"); |
| 213 | |
Nico Weber | 52fbbb1 | 2014-04-24 03:48:09 +0000 | [diff] [blame] | 214 | std::unique_ptr<FrontendActionFactory> Action( |
| 215 | newFrontendActionFactory(&EndCallback, &EndCallback)); |
| 216 | Tool.run(Action.get()); |
Manuel Klimek | 8246d87 | 2012-10-25 08:49:11 +0000 | [diff] [blame] | 217 | |
| 218 | EXPECT_TRUE(EndCallback.Matched); |
Edwin Vane | 20c6f54 | 2013-05-29 16:01:10 +0000 | [diff] [blame] | 219 | EXPECT_EQ(2u, EndCallback.BeginCalled); |
| 220 | EXPECT_EQ(2u, EndCallback.EndCalled); |
Manuel Klimek | 8246d87 | 2012-10-25 08:49:11 +0000 | [diff] [blame] | 221 | } |
NAKAMURA Takumi | 95fd41a | 2012-10-25 09:38:41 +0000 | [diff] [blame] | 222 | #endif |
Manuel Klimek | 8246d87 | 2012-10-25 08:49:11 +0000 | [diff] [blame] | 223 | |
Richard Smith | 9219d1b | 2012-11-27 21:31:01 +0000 | [diff] [blame] | 224 | struct SkipBodyConsumer : public clang::ASTConsumer { |
| 225 | /// Skip the 'skipMe' function. |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 226 | bool shouldSkipFunctionBody(Decl *D) override { |
Richard Smith | 9219d1b | 2012-11-27 21:31:01 +0000 | [diff] [blame] | 227 | FunctionDecl *F = dyn_cast<FunctionDecl>(D); |
| 228 | return F && F->getNameAsString() == "skipMe"; |
| 229 | } |
| 230 | }; |
| 231 | |
| 232 | struct SkipBodyAction : public clang::ASTFrontendAction { |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 233 | std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &Compiler, |
| 234 | StringRef) override { |
Richard Smith | 9219d1b | 2012-11-27 21:31:01 +0000 | [diff] [blame] | 235 | Compiler.getFrontendOpts().SkipFunctionBodies = true; |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 236 | return llvm::make_unique<SkipBodyConsumer>(); |
Richard Smith | 9219d1b | 2012-11-27 21:31:01 +0000 | [diff] [blame] | 237 | } |
| 238 | }; |
| 239 | |
Hal Finkel | 1d3e3d7 | 2013-01-28 04:37:38 +0000 | [diff] [blame] | 240 | TEST(runToolOnCode, TestSkipFunctionBody) { |
Richard Smith | 9219d1b | 2012-11-27 21:31:01 +0000 | [diff] [blame] | 241 | EXPECT_TRUE(runToolOnCode(new SkipBodyAction, |
| 242 | "int skipMe() { an_error_here }")); |
| 243 | EXPECT_FALSE(runToolOnCode(new SkipBodyAction, |
| 244 | "int skipMeNot() { an_error_here }")); |
| 245 | } |
| 246 | |
Peter Collingbourne | c0423b3 | 2014-03-02 23:37:26 +0000 | [diff] [blame] | 247 | TEST(runToolOnCodeWithArgs, TestNoDepFile) { |
| 248 | llvm::SmallString<32> DepFilePath; |
| 249 | ASSERT_FALSE( |
| 250 | llvm::sys::fs::createTemporaryFile("depfile", "d", DepFilePath)); |
Peter Collingbourne | f937254 | 2014-03-03 08:13:06 +0000 | [diff] [blame] | 251 | std::vector<std::string> Args; |
| 252 | Args.push_back("-MMD"); |
| 253 | Args.push_back("-MT"); |
| 254 | Args.push_back(DepFilePath.str()); |
| 255 | Args.push_back("-MF"); |
| 256 | Args.push_back(DepFilePath.str()); |
Peter Collingbourne | 2bbb029 | 2014-03-03 07:49:35 +0000 | [diff] [blame] | 257 | EXPECT_TRUE(runToolOnCodeWithArgs(new SkipBodyAction, "", Args)); |
Peter Collingbourne | c0423b3 | 2014-03-02 23:37:26 +0000 | [diff] [blame] | 258 | EXPECT_FALSE(llvm::sys::fs::exists(DepFilePath.str())); |
| 259 | EXPECT_FALSE(llvm::sys::fs::remove(DepFilePath.str())); |
| 260 | } |
| 261 | |
Manuel Klimek | d91ac93 | 2013-06-04 14:44:44 +0000 | [diff] [blame] | 262 | TEST(ClangToolTest, ArgumentAdjusters) { |
| 263 | FixedCompilationDatabase Compilations("/", std::vector<std::string>()); |
| 264 | |
| 265 | ClangTool Tool(Compilations, std::vector<std::string>(1, "/a.cc")); |
| 266 | Tool.mapVirtualFile("/a.cc", "void a() {}"); |
| 267 | |
Nico Weber | 52fbbb1 | 2014-04-24 03:48:09 +0000 | [diff] [blame] | 268 | std::unique_ptr<FrontendActionFactory> Action( |
| 269 | newFrontendActionFactory<SyntaxOnlyAction>()); |
| 270 | |
Manuel Klimek | d91ac93 | 2013-06-04 14:44:44 +0000 | [diff] [blame] | 271 | bool Found = false; |
| 272 | bool Ran = false; |
Alexander Kornienko | 74e1c46 | 2014-12-03 17:53:02 +0000 | [diff] [blame] | 273 | ArgumentsAdjuster CheckSyntaxOnlyAdjuster = |
| 274 | [&Found, &Ran](const CommandLineArguments &Args) { |
| 275 | Ran = true; |
| 276 | if (std::find(Args.begin(), Args.end(), "-fsyntax-only") != Args.end()) |
| 277 | Found = true; |
| 278 | return Args; |
| 279 | }; |
| 280 | Tool.appendArgumentsAdjuster(CheckSyntaxOnlyAdjuster); |
Nico Weber | 52fbbb1 | 2014-04-24 03:48:09 +0000 | [diff] [blame] | 281 | Tool.run(Action.get()); |
Manuel Klimek | d91ac93 | 2013-06-04 14:44:44 +0000 | [diff] [blame] | 282 | EXPECT_TRUE(Ran); |
| 283 | EXPECT_TRUE(Found); |
| 284 | |
| 285 | Ran = Found = false; |
| 286 | Tool.clearArgumentsAdjusters(); |
Alexander Kornienko | 74e1c46 | 2014-12-03 17:53:02 +0000 | [diff] [blame] | 287 | Tool.appendArgumentsAdjuster(CheckSyntaxOnlyAdjuster); |
| 288 | Tool.appendArgumentsAdjuster(getClangSyntaxOnlyAdjuster()); |
Nico Weber | 52fbbb1 | 2014-04-24 03:48:09 +0000 | [diff] [blame] | 289 | Tool.run(Action.get()); |
Manuel Klimek | d91ac93 | 2013-06-04 14:44:44 +0000 | [diff] [blame] | 290 | EXPECT_TRUE(Ran); |
| 291 | EXPECT_FALSE(Found); |
| 292 | } |
| 293 | |
Hans Wennborg | 501eadb | 2014-03-12 16:07:46 +0000 | [diff] [blame] | 294 | #ifndef LLVM_ON_WIN32 |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 295 | TEST(ClangToolTest, BuildASTs) { |
| 296 | FixedCompilationDatabase Compilations("/", std::vector<std::string>()); |
| 297 | |
| 298 | std::vector<std::string> Sources; |
| 299 | Sources.push_back("/a.cc"); |
| 300 | Sources.push_back("/b.cc"); |
| 301 | ClangTool Tool(Compilations, Sources); |
| 302 | |
| 303 | Tool.mapVirtualFile("/a.cc", "void a() {}"); |
| 304 | Tool.mapVirtualFile("/b.cc", "void b() {}"); |
| 305 | |
David Blaikie | 39808ff | 2014-04-25 14:49:37 +0000 | [diff] [blame] | 306 | std::vector<std::unique_ptr<ASTUnit>> ASTs; |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 307 | EXPECT_EQ(0, Tool.buildASTs(ASTs)); |
| 308 | EXPECT_EQ(2u, ASTs.size()); |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Manuel Klimek | 6408301 | 2013-11-07 23:18:05 +0000 | [diff] [blame] | 311 | struct TestDiagnosticConsumer : public DiagnosticConsumer { |
| 312 | TestDiagnosticConsumer() : NumDiagnosticsSeen(0) {} |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 313 | void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, |
| 314 | const Diagnostic &Info) override { |
Manuel Klimek | 6408301 | 2013-11-07 23:18:05 +0000 | [diff] [blame] | 315 | ++NumDiagnosticsSeen; |
| 316 | } |
| 317 | unsigned NumDiagnosticsSeen; |
| 318 | }; |
| 319 | |
| 320 | TEST(ClangToolTest, InjectDiagnosticConsumer) { |
| 321 | FixedCompilationDatabase Compilations("/", std::vector<std::string>()); |
| 322 | ClangTool Tool(Compilations, std::vector<std::string>(1, "/a.cc")); |
| 323 | Tool.mapVirtualFile("/a.cc", "int x = undeclared;"); |
| 324 | TestDiagnosticConsumer Consumer; |
| 325 | Tool.setDiagnosticConsumer(&Consumer); |
Nico Weber | 52fbbb1 | 2014-04-24 03:48:09 +0000 | [diff] [blame] | 326 | std::unique_ptr<FrontendActionFactory> Action( |
| 327 | newFrontendActionFactory<SyntaxOnlyAction>()); |
| 328 | Tool.run(Action.get()); |
Manuel Klimek | 6408301 | 2013-11-07 23:18:05 +0000 | [diff] [blame] | 329 | EXPECT_EQ(1u, Consumer.NumDiagnosticsSeen); |
| 330 | } |
| 331 | |
Manuel Klimek | 31cd3fc | 2013-11-12 17:53:18 +0000 | [diff] [blame] | 332 | TEST(ClangToolTest, InjectDiagnosticConsumerInBuildASTs) { |
| 333 | FixedCompilationDatabase Compilations("/", std::vector<std::string>()); |
| 334 | ClangTool Tool(Compilations, std::vector<std::string>(1, "/a.cc")); |
| 335 | Tool.mapVirtualFile("/a.cc", "int x = undeclared;"); |
| 336 | TestDiagnosticConsumer Consumer; |
| 337 | Tool.setDiagnosticConsumer(&Consumer); |
David Blaikie | 39808ff | 2014-04-25 14:49:37 +0000 | [diff] [blame] | 338 | std::vector<std::unique_ptr<ASTUnit>> ASTs; |
Manuel Klimek | 31cd3fc | 2013-11-12 17:53:18 +0000 | [diff] [blame] | 339 | Tool.buildASTs(ASTs); |
| 340 | EXPECT_EQ(1u, ASTs.size()); |
| 341 | EXPECT_EQ(1u, Consumer.NumDiagnosticsSeen); |
| 342 | } |
NAKAMURA Takumi | d3b07c6 | 2013-11-13 00:18:50 +0000 | [diff] [blame] | 343 | #endif |
Manuel Klimek | 31cd3fc | 2013-11-12 17:53:18 +0000 | [diff] [blame] | 344 | |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 345 | } // end namespace tooling |
| 346 | } // end namespace clang |