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