Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 1 | //===--- Tooling.cpp - Running clang standalone tools ---------------------===// |
| 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" |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTConsumer.h" |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 17 | #include "clang/Driver/Compilation.h" |
| 18 | #include "clang/Driver/Driver.h" |
| 19 | #include "clang/Driver/Tool.h" |
Manuel Klimek | 9b30e2b | 2015-10-06 10:45:03 +0000 | [diff] [blame] | 20 | #include "clang/Driver/ToolChain.h" |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 21 | #include "clang/Frontend/ASTUnit.h" |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 22 | #include "clang/Frontend/CompilerInstance.h" |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 23 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 24 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 25 | #include "clang/Tooling/ArgumentsAdjusters.h" |
| 26 | #include "clang/Tooling/CompilationDatabase.h" |
NAKAMURA Takumi | 3a64a4b | 2012-04-04 13:59:41 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/STLExtras.h" |
Alp Toker | 1d257e1 | 2014-06-04 03:28:55 +0000 | [diff] [blame] | 28 | #include "llvm/Config/llvm-config.h" |
Reid Kleckner | 898229a | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 29 | #include "llvm/Option/Option.h" |
Edwin Vane | 34794c5 | 2013-03-15 20:14:01 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Debug.h" |
NAKAMURA Takumi | f0c8779 | 2012-04-04 13:59:36 +0000 | [diff] [blame] | 31 | #include "llvm/Support/FileSystem.h" |
NAKAMURA Takumi | 3a64a4b | 2012-04-04 13:59:41 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Host.h" |
| 33 | #include "llvm/Support/raw_ostream.h" |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 34 | |
Manuel Klimek | 74cec54 | 2012-05-07 09:45:46 +0000 | [diff] [blame] | 35 | // For chdir, see the comment in ClangTool::run for more information. |
Hans Wennborg | 501eadb | 2014-03-12 16:07:46 +0000 | [diff] [blame] | 36 | #ifdef LLVM_ON_WIN32 |
Manuel Klimek | 74cec54 | 2012-05-07 09:45:46 +0000 | [diff] [blame] | 37 | # include <direct.h> |
Manuel Klimek | f33dcb0 | 2012-05-07 10:02:55 +0000 | [diff] [blame] | 38 | #else |
| 39 | # include <unistd.h> |
Manuel Klimek | 74cec54 | 2012-05-07 09:45:46 +0000 | [diff] [blame] | 40 | #endif |
| 41 | |
Chandler Carruth | 57f5fbe | 2014-04-21 22:55:36 +0000 | [diff] [blame] | 42 | #define DEBUG_TYPE "clang-tooling" |
| 43 | |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 44 | namespace clang { |
| 45 | namespace tooling { |
| 46 | |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 47 | ToolAction::~ToolAction() {} |
| 48 | |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 49 | FrontendActionFactory::~FrontendActionFactory() {} |
| 50 | |
| 51 | // FIXME: This file contains structural duplication with other parts of the |
| 52 | // code that sets up a compiler to run tools on it, and we should refactor |
| 53 | // it to be based on the same framework. |
| 54 | |
| 55 | /// \brief Builds a clang driver initialized for running clang tools. |
| 56 | static clang::driver::Driver *newDriver(clang::DiagnosticsEngine *Diagnostics, |
| 57 | const char *BinaryName) { |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 58 | clang::driver::Driver *CompilerDriver = new clang::driver::Driver( |
Alp Toker | 1761f11 | 2014-05-15 22:26:36 +0000 | [diff] [blame] | 59 | BinaryName, llvm::sys::getDefaultTargetTriple(), *Diagnostics); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 60 | CompilerDriver->setTitle("clang_based_tool"); |
| 61 | return CompilerDriver; |
| 62 | } |
| 63 | |
| 64 | /// \brief Retrieves the clang CC1 specific flags out of the compilation's jobs. |
| 65 | /// |
| 66 | /// Returns NULL on error. |
Reid Kleckner | 898229a | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 67 | static const llvm::opt::ArgStringList *getCC1Arguments( |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 68 | clang::DiagnosticsEngine *Diagnostics, |
| 69 | clang::driver::Compilation *Compilation) { |
| 70 | // We expect to get back exactly one Command job, if we didn't something |
| 71 | // failed. Extract that job from the Compilation. |
| 72 | const clang::driver::JobList &Jobs = Compilation->getJobs(); |
Justin Bogner | aab9792 | 2014-10-03 01:04:53 +0000 | [diff] [blame] | 73 | if (Jobs.size() != 1 || !isa<clang::driver::Command>(*Jobs.begin())) { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 74 | SmallString<256> error_msg; |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 75 | llvm::raw_svector_ostream error_stream(error_msg); |
Hans Wennborg | b212b34 | 2013-09-12 18:23:34 +0000 | [diff] [blame] | 76 | Jobs.Print(error_stream, "; ", true); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 77 | Diagnostics->Report(clang::diag::err_fe_expected_compiler_job) |
| 78 | << error_stream.str(); |
Craig Topper | ccbc35e | 2014-05-20 04:51:16 +0000 | [diff] [blame] | 79 | return nullptr; |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | // The one job we find should be to invoke clang again. |
David Blaikie | c11bf80 | 2014-09-04 16:04:28 +0000 | [diff] [blame] | 83 | const clang::driver::Command &Cmd = |
Justin Bogner | aab9792 | 2014-10-03 01:04:53 +0000 | [diff] [blame] | 84 | cast<clang::driver::Command>(*Jobs.begin()); |
David Blaikie | c11bf80 | 2014-09-04 16:04:28 +0000 | [diff] [blame] | 85 | if (StringRef(Cmd.getCreator().getName()) != "clang") { |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 86 | Diagnostics->Report(clang::diag::err_fe_expected_clang_command); |
Craig Topper | ccbc35e | 2014-05-20 04:51:16 +0000 | [diff] [blame] | 87 | return nullptr; |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 88 | } |
| 89 | |
David Blaikie | c11bf80 | 2014-09-04 16:04:28 +0000 | [diff] [blame] | 90 | return &Cmd.getArguments(); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /// \brief Returns a clang build invocation initialized from the CC1 flags. |
Manuel Klimek | bea7dfb | 2015-03-28 00:42:36 +0000 | [diff] [blame] | 94 | clang::CompilerInvocation *newInvocation( |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 95 | clang::DiagnosticsEngine *Diagnostics, |
Reid Kleckner | 898229a | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 96 | const llvm::opt::ArgStringList &CC1Args) { |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 97 | assert(!CC1Args.empty() && "Must at least contain the program name!"); |
| 98 | clang::CompilerInvocation *Invocation = new clang::CompilerInvocation; |
| 99 | clang::CompilerInvocation::CreateFromArgs( |
| 100 | *Invocation, CC1Args.data() + 1, CC1Args.data() + CC1Args.size(), |
| 101 | *Diagnostics); |
| 102 | Invocation->getFrontendOpts().DisableFree = false; |
Nick Lewycky | 39dc9f8 | 2013-06-25 17:01:21 +0000 | [diff] [blame] | 103 | Invocation->getCodeGenOpts().DisableFree = false; |
Peter Collingbourne | c0423b3 | 2014-03-02 23:37:26 +0000 | [diff] [blame] | 104 | Invocation->getDependencyOutputOpts() = DependencyOutputOptions(); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 105 | return Invocation; |
| 106 | } |
| 107 | |
| 108 | bool runToolOnCode(clang::FrontendAction *ToolAction, const Twine &Code, |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 109 | const Twine &FileName, |
| 110 | std::shared_ptr<PCHContainerOperations> PCHContainerOps) { |
| 111 | return runToolOnCodeWithArgs(ToolAction, Code, std::vector<std::string>(), |
| 112 | FileName, PCHContainerOps); |
Nico Weber | 077a53e | 2012-08-30 02:02:19 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 115 | static std::vector<std::string> |
| 116 | getSyntaxOnlyToolArgs(const std::vector<std::string> &ExtraArgs, |
| 117 | StringRef FileName) { |
| 118 | std::vector<std::string> Args; |
| 119 | Args.push_back("clang-tool"); |
| 120 | Args.push_back("-fsyntax-only"); |
| 121 | Args.insert(Args.end(), ExtraArgs.begin(), ExtraArgs.end()); |
| 122 | Args.push_back(FileName.str()); |
| 123 | return Args; |
| 124 | } |
| 125 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 126 | bool runToolOnCodeWithArgs( |
| 127 | clang::FrontendAction *ToolAction, const Twine &Code, |
| 128 | const std::vector<std::string> &Args, const Twine &FileName, |
| 129 | std::shared_ptr<PCHContainerOperations> PCHContainerOps, |
| 130 | const FileContentMappings &VirtualMappedFiles) { |
Manuel Klimek | d3aa1f4 | 2014-11-25 17:01:06 +0000 | [diff] [blame] | 131 | |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 132 | SmallString<16> FileNameStorage; |
| 133 | StringRef FileNameRef = FileName.toNullTerminatedStringRef(FileNameStorage); |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 134 | llvm::IntrusiveRefCntPtr<FileManager> Files( |
| 135 | new FileManager(FileSystemOptions())); |
Manuel Klimek | d3aa1f4 | 2014-11-25 17:01:06 +0000 | [diff] [blame] | 136 | ToolInvocation Invocation(getSyntaxOnlyToolArgs(Args, FileNameRef), |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 137 | ToolAction, Files.get(), PCHContainerOps); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 138 | |
| 139 | SmallString<1024> CodeStorage; |
| 140 | Invocation.mapVirtualFile(FileNameRef, |
| 141 | Code.toNullTerminatedStringRef(CodeStorage)); |
Manuel Klimek | d3aa1f4 | 2014-11-25 17:01:06 +0000 | [diff] [blame] | 142 | |
| 143 | for (auto &FilenameWithContent : VirtualMappedFiles) { |
| 144 | Invocation.mapVirtualFile(FilenameWithContent.first, |
| 145 | FilenameWithContent.second); |
| 146 | } |
| 147 | |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 148 | return Invocation.run(); |
| 149 | } |
| 150 | |
Manuel Klimek | 65fd0e1 | 2012-07-10 13:10:51 +0000 | [diff] [blame] | 151 | std::string getAbsolutePath(StringRef File) { |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 152 | StringRef RelativePath(File); |
NAKAMURA Takumi | 9b2d17c | 2012-05-23 22:24:20 +0000 | [diff] [blame] | 153 | // FIXME: Should '.\\' be accepted on Win32? |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 154 | if (RelativePath.startswith("./")) { |
| 155 | RelativePath = RelativePath.substr(strlen("./")); |
| 156 | } |
Rafael Espindola | c7367ff | 2013-08-10 01:40:10 +0000 | [diff] [blame] | 157 | |
| 158 | SmallString<1024> AbsolutePath = RelativePath; |
Rafael Espindola | c080917 | 2014-06-12 14:02:15 +0000 | [diff] [blame] | 159 | std::error_code EC = llvm::sys::fs::make_absolute(AbsolutePath); |
Rafael Espindola | c7367ff | 2013-08-10 01:40:10 +0000 | [diff] [blame] | 160 | assert(!EC); |
Rafael Espindola | 0b8e4a1 | 2013-08-10 04:25:53 +0000 | [diff] [blame] | 161 | (void)EC; |
Benjamin Kramer | 2d4d8cb | 2013-09-11 11:23:15 +0000 | [diff] [blame] | 162 | llvm::sys::path::native(AbsolutePath); |
| 163 | return AbsolutePath.str(); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Manuel Klimek | 9b30e2b | 2015-10-06 10:45:03 +0000 | [diff] [blame] | 166 | void addTargetAndModeForProgramName(std::vector<std::string> &CommandLine, |
| 167 | StringRef InvokedAs) { |
| 168 | if (!CommandLine.empty() && !InvokedAs.empty()) { |
| 169 | bool AlreadyHasTarget = false; |
| 170 | bool AlreadyHasMode = false; |
| 171 | // Skip CommandLine[0]. |
| 172 | for (auto Token = ++CommandLine.begin(); Token != CommandLine.end(); |
| 173 | ++Token) { |
| 174 | StringRef TokenRef(*Token); |
| 175 | AlreadyHasTarget |= |
| 176 | (TokenRef == "-target" || TokenRef.startswith("-target=")); |
| 177 | AlreadyHasMode |= (TokenRef == "--driver-mode" || |
| 178 | TokenRef.startswith("--driver-mode=")); |
| 179 | } |
| 180 | auto TargetMode = |
| 181 | clang::driver::ToolChain::getTargetAndModeFromProgramName(InvokedAs); |
| 182 | if (!AlreadyHasMode && !TargetMode.second.empty()) { |
| 183 | CommandLine.insert(++CommandLine.begin(), TargetMode.second); |
| 184 | } |
| 185 | if (!AlreadyHasTarget && !TargetMode.first.empty()) { |
| 186 | CommandLine.insert(++CommandLine.begin(), {"-target", TargetMode.first}); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 191 | namespace { |
| 192 | |
| 193 | class SingleFrontendActionFactory : public FrontendActionFactory { |
| 194 | FrontendAction *Action; |
| 195 | |
| 196 | public: |
| 197 | SingleFrontendActionFactory(FrontendAction *Action) : Action(Action) {} |
| 198 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 199 | FrontendAction *create() override { return Action; } |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 200 | }; |
| 201 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 202 | } |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 203 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 204 | ToolInvocation::ToolInvocation( |
| 205 | std::vector<std::string> CommandLine, ToolAction *Action, |
| 206 | FileManager *Files, std::shared_ptr<PCHContainerOperations> PCHContainerOps) |
| 207 | : CommandLine(std::move(CommandLine)), Action(Action), OwnsAction(false), |
| 208 | Files(Files), PCHContainerOps(PCHContainerOps), DiagConsumer(nullptr) {} |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 209 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 210 | ToolInvocation::ToolInvocation( |
| 211 | std::vector<std::string> CommandLine, FrontendAction *FAction, |
| 212 | FileManager *Files, std::shared_ptr<PCHContainerOperations> PCHContainerOps) |
Benjamin Kramer | efb1eb9 | 2014-03-20 12:48:36 +0000 | [diff] [blame] | 213 | : CommandLine(std::move(CommandLine)), |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 214 | Action(new SingleFrontendActionFactory(FAction)), OwnsAction(true), |
| 215 | Files(Files), PCHContainerOps(PCHContainerOps), DiagConsumer(nullptr) {} |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 216 | |
| 217 | ToolInvocation::~ToolInvocation() { |
| 218 | if (OwnsAction) |
| 219 | delete Action; |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | void ToolInvocation::mapVirtualFile(StringRef FilePath, StringRef Content) { |
NAKAMURA Takumi | 4de3165 | 2012-06-02 15:34:21 +0000 | [diff] [blame] | 223 | SmallString<1024> PathStorage; |
| 224 | llvm::sys::path::native(FilePath, PathStorage); |
| 225 | MappedFileContents[PathStorage] = Content; |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | bool ToolInvocation::run() { |
| 229 | std::vector<const char*> Argv; |
Benjamin Kramer | efb1eb9 | 2014-03-20 12:48:36 +0000 | [diff] [blame] | 230 | for (const std::string &Str : CommandLine) |
| 231 | Argv.push_back(Str.c_str()); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 232 | const char *const BinaryName = Argv[0]; |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 233 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 234 | TextDiagnosticPrinter DiagnosticPrinter( |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 235 | llvm::errs(), &*DiagOpts); |
| 236 | DiagnosticsEngine Diagnostics( |
Manuel Klimek | 6408301 | 2013-11-07 23:18:05 +0000 | [diff] [blame] | 237 | IntrusiveRefCntPtr<clang::DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts, |
| 238 | DiagConsumer ? DiagConsumer : &DiagnosticPrinter, false); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 239 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 240 | const std::unique_ptr<clang::driver::Driver> Driver( |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 241 | newDriver(&Diagnostics, BinaryName)); |
| 242 | // Since the input might only be virtual, don't check whether it exists. |
| 243 | Driver->setCheckInputsExist(false); |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 244 | const std::unique_ptr<clang::driver::Compilation> Compilation( |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 245 | Driver->BuildCompilation(llvm::makeArrayRef(Argv))); |
Reid Kleckner | 898229a | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 246 | const llvm::opt::ArgStringList *const CC1Args = getCC1Arguments( |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 247 | &Diagnostics, Compilation.get()); |
Craig Topper | ccbc35e | 2014-05-20 04:51:16 +0000 | [diff] [blame] | 248 | if (!CC1Args) { |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 249 | return false; |
| 250 | } |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 251 | std::unique_ptr<clang::CompilerInvocation> Invocation( |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 252 | newInvocation(&Diagnostics, *CC1Args)); |
Benjamin Kramer | efb1eb9 | 2014-03-20 12:48:36 +0000 | [diff] [blame] | 253 | for (const auto &It : MappedFileContents) { |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 254 | // Inject the code as the given file name into the preprocessor options. |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 255 | std::unique_ptr<llvm::MemoryBuffer> Input = |
| 256 | llvm::MemoryBuffer::getMemBuffer(It.getValue()); |
| 257 | Invocation->getPreprocessorOpts().addRemappedFile(It.getKey(), |
| 258 | Input.release()); |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 259 | } |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 260 | return runInvocation(BinaryName, Compilation.get(), Invocation.release(), |
| 261 | PCHContainerOps); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 264 | bool ToolInvocation::runInvocation( |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 265 | const char *BinaryName, clang::driver::Compilation *Compilation, |
| 266 | clang::CompilerInvocation *Invocation, |
| 267 | std::shared_ptr<PCHContainerOperations> PCHContainerOps) { |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 268 | // Show the invocation, with -v. |
| 269 | if (Invocation->getHeaderSearchOpts().Verbose) { |
| 270 | llvm::errs() << "clang Invocation:\n"; |
Hans Wennborg | b212b34 | 2013-09-12 18:23:34 +0000 | [diff] [blame] | 271 | Compilation->getJobs().Print(llvm::errs(), "\n", true); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 272 | llvm::errs() << "\n"; |
| 273 | } |
| 274 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 275 | return Action->runInvocation(Invocation, Files, PCHContainerOps, |
| 276 | DiagConsumer); |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 279 | bool FrontendActionFactory::runInvocation( |
| 280 | CompilerInvocation *Invocation, FileManager *Files, |
| 281 | std::shared_ptr<PCHContainerOperations> PCHContainerOps, |
| 282 | DiagnosticConsumer *DiagConsumer) { |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 283 | // Create a compiler instance to handle the actual work. |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 284 | clang::CompilerInstance Compiler(PCHContainerOps); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 285 | Compiler.setInvocation(Invocation); |
| 286 | Compiler.setFileManager(Files); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 287 | |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 288 | // The FrontendAction can have lifetime requirements for Compiler or its |
| 289 | // 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] | 290 | // pass it to an std::unique_ptr declared after the Compiler variable. |
| 291 | std::unique_ptr<FrontendAction> ScopedToolAction(create()); |
Alexander Kornienko | 21d6ec9 | 2012-05-31 17:58:43 +0000 | [diff] [blame] | 292 | |
Alp Toker | 77273fc | 2014-05-16 13:45:29 +0000 | [diff] [blame] | 293 | // Create the compiler's actual diagnostics engine. |
Manuel Klimek | 6408301 | 2013-11-07 23:18:05 +0000 | [diff] [blame] | 294 | Compiler.createDiagnostics(DiagConsumer, /*ShouldOwnClient=*/false); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 295 | if (!Compiler.hasDiagnostics()) |
| 296 | return false; |
| 297 | |
| 298 | Compiler.createSourceManager(*Files); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 299 | |
Alexander Kornienko | 21d6ec9 | 2012-05-31 17:58:43 +0000 | [diff] [blame] | 300 | const bool Success = Compiler.ExecuteAction(*ScopedToolAction); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 301 | |
Manuel Klimek | 3aad855 | 2012-07-31 13:56:54 +0000 | [diff] [blame] | 302 | Files->clearStatCaches(); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 303 | return Success; |
| 304 | } |
| 305 | |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 306 | ClangTool::ClangTool(const CompilationDatabase &Compilations, |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 307 | ArrayRef<std::string> SourcePaths, |
| 308 | std::shared_ptr<PCHContainerOperations> PCHContainerOps) |
Alexander Kornienko | 9a45fac | 2014-08-27 21:36:39 +0000 | [diff] [blame] | 309 | : Compilations(Compilations), SourcePaths(SourcePaths), |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 310 | PCHContainerOps(PCHContainerOps), |
Alexander Kornienko | 9a45fac | 2014-08-27 21:36:39 +0000 | [diff] [blame] | 311 | Files(new FileManager(FileSystemOptions())), DiagConsumer(nullptr) { |
Alexander Kornienko | 74e1c46 | 2014-12-03 17:53:02 +0000 | [diff] [blame] | 312 | appendArgumentsAdjuster(getClangStripOutputAdjuster()); |
| 313 | appendArgumentsAdjuster(getClangSyntaxOnlyAdjuster()); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Benjamin Kramer | 90e0fd8 | 2014-09-24 11:47:42 +0000 | [diff] [blame] | 316 | ClangTool::~ClangTool() {} |
Manuel Klimek | 6408301 | 2013-11-07 23:18:05 +0000 | [diff] [blame] | 317 | |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 318 | void ClangTool::mapVirtualFile(StringRef FilePath, StringRef Content) { |
| 319 | MappedFileContents.push_back(std::make_pair(FilePath, Content)); |
| 320 | } |
| 321 | |
Alexander Kornienko | 74e1c46 | 2014-12-03 17:53:02 +0000 | [diff] [blame] | 322 | void ClangTool::appendArgumentsAdjuster(ArgumentsAdjuster Adjuster) { |
| 323 | if (ArgsAdjuster) |
| 324 | ArgsAdjuster = combineAdjusters(ArgsAdjuster, Adjuster); |
| 325 | else |
| 326 | ArgsAdjuster = Adjuster; |
Manuel Klimek | d91ac93 | 2013-06-04 14:44:44 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | void ClangTool::clearArgumentsAdjusters() { |
Alexander Kornienko | 74e1c46 | 2014-12-03 17:53:02 +0000 | [diff] [blame] | 330 | ArgsAdjuster = nullptr; |
Simon Atanasyan | 32df72d | 2012-05-09 16:18:30 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 333 | int ClangTool::run(ToolAction *Action) { |
Alexander Kornienko | 8388d24 | 2012-06-04 19:02:59 +0000 | [diff] [blame] | 334 | // Exists solely for the purpose of lookup of the resource path. |
| 335 | // This just needs to be some symbol in the binary. |
| 336 | static int StaticSymbol; |
| 337 | // The driver detects the builtin header path based on the path of the |
| 338 | // executable. |
| 339 | // FIXME: On linux, GetMainExecutable is independent of the value of the |
| 340 | // first argument, thus allowing ClangTool and runToolOnCode to just |
| 341 | // pass in made-up names here. Make sure this works on other platforms. |
| 342 | std::string MainExecutable = |
Rafael Espindola | 9678d27 | 2013-06-26 05:03:40 +0000 | [diff] [blame] | 343 | llvm::sys::fs::getMainExecutable("clang_tool", &StaticSymbol); |
Alexander Kornienko | 8388d24 | 2012-06-04 19:02:59 +0000 | [diff] [blame] | 344 | |
Alexander Kornienko | c48a535 | 2014-11-10 15:42:31 +0000 | [diff] [blame] | 345 | llvm::SmallString<128> InitialDirectory; |
| 346 | if (std::error_code EC = llvm::sys::fs::current_path(InitialDirectory)) |
| 347 | llvm::report_fatal_error("Cannot detect current path: " + |
| 348 | Twine(EC.message())); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 349 | bool ProcessingFailed = false; |
Alexander Kornienko | 9a45fac | 2014-08-27 21:36:39 +0000 | [diff] [blame] | 350 | for (const auto &SourcePath : SourcePaths) { |
| 351 | std::string File(getAbsolutePath(SourcePath)); |
| 352 | |
Alexander Kornienko | c48a535 | 2014-11-10 15:42:31 +0000 | [diff] [blame] | 353 | // Currently implementations of CompilationDatabase::getCompileCommands can |
| 354 | // change the state of the file system (e.g. prepare generated headers), so |
| 355 | // this method needs to run right before we invoke the tool, as the next |
| 356 | // file may require a different (incompatible) state of the file system. |
| 357 | // |
| 358 | // FIXME: Make the compilation database interface more explicit about the |
| 359 | // requirements to the order of invocation of its members. |
Alexander Kornienko | 9a45fac | 2014-08-27 21:36:39 +0000 | [diff] [blame] | 360 | std::vector<CompileCommand> CompileCommandsForFile = |
| 361 | Compilations.getCompileCommands(File); |
| 362 | if (CompileCommandsForFile.empty()) { |
| 363 | // FIXME: There are two use cases here: doing a fuzzy |
| 364 | // "find . -name '*.cc' |xargs tool" match, where as a user I don't care |
| 365 | // about the .cc files that were not found, and the use case where I |
| 366 | // specify all files I want to run over explicitly, where this should |
| 367 | // be an error. We'll want to add an option for this. |
| 368 | llvm::errs() << "Skipping " << File << ". Compile command not found.\n"; |
| 369 | continue; |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 370 | } |
Alexander Kornienko | 9a45fac | 2014-08-27 21:36:39 +0000 | [diff] [blame] | 371 | for (CompileCommand &CompileCommand : CompileCommandsForFile) { |
| 372 | // FIXME: chdir is thread hostile; on the other hand, creating the same |
| 373 | // behavior as chdir is complex: chdir resolves the path once, thus |
| 374 | // guaranteeing that all subsequent relative path operations work |
| 375 | // on the same path the original chdir resulted in. This makes a |
| 376 | // difference for example on network filesystems, where symlinks might be |
| 377 | // switched during runtime of the tool. Fixing this depends on having a |
| 378 | // file system abstraction that allows openat() style interactions. |
| 379 | if (chdir(CompileCommand.Directory.c_str())) |
| 380 | llvm::report_fatal_error("Cannot chdir into \"" + |
| 381 | Twine(CompileCommand.Directory) + "\n!"); |
| 382 | std::vector<std::string> CommandLine = CompileCommand.CommandLine; |
Alexander Kornienko | 74e1c46 | 2014-12-03 17:53:02 +0000 | [diff] [blame] | 383 | if (ArgsAdjuster) |
| 384 | CommandLine = ArgsAdjuster(CommandLine); |
Alexander Kornienko | 9a45fac | 2014-08-27 21:36:39 +0000 | [diff] [blame] | 385 | assert(!CommandLine.empty()); |
| 386 | CommandLine[0] = MainExecutable; |
| 387 | // FIXME: We need a callback mechanism for the tool writer to output a |
| 388 | // customized message for each file. |
| 389 | DEBUG({ llvm::dbgs() << "Processing: " << File << ".\n"; }); |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 390 | ToolInvocation Invocation(std::move(CommandLine), Action, Files.get(), |
| 391 | PCHContainerOps); |
Alexander Kornienko | 9a45fac | 2014-08-27 21:36:39 +0000 | [diff] [blame] | 392 | Invocation.setDiagnosticConsumer(DiagConsumer); |
Alexander Kornienko | c48a535 | 2014-11-10 15:42:31 +0000 | [diff] [blame] | 393 | for (const auto &MappedFile : MappedFileContents) |
Alexander Kornienko | 9a45fac | 2014-08-27 21:36:39 +0000 | [diff] [blame] | 394 | Invocation.mapVirtualFile(MappedFile.first, MappedFile.second); |
Alexander Kornienko | 9a45fac | 2014-08-27 21:36:39 +0000 | [diff] [blame] | 395 | if (!Invocation.run()) { |
| 396 | // FIXME: Diagnostics should be used instead. |
| 397 | llvm::errs() << "Error while processing " << File << ".\n"; |
| 398 | ProcessingFailed = true; |
| 399 | } |
Alexander Kornienko | c48a535 | 2014-11-10 15:42:31 +0000 | [diff] [blame] | 400 | // Return to the initial directory to correctly resolve next file by |
| 401 | // relative path. |
| 402 | if (chdir(InitialDirectory.c_str())) |
| 403 | llvm::report_fatal_error("Cannot chdir into \"" + |
| 404 | Twine(InitialDirectory) + "\n!"); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | return ProcessingFailed ? 1 : 0; |
| 408 | } |
| 409 | |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 410 | namespace { |
| 411 | |
| 412 | class ASTBuilderAction : public ToolAction { |
David Blaikie | 39808ff | 2014-04-25 14:49:37 +0000 | [diff] [blame] | 413 | std::vector<std::unique_ptr<ASTUnit>> &ASTs; |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 414 | |
| 415 | public: |
David Blaikie | 39808ff | 2014-04-25 14:49:37 +0000 | [diff] [blame] | 416 | ASTBuilderAction(std::vector<std::unique_ptr<ASTUnit>> &ASTs) : ASTs(ASTs) {} |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 417 | |
Manuel Klimek | 6408301 | 2013-11-07 23:18:05 +0000 | [diff] [blame] | 418 | bool runInvocation(CompilerInvocation *Invocation, FileManager *Files, |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 419 | std::shared_ptr<PCHContainerOperations> PCHContainerOps, |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 420 | DiagnosticConsumer *DiagConsumer) override { |
David Blaikie | 103a2de | 2014-04-25 17:01:33 +0000 | [diff] [blame] | 421 | std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromCompilerInvocation( |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 422 | Invocation, PCHContainerOps, |
| 423 | CompilerInstance::createDiagnostics(&Invocation->getDiagnosticOpts(), |
| 424 | DiagConsumer, |
Benjamin Kramer | bc63290 | 2015-10-06 14:45:20 +0000 | [diff] [blame^] | 425 | /*ShouldOwnClient=*/false), |
| 426 | Files); |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 427 | if (!AST) |
| 428 | return false; |
| 429 | |
David Blaikie | 39808ff | 2014-04-25 14:49:37 +0000 | [diff] [blame] | 430 | ASTs.push_back(std::move(AST)); |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 431 | return true; |
| 432 | } |
| 433 | }; |
| 434 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 435 | } |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 436 | |
David Blaikie | 39808ff | 2014-04-25 14:49:37 +0000 | [diff] [blame] | 437 | int ClangTool::buildASTs(std::vector<std::unique_ptr<ASTUnit>> &ASTs) { |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 438 | ASTBuilderAction Action(ASTs); |
| 439 | return run(&Action); |
| 440 | } |
| 441 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 442 | std::unique_ptr<ASTUnit> |
| 443 | buildASTFromCode(const Twine &Code, const Twine &FileName, |
| 444 | std::shared_ptr<PCHContainerOperations> PCHContainerOps) { |
| 445 | return buildASTFromCodeWithArgs(Code, std::vector<std::string>(), FileName, |
| 446 | PCHContainerOps); |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 449 | std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs( |
| 450 | const Twine &Code, const std::vector<std::string> &Args, |
| 451 | const Twine &FileName, |
| 452 | std::shared_ptr<PCHContainerOperations> PCHContainerOps) { |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 453 | SmallString<16> FileNameStorage; |
| 454 | StringRef FileNameRef = FileName.toNullTerminatedStringRef(FileNameStorage); |
| 455 | |
David Blaikie | 39808ff | 2014-04-25 14:49:37 +0000 | [diff] [blame] | 456 | std::vector<std::unique_ptr<ASTUnit>> ASTs; |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 457 | ASTBuilderAction Action(ASTs); |
Craig Topper | ccbc35e | 2014-05-20 04:51:16 +0000 | [diff] [blame] | 458 | ToolInvocation Invocation(getSyntaxOnlyToolArgs(Args, FileNameRef), &Action, |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 459 | nullptr, PCHContainerOps); |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 460 | |
| 461 | SmallString<1024> CodeStorage; |
| 462 | Invocation.mapVirtualFile(FileNameRef, |
| 463 | Code.toNullTerminatedStringRef(CodeStorage)); |
| 464 | if (!Invocation.run()) |
Craig Topper | ccbc35e | 2014-05-20 04:51:16 +0000 | [diff] [blame] | 465 | return nullptr; |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 466 | |
| 467 | assert(ASTs.size() == 1); |
David Blaikie | 103a2de | 2014-04-25 17:01:33 +0000 | [diff] [blame] | 468 | return std::move(ASTs[0]); |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 471 | } // end namespace tooling |
| 472 | } // end namespace clang |