Skip to content

Commit c8e8cf2

Browse files
authored
Merge pull request #3183 from edymtt/libclang-deps-command-line-20210726
[20210726] rdar://81632946 [libclang][deps] Accept only driver invocations, don't modify them
2 parents 87bba67 + b46a4fa commit c8e8cf2

File tree

5 files changed

+29
-63
lines changed

5 files changed

+29
-63
lines changed

clang/include/clang/Tooling/DependencyScanning/DependencyScanningWorker.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,24 @@ namespace dependencies {
3030

3131
class DependencyScanningWorkerFilesystem;
3232

33+
/// Compilation database that holds and reports a single compile command.
34+
class SingleCommandCompilationDatabase : public CompilationDatabase {
35+
CompileCommand Command;
36+
37+
public:
38+
SingleCommandCompilationDatabase(CompileCommand Cmd)
39+
: Command(std::move(Cmd)) {}
40+
41+
std::vector<CompileCommand>
42+
getCompileCommands(StringRef FilePath) const override {
43+
return {Command};
44+
}
45+
46+
std::vector<CompileCommand> getAllCompileCommands() const override {
47+
return {Command};
48+
}
49+
};
50+
3351
class DependencyConsumer {
3452
public:
3553
virtual ~DependencyConsumer() {}
@@ -67,6 +85,11 @@ class DependencyScanningWorker {
6785
const CompilationDatabase &CDB,
6886
DependencyConsumer &Consumer);
6987

88+
/// Run the dependency scanning tool for a given clang driver invocation, and
89+
/// report the discovered dependencies to the provided consumer.
90+
///
91+
/// \returns A \c StringError with the diagnostic output if clang errors
92+
/// occurred, success otherwise.
7093
llvm::Error
7194
computeDependenciesForClangInvocation(StringRef WorkingDirectory,
7295
ArrayRef<std::string> Arguments,

clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -327,25 +327,9 @@ llvm::Error DependencyScanningWorker::computeDependencies(
327327
llvm::Error DependencyScanningWorker::computeDependenciesForClangInvocation(
328328
StringRef WorkingDirectory, ArrayRef<std::string> Arguments,
329329
DependencyConsumer &Consumer) {
330-
RealFS->setCurrentWorkingDirectory(WorkingDirectory);
331-
return runWithDiags(DiagOpts.get(), [&](DiagnosticConsumer &DC) {
332-
IntrusiveRefCntPtr<DiagnosticIDs> DiagID = new DiagnosticIDs();
333-
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
334-
DiagnosticsEngine Diags(DiagID, &*DiagOpts, &DC, /*ShouldOwnClient=*/false);
335-
336-
llvm::opt::ArgStringList CC1Args;
337-
for (const auto &Arg : Arguments)
338-
CC1Args.push_back(Arg.c_str());
339-
std::unique_ptr<CompilerInvocation> Invocation(
340-
newInvocation(&Diags, CC1Args, /*BinaryName=*/nullptr));
341-
342-
DependencyScanningAction Action(WorkingDirectory, Consumer, DepFS,
343-
PPSkipMappings.get(), Format);
344-
345-
llvm::IntrusiveRefCntPtr<FileManager> FM = Files;
346-
if (!FM)
347-
FM = new FileManager(FileSystemOptions(), RealFS);
348-
return Action.runInvocation(std::move(Invocation), FM.get(),
349-
PCHContainerOps, &DC);
350-
});
330+
std::string Input("dependency-scanner-fake-input-file");
331+
StringRef Output("dependency-scanner-fake-output-file");
332+
SingleCommandCompilationDatabase CDB(
333+
CompileCommand(WorkingDirectory, Input, Arguments, Output));
334+
return computeDependencies(Input, WorkingDirectory, CDB, Consumer);
351335
}

clang/test/Index/Core/scan-deps.m

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
// RUN: rm -rf %t.mcp
2-
// RUN: echo %S > %t.result
3-
// RUN: c-index-test core --scan-deps %S -- %clang -cc1 -I %S/Inputs/module \
4-
// RUN: -fmodules -fmodules-cache-path=%t.mcp -fimplicit-module-maps \
5-
// RUN: -o FoE.o -x objective-c %s >> %t.result
6-
// RUN: cat %t.result | sed 's/\\/\//g' | FileCheck %s
7-
81
// Use driver arguments.
92
// RUN: rm -rf %t.mcp
103
// RUN: echo %S > %t.result

clang/tools/clang-scan-deps/ClangScanDeps.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -201,24 +201,6 @@ llvm::cl::opt<bool> Verbose("v", llvm::cl::Optional,
201201

202202
} // end anonymous namespace
203203

204-
class SingleCommandCompilationDatabase : public tooling::CompilationDatabase {
205-
public:
206-
SingleCommandCompilationDatabase(tooling::CompileCommand Cmd)
207-
: Command(std::move(Cmd)) {}
208-
209-
std::vector<tooling::CompileCommand>
210-
getCompileCommands(StringRef FilePath) const override {
211-
return {Command};
212-
}
213-
214-
std::vector<tooling::CompileCommand> getAllCompileCommands() const override {
215-
return {Command};
216-
}
217-
218-
private:
219-
tooling::CompileCommand Command;
220-
};
221-
222204
/// Takes the result of a dependency scan and prints error / dependency files
223205
/// based on the result.
224206
///

clang/tools/libclang/CDependencies.cpp

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -221,23 +221,7 @@ getFileDependencies(CXDependencyScannerWorker W, int argc,
221221

222222
DependencyScanningWorker *Worker = unwrap(W);
223223

224-
std::vector<std::string> Compilation;
225-
if (StringRef(argv[1]) == "-cc1")
226-
for (int i = 2; i < argc; ++i)
227-
Compilation.push_back(argv[i]);
228-
else {
229-
// Run the driver to get -cc1 args.
230-
ArrayRef<const char *> CArgs = llvm::makeArrayRef(argv, argv+argc);
231-
IntrusiveRefCntPtr<DiagnosticsEngine>
232-
Diags(CompilerInstance::createDiagnostics(new DiagnosticOptions));
233-
auto CI = createInvocationFromCommandLine(CArgs, Diags, /*VFS=*/nullptr,
234-
/*ShouldRecoverOnErrors=*/false, &Compilation);
235-
if (!CI) {
236-
if (error)
237-
*error = cxstring::createRef("failed creating 'cc1' arguments");
238-
return nullptr;
239-
}
240-
}
224+
std::vector<std::string> Compilation{argv, argv + argc};
241225

242226
if (Worker->getFormat() == ScanningOutputFormat::Full)
243227
return getFullDependencies(Worker, Compilation, WorkingDirectory, MDC,

0 commit comments

Comments
 (0)