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 | |
Chandler Carruth | 57f5fbe | 2014-04-21 22:55:36 +0000 | [diff] [blame] | 35 | #define DEBUG_TYPE "clang-tooling" |
| 36 | |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 37 | namespace clang { |
| 38 | namespace tooling { |
| 39 | |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 40 | ToolAction::~ToolAction() {} |
| 41 | |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 42 | FrontendActionFactory::~FrontendActionFactory() {} |
| 43 | |
| 44 | // FIXME: This file contains structural duplication with other parts of the |
| 45 | // code that sets up a compiler to run tools on it, and we should refactor |
| 46 | // it to be based on the same framework. |
| 47 | |
| 48 | /// \brief Builds a clang driver initialized for running clang tools. |
| 49 | static clang::driver::Driver *newDriver(clang::DiagnosticsEngine *Diagnostics, |
| 50 | const char *BinaryName) { |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 51 | clang::driver::Driver *CompilerDriver = new clang::driver::Driver( |
Alp Toker | 1761f11 | 2014-05-15 22:26:36 +0000 | [diff] [blame] | 52 | BinaryName, llvm::sys::getDefaultTargetTriple(), *Diagnostics); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 53 | CompilerDriver->setTitle("clang_based_tool"); |
| 54 | return CompilerDriver; |
| 55 | } |
| 56 | |
| 57 | /// \brief Retrieves the clang CC1 specific flags out of the compilation's jobs. |
| 58 | /// |
| 59 | /// Returns NULL on error. |
Reid Kleckner | 898229a | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 60 | static const llvm::opt::ArgStringList *getCC1Arguments( |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 61 | clang::DiagnosticsEngine *Diagnostics, |
| 62 | clang::driver::Compilation *Compilation) { |
| 63 | // We expect to get back exactly one Command job, if we didn't something |
| 64 | // failed. Extract that job from the Compilation. |
| 65 | const clang::driver::JobList &Jobs = Compilation->getJobs(); |
Justin Bogner | aab9792 | 2014-10-03 01:04:53 +0000 | [diff] [blame] | 66 | if (Jobs.size() != 1 || !isa<clang::driver::Command>(*Jobs.begin())) { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 67 | SmallString<256> error_msg; |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 68 | llvm::raw_svector_ostream error_stream(error_msg); |
Hans Wennborg | b212b34 | 2013-09-12 18:23:34 +0000 | [diff] [blame] | 69 | Jobs.Print(error_stream, "; ", true); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 70 | Diagnostics->Report(clang::diag::err_fe_expected_compiler_job) |
| 71 | << error_stream.str(); |
Craig Topper | ccbc35e | 2014-05-20 04:51:16 +0000 | [diff] [blame] | 72 | return nullptr; |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | // The one job we find should be to invoke clang again. |
David Blaikie | c11bf80 | 2014-09-04 16:04:28 +0000 | [diff] [blame] | 76 | const clang::driver::Command &Cmd = |
Justin Bogner | aab9792 | 2014-10-03 01:04:53 +0000 | [diff] [blame] | 77 | cast<clang::driver::Command>(*Jobs.begin()); |
David Blaikie | c11bf80 | 2014-09-04 16:04:28 +0000 | [diff] [blame] | 78 | if (StringRef(Cmd.getCreator().getName()) != "clang") { |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 79 | Diagnostics->Report(clang::diag::err_fe_expected_clang_command); |
Craig Topper | ccbc35e | 2014-05-20 04:51:16 +0000 | [diff] [blame] | 80 | return nullptr; |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 81 | } |
| 82 | |
David Blaikie | c11bf80 | 2014-09-04 16:04:28 +0000 | [diff] [blame] | 83 | return &Cmd.getArguments(); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /// \brief Returns a clang build invocation initialized from the CC1 flags. |
Manuel Klimek | bea7dfb | 2015-03-28 00:42:36 +0000 | [diff] [blame] | 87 | clang::CompilerInvocation *newInvocation( |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 88 | clang::DiagnosticsEngine *Diagnostics, |
Reid Kleckner | 898229a | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 89 | const llvm::opt::ArgStringList &CC1Args) { |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 90 | assert(!CC1Args.empty() && "Must at least contain the program name!"); |
| 91 | clang::CompilerInvocation *Invocation = new clang::CompilerInvocation; |
| 92 | clang::CompilerInvocation::CreateFromArgs( |
| 93 | *Invocation, CC1Args.data() + 1, CC1Args.data() + CC1Args.size(), |
| 94 | *Diagnostics); |
| 95 | Invocation->getFrontendOpts().DisableFree = false; |
Nick Lewycky | 39dc9f8 | 2013-06-25 17:01:21 +0000 | [diff] [blame] | 96 | Invocation->getCodeGenOpts().DisableFree = false; |
Peter Collingbourne | c0423b3 | 2014-03-02 23:37:26 +0000 | [diff] [blame] | 97 | Invocation->getDependencyOutputOpts() = DependencyOutputOptions(); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 98 | return Invocation; |
| 99 | } |
| 100 | |
| 101 | bool runToolOnCode(clang::FrontendAction *ToolAction, const Twine &Code, |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 102 | const Twine &FileName, |
| 103 | std::shared_ptr<PCHContainerOperations> PCHContainerOps) { |
| 104 | return runToolOnCodeWithArgs(ToolAction, Code, std::vector<std::string>(), |
| 105 | FileName, PCHContainerOps); |
Nico Weber | 077a53e | 2012-08-30 02:02:19 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 108 | static std::vector<std::string> |
| 109 | getSyntaxOnlyToolArgs(const std::vector<std::string> &ExtraArgs, |
| 110 | StringRef FileName) { |
| 111 | std::vector<std::string> Args; |
| 112 | Args.push_back("clang-tool"); |
| 113 | Args.push_back("-fsyntax-only"); |
| 114 | Args.insert(Args.end(), ExtraArgs.begin(), ExtraArgs.end()); |
| 115 | Args.push_back(FileName.str()); |
| 116 | return Args; |
| 117 | } |
| 118 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 119 | bool runToolOnCodeWithArgs( |
| 120 | clang::FrontendAction *ToolAction, const Twine &Code, |
| 121 | const std::vector<std::string> &Args, const Twine &FileName, |
| 122 | std::shared_ptr<PCHContainerOperations> PCHContainerOps, |
| 123 | const FileContentMappings &VirtualMappedFiles) { |
Manuel Klimek | d3aa1f4 | 2014-11-25 17:01:06 +0000 | [diff] [blame] | 124 | |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 125 | SmallString<16> FileNameStorage; |
| 126 | StringRef FileNameRef = FileName.toNullTerminatedStringRef(FileNameStorage); |
Benjamin Kramer | c4cb3b1 | 2015-10-09 09:54:37 +0000 | [diff] [blame] | 127 | llvm::IntrusiveRefCntPtr<vfs::OverlayFileSystem> OverlayFileSystem( |
| 128 | new vfs::OverlayFileSystem(vfs::getRealFileSystem())); |
| 129 | llvm::IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem( |
| 130 | new vfs::InMemoryFileSystem); |
| 131 | OverlayFileSystem->pushOverlay(InMemoryFileSystem); |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 132 | llvm::IntrusiveRefCntPtr<FileManager> Files( |
Benjamin Kramer | c4cb3b1 | 2015-10-09 09:54:37 +0000 | [diff] [blame] | 133 | new FileManager(FileSystemOptions(), OverlayFileSystem)); |
Manuel Klimek | d3aa1f4 | 2014-11-25 17:01:06 +0000 | [diff] [blame] | 134 | ToolInvocation Invocation(getSyntaxOnlyToolArgs(Args, FileNameRef), |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 135 | ToolAction, Files.get(), PCHContainerOps); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 136 | |
| 137 | SmallString<1024> CodeStorage; |
Benjamin Kramer | c4cb3b1 | 2015-10-09 09:54:37 +0000 | [diff] [blame] | 138 | InMemoryFileSystem->addFile(FileNameRef, 0, |
| 139 | llvm::MemoryBuffer::getMemBuffer( |
| 140 | Code.toNullTerminatedStringRef(CodeStorage))); |
Manuel Klimek | d3aa1f4 | 2014-11-25 17:01:06 +0000 | [diff] [blame] | 141 | |
| 142 | for (auto &FilenameWithContent : VirtualMappedFiles) { |
Benjamin Kramer | c4cb3b1 | 2015-10-09 09:54:37 +0000 | [diff] [blame] | 143 | InMemoryFileSystem->addFile( |
| 144 | FilenameWithContent.first, 0, |
| 145 | llvm::MemoryBuffer::getMemBuffer(FilenameWithContent.second)); |
Manuel Klimek | d3aa1f4 | 2014-11-25 17:01:06 +0000 | [diff] [blame] | 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 | c4cb3b1 | 2015-10-09 09:54:37 +0000 | [diff] [blame] | 253 | // FIXME: remove this when all users have migrated! |
Benjamin Kramer | efb1eb9 | 2014-03-20 12:48:36 +0000 | [diff] [blame] | 254 | for (const auto &It : MappedFileContents) { |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 255 | // Inject the code as the given file name into the preprocessor options. |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 256 | std::unique_ptr<llvm::MemoryBuffer> Input = |
| 257 | llvm::MemoryBuffer::getMemBuffer(It.getValue()); |
| 258 | Invocation->getPreprocessorOpts().addRemappedFile(It.getKey(), |
| 259 | Input.release()); |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 260 | } |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 261 | return runInvocation(BinaryName, Compilation.get(), Invocation.release(), |
| 262 | PCHContainerOps); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 265 | bool ToolInvocation::runInvocation( |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 266 | const char *BinaryName, clang::driver::Compilation *Compilation, |
| 267 | clang::CompilerInvocation *Invocation, |
| 268 | std::shared_ptr<PCHContainerOperations> PCHContainerOps) { |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 269 | // Show the invocation, with -v. |
| 270 | if (Invocation->getHeaderSearchOpts().Verbose) { |
| 271 | llvm::errs() << "clang Invocation:\n"; |
Hans Wennborg | b212b34 | 2013-09-12 18:23:34 +0000 | [diff] [blame] | 272 | Compilation->getJobs().Print(llvm::errs(), "\n", true); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 273 | llvm::errs() << "\n"; |
| 274 | } |
| 275 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 276 | return Action->runInvocation(Invocation, Files, PCHContainerOps, |
| 277 | DiagConsumer); |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 280 | bool FrontendActionFactory::runInvocation( |
| 281 | CompilerInvocation *Invocation, FileManager *Files, |
| 282 | std::shared_ptr<PCHContainerOperations> PCHContainerOps, |
| 283 | DiagnosticConsumer *DiagConsumer) { |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 284 | // Create a compiler instance to handle the actual work. |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 285 | clang::CompilerInstance Compiler(PCHContainerOps); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 286 | Compiler.setInvocation(Invocation); |
| 287 | Compiler.setFileManager(Files); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 288 | |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 289 | // The FrontendAction can have lifetime requirements for Compiler or its |
| 290 | // 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] | 291 | // pass it to an std::unique_ptr declared after the Compiler variable. |
| 292 | std::unique_ptr<FrontendAction> ScopedToolAction(create()); |
Alexander Kornienko | 21d6ec9 | 2012-05-31 17:58:43 +0000 | [diff] [blame] | 293 | |
Alp Toker | 77273fc | 2014-05-16 13:45:29 +0000 | [diff] [blame] | 294 | // Create the compiler's actual diagnostics engine. |
Manuel Klimek | 6408301 | 2013-11-07 23:18:05 +0000 | [diff] [blame] | 295 | Compiler.createDiagnostics(DiagConsumer, /*ShouldOwnClient=*/false); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 296 | if (!Compiler.hasDiagnostics()) |
| 297 | return false; |
| 298 | |
| 299 | Compiler.createSourceManager(*Files); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 300 | |
Alexander Kornienko | 21d6ec9 | 2012-05-31 17:58:43 +0000 | [diff] [blame] | 301 | const bool Success = Compiler.ExecuteAction(*ScopedToolAction); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 302 | |
Manuel Klimek | 3aad855 | 2012-07-31 13:56:54 +0000 | [diff] [blame] | 303 | Files->clearStatCaches(); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 304 | return Success; |
| 305 | } |
| 306 | |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 307 | ClangTool::ClangTool(const CompilationDatabase &Compilations, |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 308 | ArrayRef<std::string> SourcePaths, |
| 309 | std::shared_ptr<PCHContainerOperations> PCHContainerOps) |
Alexander Kornienko | 9a45fac | 2014-08-27 21:36:39 +0000 | [diff] [blame] | 310 | : Compilations(Compilations), SourcePaths(SourcePaths), |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 311 | PCHContainerOps(PCHContainerOps), |
Benjamin Kramer | c4cb3b1 | 2015-10-09 09:54:37 +0000 | [diff] [blame] | 312 | OverlayFileSystem(new vfs::OverlayFileSystem(vfs::getRealFileSystem())), |
| 313 | InMemoryFileSystem(new vfs::InMemoryFileSystem), |
| 314 | Files(new FileManager(FileSystemOptions(), OverlayFileSystem)), |
| 315 | DiagConsumer(nullptr) { |
| 316 | OverlayFileSystem->pushOverlay(InMemoryFileSystem); |
Alexander Kornienko | 74e1c46 | 2014-12-03 17:53:02 +0000 | [diff] [blame] | 317 | appendArgumentsAdjuster(getClangStripOutputAdjuster()); |
| 318 | appendArgumentsAdjuster(getClangSyntaxOnlyAdjuster()); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Benjamin Kramer | 90e0fd8 | 2014-09-24 11:47:42 +0000 | [diff] [blame] | 321 | ClangTool::~ClangTool() {} |
Manuel Klimek | 6408301 | 2013-11-07 23:18:05 +0000 | [diff] [blame] | 322 | |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 323 | void ClangTool::mapVirtualFile(StringRef FilePath, StringRef Content) { |
| 324 | MappedFileContents.push_back(std::make_pair(FilePath, Content)); |
| 325 | } |
| 326 | |
Alexander Kornienko | 74e1c46 | 2014-12-03 17:53:02 +0000 | [diff] [blame] | 327 | void ClangTool::appendArgumentsAdjuster(ArgumentsAdjuster Adjuster) { |
| 328 | if (ArgsAdjuster) |
| 329 | ArgsAdjuster = combineAdjusters(ArgsAdjuster, Adjuster); |
| 330 | else |
| 331 | ArgsAdjuster = Adjuster; |
Manuel Klimek | d91ac93 | 2013-06-04 14:44:44 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | void ClangTool::clearArgumentsAdjusters() { |
Alexander Kornienko | 74e1c46 | 2014-12-03 17:53:02 +0000 | [diff] [blame] | 335 | ArgsAdjuster = nullptr; |
Simon Atanasyan | 32df72d | 2012-05-09 16:18:30 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 338 | int ClangTool::run(ToolAction *Action) { |
Alexander Kornienko | 8388d24 | 2012-06-04 19:02:59 +0000 | [diff] [blame] | 339 | // Exists solely for the purpose of lookup of the resource path. |
| 340 | // This just needs to be some symbol in the binary. |
| 341 | static int StaticSymbol; |
| 342 | // The driver detects the builtin header path based on the path of the |
| 343 | // executable. |
| 344 | // FIXME: On linux, GetMainExecutable is independent of the value of the |
| 345 | // first argument, thus allowing ClangTool and runToolOnCode to just |
| 346 | // pass in made-up names here. Make sure this works on other platforms. |
| 347 | std::string MainExecutable = |
Rafael Espindola | 9678d27 | 2013-06-26 05:03:40 +0000 | [diff] [blame] | 348 | llvm::sys::fs::getMainExecutable("clang_tool", &StaticSymbol); |
Alexander Kornienko | 8388d24 | 2012-06-04 19:02:59 +0000 | [diff] [blame] | 349 | |
Alexander Kornienko | c48a535 | 2014-11-10 15:42:31 +0000 | [diff] [blame] | 350 | llvm::SmallString<128> InitialDirectory; |
| 351 | if (std::error_code EC = llvm::sys::fs::current_path(InitialDirectory)) |
| 352 | llvm::report_fatal_error("Cannot detect current path: " + |
| 353 | Twine(EC.message())); |
Benjamin Kramer | c4cb3b1 | 2015-10-09 09:54:37 +0000 | [diff] [blame] | 354 | |
| 355 | // First insert all absolute paths into the in-memory VFS. These are global |
| 356 | // for all compile commands. |
| 357 | if (SeenWorkingDirectories.insert("/").second) |
| 358 | for (const auto &MappedFile : MappedFileContents) |
| 359 | if (llvm::sys::path::is_absolute(MappedFile.first)) |
| 360 | InMemoryFileSystem->addFile( |
| 361 | MappedFile.first, 0, |
| 362 | llvm::MemoryBuffer::getMemBuffer(MappedFile.second)); |
| 363 | |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 364 | bool ProcessingFailed = false; |
Alexander Kornienko | 9a45fac | 2014-08-27 21:36:39 +0000 | [diff] [blame] | 365 | for (const auto &SourcePath : SourcePaths) { |
| 366 | std::string File(getAbsolutePath(SourcePath)); |
| 367 | |
Alexander Kornienko | c48a535 | 2014-11-10 15:42:31 +0000 | [diff] [blame] | 368 | // Currently implementations of CompilationDatabase::getCompileCommands can |
| 369 | // change the state of the file system (e.g. prepare generated headers), so |
| 370 | // this method needs to run right before we invoke the tool, as the next |
| 371 | // file may require a different (incompatible) state of the file system. |
| 372 | // |
| 373 | // FIXME: Make the compilation database interface more explicit about the |
| 374 | // requirements to the order of invocation of its members. |
Alexander Kornienko | 9a45fac | 2014-08-27 21:36:39 +0000 | [diff] [blame] | 375 | std::vector<CompileCommand> CompileCommandsForFile = |
| 376 | Compilations.getCompileCommands(File); |
| 377 | if (CompileCommandsForFile.empty()) { |
| 378 | // FIXME: There are two use cases here: doing a fuzzy |
| 379 | // "find . -name '*.cc' |xargs tool" match, where as a user I don't care |
| 380 | // about the .cc files that were not found, and the use case where I |
| 381 | // specify all files I want to run over explicitly, where this should |
| 382 | // be an error. We'll want to add an option for this. |
| 383 | llvm::errs() << "Skipping " << File << ". Compile command not found.\n"; |
| 384 | continue; |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 385 | } |
Alexander Kornienko | 9a45fac | 2014-08-27 21:36:39 +0000 | [diff] [blame] | 386 | for (CompileCommand &CompileCommand : CompileCommandsForFile) { |
| 387 | // FIXME: chdir is thread hostile; on the other hand, creating the same |
| 388 | // behavior as chdir is complex: chdir resolves the path once, thus |
| 389 | // guaranteeing that all subsequent relative path operations work |
| 390 | // on the same path the original chdir resulted in. This makes a |
| 391 | // difference for example on network filesystems, where symlinks might be |
| 392 | // switched during runtime of the tool. Fixing this depends on having a |
| 393 | // file system abstraction that allows openat() style interactions. |
Benjamin Kramer | c4cb3b1 | 2015-10-09 09:54:37 +0000 | [diff] [blame] | 394 | if (OverlayFileSystem->setCurrentWorkingDirectory( |
| 395 | CompileCommand.Directory)) |
Alexander Kornienko | 9a45fac | 2014-08-27 21:36:39 +0000 | [diff] [blame] | 396 | llvm::report_fatal_error("Cannot chdir into \"" + |
| 397 | Twine(CompileCommand.Directory) + "\n!"); |
Benjamin Kramer | c4cb3b1 | 2015-10-09 09:54:37 +0000 | [diff] [blame] | 398 | |
| 399 | // Now fill the in-memory VFS with the relative file mappings so it will |
| 400 | // have the correct relative paths. We never remove mappings but that |
| 401 | // should be fine. |
| 402 | if (SeenWorkingDirectories.insert(CompileCommand.Directory).second) |
| 403 | for (const auto &MappedFile : MappedFileContents) |
| 404 | if (!llvm::sys::path::is_absolute(MappedFile.first)) |
| 405 | InMemoryFileSystem->addFile( |
| 406 | MappedFile.first, 0, |
| 407 | llvm::MemoryBuffer::getMemBuffer(MappedFile.second)); |
| 408 | |
Alexander Kornienko | 9a45fac | 2014-08-27 21:36:39 +0000 | [diff] [blame] | 409 | std::vector<std::string> CommandLine = CompileCommand.CommandLine; |
Alexander Kornienko | 74e1c46 | 2014-12-03 17:53:02 +0000 | [diff] [blame] | 410 | if (ArgsAdjuster) |
| 411 | CommandLine = ArgsAdjuster(CommandLine); |
Alexander Kornienko | 9a45fac | 2014-08-27 21:36:39 +0000 | [diff] [blame] | 412 | assert(!CommandLine.empty()); |
| 413 | CommandLine[0] = MainExecutable; |
| 414 | // FIXME: We need a callback mechanism for the tool writer to output a |
| 415 | // customized message for each file. |
| 416 | DEBUG({ llvm::dbgs() << "Processing: " << File << ".\n"; }); |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 417 | ToolInvocation Invocation(std::move(CommandLine), Action, Files.get(), |
| 418 | PCHContainerOps); |
Alexander Kornienko | 9a45fac | 2014-08-27 21:36:39 +0000 | [diff] [blame] | 419 | Invocation.setDiagnosticConsumer(DiagConsumer); |
Benjamin Kramer | c4cb3b1 | 2015-10-09 09:54:37 +0000 | [diff] [blame] | 420 | |
Alexander Kornienko | 9a45fac | 2014-08-27 21:36:39 +0000 | [diff] [blame] | 421 | if (!Invocation.run()) { |
| 422 | // FIXME: Diagnostics should be used instead. |
| 423 | llvm::errs() << "Error while processing " << File << ".\n"; |
| 424 | ProcessingFailed = true; |
| 425 | } |
Alexander Kornienko | c48a535 | 2014-11-10 15:42:31 +0000 | [diff] [blame] | 426 | // Return to the initial directory to correctly resolve next file by |
| 427 | // relative path. |
Benjamin Kramer | c4cb3b1 | 2015-10-09 09:54:37 +0000 | [diff] [blame] | 428 | if (OverlayFileSystem->setCurrentWorkingDirectory(InitialDirectory.c_str())) |
Alexander Kornienko | c48a535 | 2014-11-10 15:42:31 +0000 | [diff] [blame] | 429 | llvm::report_fatal_error("Cannot chdir into \"" + |
| 430 | Twine(InitialDirectory) + "\n!"); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | return ProcessingFailed ? 1 : 0; |
| 434 | } |
| 435 | |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 436 | namespace { |
| 437 | |
| 438 | class ASTBuilderAction : public ToolAction { |
David Blaikie | 39808ff | 2014-04-25 14:49:37 +0000 | [diff] [blame] | 439 | std::vector<std::unique_ptr<ASTUnit>> &ASTs; |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 440 | |
| 441 | public: |
David Blaikie | 39808ff | 2014-04-25 14:49:37 +0000 | [diff] [blame] | 442 | ASTBuilderAction(std::vector<std::unique_ptr<ASTUnit>> &ASTs) : ASTs(ASTs) {} |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 443 | |
Manuel Klimek | 6408301 | 2013-11-07 23:18:05 +0000 | [diff] [blame] | 444 | bool runInvocation(CompilerInvocation *Invocation, FileManager *Files, |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 445 | std::shared_ptr<PCHContainerOperations> PCHContainerOps, |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 446 | DiagnosticConsumer *DiagConsumer) override { |
David Blaikie | 103a2de | 2014-04-25 17:01:33 +0000 | [diff] [blame] | 447 | std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromCompilerInvocation( |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 448 | Invocation, PCHContainerOps, |
| 449 | CompilerInstance::createDiagnostics(&Invocation->getDiagnosticOpts(), |
| 450 | DiagConsumer, |
Benjamin Kramer | bc63290 | 2015-10-06 14:45:20 +0000 | [diff] [blame] | 451 | /*ShouldOwnClient=*/false), |
| 452 | Files); |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 453 | if (!AST) |
| 454 | return false; |
| 455 | |
David Blaikie | 39808ff | 2014-04-25 14:49:37 +0000 | [diff] [blame] | 456 | ASTs.push_back(std::move(AST)); |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 457 | return true; |
| 458 | } |
| 459 | }; |
| 460 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 461 | } |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 462 | |
David Blaikie | 39808ff | 2014-04-25 14:49:37 +0000 | [diff] [blame] | 463 | int ClangTool::buildASTs(std::vector<std::unique_ptr<ASTUnit>> &ASTs) { |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 464 | ASTBuilderAction Action(ASTs); |
| 465 | return run(&Action); |
| 466 | } |
| 467 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 468 | std::unique_ptr<ASTUnit> |
| 469 | buildASTFromCode(const Twine &Code, const Twine &FileName, |
| 470 | std::shared_ptr<PCHContainerOperations> PCHContainerOps) { |
| 471 | return buildASTFromCodeWithArgs(Code, std::vector<std::string>(), FileName, |
| 472 | PCHContainerOps); |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 475 | std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs( |
| 476 | const Twine &Code, const std::vector<std::string> &Args, |
| 477 | const Twine &FileName, |
| 478 | std::shared_ptr<PCHContainerOperations> PCHContainerOps) { |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 479 | SmallString<16> FileNameStorage; |
| 480 | StringRef FileNameRef = FileName.toNullTerminatedStringRef(FileNameStorage); |
| 481 | |
David Blaikie | 39808ff | 2014-04-25 14:49:37 +0000 | [diff] [blame] | 482 | std::vector<std::unique_ptr<ASTUnit>> ASTs; |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 483 | ASTBuilderAction Action(ASTs); |
Benjamin Kramer | c4cb3b1 | 2015-10-09 09:54:37 +0000 | [diff] [blame] | 484 | llvm::IntrusiveRefCntPtr<vfs::OverlayFileSystem> OverlayFileSystem( |
| 485 | new vfs::OverlayFileSystem(vfs::getRealFileSystem())); |
| 486 | llvm::IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem( |
| 487 | new vfs::InMemoryFileSystem); |
| 488 | OverlayFileSystem->pushOverlay(InMemoryFileSystem); |
Benjamin Kramer | fa3dcf2 | 2015-10-06 15:04:13 +0000 | [diff] [blame] | 489 | llvm::IntrusiveRefCntPtr<FileManager> Files( |
Benjamin Kramer | c4cb3b1 | 2015-10-09 09:54:37 +0000 | [diff] [blame] | 490 | new FileManager(FileSystemOptions(), OverlayFileSystem)); |
Craig Topper | ccbc35e | 2014-05-20 04:51:16 +0000 | [diff] [blame] | 491 | ToolInvocation Invocation(getSyntaxOnlyToolArgs(Args, FileNameRef), &Action, |
Benjamin Kramer | fa3dcf2 | 2015-10-06 15:04:13 +0000 | [diff] [blame] | 492 | Files.get(), PCHContainerOps); |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 493 | |
| 494 | SmallString<1024> CodeStorage; |
Benjamin Kramer | c4cb3b1 | 2015-10-09 09:54:37 +0000 | [diff] [blame] | 495 | InMemoryFileSystem->addFile(FileNameRef, 0, |
| 496 | llvm::MemoryBuffer::getMemBuffer( |
| 497 | Code.toNullTerminatedStringRef(CodeStorage))); |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 498 | if (!Invocation.run()) |
Craig Topper | ccbc35e | 2014-05-20 04:51:16 +0000 | [diff] [blame] | 499 | return nullptr; |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 500 | |
| 501 | assert(ASTs.size() == 1); |
David Blaikie | 103a2de | 2014-04-25 17:01:33 +0000 | [diff] [blame] | 502 | return std::move(ASTs[0]); |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 505 | } // end namespace tooling |
| 506 | } // end namespace clang |