From f4aee180860db43da336d5d5babc5e5ae9dd0510 Mon Sep 17 00:00:00 2001 From: Hans Wennborg Date: Tue, 24 Sep 2013 00:08:55 +0000 Subject: [PATCH] clang-cl: print diagnostics as "error(clang): foo" in /fallback mode This solves two problems: 1) MSBuild will not flag the build as unsuccessful just because we print an error in the output, since "error(clang):" doesn't seem to match the regex it's using. 2) It becomes more clear that the diagnostic is coming from clang as supposed to cl.exe. Differential Revision: http://llvm-reviews.chandlerc.com/D1735 llvm-svn: 191250 --- .../include/clang/Basic/DiagnosticOptions.def | 1 + clang/include/clang/Frontend/TextDiagnostic.h | 3 ++- clang/lib/Driver/Tools.cpp | 5 +++- clang/lib/Frontend/CompilerInvocation.cpp | 5 +++- clang/lib/Frontend/TextDiagnostic.cpp | 23 ++++++++++++++----- clang/lib/Frontend/TextDiagnosticPrinter.cpp | 3 ++- clang/test/Driver/cl-fallback.c | 1 + clang/test/Misc/diag-format.c | 3 ++- 8 files changed, 33 insertions(+), 11 deletions(-) diff --git a/clang/include/clang/Basic/DiagnosticOptions.def b/clang/include/clang/Basic/DiagnosticOptions.def index 41bbff2edec2..a360a5a84e98 100644 --- a/clang/include/clang/Basic/DiagnosticOptions.def +++ b/clang/include/clang/Basic/DiagnosticOptions.def @@ -72,6 +72,7 @@ DIAGOPT(VerifyDiagnostics, 1, 0) /// Check that diagnostics match the expected DIAGOPT(ElideType, 1, 0) /// Elide identical types in template diffing DIAGOPT(ShowTemplateTree, 1, 0) /// Print a template tree when diffing +DIAGOPT(CLFallbackMode, 1, 0) /// Format for clang-cl fallback mode VALUE_DIAGOPT(ErrorLimit, 32, 0) /// Limit # errors emitted. /// Limit depth of macro expansion backtrace. diff --git a/clang/include/clang/Frontend/TextDiagnostic.h b/clang/include/clang/Frontend/TextDiagnostic.h index 656aa57e6e5a..c8d01b0b15eb 100644 --- a/clang/include/clang/Frontend/TextDiagnostic.h +++ b/clang/include/clang/Frontend/TextDiagnostic.h @@ -51,7 +51,8 @@ public: /// TextDiagnostic logic requires. static void printDiagnosticLevel(raw_ostream &OS, DiagnosticsEngine::Level Level, - bool ShowColors); + bool ShowColors, + bool CLFallbackMode = false); /// \brief Pretty-print a diagnostic message to a raw_ostream. /// diff --git a/clang/lib/Driver/Tools.cpp b/clang/lib/Driver/Tools.cpp index 4302f1a9cd0c..e0ffd9ac66ec 100644 --- a/clang/lib/Driver/Tools.cpp +++ b/clang/lib/Driver/Tools.cpp @@ -3783,7 +3783,10 @@ void Clang::AddClangCLArgs(const ArgList &Args, ArgStringList &CmdArgs) const { if (!Args.hasArg(options::OPT_fdiagnostics_format_EQ)) { CmdArgs.push_back("-fdiagnostics-format"); - CmdArgs.push_back("msvc"); + if (Args.hasArg(options::OPT__SLASH_fallback)) + CmdArgs.push_back("msvc-fallback"); + else + CmdArgs.push_back("msvc"); } } diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index 1c3dd7da7c54..b13507a5ef19 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -589,7 +589,10 @@ bool clang::ParseDiagnosticArgs(DiagnosticOptions &Opts, ArgList &Args, Opts.setFormat(DiagnosticOptions::Clang); else if (Format == "msvc") Opts.setFormat(DiagnosticOptions::Msvc); - else if (Format == "vi") + else if (Format == "msvc-fallback") { + Opts.setFormat(DiagnosticOptions::Msvc); + Opts.CLFallbackMode = true; + } else if (Format == "vi") Opts.setFormat(DiagnosticOptions::Vi); else { Success = false; diff --git a/clang/lib/Frontend/TextDiagnostic.cpp b/clang/lib/Frontend/TextDiagnostic.cpp index 691ca3493a08..a2dc9537bc05 100644 --- a/clang/lib/Frontend/TextDiagnostic.cpp +++ b/clang/lib/Frontend/TextDiagnostic.cpp @@ -693,7 +693,8 @@ TextDiagnostic::emitDiagnosticMessage(SourceLocation Loc, if (DiagOpts->ShowColors) OS.resetColor(); - printDiagnosticLevel(OS, Level, DiagOpts->ShowColors); + printDiagnosticLevel(OS, Level, DiagOpts->ShowColors, + DiagOpts->CLFallbackMode); printDiagnosticMessage(OS, Level, Message, OS.tell() - StartOfLocationInfo, DiagOpts->MessageLength, DiagOpts->ShowColors); @@ -702,7 +703,8 @@ TextDiagnostic::emitDiagnosticMessage(SourceLocation Loc, /*static*/ void TextDiagnostic::printDiagnosticLevel(raw_ostream &OS, DiagnosticsEngine::Level Level, - bool ShowColors) { + bool ShowColors, + bool CLFallbackMode) { if (ShowColors) { // Print diagnostic category in bold and color switch (Level) { @@ -718,12 +720,21 @@ TextDiagnostic::printDiagnosticLevel(raw_ostream &OS, switch (Level) { case DiagnosticsEngine::Ignored: llvm_unreachable("Invalid diagnostic type"); - case DiagnosticsEngine::Note: OS << "note: "; break; - case DiagnosticsEngine::Warning: OS << "warning: "; break; - case DiagnosticsEngine::Error: OS << "error: "; break; - case DiagnosticsEngine::Fatal: OS << "fatal error: "; break; + case DiagnosticsEngine::Note: OS << "note"; break; + case DiagnosticsEngine::Warning: OS << "warning"; break; + case DiagnosticsEngine::Error: OS << "error"; break; + case DiagnosticsEngine::Fatal: OS << "fatal error"; break; } + // In clang-cl /fallback mode, print diagnostics as "error(clang):". This + // makes it more clear whether a message is coming from clang or cl.exe, + // and it prevents MSBuild from concluding that the build failed just because + // there is an "error:" in the output. + if (CLFallbackMode) + OS << "(clang)"; + + OS << ": "; + if (ShowColors) OS.resetColor(); } diff --git a/clang/lib/Frontend/TextDiagnosticPrinter.cpp b/clang/lib/Frontend/TextDiagnosticPrinter.cpp index c22798af60ac..994a8f74ed46 100644 --- a/clang/lib/Frontend/TextDiagnosticPrinter.cpp +++ b/clang/lib/Frontend/TextDiagnosticPrinter.cpp @@ -132,7 +132,8 @@ void TextDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level, // diagnostics in a context that lacks language options, a source manager, or // other infrastructure necessary when emitting more rich diagnostics. if (!Info.getLocation().isValid()) { - TextDiagnostic::printDiagnosticLevel(OS, Level, DiagOpts->ShowColors); + TextDiagnostic::printDiagnosticLevel(OS, Level, DiagOpts->ShowColors, + DiagOpts->CLFallbackMode); TextDiagnostic::printDiagnosticMessage(OS, Level, DiagMessageStream.str(), OS.tell() - StartOfLocationInfo, DiagOpts->MessageLength, diff --git a/clang/test/Driver/cl-fallback.c b/clang/test/Driver/cl-fallback.c index ce7e245adfe8..1a0d7e01fe0c 100644 --- a/clang/test/Driver/cl-fallback.c +++ b/clang/test/Driver/cl-fallback.c @@ -6,6 +6,7 @@ // RUN: %clang_cl /fallback /Dfoo=bar /Ubaz /Ifoo /O0 /Ox /GR /GR- /LD /LDd \ // RUN: /MD /MDd /MTd /MT -### -- %s 2>&1 | FileCheck %s +// CHECK: "-fdiagnostics-format" "msvc-fallback" // CHECK: || // CHECK: cl.exe // CHECK: "/c" diff --git a/clang/test/Misc/diag-format.c b/clang/test/Misc/diag-format.c index 959177b2bd42..3dc351d4290c 100644 --- a/clang/test/Misc/diag-format.c +++ b/clang/test/Misc/diag-format.c @@ -12,7 +12,7 @@ // // RUN: %clang -fsyntax-only -fno-show-column %s 2>&1 | FileCheck %s -check-prefix=NO_COLUMN // - +// RUN: not %clang -fsyntax-only -Werror -fdiagnostics-format=msvc-fallback %s 2>&1 | FileCheck %s -check-prefix=MSVC-FALLBACK @@ -31,4 +31,5 @@ // VI: {{.*}} +28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens] // MSVC_ORIG: {{.*}}(28) : warning: extra tokens at end of #endif directive [-Wextra-tokens] // NO_COLUMN: {{.*}}:28: warning: extra tokens at end of #endif directive [-Wextra-tokens] +// MSVC-FALLBACK: {{.*}}(28,7) : error(clang): extra tokens at end of #endif directive int x;