38#include "llvm/ADT/ArrayRef.h"
39#include "llvm/ADT/IntrusiveRefCntPtr.h"
40#include "llvm/ADT/StringRef.h"
41#include "llvm/ADT/Twine.h"
42#include "llvm/Option/ArgList.h"
43#include "llvm/Option/OptTable.h"
44#include "llvm/Option/Option.h"
45#include "llvm/Support/CommandLine.h"
46#include "llvm/Support/Debug.h"
47#include "llvm/Support/ErrorHandling.h"
48#include "llvm/Support/MemoryBuffer.h"
49#include "llvm/Support/Path.h"
50#include "llvm/Support/VirtualFileSystem.h"
51#include "llvm/Support/raw_ostream.h"
52#include "llvm/TargetParser/Host.h"
57#include <system_error>
61#define DEBUG_TYPE "clang-tooling"
79 new driver::Driver(BinaryName, llvm::sys::getDefaultTargetTriple(),
80 *Diagnostics,
"clang LLVM compiler", std::move(VFS));
81 CompilerDriver->
setTitle(
"clang_based_tool");
82 return CompilerDriver;
90 bool OffloadCompilation =
false;
95 for (
const auto &Job : Jobs)
96 if (StringRef(Job.getExecutable()) ==
"clang-offload-bundler")
97 OffloadCompilation =
true;
99 if (Jobs.
size() > 1) {
100 for (
auto *A : Actions) {
103 A = *A->input_begin();
111 if (Actions.size() > 1) {
119 OffloadCompilation =
true;
125 return OffloadCompilation;
131const llvm::opt::ArgStringList *
137 return StringRef(Cmd.getCreator().getName()) ==
"clang";
141 return isSrcFile(II.getType());
146 if (IsCC1Command(Job) && llvm::all_of(Job.getInputInfos(), IsSrcFile))
147 CC1Jobs.push_back(&Job);
153 if (IsCC1Command(Job))
154 CC1Jobs.push_back(&Job);
156 if (CC1Jobs.empty() ||
159 llvm::raw_svector_ostream error_stream(error_msg);
160 Jobs.
Print(error_stream,
"; ",
true);
161 Diagnostics->
Report(diag::err_fe_expected_compiler_job)
162 << error_stream.str();
166 return &CC1Jobs[0]->getArguments();
172 const char *
const BinaryName) {
173 assert(!CC1Args.empty() &&
"Must at least contain the program name!");
183 const Twine &Code,
const Twine &
FileName,
184 std::shared_ptr<PCHContainerOperations> PCHContainerOps) {
186 std::vector<std::string>(),
FileName,
187 "clang-tool", std::move(PCHContainerOps));
193static std::vector<std::string>
195 const std::vector<std::string> &ExtraArgs,
197 std::vector<std::string> Args;
198 Args.push_back(ToolName.str());
199 Args.push_back(
"-fsyntax-only");
200 llvm::append_range(Args, ExtraArgs);
209 std::unique_ptr<FrontendAction>
ToolAction,
const Twine &Code,
211 const std::vector<std::string> &Args,
const Twine &
FileName,
212 const Twine &ToolName,
213 std::shared_ptr<PCHContainerOperations> PCHContainerOps) {
215 StringRef FileNameRef =
FileName.toNullTerminatedStringRef(FileNameStorage);
222 std::move(
ToolAction), Files.get(), std::move(PCHContainerOps));
223 return Invocation.
run();
227 std::unique_ptr<FrontendAction>
ToolAction,
const Twine &Code,
228 const std::vector<std::string> &Args,
const Twine &
FileName,
229 const Twine &ToolName,
230 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
232 auto OverlayFileSystem =
233 llvm::makeIntrusiveRefCnt<llvm::vfs::OverlayFileSystem>(
234 llvm::vfs::getRealFileSystem());
235 auto InMemoryFileSystem =
236 llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
237 OverlayFileSystem->pushOverlay(InMemoryFileSystem);
240 InMemoryFileSystem->addFile(
FileName, 0,
241 llvm::MemoryBuffer::getMemBuffer(
242 Code.toNullTerminatedStringRef(CodeStorage)));
244 for (
auto &FilenameWithContent : VirtualMappedFiles) {
245 InMemoryFileSystem->addFile(
246 FilenameWithContent.first, 0,
247 llvm::MemoryBuffer::getMemBuffer(FilenameWithContent.second));
256 StringRef RelativePath(
File);
258 RelativePath.consume_front(
"./");
261 if (
auto EC = FS.makeAbsolute(AbsolutePath))
262 return llvm::errorCodeToError(EC);
263 llvm::sys::path::native(AbsolutePath);
264 return std::string(AbsolutePath);
272 StringRef InvokedAs) {
273 if (CommandLine.empty() || InvokedAs.empty())
277 StringRef TargetOPT = Table.getOption(options::OPT_target).getPrefixedName();
279 StringRef TargetOPTLegacy =
280 Table.getOption(options::OPT_target_legacy_spelling).getPrefixedName();
282 StringRef DriverModeOPT =
283 Table.getOption(options::OPT_driver_mode).getPrefixedName();
287 bool ShouldAddTarget = TargetMode.TargetIsValid;
288 bool ShouldAddMode = TargetMode.DriverMode !=
nullptr;
290 for (
auto Token = ++CommandLine.begin();
Token != CommandLine.end();
292 StringRef TokenRef(*
Token);
293 ShouldAddTarget = ShouldAddTarget && !TokenRef.starts_with(TargetOPT) &&
294 TokenRef != TargetOPTLegacy;
295 ShouldAddMode = ShouldAddMode && !TokenRef.starts_with(DriverModeOPT);
298 CommandLine.insert(++CommandLine.begin(), TargetMode.DriverMode);
300 if (ShouldAddTarget) {
301 CommandLine.insert(++CommandLine.begin(),
302 (TargetOPT + TargetMode.TargetPrefix).str());
307 llvm::StringRef WorkingDir,
308 llvm::cl::TokenizerCallback Tokenizer,
309 llvm::vfs::FileSystem &FS) {
310 bool SeenRSPFile =
false;
312 Argv.reserve(CommandLine.size());
313 for (
auto &Arg : CommandLine) {
314 Argv.push_back(Arg.c_str());
316 SeenRSPFile |= Arg.front() ==
'@';
320 llvm::BumpPtrAllocator Alloc;
321 llvm::cl::ExpansionContext ECtx(Alloc, Tokenizer);
323 ECtx.setVFS(&FS).setCurrentDir(WorkingDir).expandResponseFiles(Argv);
327 std::vector<std::string> ExpandedArgv(Argv.begin(), Argv.end());
328 CommandLine = std::move(ExpandedArgv);
337 std::unique_ptr<FrontendAction> Action;
340 SingleFrontendActionFactory(std::unique_ptr<FrontendAction> Action)
341 : Action(std::move(Action)) {}
343 std::unique_ptr<FrontendAction>
create()
override {
344 return std::move(Action);
351 std::vector<std::string> CommandLine,
ToolAction *Action,
352 FileManager *Files, std::shared_ptr<PCHContainerOperations> PCHContainerOps)
353 : CommandLine(
std::move(CommandLine)), Action(Action), OwnsAction(
false),
354 Files(Files), PCHContainerOps(
std::move(PCHContainerOps)) {}
357 std::vector<std::string> CommandLine,
358 std::unique_ptr<FrontendAction> FAction,
FileManager *Files,
359 std::shared_ptr<PCHContainerOperations> PCHContainerOps)
360 : CommandLine(
std::move(CommandLine)),
361 Action(new SingleFrontendActionFactory(
std::move(FAction))),
362 OwnsAction(
true), Files(Files),
363 PCHContainerOps(
std::move(PCHContainerOps)) {}
371 llvm::opt::ArgStringList Argv;
372 for (
const std::string &Str : CommandLine)
373 Argv.push_back(Str.c_str());
374 const char *
const BinaryName = Argv[0];
378 std::unique_ptr<DiagnosticOptions> ParsedDiagOpts;
382 DiagOpts = &*ParsedDiagOpts;
388 Files->getVirtualFileSystem(), *DiagOpts,
389 DiagConsumer ? DiagConsumer : &DiagnosticPrinter,
false);
393 Diagnostics->setSourceManager(&
SrcMgr);
396 if (CommandLine.size() >= 2 && CommandLine[1] ==
"-cc1") {
398 std::unique_ptr<CompilerInvocation> Invocation(
400 if (Diagnostics->hasErrorOccurred())
402 return Action->runInvocation(std::move(Invocation), Files,
403 std::move(PCHContainerOps), DiagConsumer);
406 const std::unique_ptr<driver::Driver> Driver(
407 newDriver(&*Diagnostics, BinaryName, Files->getVirtualFileSystemPtr()));
412 if (!Files->getFileSystemOpts().WorkingDir.empty())
413 Driver->setCheckInputsExist(
false);
414 const std::unique_ptr<driver::Compilation> Compilation(
418 const llvm::opt::ArgStringList *
const CC1Args =
422 std::unique_ptr<CompilerInvocation> Invocation(
424 return runInvocation(BinaryName, Compilation.get(), std::move(Invocation),
425 std::move(PCHContainerOps));
428bool ToolInvocation::runInvocation(
430 std::shared_ptr<CompilerInvocation> Invocation,
431 std::shared_ptr<PCHContainerOperations> PCHContainerOps) {
433 if (Invocation->getHeaderSearchOpts().Verbose) {
434 llvm::errs() <<
"clang Invocation:\n";
436 llvm::errs() <<
"\n";
440 std::move(PCHContainerOps), DiagConsumer);
444 std::shared_ptr<CompilerInvocation> Invocation,
FileManager *Files,
445 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
448 CompilerInstance Compiler(std::move(Invocation), std::move(PCHContainerOps));
457 std::unique_ptr<FrontendAction> ScopedToolAction(
create());
464 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
467 : Compilations(Compilations), SourcePaths(SourcePaths),
468 PCHContainerOps(
std::move(PCHContainerOps)),
469 OverlayFileSystem(
llvm::makeIntrusiveRefCnt<
llvm::
vfs::OverlayFileSystem>(
472 llvm::makeIntrusiveRefCnt<
llvm::
vfs::InMemoryFileSystem>()),
475 OverlayFileSystem)) {
476 OverlayFileSystem->pushOverlay(InMemoryFileSystem);
481 Files->setVirtualFileSystem(OverlayFileSystem);
487 MappedFileContents.push_back(std::make_pair(FilePath, Content));
491 ArgsAdjuster =
combineAdjusters(std::move(ArgsAdjuster), std::move(Adjuster));
499 for (StringRef Arg : Args)
500 if (Arg.starts_with(
"-resource-dir"))
511 static int StaticSymbol;
515 if (SeenWorkingDirectories.insert(
"/").second)
516 for (
const auto &MappedFile : MappedFileContents)
517 if (llvm::sys::path::is_absolute(MappedFile.first))
518 InMemoryFileSystem->addFile(
520 llvm::MemoryBuffer::getMemBuffer(MappedFile.second));
522 bool ProcessingFailed =
false;
523 bool FileSkipped =
false;
526 std::vector<std::string> AbsolutePaths;
527 AbsolutePaths.reserve(SourcePaths.size());
528 for (
const auto &SourcePath : SourcePaths) {
531 llvm::errs() <<
"Skipping " << SourcePath
532 <<
". Error while getting an absolute path: "
533 << llvm::toString(AbsPath.takeError()) <<
"\n";
536 AbsolutePaths.push_back(std::move(*AbsPath));
540 std::string InitialWorkingDir;
541 if (
auto CWD = OverlayFileSystem->getCurrentWorkingDirectory()) {
542 InitialWorkingDir = std::move(*CWD);
544 llvm::errs() <<
"Could not get working directory: "
545 << CWD.getError().message() <<
"\n";
548 size_t NumOfTotalFiles = AbsolutePaths.size();
549 unsigned CurrentFileIndex = 0;
550 for (llvm::StringRef
File : AbsolutePaths) {
559 std::vector<CompileCommand> CompileCommandsForFile =
560 Compilations.getCompileCommands(
File);
561 if (CompileCommandsForFile.empty()) {
562 llvm::errs() <<
"Skipping " <<
File <<
". Compile command not found.\n";
566 unsigned CurrentCommandIndexForFile = 0;
571 if (Directory.empty()) {
572 llvm::errs() <<
"'directory' field of compilation database is empty; "
573 "using the current working directory instead.\n";
574 Directory = InitialWorkingDir;
584 if (OverlayFileSystem->setCurrentWorkingDirectory(Directory))
585 llvm::report_fatal_error(
"Cannot chdir into \"" + Twine(Directory) +
591 if (SeenWorkingDirectories.insert(Directory).second)
592 for (
const auto &MappedFile : MappedFileContents)
593 if (!llvm::sys::path::is_absolute(MappedFile.first))
594 InMemoryFileSystem->addFile(
596 llvm::MemoryBuffer::getMemBuffer(MappedFile.second));
601 assert(!CommandLine.empty());
613 ++CurrentCommandIndexForFile;
617 if (NumOfTotalFiles > 1 || CompileCommandsForFile.size() > 1) {
618 llvm::errs() <<
"[" << std::to_string(CurrentFileIndex) <<
"/"
619 << std::to_string(NumOfTotalFiles) <<
"]";
620 if (CompileCommandsForFile.size() > 1) {
621 llvm::errs() <<
" (" << std::to_string(CurrentCommandIndexForFile)
622 <<
"/" << std::to_string(CompileCommandsForFile.size())
625 llvm::errs() <<
" Processing file " <<
File <<
".\n";
627 ToolInvocation Invocation(std::move(CommandLine), Action, Files.get(),
631 if (!Invocation.
run()) {
633 if (PrintErrorMessage)
634 llvm::errs() <<
"Error while processing " <<
File <<
".\n";
635 ProcessingFailed =
true;
640 if (!InitialWorkingDir.empty()) {
642 OverlayFileSystem->setCurrentWorkingDirectory(InitialWorkingDir))
643 llvm::errs() <<
"Error when trying to restore working dir: "
644 << EC.message() <<
"\n";
646 return ProcessingFailed ? 1 : (FileSkipped ? 2 : 0);
652 std::vector<std::unique_ptr<ASTUnit>> &ASTs;
657 std::vector<std::unique_ptr<ASTUnit>> &ASTs,
659 : ASTs(ASTs), CaptureKind(CaptureDiagnosticsKind) {}
661 bool runInvocation(std::shared_ptr<CompilerInvocation> Invocation,
663 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
665 std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromCompilerInvocation(
666 Invocation, std::move(PCHContainerOps),
nullptr,
668 Invocation->getDiagnosticOpts(),
671 Files,
false, CaptureKind);
675 ASTs.push_back(std::move(AST));
683 ASTBuilderAction Action(ASTs);
688 this->PrintErrorMessage = PrintErrorMessage;
694std::unique_ptr<ASTUnit>
696 std::shared_ptr<PCHContainerOperations> PCHContainerOps) {
698 "clang-tool", std::move(PCHContainerOps));
702 StringRef Code,
const std::vector<std::string> &Args, StringRef
FileName,
703 StringRef ToolName, std::shared_ptr<PCHContainerOperations> PCHContainerOps,
708 std::vector<std::unique_ptr<ASTUnit>> ASTs;
710 ASTBuilderAction Action(ASTs, CaptureKind);
712 auto OverlayFileSystem =
713 llvm::makeIntrusiveRefCnt<llvm::vfs::OverlayFileSystem>(
715 auto InMemoryFileSystem =
716 llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
717 OverlayFileSystem->pushOverlay(InMemoryFileSystem);
724 &Action, Files.get(), std::move(PCHContainerOps));
727 InMemoryFileSystem->addFile(
FileName, 0,
728 llvm::MemoryBuffer::getMemBufferCopy(Code));
729 for (
auto &FilenameWithContent : VirtualMappedFiles) {
730 InMemoryFileSystem->addFile(
731 FilenameWithContent.first, 0,
732 llvm::MemoryBuffer::getMemBuffer(FilenameWithContent.second));
735 if (!Invocation.
run())
738 assert(ASTs.size() == 1);
739 return std::move(ASTs[0]);
Defines the Diagnostic-related interfaces.
Defines the Diagnostic IDs-related interfaces.
Defines the clang::FileManager interface and associated types.
Defines the clang::FileSystemOptions interface.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
void createDiagnostics(DiagnosticConsumer *Client=nullptr, bool ShouldOwnClient=true)
Create the diagnostics engine using the invocation's diagnostic options and replace any existing one ...
void setVirtualFileSystem(IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS)
Use the given file system.
void createSourceManager()
Create the source manager and replace any existing one with it.
void setFileManager(IntrusiveRefCntPtr< FileManager > Value)
Replace the current file manager.
bool ExecuteAction(FrontendAction &Act)
ExecuteAction - Execute the provided action against the compiler's CompilerInvocation object.
Helper class for holding the data necessary to invoke the compiler.
static bool CreateFromArgs(CompilerInvocation &Res, ArrayRef< const char * > CommandLineArgs, DiagnosticsEngine &Diags, const char *Argv0=nullptr)
Create a compiler invocation from a list of input options.
FrontendOptions & getFrontendOpts()
CodeGenOptions & getCodeGenOpts()
Abstract interface, implemented by clients of the front-end, which formats and prints fully processed...
Options for controlling the compiler diagnostics engine.
Concrete class used by the front-end to report problems and issues.
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Implements support for file system lookup, file system caching, and directory search management.
llvm::vfs::FileSystem & getVirtualFileSystem() const
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > getVirtualFileSystemPtr() const
Keeps track of options that affect how file operations are performed.
unsigned DisableFree
Disable memory freeing on exit.
This class handles loading and caching of source files into memory.
Token - This structure provides full information about a lexed token.
Command - An executable path/name and argument vector to execute.
Compilation - A set of tasks to perform for a single driver invocation.
ActionList & getActions()
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
void setTitle(std::string Value)
JobList - A sequence of jobs to perform.
void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote, CrashReportInfo *CrashInfo=nullptr) const
Public enums and private classes that are part of the SourceManager implementation.
SmallVector< Action *, 3 > ActionList
ActionList - Type used for lists of actions.
std::unique_ptr< DiagnosticConsumer > create(StringRef OutputFile, DiagnosticOptions &DiagOpts, bool MergeChildRecords=false)
Returns a DiagnosticConsumer that serializes diagnostics to a bitcode file.
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
std::unique_ptr< DiagnosticOptions > CreateAndPopulateDiagOpts(ArrayRef< const char * > Argv)
CaptureDiagsKind
Enumerates the available kinds for capturing diagnostics.
std::string GetResourcesPath(StringRef BinaryPath)
Get the directory where the compiler headers reside, relative to the compiler binary path BinaryPath.
const llvm::opt::OptTable & getDriverOptTable()
Diagnostic wrappers for TextAPI types for error reporting.