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