From f0970beaf8c9aba33bb058e967537389b755e7e8 Mon Sep 17 00:00:00 2001 From: Jonas Meyer Date: Tue, 27 Feb 2024 19:18:42 +0100 Subject: [PATCH] Expose clang option `-fdiagnostics-format=` in dxc (#6357) This change exposes the clang 3.7 `-fdiagnostics-format=` flag in DXC with supported values `clang`, `msvc`, `mdvc-fallback`, and `vi`. This option allows having DXC output diagnostic messages in string formats that are recognized natively by Visual Studio. Resolves #6160 & #1811, both of which had been closed as not planned. --- include/dxc/Support/HLSLOptions.h | 1 + include/dxc/Support/HLSLOptions.td | 3 +++ lib/DxcSupport/HLSLOptions.cpp | 2 ++ tools/clang/test/DXC/fdiagnostics-format.test | 14 ++++++++++++++ tools/clang/tools/dxcompiler/dxcompilerobj.cpp | 12 ++++++++++++ 5 files changed, 32 insertions(+) create mode 100644 tools/clang/test/DXC/fdiagnostics-format.test diff --git a/include/dxc/Support/HLSLOptions.h b/include/dxc/Support/HLSLOptions.h index 34c17ed17..31fc04616 100644 --- a/include/dxc/Support/HLSLOptions.h +++ b/include/dxc/Support/HLSLOptions.h @@ -156,6 +156,7 @@ public: llvm::StringRef DefaultLinkage; // OPT_default_linkage llvm::StringRef ImportBindingTable; // OPT_import_binding_table llvm::StringRef BindingTableDefine; // OPT_binding_table_define + llvm::StringRef DiagnosticsFormat; // OPT_fdiagnostics_format unsigned DefaultTextCodePage = DXC_CP_UTF8; // OPT_encoding bool AllResourcesBound = false; // OPT_all_resources_bound diff --git a/include/dxc/Support/HLSLOptions.td b/include/dxc/Support/HLSLOptions.td index baee4225d..a1cc8a6b0 100644 --- a/include/dxc/Support/HLSLOptions.td +++ b/include/dxc/Support/HLSLOptions.td @@ -121,6 +121,9 @@ def fconstexpr_depth_EQ : Joined<["-"], "fconstexpr-depth=">, Group; def fconstexpr_steps_EQ : Joined<["-"], "fconstexpr-steps=">, Group; def fconstexpr_backtrace_limit_EQ : Joined<["-"], "fconstexpr-backtrace-limit=">, Group; +def fdiagnostics_format_EQ : Joined<["-"], "fdiagnostics-format=">, Group, + Flags<[CoreOption]>, + HelpText<"Select diagnostic message format. Supported values: clang, msvc, mdvc-fallback, vi">; def fdiagnostics_show_option : Flag<["-"], "fdiagnostics-show-option">, Group, Flags<[CoreOption]>, HelpText<"Print option name with mappable diagnostics">; def fno_diagnostics_show_option : Flag<["-"], "fno-diagnostics-show-option">, Group, diff --git a/lib/DxcSupport/HLSLOptions.cpp b/lib/DxcSupport/HLSLOptions.cpp index e21296436..5040e4064 100644 --- a/lib/DxcSupport/HLSLOptions.cpp +++ b/lib/DxcSupport/HLSLOptions.cpp @@ -538,6 +538,8 @@ int ReadDxcOpts(const OptTable *optionTable, unsigned flagsToInclude, opts.OutputReflectionFile = Args.getLastArgValue(OPT_Fre); opts.OutputRootSigFile = Args.getLastArgValue(OPT_Frs); opts.OutputShaderHashFile = Args.getLastArgValue(OPT_Fsh); + opts.DiagnosticsFormat = + Args.getLastArgValue(OPT_fdiagnostics_format_EQ, "clang"); opts.ShowOptionNames = Args.hasFlag(OPT_fdiagnostics_show_option, OPT_fno_diagnostics_show_option, true); opts.UseColor = Args.hasFlag(OPT_Cc, OPT_INVALID, false); diff --git a/tools/clang/test/DXC/fdiagnostics-format.test b/tools/clang/test/DXC/fdiagnostics-format.test new file mode 100644 index 000000000..ad1169c3e --- /dev/null +++ b/tools/clang/test/DXC/fdiagnostics-format.test @@ -0,0 +1,14 @@ +// RUN: %dxc -T ps_6_0 %s -verify +// RUN: not %dxc -T ps_6_0 %s 2>&1 | FileCheck %s --check-prefix=CLANG +// RUN: not %dxc -T ps_6_0 %s -fdiagnostics-format=clang 2>&1 | FileCheck %s --check-prefix=CLANG +// RUN: not %dxc -T ps_6_0 %s -fdiagnostics-format=msvc 2>&1 | FileCheck %s --check-prefix=MSVC +// RUN: not %dxc -T ps_6_0 %s -fdiagnostics-format=msvc-fallback 2>&1 | FileCheck %s --check-prefix=MSVC +// RUN: not %dxc -T ps_6_0 %s -fdiagnostics-format=vi 2>&1 | FileCheck %s --check-prefix=VI +// RUN: not %dxc -T ps_6_0 %s -fdiagnostics-format=nonsense 2>&1 | FileCheck %s --check-prefixes=NONSENSE,CLANG + +main() {} // expected-error{{HLSL requires a type specifier for all declarations}} + +// NONSENSE: warning: invalid option nonsense to -fdiagnostics-format: supported values are clang, msvc, msvc-fallback, and vi +// VI: +9:1: error: HLSL requires a type specifier for all declarations +// MSVC: (9,1) : error: HLSL requires a type specifier for all declarations +// CLANG: :9:1: error: HLSL requires a type specifier for all declarations \ No newline at end of file diff --git a/tools/clang/tools/dxcompiler/dxcompilerobj.cpp b/tools/clang/tools/dxcompiler/dxcompilerobj.cpp index 7b1e32380..1b1066d93 100644 --- a/tools/clang/tools/dxcompiler/dxcompilerobj.cpp +++ b/tools/clang/tools/dxcompiler/dxcompilerobj.cpp @@ -1359,6 +1359,18 @@ public: compiler.createDiagnostics(diagPrinter, false); // don't output warning to stderr/file if "/no-warnings" is present. compiler.getDiagnostics().setIgnoreAllWarnings(!Opts.OutputWarnings); + if (Opts.DiagnosticsFormat.equals_lower("msvc") || + Opts.DiagnosticsFormat.equals_lower("msvc-fallback")) + compiler.getDiagnosticOpts().setFormat(DiagnosticOptions::MSVC); + else if (Opts.DiagnosticsFormat.equals_lower("vi")) + compiler.getDiagnosticOpts().setFormat(DiagnosticOptions::Vi); + else if (!Opts.DiagnosticsFormat.equals_lower("clang")) { + auto const ID = compiler.getDiagnostics().getCustomDiagID( + clang::DiagnosticsEngine::Warning, + "invalid option %0 to -fdiagnostics-format: supported values are " + "clang, msvc, msvc-fallback, and vi"); + compiler.getDiagnostics().Report(ID) << Opts.DiagnosticsFormat; + } compiler.createFileManager(); compiler.createSourceManager(compiler.getFileManager()); compiler.setTarget(