27#include "llvm/ADT/ScopeExit.h"
28#include "llvm/Support/Allocator.h"
29#include "llvm/Support/Error.h"
30#include "llvm/TargetParser/Host.h"
34using namespace tooling;
35using namespace dependencies;
42 DependencyConsumerForwarder(std::unique_ptr<DependencyOutputOptions> Opts,
45 Opts(
std::move(Opts)),
C(
C) {}
48 C.handleDependencyOutputOpts(*Opts);
52 llvm::sys::path::remove_dots(CanonPath,
true);
53 llvm::sys::fs::make_absolute(WorkingDirectory, CanonPath);
54 C.handleFileDependency(CanonPath);
59 StringRef WorkingDirectory;
60 std::unique_ptr<DependencyOutputOptions> Opts;
68 if (LangOpts.Modules) {
71 Diags->
Report(diag::warn_pch_vfsoverlay_mismatch);
73 if (VFSOverlays.empty()) {
74 Diags->
Report(diag::note_pch_vfsoverlay_empty) <<
Type;
76 std::string Files = llvm::join(VFSOverlays,
"\n");
77 Diags->
Report(diag::note_pch_vfsoverlay_files) <<
Type << Files;
94 PrebuiltModuleListener(PrebuiltModuleFilesT &PrebuiltModuleFiles,
99 : PrebuiltModuleFiles(PrebuiltModuleFiles),
100 NewModuleFiles(NewModuleFiles),
101 PrebuiltModuleVFSMap(PrebuiltModuleVFSMap), ExistingHSOpts(HSOpts),
102 ExistingLangOpts(LangOpts), Diags(Diags) {}
107 if (PrebuiltModuleFiles.insert({ModuleName.str(), Filename.str()}).second)
108 NewModuleFiles.push_back(
Filename.str());
117 bool Complain)
override {
119 PrebuiltModuleVFSMap.insert(
120 {CurrentFile, llvm::StringSet<>(VFSOverlayFiles)});
121 return checkHeaderSearchPaths(
122 HSOpts, ExistingHSOpts, Complain ? &Diags :
nullptr, ExistingLangOpts);
126 PrebuiltModuleFilesT &PrebuiltModuleFiles;
132 std::string CurrentFile;
137static bool visitPrebuiltModule(StringRef PrebuiltModuleFilename,
139 PrebuiltModuleFilesT &ModuleFiles,
144 PrebuiltModuleListener Listener(ModuleFiles, Worklist, PrebuiltModuleVFSMap,
148 Listener.visitModuleFile(PrebuiltModuleFilename,
157 while (!Worklist.empty()) {
170static std::string makeObjFileName(StringRef
FileName) {
172 llvm::sys::path::replace_extension(ObjFileName,
"o");
173 return std::string(ObjFileName);
178deduceDepTarget(
const std::string &OutputFile,
180 if (OutputFile !=
"-")
183 if (InputFiles.empty() || !InputFiles.front().isFile())
184 return "clang-scan-deps\\ dependency";
186 return makeObjFileName(InputFiles.front().getFile());
192 DiagOpts.ShowCarets =
false;
203 return llvm::StringSwitch<bool>(Warning)
204 .Cases(
"pch-vfs-diff",
"error=pch-vfs-diff", false)
205 .StartsWith(
"no-error=", false)
220static std::optional<StringRef> getSimpleMacroName(StringRef Macro) {
221 StringRef Name =
Macro.split(
"=").first.ltrim(
" \t");
224 auto FinishName = [&]() -> std::optional<StringRef> {
225 StringRef SimpleName = Name.slice(0, I);
226 if (SimpleName.empty())
231 for (; I != Name.size(); ++I) {
240 if (llvm::isAlnum(Name[I]))
249 using MacroOpt = std::pair<StringRef, std::size_t>;
250 std::vector<MacroOpt> SimpleNames;
251 SimpleNames.reserve(PPOpts.
Macros.size());
252 std::size_t Index = 0;
253 for (
const auto &M : PPOpts.
Macros) {
254 auto SName = getSimpleMacroName(M.first);
258 SimpleNames.emplace_back(*SName, Index);
262 llvm::stable_sort(SimpleNames, llvm::less_first());
264 auto NewEnd = std::unique(
265 SimpleNames.rbegin(), SimpleNames.rend(),
266 [](
const MacroOpt &A,
const MacroOpt &B) { return A.first == B.first; });
267 SimpleNames.erase(SimpleNames.begin(), NewEnd.base());
270 decltype(PPOpts.
Macros) NewMacros;
271 NewMacros.reserve(SimpleNames.size());
272 for (std::size_t I = 0,
E = SimpleNames.size(); I !=
E; ++I) {
273 std::size_t OriginalIndex = SimpleNames[I].second;
275 NewMacros.push_back(std::move(PPOpts.
Macros[OriginalIndex]));
277 std::swap(PPOpts.
Macros, NewMacros);
284 DependencyScanningAction(
289 bool EagerLoadModules,
bool DisableFree,
290 std::optional<StringRef> ModuleName = std::nullopt)
291 : WorkingDirectory(WorkingDirectory), Consumer(Consumer),
292 Controller(Controller), DepFS(
std::move(DepFS)), Format(Format),
293 OptimizeArgs(OptimizeArgs), EagerLoadModules(EagerLoadModules),
294 DisableFree(DisableFree), ModuleName(ModuleName) {}
296 bool runInvocation(std::shared_ptr<CompilerInvocation> Invocation,
298 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
303 OriginalInvocation.getFrontendOpts().DisableFree = DisableFree;
304 if (
any(OptimizeArgs & ScanningOptimizations::Macros))
305 canonicalizeDefines(OriginalInvocation.getPreprocessorOpts());
312 setLastCC1Arguments(std::move(OriginalInvocation));
319 ScanInstanceStorage.emplace(std::move(PCHContainerOps));
330 auto DiagConsumerFinisher =
331 llvm::make_scope_exit([DiagConsumer]() { DiagConsumer->
finish(); });
341 any(OptimizeArgs & ScanningOptimizations::VFS);
357 if (visitPrebuiltModule(
369 if (llvm::ErrorOr<EntryRef> Entry =
370 LocalDepFS->getOrCreateFileSystemEntry(
File.getName()))
371 if (LocalDepFS->ensureDirectiveTokensArePopulated(*Entry))
372 return Entry->getDirectiveTokens();
383 auto Opts = std::make_unique<DependencyOutputOptions>();
387 if (Opts->Targets.empty())
391 Opts->IncludeSystemHeaders =
true;
394 case ScanningOutputFormat::Make:
396 std::make_shared<DependencyConsumerForwarder>(
397 std::move(Opts), WorkingDirectory, Consumer));
399 case ScanningOutputFormat::P1689:
400 case ScanningOutputFormat::Full:
401 MDC = std::make_shared<ModuleDepCollector>(
402 std::move(Opts), ScanInstance, Consumer, Controller,
403 OriginalInvocation, std::move(PrebuiltModuleVFSMap), OptimizeArgs,
404 EagerLoadModules, Format == ScanningOutputFormat::P1689);
423 std::unique_ptr<FrontendAction> Action;
426 Action = std::make_unique<GetDependenciesByModuleNameAction>(*ModuleName);
428 Action = std::make_unique<ReadPCHAndPreprocessAction>();
434 DiagConsumerFinisher.release();
438 setLastCC1Arguments(std::move(OriginalInvocation));
446 bool hasScanned()
const {
return Scanned; }
451 std::vector<std::string> takeLastCC1Arguments() {
452 std::vector<std::string>
Result;
453 std::swap(Result, LastCC1Arguments);
460 MDC->applyDiscoveredDependencies(CI);
461 LastCC1Arguments = CI.getCC1CommandLine();
465 StringRef WorkingDirectory;
471 bool EagerLoadModules;
473 std::optional<StringRef> ModuleName;
474 std::optional<CompilerInstance> ScanInstanceStorage;
475 std::shared_ptr<ModuleDepCollector> MDC;
476 std::vector<std::string> LastCC1Arguments;
477 bool Scanned =
false;
485 : Format(Service.getFormat()), OptimizeArgs(Service.getOptimizeArgs()),
486 EagerLoadModules(Service.shouldEagerLoadModules()) {
487 PCHContainerOps = std::make_shared<PCHContainerOperations>();
489 PCHContainerOps->registerReader(
490 std::make_unique<ObjectFilePCHContainerReader>());
492 PCHContainerOps->registerWriter(std::make_unique<RawPCHContainerWriter>());
508 StringRef WorkingDirectory,
const std::vector<std::string> &CommandLine,
510 std::optional<StringRef> ModuleName) {
511 std::vector<const char *> CLI;
512 for (
const std::string &Arg : CommandLine)
513 CLI.push_back(Arg.c_str());
515 sanitizeDiagOpts(*DiagOpts);
519 std::string DiagnosticOutput;
520 llvm::raw_string_ostream DiagnosticsOS(DiagnosticOutput);
524 DiagPrinter, ModuleName))
525 return llvm::Error::success();
526 return llvm::make_error<llvm::StringError>(DiagnosticsOS.str(),
527 llvm::inconvertibleErrorCode());
534 Argv.reserve(ArgStrs.size());
535 for (
const std::string &Arg : ArgStrs)
536 Argv.push_back(Arg.c_str());
540 std::unique_ptr<driver::Driver> Driver = std::make_unique<driver::Driver>(
541 Argv[0], llvm::sys::getDefaultTargetTriple(), Diags,
542 "clang LLVM compiler", FS);
543 Driver->setTitle(
"clang_based_tool");
545 llvm::BumpPtrAllocator Alloc;
550 Diags.
Report(diag::err_drv_expand_response_file)
551 << llvm::toString(std::move(
E));
555 const std::unique_ptr<driver::Compilation> Compilation(
560 if (Compilation->containsError())
571 std::vector<std::string> CommandLine, DependencyScanningAction &Action,
573 std::shared_ptr<clang::PCHContainerOperations> &PCHContainerOps,
577 std::string Executable = CommandLine[0];
582 if (!Invocation.
run())
585 std::vector<std::string> Args = Action.takeLastCC1Arguments();
591 StringRef WorkingDirectory,
const std::vector<std::string> &CommandLine,
595 BaseFS->setCurrentWorkingDirectory(WorkingDirectory);
597 std::optional<std::vector<std::string>> ModifiedCommandLine;
605 llvm::makeIntrusiveRefCnt<llvm::vfs::OverlayFileSystem>(BaseFS);
607 llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
608 InMemoryFS->setCurrentWorkingDirectory(WorkingDirectory);
609 OverlayFS->pushOverlay(InMemoryFS);
610 ModifiedFS = OverlayFS;
614 llvm::sys::fs::createUniquePath(*ModuleName +
"-%%%%%%%%.input",
617 InMemoryFS->addFile(FakeInputPath, 0, llvm::MemoryBuffer::getMemBuffer(
""));
619 ModifiedCommandLine = CommandLine;
620 ModifiedCommandLine->emplace_back(FakeInputPath);
623 const std::vector<std::string> &FinalCommandLine =
624 ModifiedCommandLine ? *ModifiedCommandLine : CommandLine;
625 auto &FinalFS = ModifiedFS ? ModifiedFS : BaseFS;
630 std::vector<const char *> FinalCCommandLine(FinalCommandLine.size(),
nullptr);
631 llvm::transform(FinalCommandLine, FinalCCommandLine.begin(),
632 [](
const std::string &Str) { return Str.c_str(); });
635 sanitizeDiagOpts(*DiagOpts);
643 Diags->setSourceManager(&SrcMgr);
647 bool DisableFree =
true;
648 DependencyScanningAction Action(WorkingDirectory, Consumer, Controller, DepFS,
649 Format, OptimizeArgs, EagerLoadModules,
650 DisableFree, ModuleName);
653 if (FinalCommandLine[1] ==
"-cc1") {
655 PCHContainerOps, *Diags, Consumer);
659 if (StringRef(
Cmd.getCreator().getName()) !=
"clang") {
662 Consumer.handleBuildCommand(
663 {Cmd.getExecutable(),
664 {Cmd.getArguments().begin(), Cmd.getArguments().end()}});
669 std::vector<std::string> Argv;
670 Argv.push_back(
Cmd.getExecutable());
671 Argv.insert(Argv.end(),
Cmd.getArguments().begin(),
672 Cmd.getArguments().end());
679 PCHContainerOps, *Diags, Consumer);
683 if (
Success && !Action.hasScanned())
684 Diags->
Report(diag::err_fe_expected_compiler_job)
685 << llvm::join(FinalCommandLine,
" ");
686 return Success && Action.hasScanned();
static bool forEachDriverJob(ArrayRef< std::string > ArgStrs, DiagnosticsEngine &Diags, FileManager &FM, llvm::function_ref< bool(const driver::Command &Cmd)> Callback)
static bool createAndRunToolInvocation(std::vector< std::string > CommandLine, DependencyScanningAction &Action, FileManager &FM, std::shared_ptr< clang::PCHContainerOperations > &PCHContainerOps, DiagnosticsEngine &Diags, DependencyConsumer &Consumer)
Abstract interface for callback invocations by the ASTReader.
virtual bool ReadHeaderSearchPaths(const HeaderSearchOptions &HSOpts, bool Complain)
Receives the header search paths.
virtual void visitImport(StringRef ModuleName, StringRef Filename)
If needsImportVisitation returns true, this is called for each AST file imported by this AST file.
virtual void visitModuleFile(StringRef Filename, serialization::ModuleKind Kind)
This is called for each AST file loaded.
virtual bool needsImportVisitation() const
Returns true if this ASTReaderListener wants to receive the imports of the AST file via visitImport,...
@ ARR_OutOfDate
The client can handle an AST file that cannot load because it is out-of-date relative to its input fi...
static bool readASTFileControlBlock(StringRef Filename, FileManager &FileMgr, const InMemoryModuleCache &ModuleCache, const PCHContainerReader &PCHContainerRdr, bool FindModuleFileExtensions, ASTReaderListener &Listener, bool ValidateDiagnosticOptions, unsigned ClientLoadCapabilities=ARR_ConfigurationMismatch|ARR_OutOfDate)
Read the control block for the named AST file.
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
void createSourceManager(FileManager &FileMgr)
Create the source manager and replace any existing one with it.
void createDiagnostics(DiagnosticConsumer *Client=nullptr, bool ShouldOwnClient=true)
Create the diagnostics engine using the invocation's diagnostic options and replace any existing one ...
FileManager * createFileManager(IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS=nullptr)
Create the file manager and replace any existing one with it.
const PCHContainerReader & getPCHContainerReader() const
Return the appropriate PCHContainerReader depending on the current CodeGenOptions.
DiagnosticsEngine & getDiagnostics() const
Get the current diagnostics engine.
void setInvocation(std::shared_ptr< CompilerInvocation > Value)
setInvocation - Replace the current invocation.
FileManager & getFileManager() const
Return the current file manager to the caller.
InMemoryModuleCache & getModuleCache() const
void addDependencyCollector(std::shared_ptr< DependencyCollector > Listener)
FrontendOptions & getFrontendOpts()
bool hasDiagnostics() const
HeaderSearchOptions & getHeaderSearchOpts()
CompilerInvocation & getInvocation()
PreprocessorOptions & getPreprocessorOpts()
bool ExecuteAction(FrontendAction &Act)
ExecuteAction - Execute the provided action against the compiler's CompilerInvocation object.
DiagnosticOptions & getDiagnosticOpts()
LangOptions & getLangOpts()
Helper class for holding the data necessary to invoke the compiler.
DependencyOutputOptions & getDependencyOutputOpts()
ArrayRef< std::string > getDependencies() const
Builds a dependency file when attached to a Preprocessor (for includes) and ASTReader (for module imp...
void finishedMainFile(DiagnosticsEngine &Diags) override
Called when the end of the main file is reached.
Abstract interface, implemented by clients of the front-end, which formats and prints fully processed...
virtual void finish()
Callback to inform the diagnostic client that processing of all source files has ended.
Options for controlling the compiler diagnostics engine.
std::vector< std::string > Warnings
The list of -W... options used to alter the diagnostic mappings, with the prefixes removed.
std::string DiagnosticSerializationFile
The file to serialize diagnostics to (non-appending).
Concrete class used by the front-end to report problems and issues.
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
bool hasErrorOccurred() const
DiagnosticOptions & getDiagnosticOptions() const
Retrieve the diagnostic options.
DiagnosticConsumer * getClient()
A reference to a FileEntry that includes the name of the file as it was accessed by the FileManager's...
Implements support for file system lookup, file system caching, and directory search management.
void AddStats(const FileManager &Other)
Import statistics from a child FileManager and add them to this current FileManager.
llvm::vfs::FileSystem & getVirtualFileSystem() const
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > getVirtualFileSystemPtr() const
Keeps track of options that affect how file operations are performed.
unsigned ModulesShareFileManager
Whether to share the FileManager when building modules.
std::string OutputFile
The output file, if any.
unsigned GenerateGlobalModuleIndex
Whether we can generate the global module index if needed.
SmallVector< FrontendInputFile, 0 > Inputs
The input files and their types.
unsigned UseGlobalModuleIndex
Whether we can use the global module index if available.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
PreprocessorOptions - This class is used for passing the various options used in preprocessor initial...
bool ModulesCheckRelocated
Perform extra checks when loading PCM files for mutable file systems.
std::string ImplicitPCHInclude
The implicit PCH included at the start of the translation unit, or empty.
bool AllowPCHWithDifferentModulesCachePath
When true, a PCH with modules cache path different to the current compilation will not be rejected.
std::vector< std::pair< std::string, bool > > Macros
std::function< std::optional< ArrayRef< dependency_directives_scan::Directive > >(FileEntryRef)> DependencyDirectivesForFile
Function for getting the dependency preprocessor directives of a file.
This class handles loading and caching of source files into memory.
The base class of the type hierarchy.
Command - An executable path/name and argument vector to execute.
llvm::StringRef getDriverMode(StringRef ProgName, ArrayRef< const char * > Args)
Returns the driver mode option's value, i.e.
llvm::Error expandResponseFiles(SmallVectorImpl< const char * > &Args, bool ClangCLMode, llvm::BumpPtrAllocator &Alloc, llvm::vfs::FileSystem *FS=nullptr)
Expand response files from a clang driver or cc1 invocation.
bool IsClangCL(StringRef DriverMode)
Checks whether the value produced by getDriverMode is for CL mode.
ModuleKind
Specifies the kind of module that has been loaded.
@ MK_ExplicitModule
File is an explicitly-loaded module.
The JSON file list parser is used to communicate input to InstallAPI.
std::unique_ptr< DiagnosticOptions > CreateAndPopulateDiagOpts(ArrayRef< const char * > Argv)
@ Result
The result type of a method or function.
IntrusiveRefCntPtr< llvm::vfs::FileSystem > createVFSFromCompilerInvocation(const CompilerInvocation &CI, DiagnosticsEngine &Diags)
@ Success
Template argument deduction was successful.
int __ovld __cnfn any(char)
Returns 1 if the most significant bit in any component of x is set; otherwise returns 0.