Skip to content

Commit

Permalink
[clang][deps] Use correct DiagnosticOptions for command-line handling
Browse files Browse the repository at this point in the history
In this patch the dependency scanner starts using proper `DiagnosticOptions` parsed from the actual TU command-line in order to mimic what the actual compiler would do. The actual functionality will be enabled and tested in follow-up patches. (This split is necessary to avoid temporary regression.)

Depends on D108976.

Reviewed By: dexonsmith, arphaman

Differential Revision: https://reviews.llvm.org/D108982
  • Loading branch information
jansvoboda11 committed Sep 10, 2021
1 parent 5e6c170 commit 1e760b5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
Expand Up @@ -68,7 +68,6 @@ class DependencyScanningWorker {
llvm::Optional<StringRef> ModuleName = None);

private:
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
std::shared_ptr<PCHContainerOperations> PCHContainerOps;
std::unique_ptr<ExcludedPreprocessorDirectiveSkipMapping> PPSkipMappings;

Expand Down
45 changes: 28 additions & 17 deletions clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
Expand Up @@ -276,8 +276,6 @@ class DependencyScanningAction : public tooling::ToolAction {
DependencyScanningWorker::DependencyScanningWorker(
DependencyScanningService &Service)
: Format(Service.getFormat()) {
DiagOpts = new DiagnosticOptions();

PCHContainerOps = std::make_shared<PCHContainerOperations>();
PCHContainerOps->registerReader(
std::make_unique<ObjectFilePCHContainerReader>());
Expand All @@ -302,16 +300,20 @@ DependencyScanningWorker::DependencyScanningWorker(
Files = new FileManager(FileSystemOptions(), RealFS);
}

static llvm::Error runWithDiags(
DiagnosticOptions *DiagOpts,
llvm::function_ref<bool(DiagnosticConsumer &DC)> BodyShouldSucceed) {
static llvm::Error
runWithDiags(DiagnosticOptions *DiagOpts,
llvm::function_ref<bool(DiagnosticConsumer &, DiagnosticOptions &)>
BodyShouldSucceed) {
// Avoid serializing diagnostics.
DiagOpts->DiagnosticSerializationFile.clear();

// Capture the emitted diagnostics and report them to the client
// in the case of a failure.
std::string DiagnosticOutput;
llvm::raw_string_ostream DiagnosticsOS(DiagnosticOutput);
TextDiagnosticPrinter DiagPrinter(DiagnosticsOS, DiagOpts);

if (BodyShouldSucceed(DiagPrinter))
if (BodyShouldSucceed(DiagPrinter, *DiagOpts))
return llvm::Error::success();
return llvm::make_error<llvm::StringError>(DiagnosticsOS.str(),
llvm::inconvertibleErrorCode());
Expand All @@ -338,15 +340,24 @@ llvm::Error DependencyScanningWorker::computeDependencies(
const std::vector<std::string> &FinalCommandLine =
ModifiedCommandLine ? *ModifiedCommandLine : CommandLine;

return runWithDiags(DiagOpts.get(), [&](DiagnosticConsumer &DC) {
DependencyScanningAction Action(WorkingDirectory, Consumer, DepFS,
PPSkipMappings.get(), Format, ModuleName);
// Create an invocation that uses the underlying file system to ensure that
// any file system requests that are made by the driver do not go through
// the dependency scanning filesystem.
ToolInvocation Invocation(FinalCommandLine, &Action, CurrentFiles.get(),
PCHContainerOps);
Invocation.setDiagnosticConsumer(&DC);
return Invocation.run();
});
std::vector<const char *> FinalCCommandLine(CommandLine.size(), nullptr);
llvm::transform(CommandLine, FinalCCommandLine.begin(),
[](const std::string &Str) { return Str.c_str(); });

return runWithDiags(CreateAndPopulateDiagOpts(FinalCCommandLine).release(),
[&](DiagnosticConsumer &DC, DiagnosticOptions &DiagOpts) {
DependencyScanningAction Action(
WorkingDirectory, Consumer, DepFS,
PPSkipMappings.get(), Format, ModuleName);
// Create an invocation that uses the underlying file
// system to ensure that any file system requests that
// are made by the driver do not go through the
// dependency scanning filesystem.
ToolInvocation Invocation(FinalCommandLine, &Action,
CurrentFiles.get(),
PCHContainerOps);
Invocation.setDiagnosticConsumer(&DC);
Invocation.setDiagnosticOptions(&DiagOpts);
return Invocation.run();
});
}

0 comments on commit 1e760b5

Please sign in to comment.