[clang] 119d223 - [clang] Add -fdiagnostics-format=sarif option for future SARIF output
Christopher Di Bella via cfe-commits
cfe-commits at lists.llvm.org
Thu Jul 21 09:52:05 PDT 2022
Author: Abraham Corea Diaz
Date: 2022-07-21T16:51:15Z
New Revision: 119d22310bd622d4b39e9760667b6fa58d15a8c5
URL: https://github.com/llvm/llvm-project/commit/119d22310bd622d4b39e9760667b6fa58d15a8c5
DIFF: https://github.com/llvm/llvm-project/commit/119d22310bd622d4b39e9760667b6fa58d15a8c5.diff
LOG: [clang] Add -fdiagnostics-format=sarif option for future SARIF output
Adds `sarif` option to the existing `-fdiagnostics-format` flag
for intended future work with SARIF diagnostics. Currently issues a warning
against the use of diagnostics in SARIF mode, then defaults to clang style for
diagnostics.
Reviewed By: cjdb, denik, aaron.ballman
Differential Revision: https://reviews.llvm.org/D129886
Added:
clang/test/Driver/fdiagnostics-format-sarif.cpp
Modified:
clang/include/clang/Basic/DiagnosticDriverKinds.td
clang/include/clang/Basic/DiagnosticOptions.h
clang/include/clang/Driver/Options.td
clang/lib/Driver/ToolChains/Clang.cpp
clang/lib/Frontend/TextDiagnostic.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index 68685baf76331..3c5f7e087de85 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -678,4 +678,8 @@ def err_drv_invalid_format_dxil_validator_version : Error<
def err_drv_invalid_empty_dxil_validator_version : Error<
"invalid validator version : %0\n"
"If validator major version is 0, minor version must also be 0.">;
+
+def warn_drv_sarif_format_unstable : Warning<
+ "diagnostic formatting in SARIF mode is currently unstable">,
+ InGroup<DiagGroup<"sarif-format-unstable">>;
}
diff --git a/clang/include/clang/Basic/DiagnosticOptions.h b/clang/include/clang/Basic/DiagnosticOptions.h
index 17533b38ff5f3..c4134835b5dec 100644
--- a/clang/include/clang/Basic/DiagnosticOptions.h
+++ b/clang/include/clang/Basic/DiagnosticOptions.h
@@ -74,7 +74,7 @@ class DiagnosticOptions : public RefCountedBase<DiagnosticOptions>{
friend class CompilerInvocation;
public:
- enum TextDiagnosticFormat { Clang, MSVC, Vi };
+ enum TextDiagnosticFormat { Clang, MSVC, Vi, SARIF };
// Default values.
enum {
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 3bd3550c9c60f..b9c2e4d528e44 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -5588,8 +5588,8 @@ def diagnostic_serialized_file : Separate<["-"], "serialize-diagnostic-file">,
def fdiagnostics_format : Separate<["-"], "fdiagnostics-format">,
HelpText<"Change diagnostic formatting to match IDE and command line tools">,
- Values<"clang,msvc,vi">,
- NormalizedValuesScope<"DiagnosticOptions">, NormalizedValues<["Clang", "MSVC", "Vi"]>,
+ Values<"clang,msvc,vi,sarif,SARIF">,
+ NormalizedValuesScope<"DiagnosticOptions">, NormalizedValues<["Clang", "MSVC", "Vi", "SARIF", "SARIF"]>,
MarshallingInfoEnum<DiagnosticOpts<"Format">, "Clang">;
def fdiagnostics_show_category : Separate<["-"], "fdiagnostics-show-category">,
HelpText<"Print diagnostic category">,
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 6337a996a9ace..3ad08ad7e11d1 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -4007,6 +4007,9 @@ static void RenderDiagnosticsOptions(const Driver &D, const ArgList &Args,
if (const Arg *A = Args.getLastArg(options::OPT_fdiagnostics_format_EQ)) {
CmdArgs.push_back("-fdiagnostics-format");
CmdArgs.push_back(A->getValue());
+ if (StringRef(A->getValue()) == "sarif" ||
+ StringRef(A->getValue()) == "SARIF")
+ D.Diag(diag::warn_drv_sarif_format_unstable);
}
if (const Arg *A = Args.getLastArg(
diff --git a/clang/lib/Frontend/TextDiagnostic.cpp b/clang/lib/Frontend/TextDiagnostic.cpp
index 6c0ea0cde3589..ab0dbcef65344 100644
--- a/clang/lib/Frontend/TextDiagnostic.cpp
+++ b/clang/lib/Frontend/TextDiagnostic.cpp
@@ -815,6 +815,7 @@ void TextDiagnostic::emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc,
emitFilename(PLoc.getFilename(), Loc.getManager());
switch (DiagOpts->getFormat()) {
+ case DiagnosticOptions::SARIF:
case DiagnosticOptions::Clang:
if (DiagOpts->ShowLine)
OS << ':' << LineNo;
@@ -837,6 +838,7 @@ void TextDiagnostic::emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc,
OS << ColNo;
}
switch (DiagOpts->getFormat()) {
+ case DiagnosticOptions::SARIF:
case DiagnosticOptions::Clang:
case DiagnosticOptions::Vi: OS << ':'; break;
case DiagnosticOptions::MSVC:
diff --git a/clang/test/Driver/fdiagnostics-format-sarif.cpp b/clang/test/Driver/fdiagnostics-format-sarif.cpp
new file mode 100644
index 0000000000000..f7e2274e532b1
--- /dev/null
+++ b/clang/test/Driver/fdiagnostics-format-sarif.cpp
@@ -0,0 +1,5 @@
+// RUN: %clang -fsyntax-only -fdiagnostics-format=sarif %s -### 2>&1 | FileCheck %s --check-prefix=WARN
+// WARN: warning: diagnostic formatting in SARIF mode is currently unstable [-Wsarif-format-unstable]
+
+// RUN: %clang -fsyntax-only -fdiagnostics-format=SARIF %s -### 2>&1 | FileCheck %s --check-prefix=WARN2
+// WARN2: warning: diagnostic formatting in SARIF mode is currently unstable [-Wsarif-format-unstable]
More information about the cfe-commits
mailing list