Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 1 | //===- Tooling.cpp - Running clang standalone tools -----------------------===// |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 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 | // This file implements functions to run clang tools standalone instead |
| 11 | // of running them as a plugin. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Tooling/Tooling.h" |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 16 | #include "clang/Basic/Diagnostic.h" |
| 17 | #include "clang/Basic/DiagnosticIDs.h" |
| 18 | #include "clang/Basic/DiagnosticOptions.h" |
| 19 | #include "clang/Basic/FileManager.h" |
| 20 | #include "clang/Basic/FileSystemOptions.h" |
| 21 | #include "clang/Basic/LLVM.h" |
| 22 | #include "clang/Basic/VirtualFileSystem.h" |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 23 | #include "clang/Driver/Compilation.h" |
| 24 | #include "clang/Driver/Driver.h" |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 25 | #include "clang/Driver/Job.h" |
Olivier Goffart | b37a5e3 | 2016-08-30 17:42:29 +0000 | [diff] [blame] | 26 | #include "clang/Driver/Options.h" |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 27 | #include "clang/Driver/Tool.h" |
Manuel Klimek | 9b30e2b | 2015-10-06 10:45:03 +0000 | [diff] [blame] | 28 | #include "clang/Driver/ToolChain.h" |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 29 | #include "clang/Frontend/ASTUnit.h" |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 30 | #include "clang/Frontend/CompilerInstance.h" |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 31 | #include "clang/Frontend/CompilerInvocation.h" |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 32 | #include "clang/Frontend/FrontendDiagnostic.h" |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 33 | #include "clang/Frontend/FrontendOptions.h" |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 34 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 35 | #include "clang/Lex/HeaderSearchOptions.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 36 | #include "clang/Lex/PreprocessorOptions.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 37 | #include "clang/Tooling/ArgumentsAdjusters.h" |
| 38 | #include "clang/Tooling/CompilationDatabase.h" |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 39 | #include "llvm/ADT/ArrayRef.h" |
| 40 | #include "llvm/ADT/IntrusiveRefCntPtr.h" |
| 41 | #include "llvm/ADT/SmallString.h" |
| 42 | #include "llvm/ADT/StringRef.h" |
| 43 | #include "llvm/ADT/Twine.h" |
Alp Toker | 1d257e1 | 2014-06-04 03:28:55 +0000 | [diff] [blame] | 44 | #include "llvm/Config/llvm-config.h" |
Olivier Goffart | b37a5e3 | 2016-08-30 17:42:29 +0000 | [diff] [blame] | 45 | #include "llvm/Option/ArgList.h" |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 46 | #include "llvm/Option/OptTable.h" |
Reid Kleckner | 898229a | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 47 | #include "llvm/Option/Option.h" |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 48 | #include "llvm/Support/Casting.h" |
Edwin Vane | 34794c5 | 2013-03-15 20:14:01 +0000 | [diff] [blame] | 49 | #include "llvm/Support/Debug.h" |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 50 | #include "llvm/Support/ErrorHandling.h" |
NAKAMURA Takumi | f0c8779 | 2012-04-04 13:59:36 +0000 | [diff] [blame] | 51 | #include "llvm/Support/FileSystem.h" |
NAKAMURA Takumi | 3a64a4b | 2012-04-04 13:59:41 +0000 | [diff] [blame] | 52 | #include "llvm/Support/Host.h" |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 53 | #include "llvm/Support/MemoryBuffer.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 54 | #include "llvm/Support/Path.h" |
NAKAMURA Takumi | 3a64a4b | 2012-04-04 13:59:41 +0000 | [diff] [blame] | 55 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 56 | #include <cassert> |
| 57 | #include <cstring> |
| 58 | #include <memory> |
| 59 | #include <string> |
| 60 | #include <system_error> |
Benjamin Kramer | cfeacf5 | 2016-05-27 14:27:13 +0000 | [diff] [blame] | 61 | #include <utility> |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 62 | #include <vector> |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 63 | |
Chandler Carruth | 57f5fbe | 2014-04-21 22:55:36 +0000 | [diff] [blame] | 64 | #define DEBUG_TYPE "clang-tooling" |
| 65 | |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 66 | using namespace clang; |
| 67 | using namespace tooling; |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 68 | |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 69 | ToolAction::~ToolAction() = default; |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 70 | |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 71 | FrontendActionFactory::~FrontendActionFactory() = default; |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 72 | |
| 73 | // FIXME: This file contains structural duplication with other parts of the |
| 74 | // code that sets up a compiler to run tools on it, and we should refactor |
| 75 | // it to be based on the same framework. |
| 76 | |
| 77 | /// \brief Builds a clang driver initialized for running clang tools. |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 78 | static driver::Driver *newDriver( |
| 79 | DiagnosticsEngine *Diagnostics, const char *BinaryName, |
Benjamin Kramer | 407eb79 | 2015-10-09 13:03:25 +0000 | [diff] [blame] | 80 | IntrusiveRefCntPtr<vfs::FileSystem> VFS) { |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 81 | driver::Driver *CompilerDriver = |
| 82 | new driver::Driver(BinaryName, llvm::sys::getDefaultTargetTriple(), |
| 83 | *Diagnostics, std::move(VFS)); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 84 | CompilerDriver->setTitle("clang_based_tool"); |
| 85 | return CompilerDriver; |
| 86 | } |
| 87 | |
| 88 | /// \brief Retrieves the clang CC1 specific flags out of the compilation's jobs. |
| 89 | /// |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 90 | /// Returns nullptr on error. |
Reid Kleckner | 898229a | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 91 | static const llvm::opt::ArgStringList *getCC1Arguments( |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 92 | DiagnosticsEngine *Diagnostics, driver::Compilation *Compilation) { |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 93 | // We expect to get back exactly one Command job, if we didn't something |
| 94 | // failed. Extract that job from the Compilation. |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 95 | const driver::JobList &Jobs = Compilation->getJobs(); |
| 96 | if (Jobs.size() != 1 || !isa<driver::Command>(*Jobs.begin())) { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 97 | SmallString<256> error_msg; |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 98 | llvm::raw_svector_ostream error_stream(error_msg); |
Hans Wennborg | b212b34 | 2013-09-12 18:23:34 +0000 | [diff] [blame] | 99 | Jobs.Print(error_stream, "; ", true); |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 100 | Diagnostics->Report(diag::err_fe_expected_compiler_job) |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 101 | << error_stream.str(); |
Craig Topper | ccbc35e | 2014-05-20 04:51:16 +0000 | [diff] [blame] | 102 | return nullptr; |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | // The one job we find should be to invoke clang again. |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 106 | const auto &Cmd = cast<driver::Command>(*Jobs.begin()); |
David Blaikie | c11bf80 | 2014-09-04 16:04:28 +0000 | [diff] [blame] | 107 | if (StringRef(Cmd.getCreator().getName()) != "clang") { |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 108 | Diagnostics->Report(diag::err_fe_expected_clang_command); |
Craig Topper | ccbc35e | 2014-05-20 04:51:16 +0000 | [diff] [blame] | 109 | return nullptr; |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 110 | } |
| 111 | |
David Blaikie | c11bf80 | 2014-09-04 16:04:28 +0000 | [diff] [blame] | 112 | return &Cmd.getArguments(); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 115 | namespace clang { |
| 116 | namespace tooling { |
| 117 | |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 118 | /// \brief Returns a clang build invocation initialized from the CC1 flags. |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 119 | CompilerInvocation *newInvocation( |
| 120 | DiagnosticsEngine *Diagnostics, const llvm::opt::ArgStringList &CC1Args) { |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 121 | assert(!CC1Args.empty() && "Must at least contain the program name!"); |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 122 | CompilerInvocation *Invocation = new CompilerInvocation; |
| 123 | CompilerInvocation::CreateFromArgs( |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 124 | *Invocation, CC1Args.data() + 1, CC1Args.data() + CC1Args.size(), |
| 125 | *Diagnostics); |
| 126 | Invocation->getFrontendOpts().DisableFree = false; |
Nick Lewycky | 39dc9f8 | 2013-06-25 17:01:21 +0000 | [diff] [blame] | 127 | Invocation->getCodeGenOpts().DisableFree = false; |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 128 | return Invocation; |
| 129 | } |
| 130 | |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 131 | bool runToolOnCode(FrontendAction *ToolAction, const Twine &Code, |
Roman Lebedev | 497fd98 | 2018-02-27 15:54:55 +0000 | [diff] [blame] | 132 | const Twine &FileName, |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 133 | std::shared_ptr<PCHContainerOperations> PCHContainerOps) { |
Roman Lebedev | 497fd98 | 2018-02-27 15:54:55 +0000 | [diff] [blame] | 134 | return runToolOnCodeWithArgs(ToolAction, Code, std::vector<std::string>(), |
| 135 | FileName, "clang-tool", |
| 136 | std::move(PCHContainerOps)); |
Nico Weber | 077a53e | 2012-08-30 02:02:19 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 139 | } // namespace tooling |
| 140 | } // namespace clang |
| 141 | |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 142 | static std::vector<std::string> |
Benjamin Kramer | 987a1d2 | 2016-01-29 11:29:02 +0000 | [diff] [blame] | 143 | getSyntaxOnlyToolArgs(const Twine &ToolName, |
| 144 | const std::vector<std::string> &ExtraArgs, |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 145 | StringRef FileName) { |
| 146 | std::vector<std::string> Args; |
Benjamin Kramer | 987a1d2 | 2016-01-29 11:29:02 +0000 | [diff] [blame] | 147 | Args.push_back(ToolName.str()); |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 148 | Args.push_back("-fsyntax-only"); |
| 149 | Args.insert(Args.end(), ExtraArgs.begin(), ExtraArgs.end()); |
| 150 | Args.push_back(FileName.str()); |
| 151 | return Args; |
| 152 | } |
| 153 | |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 154 | namespace clang { |
| 155 | namespace tooling { |
| 156 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 157 | bool runToolOnCodeWithArgs( |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 158 | FrontendAction *ToolAction, const Twine &Code, |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 159 | const std::vector<std::string> &Args, const Twine &FileName, |
Benjamin Kramer | 987a1d2 | 2016-01-29 11:29:02 +0000 | [diff] [blame] | 160 | const Twine &ToolName, |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 161 | std::shared_ptr<PCHContainerOperations> PCHContainerOps, |
| 162 | const FileContentMappings &VirtualMappedFiles) { |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 163 | SmallString<16> FileNameStorage; |
| 164 | StringRef FileNameRef = FileName.toNullTerminatedStringRef(FileNameStorage); |
Benjamin Kramer | c4cb3b1 | 2015-10-09 09:54:37 +0000 | [diff] [blame] | 165 | llvm::IntrusiveRefCntPtr<vfs::OverlayFileSystem> OverlayFileSystem( |
| 166 | new vfs::OverlayFileSystem(vfs::getRealFileSystem())); |
| 167 | llvm::IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem( |
| 168 | new vfs::InMemoryFileSystem); |
| 169 | OverlayFileSystem->pushOverlay(InMemoryFileSystem); |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 170 | llvm::IntrusiveRefCntPtr<FileManager> Files( |
Benjamin Kramer | c4cb3b1 | 2015-10-09 09:54:37 +0000 | [diff] [blame] | 171 | new FileManager(FileSystemOptions(), OverlayFileSystem)); |
Sterling Augustine | 6b030ab | 2017-07-06 22:47:19 +0000 | [diff] [blame] | 172 | ArgumentsAdjuster Adjuster = getClangStripDependencyFileAdjuster(); |
| 173 | ToolInvocation Invocation( |
| 174 | getSyntaxOnlyToolArgs(ToolName, Adjuster(Args, FileNameRef), FileNameRef), |
Roman Lebedev | 497fd98 | 2018-02-27 15:54:55 +0000 | [diff] [blame] | 175 | ToolAction, Files.get(), |
| 176 | std::move(PCHContainerOps)); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 177 | |
| 178 | SmallString<1024> CodeStorage; |
Benjamin Kramer | c4cb3b1 | 2015-10-09 09:54:37 +0000 | [diff] [blame] | 179 | InMemoryFileSystem->addFile(FileNameRef, 0, |
| 180 | llvm::MemoryBuffer::getMemBuffer( |
| 181 | Code.toNullTerminatedStringRef(CodeStorage))); |
Manuel Klimek | d3aa1f4 | 2014-11-25 17:01:06 +0000 | [diff] [blame] | 182 | |
| 183 | for (auto &FilenameWithContent : VirtualMappedFiles) { |
Benjamin Kramer | c4cb3b1 | 2015-10-09 09:54:37 +0000 | [diff] [blame] | 184 | InMemoryFileSystem->addFile( |
| 185 | FilenameWithContent.first, 0, |
| 186 | llvm::MemoryBuffer::getMemBuffer(FilenameWithContent.second)); |
Manuel Klimek | d3aa1f4 | 2014-11-25 17:01:06 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 189 | return Invocation.run(); |
| 190 | } |
| 191 | |
Manuel Klimek | 65fd0e1 | 2012-07-10 13:10:51 +0000 | [diff] [blame] | 192 | std::string getAbsolutePath(StringRef File) { |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 193 | StringRef RelativePath(File); |
NAKAMURA Takumi | 9b2d17c | 2012-05-23 22:24:20 +0000 | [diff] [blame] | 194 | // FIXME: Should '.\\' be accepted on Win32? |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 195 | if (RelativePath.startswith("./")) { |
| 196 | RelativePath = RelativePath.substr(strlen("./")); |
| 197 | } |
Rafael Espindola | c7367ff | 2013-08-10 01:40:10 +0000 | [diff] [blame] | 198 | |
| 199 | SmallString<1024> AbsolutePath = RelativePath; |
Rafael Espindola | c080917 | 2014-06-12 14:02:15 +0000 | [diff] [blame] | 200 | std::error_code EC = llvm::sys::fs::make_absolute(AbsolutePath); |
Rafael Espindola | c7367ff | 2013-08-10 01:40:10 +0000 | [diff] [blame] | 201 | assert(!EC); |
Rafael Espindola | 0b8e4a1 | 2013-08-10 04:25:53 +0000 | [diff] [blame] | 202 | (void)EC; |
Benjamin Kramer | 2d4d8cb | 2013-09-11 11:23:15 +0000 | [diff] [blame] | 203 | llvm::sys::path::native(AbsolutePath); |
| 204 | return AbsolutePath.str(); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Manuel Klimek | 9b30e2b | 2015-10-06 10:45:03 +0000 | [diff] [blame] | 207 | void addTargetAndModeForProgramName(std::vector<std::string> &CommandLine, |
| 208 | StringRef InvokedAs) { |
| 209 | if (!CommandLine.empty() && !InvokedAs.empty()) { |
| 210 | bool AlreadyHasTarget = false; |
| 211 | bool AlreadyHasMode = false; |
| 212 | // Skip CommandLine[0]. |
| 213 | for (auto Token = ++CommandLine.begin(); Token != CommandLine.end(); |
| 214 | ++Token) { |
| 215 | StringRef TokenRef(*Token); |
| 216 | AlreadyHasTarget |= |
| 217 | (TokenRef == "-target" || TokenRef.startswith("-target=")); |
| 218 | AlreadyHasMode |= (TokenRef == "--driver-mode" || |
| 219 | TokenRef.startswith("--driver-mode=")); |
| 220 | } |
| 221 | auto TargetMode = |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 222 | driver::ToolChain::getTargetAndModeFromProgramName(InvokedAs); |
Serge Pavlov | 4e76984 | 2017-08-29 05:22:26 +0000 | [diff] [blame] | 223 | if (!AlreadyHasMode && TargetMode.DriverMode) { |
| 224 | CommandLine.insert(++CommandLine.begin(), TargetMode.DriverMode); |
Manuel Klimek | 9b30e2b | 2015-10-06 10:45:03 +0000 | [diff] [blame] | 225 | } |
Serge Pavlov | 4e76984 | 2017-08-29 05:22:26 +0000 | [diff] [blame] | 226 | if (!AlreadyHasTarget && TargetMode.TargetIsValid) { |
| 227 | CommandLine.insert(++CommandLine.begin(), {"-target", |
| 228 | TargetMode.TargetPrefix}); |
Manuel Klimek | 9b30e2b | 2015-10-06 10:45:03 +0000 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 233 | } // namespace tooling |
| 234 | } // namespace clang |
| 235 | |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 236 | namespace { |
| 237 | |
| 238 | class SingleFrontendActionFactory : public FrontendActionFactory { |
Roman Lebedev | 497fd98 | 2018-02-27 15:54:55 +0000 | [diff] [blame] | 239 | FrontendAction *Action; |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 240 | |
| 241 | public: |
Roman Lebedev | 497fd98 | 2018-02-27 15:54:55 +0000 | [diff] [blame] | 242 | SingleFrontendActionFactory(FrontendAction *Action) : Action(Action) {} |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 243 | |
Roman Lebedev | 497fd98 | 2018-02-27 15:54:55 +0000 | [diff] [blame] | 244 | FrontendAction *create() override { return Action; } |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 245 | }; |
| 246 | |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 247 | } // namespace |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 248 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 249 | ToolInvocation::ToolInvocation( |
| 250 | std::vector<std::string> CommandLine, ToolAction *Action, |
| 251 | FileManager *Files, std::shared_ptr<PCHContainerOperations> PCHContainerOps) |
| 252 | : CommandLine(std::move(CommandLine)), Action(Action), OwnsAction(false), |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 253 | Files(Files), PCHContainerOps(std::move(PCHContainerOps)) {} |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 254 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 255 | ToolInvocation::ToolInvocation( |
Roman Lebedev | 497fd98 | 2018-02-27 15:54:55 +0000 | [diff] [blame] | 256 | std::vector<std::string> CommandLine, FrontendAction *FAction, |
| 257 | FileManager *Files, std::shared_ptr<PCHContainerOperations> PCHContainerOps) |
Benjamin Kramer | efb1eb9 | 2014-03-20 12:48:36 +0000 | [diff] [blame] | 258 | : CommandLine(std::move(CommandLine)), |
Roman Lebedev | 497fd98 | 2018-02-27 15:54:55 +0000 | [diff] [blame] | 259 | Action(new SingleFrontendActionFactory(FAction)), OwnsAction(true), |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 260 | Files(Files), PCHContainerOps(std::move(PCHContainerOps)) {} |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 261 | |
| 262 | ToolInvocation::~ToolInvocation() { |
| 263 | if (OwnsAction) |
| 264 | delete Action; |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | void ToolInvocation::mapVirtualFile(StringRef FilePath, StringRef Content) { |
NAKAMURA Takumi | 4de3165 | 2012-06-02 15:34:21 +0000 | [diff] [blame] | 268 | SmallString<1024> PathStorage; |
| 269 | llvm::sys::path::native(FilePath, PathStorage); |
| 270 | MappedFileContents[PathStorage] = Content; |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | bool ToolInvocation::run() { |
| 274 | std::vector<const char*> Argv; |
Benjamin Kramer | efb1eb9 | 2014-03-20 12:48:36 +0000 | [diff] [blame] | 275 | for (const std::string &Str : CommandLine) |
| 276 | Argv.push_back(Str.c_str()); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 277 | const char *const BinaryName = Argv[0]; |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 278 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); |
Olivier Goffart | b37a5e3 | 2016-08-30 17:42:29 +0000 | [diff] [blame] | 279 | unsigned MissingArgIndex, MissingArgCount; |
David Blaikie | 0aaa762 | 2017-01-13 17:34:15 +0000 | [diff] [blame] | 280 | std::unique_ptr<llvm::opt::OptTable> Opts = driver::createDriverOptTable(); |
Richard Trieu | 070937a | 2016-08-30 21:12:48 +0000 | [diff] [blame] | 281 | llvm::opt::InputArgList ParsedArgs = Opts->ParseArgs( |
| 282 | ArrayRef<const char *>(Argv).slice(1), MissingArgIndex, MissingArgCount); |
Olivier Goffart | b37a5e3 | 2016-08-30 17:42:29 +0000 | [diff] [blame] | 283 | ParseDiagnosticArgs(*DiagOpts, ParsedArgs); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 284 | TextDiagnosticPrinter DiagnosticPrinter( |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 285 | llvm::errs(), &*DiagOpts); |
| 286 | DiagnosticsEngine Diagnostics( |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 287 | IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts, |
Manuel Klimek | 6408301 | 2013-11-07 23:18:05 +0000 | [diff] [blame] | 288 | DiagConsumer ? DiagConsumer : &DiagnosticPrinter, false); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 289 | |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 290 | const std::unique_ptr<driver::Driver> Driver( |
Benjamin Kramer | 407eb79 | 2015-10-09 13:03:25 +0000 | [diff] [blame] | 291 | newDriver(&Diagnostics, BinaryName, Files->getVirtualFileSystem())); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 292 | // Since the input might only be virtual, don't check whether it exists. |
| 293 | Driver->setCheckInputsExist(false); |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 294 | const std::unique_ptr<driver::Compilation> Compilation( |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 295 | Driver->BuildCompilation(llvm::makeArrayRef(Argv))); |
Serge Pavlov | c46064c | 2017-05-24 11:57:37 +0000 | [diff] [blame] | 296 | if (!Compilation) |
| 297 | return false; |
Reid Kleckner | 898229a | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 298 | const llvm::opt::ArgStringList *const CC1Args = getCC1Arguments( |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 299 | &Diagnostics, Compilation.get()); |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 300 | if (!CC1Args) |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 301 | return false; |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 302 | std::unique_ptr<CompilerInvocation> Invocation( |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 303 | newInvocation(&Diagnostics, *CC1Args)); |
Benjamin Kramer | c4cb3b1 | 2015-10-09 09:54:37 +0000 | [diff] [blame] | 304 | // FIXME: remove this when all users have migrated! |
Benjamin Kramer | efb1eb9 | 2014-03-20 12:48:36 +0000 | [diff] [blame] | 305 | for (const auto &It : MappedFileContents) { |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 306 | // Inject the code as the given file name into the preprocessor options. |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 307 | std::unique_ptr<llvm::MemoryBuffer> Input = |
| 308 | llvm::MemoryBuffer::getMemBuffer(It.getValue()); |
| 309 | Invocation->getPreprocessorOpts().addRemappedFile(It.getKey(), |
| 310 | Input.release()); |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 311 | } |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 312 | return runInvocation(BinaryName, Compilation.get(), std::move(Invocation), |
Benjamin Kramer | d6da1a0 | 2016-06-12 20:05:23 +0000 | [diff] [blame] | 313 | std::move(PCHContainerOps)); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 316 | bool ToolInvocation::runInvocation( |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 317 | const char *BinaryName, driver::Compilation *Compilation, |
| 318 | std::shared_ptr<CompilerInvocation> Invocation, |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 319 | std::shared_ptr<PCHContainerOperations> PCHContainerOps) { |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 320 | // Show the invocation, with -v. |
| 321 | if (Invocation->getHeaderSearchOpts().Verbose) { |
| 322 | llvm::errs() << "clang Invocation:\n"; |
Hans Wennborg | b212b34 | 2013-09-12 18:23:34 +0000 | [diff] [blame] | 323 | Compilation->getJobs().Print(llvm::errs(), "\n", true); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 324 | llvm::errs() << "\n"; |
| 325 | } |
| 326 | |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 327 | return Action->runInvocation(std::move(Invocation), Files, |
| 328 | std::move(PCHContainerOps), DiagConsumer); |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 331 | bool FrontendActionFactory::runInvocation( |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 332 | std::shared_ptr<CompilerInvocation> Invocation, FileManager *Files, |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 333 | std::shared_ptr<PCHContainerOperations> PCHContainerOps, |
| 334 | DiagnosticConsumer *DiagConsumer) { |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 335 | // Create a compiler instance to handle the actual work. |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 336 | CompilerInstance Compiler(std::move(PCHContainerOps)); |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 337 | Compiler.setInvocation(std::move(Invocation)); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 338 | Compiler.setFileManager(Files); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 339 | |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 340 | // The FrontendAction can have lifetime requirements for Compiler or its |
| 341 | // members, and we need to ensure it's deleted earlier than Compiler. So we |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 342 | // pass it to an std::unique_ptr declared after the Compiler variable. |
| 343 | std::unique_ptr<FrontendAction> ScopedToolAction(create()); |
Alexander Kornienko | 21d6ec9 | 2012-05-31 17:58:43 +0000 | [diff] [blame] | 344 | |
Alp Toker | 77273fc | 2014-05-16 13:45:29 +0000 | [diff] [blame] | 345 | // Create the compiler's actual diagnostics engine. |
Manuel Klimek | 6408301 | 2013-11-07 23:18:05 +0000 | [diff] [blame] | 346 | Compiler.createDiagnostics(DiagConsumer, /*ShouldOwnClient=*/false); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 347 | if (!Compiler.hasDiagnostics()) |
| 348 | return false; |
| 349 | |
| 350 | Compiler.createSourceManager(*Files); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 351 | |
Alexander Kornienko | 21d6ec9 | 2012-05-31 17:58:43 +0000 | [diff] [blame] | 352 | const bool Success = Compiler.ExecuteAction(*ScopedToolAction); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 353 | |
Manuel Klimek | 3aad855 | 2012-07-31 13:56:54 +0000 | [diff] [blame] | 354 | Files->clearStatCaches(); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 355 | return Success; |
| 356 | } |
| 357 | |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 358 | ClangTool::ClangTool(const CompilationDatabase &Compilations, |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 359 | ArrayRef<std::string> SourcePaths, |
Ilya Biryukov | 5da21ed | 2018-01-23 12:30:02 +0000 | [diff] [blame] | 360 | std::shared_ptr<PCHContainerOperations> PCHContainerOps, |
| 361 | IntrusiveRefCntPtr<vfs::FileSystem> BaseFS) |
Alexander Kornienko | 9a45fac | 2014-08-27 21:36:39 +0000 | [diff] [blame] | 362 | : Compilations(Compilations), SourcePaths(SourcePaths), |
Benjamin Kramer | cfeacf5 | 2016-05-27 14:27:13 +0000 | [diff] [blame] | 363 | PCHContainerOps(std::move(PCHContainerOps)), |
Alexander Kornienko | 156adaf | 2018-03-27 14:02:06 +0000 | [diff] [blame] | 364 | OverlayFileSystem(new vfs::OverlayFileSystem(std::move(BaseFS))), |
Benjamin Kramer | c4cb3b1 | 2015-10-09 09:54:37 +0000 | [diff] [blame] | 365 | InMemoryFileSystem(new vfs::InMemoryFileSystem), |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 366 | Files(new FileManager(FileSystemOptions(), OverlayFileSystem)) { |
Benjamin Kramer | c4cb3b1 | 2015-10-09 09:54:37 +0000 | [diff] [blame] | 367 | OverlayFileSystem->pushOverlay(InMemoryFileSystem); |
Alexander Kornienko | 74e1c46 | 2014-12-03 17:53:02 +0000 | [diff] [blame] | 368 | appendArgumentsAdjuster(getClangStripOutputAdjuster()); |
| 369 | appendArgumentsAdjuster(getClangSyntaxOnlyAdjuster()); |
Sterling Augustine | 78f4612 | 2017-07-14 18:33:30 +0000 | [diff] [blame] | 370 | appendArgumentsAdjuster(getClangStripDependencyFileAdjuster()); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 373 | ClangTool::~ClangTool() = default; |
Manuel Klimek | 6408301 | 2013-11-07 23:18:05 +0000 | [diff] [blame] | 374 | |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 375 | void ClangTool::mapVirtualFile(StringRef FilePath, StringRef Content) { |
| 376 | MappedFileContents.push_back(std::make_pair(FilePath, Content)); |
| 377 | } |
| 378 | |
Alexander Kornienko | 74e1c46 | 2014-12-03 17:53:02 +0000 | [diff] [blame] | 379 | void ClangTool::appendArgumentsAdjuster(ArgumentsAdjuster Adjuster) { |
Eric Liu | 826b783 | 2017-10-26 10:38:14 +0000 | [diff] [blame] | 380 | ArgsAdjuster = combineAdjusters(std::move(ArgsAdjuster), std::move(Adjuster)); |
Manuel Klimek | d91ac93 | 2013-06-04 14:44:44 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | void ClangTool::clearArgumentsAdjusters() { |
Alexander Kornienko | 74e1c46 | 2014-12-03 17:53:02 +0000 | [diff] [blame] | 384 | ArgsAdjuster = nullptr; |
Simon Atanasyan | 32df72d | 2012-05-09 16:18:30 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Benjamin Kramer | b5737c1 | 2016-04-21 10:18:18 +0000 | [diff] [blame] | 387 | static void injectResourceDir(CommandLineArguments &Args, const char *Argv0, |
| 388 | void *MainAddr) { |
| 389 | // Allow users to override the resource dir. |
| 390 | for (StringRef Arg : Args) |
| 391 | if (Arg.startswith("-resource-dir")) |
| 392 | return; |
| 393 | |
| 394 | // If there's no override in place add our resource dir. |
| 395 | Args.push_back("-resource-dir=" + |
| 396 | CompilerInvocation::GetResourcesPath(Argv0, MainAddr)); |
| 397 | } |
| 398 | |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 399 | int ClangTool::run(ToolAction *Action) { |
Alexander Kornienko | 8388d24 | 2012-06-04 19:02:59 +0000 | [diff] [blame] | 400 | // Exists solely for the purpose of lookup of the resource path. |
| 401 | // This just needs to be some symbol in the binary. |
| 402 | static int StaticSymbol; |
Alexander Kornienko | 8388d24 | 2012-06-04 19:02:59 +0000 | [diff] [blame] | 403 | |
Alexander Kornienko | c48a535 | 2014-11-10 15:42:31 +0000 | [diff] [blame] | 404 | llvm::SmallString<128> InitialDirectory; |
| 405 | if (std::error_code EC = llvm::sys::fs::current_path(InitialDirectory)) |
| 406 | llvm::report_fatal_error("Cannot detect current path: " + |
| 407 | Twine(EC.message())); |
Benjamin Kramer | c4cb3b1 | 2015-10-09 09:54:37 +0000 | [diff] [blame] | 408 | |
| 409 | // First insert all absolute paths into the in-memory VFS. These are global |
| 410 | // for all compile commands. |
| 411 | if (SeenWorkingDirectories.insert("/").second) |
| 412 | for (const auto &MappedFile : MappedFileContents) |
| 413 | if (llvm::sys::path::is_absolute(MappedFile.first)) |
| 414 | InMemoryFileSystem->addFile( |
| 415 | MappedFile.first, 0, |
| 416 | llvm::MemoryBuffer::getMemBuffer(MappedFile.second)); |
| 417 | |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 418 | bool ProcessingFailed = false; |
Eric Liu | 3a2cf86 | 2018-02-02 18:19:22 +0000 | [diff] [blame] | 419 | bool FileSkipped = false; |
Alexander Kornienko | 9a45fac | 2014-08-27 21:36:39 +0000 | [diff] [blame] | 420 | for (const auto &SourcePath : SourcePaths) { |
| 421 | std::string File(getAbsolutePath(SourcePath)); |
| 422 | |
Alexander Kornienko | c48a535 | 2014-11-10 15:42:31 +0000 | [diff] [blame] | 423 | // Currently implementations of CompilationDatabase::getCompileCommands can |
| 424 | // change the state of the file system (e.g. prepare generated headers), so |
| 425 | // this method needs to run right before we invoke the tool, as the next |
| 426 | // file may require a different (incompatible) state of the file system. |
| 427 | // |
| 428 | // FIXME: Make the compilation database interface more explicit about the |
| 429 | // requirements to the order of invocation of its members. |
Alexander Kornienko | 9a45fac | 2014-08-27 21:36:39 +0000 | [diff] [blame] | 430 | std::vector<CompileCommand> CompileCommandsForFile = |
| 431 | Compilations.getCompileCommands(File); |
| 432 | if (CompileCommandsForFile.empty()) { |
Alexander Kornienko | 9a45fac | 2014-08-27 21:36:39 +0000 | [diff] [blame] | 433 | llvm::errs() << "Skipping " << File << ". Compile command not found.\n"; |
Eric Liu | 3a2cf86 | 2018-02-02 18:19:22 +0000 | [diff] [blame] | 434 | FileSkipped = true; |
Alexander Kornienko | 9a45fac | 2014-08-27 21:36:39 +0000 | [diff] [blame] | 435 | continue; |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 436 | } |
Alexander Kornienko | 9a45fac | 2014-08-27 21:36:39 +0000 | [diff] [blame] | 437 | for (CompileCommand &CompileCommand : CompileCommandsForFile) { |
| 438 | // FIXME: chdir is thread hostile; on the other hand, creating the same |
| 439 | // behavior as chdir is complex: chdir resolves the path once, thus |
| 440 | // guaranteeing that all subsequent relative path operations work |
| 441 | // on the same path the original chdir resulted in. This makes a |
| 442 | // difference for example on network filesystems, where symlinks might be |
| 443 | // switched during runtime of the tool. Fixing this depends on having a |
| 444 | // file system abstraction that allows openat() style interactions. |
Benjamin Kramer | c4cb3b1 | 2015-10-09 09:54:37 +0000 | [diff] [blame] | 445 | if (OverlayFileSystem->setCurrentWorkingDirectory( |
| 446 | CompileCommand.Directory)) |
Alexander Kornienko | 9a45fac | 2014-08-27 21:36:39 +0000 | [diff] [blame] | 447 | llvm::report_fatal_error("Cannot chdir into \"" + |
| 448 | Twine(CompileCommand.Directory) + "\n!"); |
Benjamin Kramer | c4cb3b1 | 2015-10-09 09:54:37 +0000 | [diff] [blame] | 449 | |
| 450 | // Now fill the in-memory VFS with the relative file mappings so it will |
| 451 | // have the correct relative paths. We never remove mappings but that |
| 452 | // should be fine. |
| 453 | if (SeenWorkingDirectories.insert(CompileCommand.Directory).second) |
| 454 | for (const auto &MappedFile : MappedFileContents) |
| 455 | if (!llvm::sys::path::is_absolute(MappedFile.first)) |
| 456 | InMemoryFileSystem->addFile( |
| 457 | MappedFile.first, 0, |
| 458 | llvm::MemoryBuffer::getMemBuffer(MappedFile.second)); |
| 459 | |
Alexander Kornienko | 9a45fac | 2014-08-27 21:36:39 +0000 | [diff] [blame] | 460 | std::vector<std::string> CommandLine = CompileCommand.CommandLine; |
Alexander Kornienko | 74e1c46 | 2014-12-03 17:53:02 +0000 | [diff] [blame] | 461 | if (ArgsAdjuster) |
Alexander Kornienko | 857b10f | 2015-11-05 02:19:53 +0000 | [diff] [blame] | 462 | CommandLine = ArgsAdjuster(CommandLine, CompileCommand.Filename); |
Alexander Kornienko | 9a45fac | 2014-08-27 21:36:39 +0000 | [diff] [blame] | 463 | assert(!CommandLine.empty()); |
Benjamin Kramer | b5737c1 | 2016-04-21 10:18:18 +0000 | [diff] [blame] | 464 | |
| 465 | // Add the resource dir based on the binary of this tool. argv[0] in the |
| 466 | // compilation database may refer to a different compiler and we want to |
| 467 | // pick up the very same standard library that compiler is using. The |
| 468 | // builtin headers in the resource dir need to match the exact clang |
| 469 | // version the tool is using. |
| 470 | // FIXME: On linux, GetMainExecutable is independent of the value of the |
| 471 | // first argument, thus allowing ClangTool and runToolOnCode to just |
| 472 | // pass in made-up names here. Make sure this works on other platforms. |
| 473 | injectResourceDir(CommandLine, "clang_tool", &StaticSymbol); |
| 474 | |
Alexander Kornienko | 9a45fac | 2014-08-27 21:36:39 +0000 | [diff] [blame] | 475 | // FIXME: We need a callback mechanism for the tool writer to output a |
| 476 | // customized message for each file. |
| 477 | DEBUG({ llvm::dbgs() << "Processing: " << File << ".\n"; }); |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 478 | ToolInvocation Invocation(std::move(CommandLine), Action, Files.get(), |
| 479 | PCHContainerOps); |
Alexander Kornienko | 9a45fac | 2014-08-27 21:36:39 +0000 | [diff] [blame] | 480 | Invocation.setDiagnosticConsumer(DiagConsumer); |
Benjamin Kramer | c4cb3b1 | 2015-10-09 09:54:37 +0000 | [diff] [blame] | 481 | |
Alexander Kornienko | 9a45fac | 2014-08-27 21:36:39 +0000 | [diff] [blame] | 482 | if (!Invocation.run()) { |
| 483 | // FIXME: Diagnostics should be used instead. |
| 484 | llvm::errs() << "Error while processing " << File << ".\n"; |
| 485 | ProcessingFailed = true; |
| 486 | } |
Alexander Kornienko | c48a535 | 2014-11-10 15:42:31 +0000 | [diff] [blame] | 487 | // Return to the initial directory to correctly resolve next file by |
| 488 | // relative path. |
Benjamin Kramer | c4cb3b1 | 2015-10-09 09:54:37 +0000 | [diff] [blame] | 489 | if (OverlayFileSystem->setCurrentWorkingDirectory(InitialDirectory.c_str())) |
Alexander Kornienko | c48a535 | 2014-11-10 15:42:31 +0000 | [diff] [blame] | 490 | llvm::report_fatal_error("Cannot chdir into \"" + |
| 491 | Twine(InitialDirectory) + "\n!"); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 492 | } |
| 493 | } |
Eric Liu | 3a2cf86 | 2018-02-02 18:19:22 +0000 | [diff] [blame] | 494 | return ProcessingFailed ? 1 : (FileSkipped ? 2 : 0); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 497 | namespace { |
| 498 | |
| 499 | class ASTBuilderAction : public ToolAction { |
David Blaikie | 39808ff | 2014-04-25 14:49:37 +0000 | [diff] [blame] | 500 | std::vector<std::unique_ptr<ASTUnit>> &ASTs; |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 501 | |
| 502 | public: |
David Blaikie | 39808ff | 2014-04-25 14:49:37 +0000 | [diff] [blame] | 503 | ASTBuilderAction(std::vector<std::unique_ptr<ASTUnit>> &ASTs) : ASTs(ASTs) {} |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 504 | |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 505 | bool runInvocation(std::shared_ptr<CompilerInvocation> Invocation, |
| 506 | FileManager *Files, |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 507 | std::shared_ptr<PCHContainerOperations> PCHContainerOps, |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 508 | DiagnosticConsumer *DiagConsumer) override { |
David Blaikie | 103a2de | 2014-04-25 17:01:33 +0000 | [diff] [blame] | 509 | std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromCompilerInvocation( |
Benjamin Kramer | d6da1a0 | 2016-06-12 20:05:23 +0000 | [diff] [blame] | 510 | Invocation, std::move(PCHContainerOps), |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 511 | CompilerInstance::createDiagnostics(&Invocation->getDiagnosticOpts(), |
| 512 | DiagConsumer, |
Benjamin Kramer | bc63290 | 2015-10-06 14:45:20 +0000 | [diff] [blame] | 513 | /*ShouldOwnClient=*/false), |
| 514 | Files); |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 515 | if (!AST) |
| 516 | return false; |
| 517 | |
David Blaikie | 39808ff | 2014-04-25 14:49:37 +0000 | [diff] [blame] | 518 | ASTs.push_back(std::move(AST)); |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 519 | return true; |
| 520 | } |
| 521 | }; |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 522 | |
| 523 | } // namespace |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 524 | |
David Blaikie | 39808ff | 2014-04-25 14:49:37 +0000 | [diff] [blame] | 525 | int ClangTool::buildASTs(std::vector<std::unique_ptr<ASTUnit>> &ASTs) { |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 526 | ASTBuilderAction Action(ASTs); |
| 527 | return run(&Action); |
| 528 | } |
| 529 | |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 530 | namespace clang { |
| 531 | namespace tooling { |
| 532 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 533 | std::unique_ptr<ASTUnit> |
| 534 | buildASTFromCode(const Twine &Code, const Twine &FileName, |
| 535 | std::shared_ptr<PCHContainerOperations> PCHContainerOps) { |
| 536 | return buildASTFromCodeWithArgs(Code, std::vector<std::string>(), FileName, |
Benjamin Kramer | d6da1a0 | 2016-06-12 20:05:23 +0000 | [diff] [blame] | 537 | "clang-tool", std::move(PCHContainerOps)); |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 540 | std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs( |
| 541 | const Twine &Code, const std::vector<std::string> &Args, |
Benjamin Kramer | 987a1d2 | 2016-01-29 11:29:02 +0000 | [diff] [blame] | 542 | const Twine &FileName, const Twine &ToolName, |
Sterling Augustine | 1cda1d7 | 2017-07-06 21:02:52 +0000 | [diff] [blame] | 543 | std::shared_ptr<PCHContainerOperations> PCHContainerOps, |
| 544 | ArgumentsAdjuster Adjuster) { |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 545 | SmallString<16> FileNameStorage; |
| 546 | StringRef FileNameRef = FileName.toNullTerminatedStringRef(FileNameStorage); |
| 547 | |
David Blaikie | 39808ff | 2014-04-25 14:49:37 +0000 | [diff] [blame] | 548 | std::vector<std::unique_ptr<ASTUnit>> ASTs; |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 549 | ASTBuilderAction Action(ASTs); |
Benjamin Kramer | c4cb3b1 | 2015-10-09 09:54:37 +0000 | [diff] [blame] | 550 | llvm::IntrusiveRefCntPtr<vfs::OverlayFileSystem> OverlayFileSystem( |
| 551 | new vfs::OverlayFileSystem(vfs::getRealFileSystem())); |
| 552 | llvm::IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem( |
| 553 | new vfs::InMemoryFileSystem); |
| 554 | OverlayFileSystem->pushOverlay(InMemoryFileSystem); |
Benjamin Kramer | fa3dcf2 | 2015-10-06 15:04:13 +0000 | [diff] [blame] | 555 | llvm::IntrusiveRefCntPtr<FileManager> Files( |
Benjamin Kramer | c4cb3b1 | 2015-10-09 09:54:37 +0000 | [diff] [blame] | 556 | new FileManager(FileSystemOptions(), OverlayFileSystem)); |
Sterling Augustine | 1cda1d7 | 2017-07-06 21:02:52 +0000 | [diff] [blame] | 557 | |
| 558 | ToolInvocation Invocation( |
| 559 | getSyntaxOnlyToolArgs(ToolName, Adjuster(Args, FileNameRef), FileNameRef), |
| 560 | &Action, Files.get(), std::move(PCHContainerOps)); |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 561 | |
| 562 | SmallString<1024> CodeStorage; |
Benjamin Kramer | c4cb3b1 | 2015-10-09 09:54:37 +0000 | [diff] [blame] | 563 | InMemoryFileSystem->addFile(FileNameRef, 0, |
| 564 | llvm::MemoryBuffer::getMemBuffer( |
| 565 | Code.toNullTerminatedStringRef(CodeStorage))); |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 566 | if (!Invocation.run()) |
Craig Topper | ccbc35e | 2014-05-20 04:51:16 +0000 | [diff] [blame] | 567 | return nullptr; |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 568 | |
| 569 | assert(ASTs.size() == 1); |
David Blaikie | 103a2de | 2014-04-25 17:01:33 +0000 | [diff] [blame] | 570 | return std::move(ASTs[0]); |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 571 | } |
| 572 | |
Eugene Zelenko | 6366efe | 2018-03-14 21:05:51 +0000 | [diff] [blame] | 573 | } // namespace tooling |
| 574 | } // namespace clang |