;
66using namespacetooling;
81 new driver::Driver(BinaryName, llvm::sys::getDefaultTargetTriple(),
82*Diagnostics,
"clang LLVM compiler", std::move(VFS));
83CompilerDriver->
setTitle(
"clang_based_tool");
84 returnCompilerDriver;
92 boolOffloadCompilation =
false;
97 for(
const auto&Job : Jobs)
98 if(StringRef(Job.getExecutable()) ==
"clang-offload-bundler")
99OffloadCompilation =
true;
101 if(Jobs.
size() > 1) {
102 for(
auto*A : Actions){
104 if(isa<driver::BindArchAction>(A))
105A = *A->input_begin();
106 if(isa<driver::OffloadAction>(A)) {
113assert(Actions.size() > 1);
115isa<driver::CompileJobAction>(Actions.front()) ||
118(isa<driver::BindArchAction>(Actions.front()) &&
119isa<driver::CompileJobAction>(*Actions.front()->input_begin())));
120OffloadCompilation =
true;
126 returnOffloadCompilation;
132constllvm::opt::ArgStringList *
138 returnStringRef(
Cmd.getCreator().getName()) ==
"clang";
142 returnisSrcFile(II.getType());
147 if(IsCC1Command(Job) && llvm::all_of(Job.getInputInfos(), IsSrcFile))
148CC1Jobs.push_back(&Job);
154 if(IsCC1Command(Job))
155CC1Jobs.push_back(&Job);
157 if(CC1Jobs.empty() ||
160llvm::raw_svector_ostream error_stream(error_msg);
161Jobs.
Print(error_stream,
"; ",
true);
162Diagnostics->
Report(diag::err_fe_expected_compiler_job)
163<< error_stream.str();
167 return&CC1Jobs[0]->getArguments();
173 const char*
constBinaryName) {
174assert(!CC1Args.empty() &&
"Must at least contain the program name!");
184 constTwine &Code,
constTwine &
FileName,
185std::shared_ptr<PCHContainerOperations> PCHContainerOps) {
187std::vector<std::string>(),
FileName,
188 "clang-tool", std::move(PCHContainerOps));
194staticstd::vector<std::string>
196 conststd::vector<std::string> &ExtraArgs,
198std::vector<std::string> Args;
199Args.push_back(ToolName.str());
200Args.push_back(
"-fsyntax-only");
201Args.insert(Args.end(), ExtraArgs.begin(), ExtraArgs.end());
210std::unique_ptr<FrontendAction>
ToolAction,
constTwine &Code,
212 conststd::vector<std::string> &Args,
constTwine &
FileName,
213 constTwine &ToolName,
214std::shared_ptr<PCHContainerOperations> PCHContainerOps) {
216StringRef FileNameRef =
FileName.toNullTerminatedStringRef(FileNameStorage);
223std::move(
ToolAction), Files.get(), std::move(PCHContainerOps));
224 returnInvocation.
run();
228std::unique_ptr<FrontendAction>
ToolAction,
constTwine &Code,
229 conststd::vector<std::string> &Args,
constTwine &
FileName,
230 constTwine &ToolName,
231std::shared_ptr<PCHContainerOperations> PCHContainerOps,
234 newllvm::vfs::OverlayFileSystem(llvm::vfs::getRealFileSystem()));
236 newllvm::vfs::InMemoryFileSystem);
237OverlayFileSystem->pushOverlay(InMemoryFileSystem);
240InMemoryFileSystem->addFile(
FileName, 0,
241llvm::MemoryBuffer::getMemBuffer(
242Code.toNullTerminatedStringRef(CodeStorage)));
244 for(
auto&FilenameWithContent : VirtualMappedFiles) {
245InMemoryFileSystem->addFile(
246FilenameWithContent.first, 0,
247llvm::MemoryBuffer::getMemBuffer(FilenameWithContent.second));
256StringRef RelativePath(
File);
258RelativePath.consume_front(
"./");
261 if(
autoEC = FS.makeAbsolute(AbsolutePath))
262 returnllvm::errorCodeToError(EC);
263llvm::sys::path::native(AbsolutePath);
264 returnstd::string(AbsolutePath);
272StringRef InvokedAs) {
273 if(CommandLine.empty() || InvokedAs.empty())
277StringRef TargetOPT =
278Table.getOption(driver::options::OPT_target).getPrefixedName();
280StringRef TargetOPTLegacy =
281Table.getOption(driver::options::OPT_target_legacy_spelling)
284StringRef DriverModeOPT =
285Table.getOption(driver::options::OPT_driver_mode).getPrefixedName();
289 boolShouldAddTarget = TargetMode.TargetIsValid;
290 boolShouldAddMode = TargetMode.DriverMode !=
nullptr;
292 for(
auto Token= ++CommandLine.begin();
Token!= CommandLine.end();
294StringRef TokenRef(*
Token);
295ShouldAddTarget = ShouldAddTarget && !TokenRef.starts_with(TargetOPT) &&
296TokenRef != TargetOPTLegacy;
297ShouldAddMode = ShouldAddMode && !TokenRef.starts_with(DriverModeOPT);
300CommandLine.insert(++CommandLine.begin(), TargetMode.DriverMode);
302 if(ShouldAddTarget) {
303CommandLine.insert(++CommandLine.begin(),
304(TargetOPT + TargetMode.TargetPrefix).str());
309llvm::StringRef WorkingDir,
310llvm::cl::TokenizerCallback Tokenizer,
311llvm::vfs::FileSystem &FS) {
312 boolSeenRSPFile =
false;
314Argv.reserve(CommandLine.size());
315 for(
auto&Arg : CommandLine) {
316Argv.push_back(Arg.c_str());
318SeenRSPFile |= Arg.front() ==
'@';
322llvm::BumpPtrAllocator Alloc;
323llvm::cl::ExpansionContext ECtx(Alloc, Tokenizer);
325ECtx.setVFS(&FS).setCurrentDir(WorkingDir).expandResponseFiles(Argv);
329std::vector<std::string> ExpandedArgv(Argv.begin(), Argv.end());
330CommandLine = std::move(ExpandedArgv);
339std::unique_ptr<FrontendAction> Action;
342SingleFrontendActionFactory(std::unique_ptr<FrontendAction> Action)
343: Action(
std::move(Action)) {}
345std::unique_ptr<FrontendAction>
create()
override{
346 returnstd::move(Action);
353std::vector<std::string> CommandLine,
ToolAction*Action,
354 FileManager*Files, std::shared_ptr<PCHContainerOperations> PCHContainerOps)
355: CommandLine(
std::move(CommandLine)), Action(Action), OwnsAction(
false),
356Files(Files), PCHContainerOps(
std::move(PCHContainerOps)) {}
359std::vector<std::string> CommandLine,
360std::unique_ptr<FrontendAction> FAction,
FileManager*Files,
361std::shared_ptr<PCHContainerOperations> PCHContainerOps)
362: CommandLine(
std::move(CommandLine)),
363Action(new SingleFrontendActionFactory(
std::move(FAction))),
364OwnsAction(
true), Files(Files),
365PCHContainerOps(
std::move(PCHContainerOps)) {}
373llvm::opt::ArgStringList Argv;
374 for(
conststd::string &Str : CommandLine)
375Argv.push_back(Str.c_str());
376 const char*
constBinaryName = Argv[0];
384DiagOpts = &*ParsedDiagOpts;
391DiagConsumer ? DiagConsumer : &DiagnosticPrinter,
false);
395Diagnostics->setSourceManager(&SrcMgr);
398 if(CommandLine.size() >= 2 && CommandLine[1] ==
"-cc1") {
400std::unique_ptr<CompilerInvocation> Invocation(
402 if(Diagnostics->hasErrorOccurred())
405std::move(PCHContainerOps), DiagConsumer);
408 conststd::unique_ptr<driver::Driver> Driver(
415Driver->setCheckInputsExist(
false);
416 conststd::unique_ptr<driver::Compilation> Compilation(
421&*Diagnostics, Compilation.get());
424std::unique_ptr<CompilerInvocation> Invocation(
426 returnrunInvocation(BinaryName, Compilation.get(), std::move(Invocation),
427std::move(PCHContainerOps));
430boolToolInvocation::runInvocation(
432std::shared_ptr<CompilerInvocation> Invocation,
433std::shared_ptr<PCHContainerOperations> PCHContainerOps) {
435 if(Invocation->getHeaderSearchOpts().Verbose) {
436llvm::errs() <<
"clang Invocation:\n";
437Compilation->
getJobs().
Print(llvm::errs(),
"\n",
true);
438llvm::errs() <<
"\n";
442std::move(PCHContainerOps), DiagConsumer);
446std::shared_ptr<CompilerInvocation> Invocation,
FileManager*Files,
447std::shared_ptr<PCHContainerOperations> PCHContainerOps,
457std::unique_ptr<FrontendAction> ScopedToolAction(
create());
475std::shared_ptr<PCHContainerOperations> PCHContainerOps,
478: Compilations(Compilations), SourcePaths(SourcePaths),
479PCHContainerOps(
std::move(PCHContainerOps)),
480OverlayFileSystem(new
llvm::vfs::OverlayFileSystem(
std::move(BaseFS))),
481InMemoryFileSystem(new
llvm::vfs::InMemoryFileSystem),
484OverlayFileSystem->pushOverlay(InMemoryFileSystem);
489Files->setVirtualFileSystem(OverlayFileSystem);
495MappedFileContents.push_back(std::make_pair(FilePath, Content));
499ArgsAdjuster =
combineAdjusters(std::move(ArgsAdjuster), std::move(Adjuster));
503ArgsAdjuster =
nullptr;
509 for(StringRef Arg : Args)
510 if(Arg.starts_with(
"-resource-dir"))
516.c_str())(Args,
"");
522 static intStaticSymbol;
526 if(SeenWorkingDirectories.insert(
"/").second)
527 for(
const auto&MappedFile : MappedFileContents)
528 if(llvm::sys::path::is_absolute(MappedFile.first))
529InMemoryFileSystem->addFile(
531llvm::MemoryBuffer::getMemBuffer(MappedFile.second));
533 boolProcessingFailed =
false;
534 boolFileSkipped =
false;
537std::vector<std::string> AbsolutePaths;
538AbsolutePaths.reserve(SourcePaths.size());
539 for(
const auto&SourcePath : SourcePaths) {
542llvm::errs() <<
"Skipping "<< SourcePath
543<<
". Error while getting an absolute path: " 544<< llvm::toString(AbsPath.takeError()) <<
"\n";
547AbsolutePaths.push_back(std::move(*AbsPath));
551std::string InitialWorkingDir;
552 if(
autoCWD = OverlayFileSystem->getCurrentWorkingDirectory()) {
553InitialWorkingDir = std::move(*CWD);
555llvm::errs() <<
"Could not get working directory: " 556<< CWD.getError().message() <<
"\n";
559 size_tNumOfTotalFiles = AbsolutePaths.size();
560 unsignedProcessedFileCounter = 0;
561 for(llvm::StringRef
File: AbsolutePaths) {
569std::vector<CompileCommand> CompileCommandsForFile =
571 if(CompileCommandsForFile.empty()) {
572llvm::errs() <<
"Skipping "<<
File<<
". Compile command not found.\n";
584 if(OverlayFileSystem->setCurrentWorkingDirectory(
586llvm::report_fatal_error(
"Cannot chdir into \""+
593 for(
const auto&MappedFile : MappedFileContents)
594 if(!llvm::sys::path::is_absolute(MappedFile.first))
595InMemoryFileSystem->addFile(
597llvm::MemoryBuffer::getMemBuffer(MappedFile.second));
602assert(!CommandLine.empty());
616 if(NumOfTotalFiles > 1)
617llvm::errs() <<
"["+ std::to_string(++ProcessedFileCounter) +
"/"+
618std::to_string(NumOfTotalFiles) +
619 "] Processing file "+
File 621 ToolInvocationInvocation(std::move(CommandLine), Action, Files.get(),
625 if(!Invocation.
run()) {
627 if(PrintErrorMessage)
628llvm::errs() <<
"Error while processing "<<
File<<
".\n";
629ProcessingFailed =
true;
634 if(!InitialWorkingDir.empty()) {
636OverlayFileSystem->setCurrentWorkingDirectory(InitialWorkingDir))
637llvm::errs() <<
"Error when trying to restore working dir: " 638<< EC.message() <<
"\n";
640 returnProcessingFailed ? 1 : (FileSkipped ? 2 : 0);
646std::vector<std::unique_ptr<ASTUnit>> &ASTs;
649ASTBuilderAction(std::vector<std::unique_ptr<ASTUnit>> &ASTs) : ASTs(ASTs) {}
651 boolrunInvocation(std::shared_ptr<CompilerInvocation> Invocation,
653std::shared_ptr<PCHContainerOperations> PCHContainerOps,
655std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromCompilerInvocation(
656Invocation, std::move(PCHContainerOps),
658&Invocation->getDiagnosticOpts(),
665ASTs.push_back(std::move(AST));
673ASTBuilderAction Action(ASTs);
674 return run(&Action);
678this->PrintErrorMessage = PrintErrorMessage;
684std::unique_ptr<ASTUnit>
686std::shared_ptr<PCHContainerOperations> PCHContainerOps) {
688 "clang-tool", std::move(PCHContainerOps));
692StringRef Code,
conststd::vector<std::string> &Args, StringRef
FileName,
693StringRef ToolName, std::shared_ptr<PCHContainerOperations> PCHContainerOps,
697std::vector<std::unique_ptr<ASTUnit>> ASTs;
698ASTBuilderAction Action(ASTs);
700 newllvm::vfs::OverlayFileSystem(std::move(BaseFS)));
702 newllvm::vfs::InMemoryFileSystem);
703OverlayFileSystem->pushOverlay(InMemoryFileSystem);
709&Action, Files.get(), std::move(PCHContainerOps));
712InMemoryFileSystem->addFile(
FileName, 0,
713llvm::MemoryBuffer::getMemBufferCopy(Code));
714 for(
auto&FilenameWithContent : VirtualMappedFiles) {
715InMemoryFileSystem->addFile(
716FilenameWithContent.first, 0,
717llvm::MemoryBuffer::getMemBuffer(FilenameWithContent.second));
720 if(!Invocation.
run())
723assert(ASTs.size() == 1);
724 returnstd::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 setFileManager(FileManager *Value)
Replace the current file manager and virtual file system.
void createSourceManager(FileManager &FileMgr)
Create the source manager and replace any existing one with it.
void createDiagnostics(llvm::vfs::FileSystem &VFS, DiagnosticConsumer *Client=nullptr, bool ShouldOwnClient=true)
Create the diagnostics engine using the invocation's diagnostic options and replace any existing one ...
void setInvocation(std::shared_ptr< CompilerInvocation > Value)
setInvocation - Replace the current invocation.
bool hasDiagnostics() const
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 std::string GetResourcesPath(const char *Argv0, void *MainAddr)
Get the directory where the compiler headers reside, relative to the compiler binary (found by the pa...
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.
void clearStatCache()
Removes the FileSystemStatCache object from the manager.
llvm::vfs::FileSystem & getVirtualFileSystem() const
FileSystemOptions & getFileSystemOpts()
Returns the current file system options.
Keeps track of options that affect how file operations are performed.
std::string WorkingDir
If set, paths are resolved as if the working directory was set to the value of WorkingDir.
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)
InputInfo - Wrapper for information about an input source.
JobList - A sequence of jobs to perform.
void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote, CrashReportInfo *CrashInfo=nullptr) const
const llvm::opt::OptTable & getDriverOptTable()
The JSON file list parser is used to communicate input to InstallAPI.
std::unique_ptr< DiagnosticOptions > CreateAndPopulateDiagOpts(ArrayRef< const char * > Argv)
@ Success
Template argument deduction was successful.
Diagnostic wrappers for TextAPI types for error reporting.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4