Skip to content

Commit 543d560

Browse files
committed
[clang][deps] Simplify consuming of the build command
1 parent 925ce5a commit 543d560

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
@@ -84,18 +84,14 @@ static bool createAndRunToolInvocation(
8484
DependencyScanningAction &Action,
8585
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
8686
std::shared_ptr<clang::PCHContainerOperations> &PCHContainerOps,
87-
DiagnosticsEngine &Diags, DependencyConsumer &Consumer) {
87+
DiagnosticsEngine &Diags) {
8888
auto Invocation = createCompilerInvocation(CommandLine, Diags);
8989
if (!Invocation)
9090
return false;
9191

92-
if (!Action.runInvocation(std::move(Invocation), std::move(FS),
93-
PCHContainerOps, Diags.getClient()))
94-
return false;
95-
96-
std::vector<std::string> Args = Action.takeLastCC1Arguments();
97-
Consumer.handleBuildCommand({CommandLine[0], std::move(Args)});
98-
return true;
92+
return Action.runInvocation(CommandLine[0], std::move(Invocation),
93+
std::move(FS), PCHContainerOps,
94+
Diags.getClient());
9995
}
10096

10197
bool DependencyScanningWorker::scanDependencies(
@@ -109,9 +105,9 @@ bool DependencyScanningWorker::scanDependencies(
109105

110106
bool Success = false;
111107
if (CommandLine[1] == "-cc1") {
112-
Success = createAndRunToolInvocation(
113-
CommandLine, Action, FS, PCHContainerOps,
114-
*DiagEngineWithCmdAndOpts.DiagEngine, Consumer);
108+
Success =
109+
createAndRunToolInvocation(CommandLine, Action, FS, PCHContainerOps,
110+
*DiagEngineWithCmdAndOpts.DiagEngine);
115111
} else {
116112
Success = forEachDriverJob(
117113
CommandLine, *DiagEngineWithCmdAndOpts.DiagEngine, FS,
@@ -125,7 +121,7 @@ bool DependencyScanningWorker::scanDependencies(
125121
return true;
126122
}
127123

128-
// Insert -cc1 comand line options into Argv
124+
// Insert -cc1 command line options into Argv
129125
std::vector<std::string> Argv;
130126
Argv.push_back(Cmd.getExecutable());
131127
llvm::append_range(Argv, Cmd.getArguments());
@@ -136,7 +132,7 @@ bool DependencyScanningWorker::scanDependencies(
136132
// dependency scanning filesystem.
137133
return createAndRunToolInvocation(
138134
std::move(Argv), Action, FS, PCHContainerOps,
139-
*DiagEngineWithCmdAndOpts.DiagEngine, Consumer);
135+
*DiagEngineWithCmdAndOpts.DiagEngine);
140136
});
141137
}
142138

0 commit comments

Comments
 (0)