[clang][tooling] Accept custom diagnostic options in ToolInvocation

This patch allows the clients of `ToolInvocation` to provide custom diagnostic options to be used during driver -> cc1 command-line transformation and parsing.

Tests covering this functionality are in a follow-up commit. To make this testable, the `DiagnosticsEngine` needs to be properly initialized via `CompilerInstance::createDiagnostics`.

Reviewed By: dexonsmith, arphaman

Differential Revision: https://reviews.llvm.org/D108976
This commit is contained in:
Jan Svoboda 2021-09-10 12:50:51 +02:00
parent 9685631cbe
commit e08911e17b
2 changed files with 19 additions and 5 deletions

View File

@ -268,11 +268,17 @@ public:
~ToolInvocation();
/// Set a \c DiagnosticConsumer to use during parsing.
/// Set a \c DiagnosticConsumer to use during driver command-line parsing and
/// the action invocation itself.
void setDiagnosticConsumer(DiagnosticConsumer *DiagConsumer) {
this->DiagConsumer = DiagConsumer;
}
/// Set a \c DiagnosticOptions to use during driver command-line parsing.
void setDiagnosticOptions(DiagnosticOptions *DiagOpts) {
this->DiagOpts = DiagOpts;
}
/// Run the clang invocation.
///
/// \returns True if there were no errors during execution.
@ -290,6 +296,7 @@ public:
FileManager *Files;
std::shared_ptr<PCHContainerOperations> PCHContainerOps;
DiagnosticConsumer *DiagConsumer = nullptr;
DiagnosticOptions *DiagOpts = nullptr;
};
/// Utility to run a FrontendAction over a set of files.

View File

@ -343,10 +343,17 @@ bool ToolInvocation::run() {
for (const std::string &Str : CommandLine)
Argv.push_back(Str.c_str());
const char *const BinaryName = Argv[0];
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts =
CreateAndPopulateDiagOpts(Argv);
TextDiagnosticPrinter DiagnosticPrinter(
llvm::errs(), &*DiagOpts);
// Parse diagnostic options from the driver command-line only if none were
// explicitly set.
IntrusiveRefCntPtr<DiagnosticOptions> ParsedDiagOpts;
DiagnosticOptions *DiagOpts = this->DiagOpts;
if (!DiagOpts) {
ParsedDiagOpts = CreateAndPopulateDiagOpts(Argv);
DiagOpts = &*ParsedDiagOpts;
}
TextDiagnosticPrinter DiagnosticPrinter(llvm::errs(), DiagOpts);
DiagnosticsEngine Diagnostics(
IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts,
DiagConsumer ? DiagConsumer : &DiagnosticPrinter, false);