Skip to content

[20210726] rdar://81632946 [libclang][deps] Accept only driver invocations, don't modify them #3183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ namespace dependencies {

class DependencyScanningWorkerFilesystem;

/// Compilation database that holds and reports a single compile command.
class SingleCommandCompilationDatabase : public CompilationDatabase {
CompileCommand Command;

public:
SingleCommandCompilationDatabase(CompileCommand Cmd)
: Command(std::move(Cmd)) {}

std::vector<CompileCommand>
getCompileCommands(StringRef FilePath) const override {
return {Command};
}

std::vector<CompileCommand> getAllCompileCommands() const override {
return {Command};
}
};

class DependencyConsumer {
public:
virtual ~DependencyConsumer() {}
Expand Down Expand Up @@ -67,6 +85,11 @@ class DependencyScanningWorker {
const CompilationDatabase &CDB,
DependencyConsumer &Consumer);

/// Run the dependency scanning tool for a given clang driver invocation, and
/// report the discovered dependencies to the provided consumer.
///
/// \returns A \c StringError with the diagnostic output if clang errors
/// occurred, success otherwise.
llvm::Error
computeDependenciesForClangInvocation(StringRef WorkingDirectory,
ArrayRef<std::string> Arguments,
Expand Down
26 changes: 5 additions & 21 deletions clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,25 +327,9 @@ llvm::Error DependencyScanningWorker::computeDependencies(
llvm::Error DependencyScanningWorker::computeDependenciesForClangInvocation(
StringRef WorkingDirectory, ArrayRef<std::string> Arguments,
DependencyConsumer &Consumer) {
RealFS->setCurrentWorkingDirectory(WorkingDirectory);
return runWithDiags(DiagOpts.get(), [&](DiagnosticConsumer &DC) {
IntrusiveRefCntPtr<DiagnosticIDs> DiagID = new DiagnosticIDs();
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
DiagnosticsEngine Diags(DiagID, &*DiagOpts, &DC, /*ShouldOwnClient=*/false);

llvm::opt::ArgStringList CC1Args;
for (const auto &Arg : Arguments)
CC1Args.push_back(Arg.c_str());
std::unique_ptr<CompilerInvocation> Invocation(
newInvocation(&Diags, CC1Args, /*BinaryName=*/nullptr));

DependencyScanningAction Action(WorkingDirectory, Consumer, DepFS,
PPSkipMappings.get(), Format);

llvm::IntrusiveRefCntPtr<FileManager> FM = Files;
if (!FM)
FM = new FileManager(FileSystemOptions(), RealFS);
return Action.runInvocation(std::move(Invocation), FM.get(),
PCHContainerOps, &DC);
});
std::string Input("dependency-scanner-fake-input-file");
StringRef Output("dependency-scanner-fake-output-file");
SingleCommandCompilationDatabase CDB(
CompileCommand(WorkingDirectory, Input, Arguments, Output));
return computeDependencies(Input, WorkingDirectory, CDB, Consumer);
}
7 changes: 0 additions & 7 deletions clang/test/Index/Core/scan-deps.m
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
// RUN: rm -rf %t.mcp
// RUN: echo %S > %t.result
// RUN: c-index-test core --scan-deps %S -- %clang -cc1 -I %S/Inputs/module \
// RUN: -fmodules -fmodules-cache-path=%t.mcp -fimplicit-module-maps \
// RUN: -o FoE.o -x objective-c %s >> %t.result
// RUN: cat %t.result | sed 's/\\/\//g' | FileCheck %s

// Use driver arguments.
// RUN: rm -rf %t.mcp
// RUN: echo %S > %t.result
Expand Down
18 changes: 0 additions & 18 deletions clang/tools/clang-scan-deps/ClangScanDeps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,24 +201,6 @@ llvm::cl::opt<bool> Verbose("v", llvm::cl::Optional,

} // end anonymous namespace

class SingleCommandCompilationDatabase : public tooling::CompilationDatabase {
public:
SingleCommandCompilationDatabase(tooling::CompileCommand Cmd)
: Command(std::move(Cmd)) {}

std::vector<tooling::CompileCommand>
getCompileCommands(StringRef FilePath) const override {
return {Command};
}

std::vector<tooling::CompileCommand> getAllCompileCommands() const override {
return {Command};
}

private:
tooling::CompileCommand Command;
};

/// Takes the result of a dependency scan and prints error / dependency files
/// based on the result.
///
Expand Down
18 changes: 1 addition & 17 deletions clang/tools/libclang/CDependencies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,23 +221,7 @@ getFileDependencies(CXDependencyScannerWorker W, int argc,

DependencyScanningWorker *Worker = unwrap(W);

std::vector<std::string> Compilation;
if (StringRef(argv[1]) == "-cc1")
for (int i = 2; i < argc; ++i)
Compilation.push_back(argv[i]);
else {
// Run the driver to get -cc1 args.
ArrayRef<const char *> CArgs = llvm::makeArrayRef(argv, argv+argc);
IntrusiveRefCntPtr<DiagnosticsEngine>
Diags(CompilerInstance::createDiagnostics(new DiagnosticOptions));
auto CI = createInvocationFromCommandLine(CArgs, Diags, /*VFS=*/nullptr,
/*ShouldRecoverOnErrors=*/false, &Compilation);
if (!CI) {
if (error)
*error = cxstring::createRef("failed creating 'cc1' arguments");
return nullptr;
}
}
std::vector<std::string> Compilation{argv, argv + argc};

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