[clang] e08911e - [clang][tooling] Accept custom diagnostic options in ToolInvocation
Jan Svoboda via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 10 04:31:38 PDT 2021
Author: Jan Svoboda
Date: 2021-09-10T13:31:17+02:00
New Revision: e08911e17b2bf7030a587bbf158e6a4fe0164f38
URL: https://github.com/llvm/llvm-project/commit/e08911e17b2bf7030a587bbf158e6a4fe0164f38
DIFF: https://github.com/llvm/llvm-project/commit/e08911e17b2bf7030a587bbf158e6a4fe0164f38.diff
LOG: [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
Added:
Modified:
clang/include/clang/Tooling/Tooling.h
clang/lib/Tooling/Tooling.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/Tooling/Tooling.h b/clang/include/clang/Tooling/Tooling.h
index 73d09662562b..56880724fbe3 100644
--- a/clang/include/clang/Tooling/Tooling.h
+++ b/clang/include/clang/Tooling/Tooling.h
@@ -268,11 +268,17 @@ class ToolInvocation {
~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 @@ class ToolInvocation {
FileManager *Files;
std::shared_ptr<PCHContainerOperations> PCHContainerOps;
DiagnosticConsumer *DiagConsumer = nullptr;
+ DiagnosticOptions *DiagOpts = nullptr;
};
/// Utility to run a FrontendAction over a set of files.
diff --git a/clang/lib/Tooling/Tooling.cpp b/clang/lib/Tooling/Tooling.cpp
index 2a051c4e2b29..383fb59fd4f3 100644
--- a/clang/lib/Tooling/Tooling.cpp
+++ b/clang/lib/Tooling/Tooling.cpp
@@ -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);
More information about the cfe-commits
mailing list