@@ -357,9 +357,9 @@ void sanitizeDiagOpts(DiagnosticOptions &DiagOpts) {
357357}
358358} // namespace
359359
360+ namespace clang ::tooling::dependencies {
360361std::unique_ptr<DiagnosticOptions>
361- clang::tooling::dependencies::createDiagOptions (
362- const std::vector<std::string> &CommandLine) {
362+ createDiagOptions (ArrayRef<std::string> CommandLine) {
363363 std::vector<const char *> CLI;
364364 for (const std::string &Arg : CommandLine)
365365 CLI.push_back (Arg.c_str ());
@@ -368,11 +368,10 @@ clang::tooling::dependencies::createDiagOptions(
368368 return DiagOpts;
369369}
370370
371- clang::tooling::dependencies::DignosticsEngineWithCCommandLineAndDiagOpts::
372- DignosticsEngineWithCCommandLineAndDiagOpts (
373- const std::vector<std::string> CommandLine,
374- IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS, DiagnosticConsumer &DC)
375- : CCommandLine(CommandLine.size(), nullptr) {
371+ DignosticsEngineWithDiagOpts::DignosticsEngineWithDiagOpts (
372+ ArrayRef<std::string> CommandLine,
373+ IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS, DiagnosticConsumer &DC) {
374+ std::vector<const char *> CCommandLine (CommandLine.size (), nullptr );
376375 llvm::transform (CommandLine, CCommandLine.begin (),
377376 [](const std::string &Str) { return Str.c_str (); });
378377 DiagOpts = CreateAndPopulateDiagOpts (CCommandLine);
@@ -382,9 +381,8 @@ clang::tooling::dependencies::DignosticsEngineWithCCommandLineAndDiagOpts::
382381}
383382
384383std::pair<std::unique_ptr<driver::Driver>, std::unique_ptr<driver::Compilation>>
385- clang::tooling::dependencies::buildCompilation (
386- ArrayRef<std::string> ArgStrs, DiagnosticsEngine &Diags,
387- IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS) {
384+ buildCompilation (ArrayRef<std::string> ArgStrs, DiagnosticsEngine &Diags,
385+ IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS) {
388386 SmallVector<const char *, 256 > Argv;
389387 Argv.reserve (ArgStrs.size ());
390388 for (const std::string &Arg : ArgStrs)
@@ -417,11 +415,26 @@ clang::tooling::dependencies::buildCompilation(
417415 return std::make_pair (std::move (Driver), std::move (Compilation));
418416}
419417
418+ std::unique_ptr<CompilerInvocation>
419+ createCompilerInvocation (ArrayRef<std::string> CommandLine,
420+ DiagnosticsEngine &Diags) {
421+ llvm::opt::ArgStringList Argv;
422+ for (const std::string &Str : ArrayRef (CommandLine).drop_front ())
423+ Argv.push_back (Str.c_str ());
424+
425+ auto Invocation = std::make_unique<CompilerInvocation>();
426+ if (!CompilerInvocation::CreateFromArgs (*Invocation, Argv, Diags)) {
427+ // FIXME: Should we just go on like cc1_main does?
428+ return nullptr ;
429+ }
430+ return Invocation;
431+ }
432+
420433std::pair<IntrusiveRefCntPtr<llvm::vfs::FileSystem>, std::vector<std::string>>
421- clang::tooling::dependencies::initVFSForTUBuferScanning (
422- IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS ,
423- const std::vector<std::string> &CommandLine, StringRef WorkingDirectory,
424- llvm::MemoryBufferRef TUBuffer) {
434+ initVFSForTUBuferScanning (IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS,
435+ ArrayRef<std::string> CommandLine ,
436+ StringRef WorkingDirectory,
437+ llvm::MemoryBufferRef TUBuffer) {
425438 // Reset what might have been modified in the previous worker invocation.
426439 BaseFS->setCurrentWorkingDirectory (WorkingDirectory);
427440
@@ -437,17 +450,16 @@ clang::tooling::dependencies::initVFSForTUBuferScanning(
437450
438451 OverlayFS->pushOverlay (InMemoryOverlay);
439452 ModifiedFS = OverlayFS;
440- auto ModifiedCommandLine = CommandLine;
453+ std::vector<std::string> ModifiedCommandLine ( CommandLine) ;
441454 ModifiedCommandLine.emplace_back (InputPath);
442455
443456 return std::make_pair (ModifiedFS, ModifiedCommandLine);
444457}
445458
446459std::pair<IntrusiveRefCntPtr<llvm::vfs::FileSystem>, std::vector<std::string>>
447- clang::tooling::dependencies::initVFSForByNameScanning (
448- IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS,
449- const std::vector<std::string> &CommandLine, StringRef WorkingDirectory,
450- StringRef ModuleName) {
460+ initVFSForByNameScanning (IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS,
461+ ArrayRef<std::string> CommandLine,
462+ StringRef WorkingDirectory, StringRef ModuleName) {
451463 // Reset what might have been modified in the previous worker invocation.
452464 BaseFS->setCurrentWorkingDirectory (WorkingDirectory);
453465
@@ -466,28 +478,13 @@ clang::tooling::dependencies::initVFSForByNameScanning(
466478 IntrusiveRefCntPtr<llvm::vfs::FileSystem> InMemoryOverlay = InMemoryFS;
467479 OverlayFS->pushOverlay (InMemoryOverlay);
468480
469- auto ModifiedCommandLine = CommandLine;
481+ std::vector<std::string> ModifiedCommandLine ( CommandLine) ;
470482 ModifiedCommandLine.emplace_back (FakeInputPath);
471483
472484 return std::make_pair (OverlayFS, ModifiedCommandLine);
473485}
474486
475- std::unique_ptr<CompilerInvocation>
476- clang::tooling::dependencies::createCompilerInvocation (
477- const std::vector<std::string> &CommandLine, DiagnosticsEngine &Diags) {
478- llvm::opt::ArgStringList Argv;
479- for (const std::string &Str : ArrayRef (CommandLine).drop_front ())
480- Argv.push_back (Str.c_str ());
481-
482- auto Invocation = std::make_unique<CompilerInvocation>();
483- if (!CompilerInvocation::CreateFromArgs (*Invocation, Argv, Diags)) {
484- // FIXME: Should we just go on like cc1_main does?
485- return nullptr ;
486- }
487- return Invocation;
488- }
489-
490- bool clang::tooling::dependencies::initializeScanCompilerInstance (
487+ bool initializeScanCompilerInstance (
491488 CompilerInstance &ScanInstance,
492489 IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
493490 DiagnosticConsumer *DiagConsumer, DependencyScanningService &Service,
@@ -557,8 +554,8 @@ bool clang::tooling::dependencies::initializeScanCompilerInstance(
557554 return true ;
558555}
559556
560- llvm::SmallVector<StringRef> clang::tooling::dependencies::computeStableDirs (
561- const CompilerInstance &ScanInstance) {
557+ llvm::SmallVector<StringRef>
558+ getInitialStableDirs ( const CompilerInstance &ScanInstance) {
562559 // Create a collection of stable directories derived from the ScanInstance
563560 // for determining whether module dependencies would fully resolve from
564561 // those directories.
@@ -570,8 +567,8 @@ llvm::SmallVector<StringRef> clang::tooling::dependencies::computeStableDirs(
570567}
571568
572569std::optional<PrebuiltModulesAttrsMap>
573- clang::tooling::dependencies:: computePrebuiltModulesASTMap (
574- CompilerInstance &ScanInstance, llvm::SmallVector<StringRef> &StableDirs) {
570+ computePrebuiltModulesASTMap (CompilerInstance &ScanInstance,
571+ llvm::SmallVector<StringRef> &StableDirs) {
575572 // Store a mapping of prebuilt module files and their properties like header
576573 // search options. This will prevent the implicit build to create duplicate
577574 // modules and will force reuse of the existing prebuilt module files
@@ -588,34 +585,36 @@ clang::tooling::dependencies::computePrebuiltModulesASTMap(
588585 return PrebuiltModulesASTMap;
589586}
590587
591- void clang::tooling::dependencies::initializeModuleDepCollector (
592- CompilerInstance &ScanInstance, std::shared_ptr<ModuleDepCollector> &MDC,
593- StringRef WorkingDirectory, DependencyConsumer &Consumer,
594- DependencyScanningService &Service, CompilerInvocation &Inv,
595- DependencyActionController &Controller,
596- PrebuiltModulesAttrsMap PrebuiltModulesASTMap,
597- llvm::SmallVector<StringRef> &StableDirs) {
598- // Create the dependency collector that will collect the produced
599- // dependencies.
600- //
601- // This also moves the existing dependency output options from the
588+ std::unique_ptr<DependencyOutputOptions>
589+ getDependencyOutputOptions (CompilerInstance &ScanInstance) {
590+ // This function moves the existing dependency output options from the
602591 // invocation to the collector. The options in the invocation are reset,
603592 // which ensures that the compiler won't create new dependency collectors,
604593 // and thus won't write out the extra '.d' files to disk.
605594 auto Opts = std::make_unique<DependencyOutputOptions>();
606-
607- // We rely on the behaviour that the ScanInstance's Invocation instance's
608- // dependency output opts are cleared here.
609- // TODO: we will need to preserve and recover the original dependency output
610- // opts if we want to reuse ScanInstance.
611595 std::swap (*Opts, ScanInstance.getInvocation ().getDependencyOutputOpts ());
612- // We need at least one -MT equivalent for the generator of make dependency
613- // files to work.
614596 if (Opts->Targets .empty ())
615597 Opts->Targets = {deduceDepTarget (ScanInstance.getFrontendOpts ().OutputFile ,
616598 ScanInstance.getFrontendOpts ().Inputs )};
617599 Opts->IncludeSystemHeaders = true ;
618600
601+ return Opts;
602+ }
603+
604+ std::shared_ptr<ModuleDepCollector> initializeScanInstanceDependencyCollector (
605+ CompilerInstance &ScanInstance,
606+ const DependencyOutputOptions &DepOutputOpts, StringRef WorkingDirectory,
607+ DependencyConsumer &Consumer, DependencyScanningService &Service,
608+ CompilerInvocation &Inv, DependencyActionController &Controller,
609+ PrebuiltModulesAttrsMap PrebuiltModulesASTMap,
610+ llvm::SmallVector<StringRef> &StableDirs) {
611+ // Create the dependency collector that will collect the produced
612+ // dependencies. May return the created ModuleDepCollector depending
613+ // on the scanning format.
614+
615+ auto Opts = std::make_unique<DependencyOutputOptions>(DepOutputOpts);
616+
617+ std::shared_ptr<ModuleDepCollector> MDC;
619618 switch (Service.getFormat ()) {
620619 case ScanningOutputFormat::Make:
621620 ScanInstance.addDependencyCollector (
@@ -630,7 +629,10 @@ void clang::tooling::dependencies::initializeModuleDepCollector(
630629 ScanInstance.addDependencyCollector (MDC);
631630 break ;
632631 }
632+
633+ return MDC;
633634}
635+ } // namespace clang::tooling::dependencies
634636
635637bool DependencyScanningAction::runInvocation (
636638 std::unique_ptr<CompilerInvocation> Invocation,
@@ -668,15 +670,17 @@ bool DependencyScanningAction::runInvocation(
668670 DepFS))
669671 return false ;
670672
671- llvm::SmallVector<StringRef> StableDirs = computeStableDirs (ScanInstance);
673+ llvm::SmallVector<StringRef> StableDirs = getInitialStableDirs (ScanInstance);
672674 auto MaybePrebuiltModulesASTMap =
673675 computePrebuiltModulesASTMap (ScanInstance, StableDirs);
674676 if (!MaybePrebuiltModulesASTMap)
675677 return false ;
676678
677- initializeModuleDepCollector (ScanInstance, MDC, WorkingDirectory, Consumer,
678- Service, OriginalInvocation, Controller,
679- *MaybePrebuiltModulesASTMap, StableDirs);
679+ auto DepOutputOpts = getDependencyOutputOptions (ScanInstance);
680+
681+ MDC = initializeScanInstanceDependencyCollector (
682+ ScanInstance, *DepOutputOpts, WorkingDirectory, Consumer, Service,
683+ OriginalInvocation, Controller, *MaybePrebuiltModulesASTMap, StableDirs);
680684
681685 std::unique_ptr<FrontendAction> Action;
682686
0 commit comments