Skip to content

Commit 9d8f393

Browse files
jansvoboda11kcloudy0717
authored andcommitted
[clang][deps] Simplify consuming of the build command (llvm#169064)
This is an NFC patch that aims to simplify how the scanner calls `Consumer.handleBuildCommand()` by doing it directly in `DependencyScanningAction` instead of going through the `setLastCC1Arguments()` and `takeLastCC1Arguments()` dance with the client.
1 parent 11e438d commit 9d8f393

File tree

3 files changed

+23
-35
lines changed

3 files changed

+23
-35
lines changed

clang/lib/Tooling/DependencyScanning/DependencyScannerImpl.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ dependencies::initializeScanInstanceDependencyCollector(
638638
}
639639

640640
bool DependencyScanningAction::runInvocation(
641-
std::unique_ptr<CompilerInvocation> Invocation,
641+
std::string Executable, std::unique_ptr<CompilerInvocation> Invocation,
642642
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
643643
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
644644
DiagnosticConsumer *DiagConsumer) {
@@ -654,9 +654,12 @@ bool DependencyScanningAction::runInvocation(
654654
if (Scanned) {
655655
// Scanning runs once for the first -cc1 invocation in a chain of driver
656656
// jobs. For any dependent jobs, reuse the scanning result and just
657-
// update the LastCC1Arguments to correspond to the new invocation.
657+
// update the new invocation.
658658
// FIXME: to support multi-arch builds, each arch requires a separate scan
659-
setLastCC1Arguments(std::move(OriginalInvocation));
659+
if (MDC)
660+
MDC->applyDiscoveredDependencies(OriginalInvocation);
661+
Consumer.handleBuildCommand(
662+
{Executable, OriginalInvocation.getCC1CommandLine()});
660663
return true;
661664
}
662665

@@ -701,8 +704,12 @@ bool DependencyScanningAction::runInvocation(
701704
// ExecuteAction is responsible for calling finish.
702705
DiagConsumerFinished = true;
703706

704-
if (Result)
705-
setLastCC1Arguments(std::move(OriginalInvocation));
707+
if (Result) {
708+
if (MDC)
709+
MDC->applyDiscoveredDependencies(OriginalInvocation);
710+
Consumer.handleBuildCommand(
711+
{Executable, OriginalInvocation.getCC1CommandLine()});
712+
}
706713

707714
return Result;
708715
}

clang/lib/Tooling/DependencyScanning/DependencyScannerImpl.h

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,38 +38,23 @@ class DependencyScanningAction {
3838
std::optional<StringRef> ModuleName = std::nullopt)
3939
: Service(Service), WorkingDirectory(WorkingDirectory),
4040
Consumer(Consumer), Controller(Controller), DepFS(std::move(DepFS)) {}
41-
bool runInvocation(std::unique_ptr<CompilerInvocation> Invocation,
41+
bool runInvocation(std::string Executable,
42+
std::unique_ptr<CompilerInvocation> Invocation,
4243
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
4344
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
4445
DiagnosticConsumer *DiagConsumer);
4546

4647
bool hasScanned() const { return Scanned; }
4748
bool hasDiagConsumerFinished() const { return DiagConsumerFinished; }
4849

49-
/// Take the cc1 arguments corresponding to the most recent invocation used
50-
/// with this action. Any modifications implied by the discovered dependencies
51-
/// will have already been applied.
52-
std::vector<std::string> takeLastCC1Arguments() {
53-
std::vector<std::string> Result;
54-
std::swap(Result, LastCC1Arguments); // Reset LastCC1Arguments to empty.
55-
return Result;
56-
}
57-
5850
private:
59-
void setLastCC1Arguments(CompilerInvocation &&CI) {
60-
if (MDC)
61-
MDC->applyDiscoveredDependencies(CI);
62-
LastCC1Arguments = CI.getCC1CommandLine();
63-
}
64-
6551
DependencyScanningService &Service;
6652
StringRef WorkingDirectory;
6753
DependencyConsumer &Consumer;
6854
DependencyActionController &Controller;
6955
IntrusiveRefCntPtr<DependencyScanningWorkerFilesystem> DepFS;
7056
std::optional<CompilerInstance> ScanInstanceStorage;
7157
std::shared_ptr<ModuleDepCollector> MDC;
72-
std::vector<std::string> LastCC1Arguments;
7358
bool Scanned = false;
7459
bool DiagConsumerFinished = false;
7560
};

clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,14 @@ static bool createAndRunToolInvocation(
7676
DependencyScanningAction &Action,
7777
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
7878
std::shared_ptr<clang::PCHContainerOperations> &PCHContainerOps,
79-
DiagnosticsEngine &Diags, DependencyConsumer &Consumer) {
79+
DiagnosticsEngine &Diags) {
8080
auto Invocation = createCompilerInvocation(CommandLine, Diags);
8181
if (!Invocation)
8282
return false;
8383

84-
if (!Action.runInvocation(std::move(Invocation), std::move(FS),
85-
PCHContainerOps, Diags.getClient()))
86-
return false;
87-
88-
std::vector<std::string> Args = Action.takeLastCC1Arguments();
89-
Consumer.handleBuildCommand({CommandLine[0], std::move(Args)});
90-
return true;
84+
return Action.runInvocation(CommandLine[0], std::move(Invocation),
85+
std::move(FS), PCHContainerOps,
86+
Diags.getClient());
9187
}
9288

9389
bool DependencyScanningWorker::scanDependencies(
@@ -112,9 +108,9 @@ bool DependencyScanningWorker::scanDependencies(
112108

113109
bool Success = false;
114110
if (CommandLine[1] == "-cc1") {
115-
Success = createAndRunToolInvocation(
116-
CommandLine, Action, FS, PCHContainerOps,
117-
*DiagEngineWithCmdAndOpts.DiagEngine, Consumer);
111+
Success =
112+
createAndRunToolInvocation(CommandLine, Action, FS, PCHContainerOps,
113+
*DiagEngineWithCmdAndOpts.DiagEngine);
118114
} else {
119115
Success = forEachDriverJob(
120116
CommandLine, *DiagEngineWithCmdAndOpts.DiagEngine, FS,
@@ -128,7 +124,7 @@ bool DependencyScanningWorker::scanDependencies(
128124
return true;
129125
}
130126

131-
// Insert -cc1 comand line options into Argv
127+
// Insert -cc1 command line options into Argv
132128
std::vector<std::string> Argv;
133129
Argv.push_back(Cmd.getExecutable());
134130
llvm::append_range(Argv, Cmd.getArguments());
@@ -139,7 +135,7 @@ bool DependencyScanningWorker::scanDependencies(
139135
// dependency scanning filesystem.
140136
return createAndRunToolInvocation(
141137
std::move(Argv), Action, FS, PCHContainerOps,
142-
*DiagEngineWithCmdAndOpts.DiagEngine, Consumer);
138+
*DiagEngineWithCmdAndOpts.DiagEngine);
143139
});
144140
}
145141

0 commit comments

Comments
 (0)