Skip to content

Commit b7b08e8

Browse files
committed
Address code review comments.
1 parent e076b56 commit b7b08e8

File tree

3 files changed

+96
-93
lines changed

3 files changed

+96
-93
lines changed

clang/lib/Tooling/DependencyScanning/DependencyScannerImpl.cpp

Lines changed: 66 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,9 @@ void sanitizeDiagOpts(DiagnosticOptions &DiagOpts) {
357357
}
358358
} // namespace
359359

360+
namespace clang::tooling::dependencies {
360361
std::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

384383
std::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+
420433
std::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

446459
std::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

572569
std::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

635637
bool 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

clang/lib/Tooling/DependencyScanning/DependencyScannerImpl.h

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class DependencyScanningAction {
3232
DependencyScanningAction(
3333
DependencyScanningService &Service, StringRef WorkingDirectory,
3434
DependencyConsumer &Consumer, DependencyActionController &Controller,
35-
llvm::IntrusiveRefCntPtr<DependencyScanningWorkerFilesystem> DepFS,
35+
IntrusiveRefCntPtr<DependencyScanningWorkerFilesystem> DepFS,
3636
std::optional<StringRef> ModuleName = std::nullopt)
3737
: Service(Service), WorkingDirectory(WorkingDirectory),
3838
Consumer(Consumer), Controller(Controller), DepFS(std::move(DepFS)),
@@ -65,7 +65,7 @@ class DependencyScanningAction {
6565
StringRef WorkingDirectory;
6666
DependencyConsumer &Consumer;
6767
DependencyActionController &Controller;
68-
llvm::IntrusiveRefCntPtr<DependencyScanningWorkerFilesystem> DepFS;
68+
IntrusiveRefCntPtr<DependencyScanningWorkerFilesystem> DepFS;
6969
std::optional<StringRef> ModuleName;
7070
std::optional<CompilerInstance> ScanInstanceStorage;
7171
std::shared_ptr<ModuleDepCollector> MDC;
@@ -76,18 +76,17 @@ class DependencyScanningAction {
7676

7777
// Helper functions and data types.
7878
std::unique_ptr<DiagnosticOptions>
79-
createDiagOptions(const std::vector<std::string> &CommandLine);
79+
createDiagOptions(ArrayRef<std::string> CommandLine);
8080

81-
struct DignosticsEngineWithCCommandLineAndDiagOpts {
82-
// We need to bound the lifetime of the CCommandLine and the DiagOpts
83-
// used to create the DiganosticsEngine with the DiagnosticsEngine itself.
84-
std::vector<const char *> CCommandLine;
81+
struct DignosticsEngineWithDiagOpts {
82+
// We need to bound the lifetime of the DiagOpts used to create the
83+
// DiganosticsEngine with the DiagnosticsEngine itself.
8584
std::unique_ptr<DiagnosticOptions> DiagOpts;
8685
IntrusiveRefCntPtr<DiagnosticsEngine> DiagEngine;
8786

88-
DignosticsEngineWithCCommandLineAndDiagOpts(
89-
const std::vector<std::string> CommandLine,
90-
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS, DiagnosticConsumer &DC);
87+
DignosticsEngineWithDiagOpts(ArrayRef<std::string> CommandLine,
88+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
89+
DiagnosticConsumer &DC);
9190
};
9291

9392
struct TextDiagnosticsPrinterWithOutput {
@@ -98,7 +97,7 @@ struct TextDiagnosticsPrinterWithOutput {
9897
std::unique_ptr<DiagnosticOptions> DiagOpts;
9998
TextDiagnosticPrinter DiagPrinter;
10099

101-
TextDiagnosticsPrinterWithOutput(const std::vector<std::string> &CommandLine)
100+
TextDiagnosticsPrinterWithOutput(ArrayRef<std::string> CommandLine)
102101
: DiagnosticsOS(DiagnosticOutput),
103102
DiagOpts(createDiagOptions(CommandLine)),
104103
DiagPrinter(DiagnosticsOS, *DiagOpts) {}
@@ -109,42 +108,43 @@ buildCompilation(ArrayRef<std::string> ArgStrs, DiagnosticsEngine &Diags,
109108
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS);
110109

111110
std::unique_ptr<CompilerInvocation>
112-
createCompilerInvocation(const std::vector<std::string> &CommandLine,
111+
createCompilerInvocation(ArrayRef<std::string> CommandLine,
113112
DiagnosticsEngine &Diags);
114113

115114
std::pair<IntrusiveRefCntPtr<llvm::vfs::FileSystem>, std::vector<std::string>>
116115
initVFSForTUBuferScanning(IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS,
117-
const std::vector<std::string> &CommandLine,
116+
ArrayRef<std::string> CommandLine,
118117
StringRef WorkingDirectory,
119118
llvm::MemoryBufferRef TUBuffer);
120119

121120
std::pair<IntrusiveRefCntPtr<llvm::vfs::FileSystem>, std::vector<std::string>>
122-
initVFSForByNameScanning(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS,
123-
const std::vector<std::string> &CommandLine,
121+
initVFSForByNameScanning(IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS,
122+
ArrayRef<std::string> CommandLine,
124123
StringRef WorkingDirectory, StringRef ModuleName);
125124

126125
bool initializeScanCompilerInstance(
127126
CompilerInstance &ScanInstance,
128127
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
129128
DiagnosticConsumer *DiagConsumer, DependencyScanningService &Service,
130-
llvm::IntrusiveRefCntPtr<DependencyScanningWorkerFilesystem> DepFS);
129+
IntrusiveRefCntPtr<DependencyScanningWorkerFilesystem> DepFS);
131130

132-
llvm::SmallVector<StringRef>
133-
computeStableDirs(const CompilerInstance &ScanInstance);
131+
SmallVector<StringRef>
132+
getInitialStableDirs(const CompilerInstance &ScanInstance);
134133

135134
std::optional<PrebuiltModulesAttrsMap>
136135
computePrebuiltModulesASTMap(CompilerInstance &ScanInstance,
137-
llvm::SmallVector<StringRef> &StableDirs);
138-
139-
void initializeModuleDepCollector(CompilerInstance &ScanInstance,
140-
std::shared_ptr<ModuleDepCollector> &MDC,
141-
StringRef WorkingDirectory,
142-
DependencyConsumer &Consumer,
143-
DependencyScanningService &Service,
144-
CompilerInvocation &Inv,
145-
DependencyActionController &Controller,
146-
PrebuiltModulesAttrsMap PrebuiltModulesASTMap,
147-
llvm::SmallVector<StringRef> &StableDirs);
136+
SmallVector<StringRef> &StableDirs);
137+
138+
std::unique_ptr<DependencyOutputOptions>
139+
getDependencyOutputOptions(CompilerInstance &ScanInstance);
140+
141+
std::shared_ptr<ModuleDepCollector> initializeScanInstanceDependencyCollector(
142+
CompilerInstance &ScanInstance,
143+
const DependencyOutputOptions &DepOutputOpts, StringRef WorkingDirectory,
144+
DependencyConsumer &Consumer, DependencyScanningService &Service,
145+
CompilerInvocation &Inv, DependencyActionController &Controller,
146+
PrebuiltModulesAttrsMap PrebuiltModulesASTMap,
147+
llvm::SmallVector<StringRef> &StableDirs);
148148
} // namespace dependencies
149149
} // namespace tooling
150150
} // namespace clang

clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,7 @@ bool DependencyScanningWorker::scanDependencies(
113113
DependencyConsumer &Consumer, DependencyActionController &Controller,
114114
DiagnosticConsumer &DC, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
115115
std::optional<StringRef> ModuleName) {
116-
DignosticsEngineWithCCommandLineAndDiagOpts DiagEngineWithCmdAndOpts(
117-
CommandLine, FS, DC);
116+
DignosticsEngineWithDiagOpts DiagEngineWithCmdAndOpts(CommandLine, FS, DC);
118117
DependencyScanningAction Action(Service, WorkingDirectory, Consumer,
119118
Controller, DepFS, ModuleName);
120119

0 commit comments

Comments
 (0)