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