Index: include/clang/Basic/DiagnosticIDs.h =================================================================== --- include/clang/Basic/DiagnosticIDs.h +++ include/clang/Basic/DiagnosticIDs.h @@ -217,6 +217,8 @@ /// or 0 if no category. static unsigned getCategoryNumberForDiag(unsigned DiagID); + static StringRef getDiagIDName(unsigned DiagID); + /// \brief Return the number of diagnostic categories. static unsigned getNumberOfCategories(); Index: include/clang/Basic/DiagnosticOptions.def =================================================================== --- include/clang/Basic/DiagnosticOptions.def +++ include/clang/Basic/DiagnosticOptions.def @@ -61,6 +61,7 @@ DIAGOPT(ShowNoteIncludeStack, 1, 0) /// Show include stacks for notes. VALUE_DIAGOPT(ShowCategories, 2, 0) /// Show categories: 0 -> none, 1 -> Number, /// 2 -> Full Name. +DIAGOPT(ShowDiagIDs, 1, 0) /// Show Diagnostic ID ENUM_DIAGOPT(Format, TextDiagnosticFormat, 2, Clang) /// Format for diagnostics: Index: include/clang/Driver/Options.td =================================================================== --- include/clang/Driver/Options.td +++ include/clang/Driver/Options.td @@ -742,6 +742,10 @@ Group, Flags<[CC1Option]>, HelpText<"Display include stacks for diagnostic notes">; def fdiagnostics_format_EQ : Joined<["-"], "fdiagnostics-format=">, Group; def fdiagnostics_show_category_EQ : Joined<["-"], "fdiagnostics-show-category=">, Group; +def fdiagnostics_show_diag_ids : Flag<["-"], "fdiagnostics-show-diag-ids">, + Group, Flags<[CC1Option]>; +def fno_diagnostics_show_diag_ids : Flag<["-"], "fno-diagnostics-show-diag-ids">, + Group, Flags<[CC1Option]>; def fdiagnostics_show_template_tree : Flag<["-"], "fdiagnostics-show-template-tree">, Group, Flags<[CC1Option]>, HelpText<"Print a template comparison tree for differing templates">; Index: lib/Basic/DiagnosticIDs.cpp =================================================================== --- lib/Basic/DiagnosticIDs.cpp +++ lib/Basic/DiagnosticIDs.cpp @@ -37,6 +37,7 @@ }; struct StaticDiagInfoRec { + const char *IDName; uint16_t DiagID; unsigned DefaultSeverity : 3; unsigned Class : 3; @@ -74,8 +75,9 @@ #define DIAG(ENUM, CLASS, DEFAULT_SEVERITY, DESC, GROUP, SFINAE, NOWERROR, \ SHOWINSYSHEADER, CATEGORY) \ { \ - diag::ENUM, DEFAULT_SEVERITY, CLASS, DiagnosticIDs::SFINAE, NOWERROR, \ - SHOWINSYSHEADER, CATEGORY, GROUP, STR_SIZE(DESC, uint16_t), DESC \ + #ENUM, diag::ENUM, DEFAULT_SEVERITY, CLASS, DiagnosticIDs::SFINAE, \ + NOWERROR, SHOWINSYSHEADER, CATEGORY, GROUP, STR_SIZE(DESC, uint16_t), \ + DESC \ } \ , #include "clang/Basic/DiagnosticCommonKinds.inc" @@ -179,6 +181,12 @@ return 0; } +StringRef DiagnosticIDs::getDiagIDName(unsigned DiagID) { + if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) + return StringRef(Info->IDName); + return StringRef(); +} + namespace { // The diagnostic category names. struct StaticDiagCategoryRec { Index: lib/Driver/ToolChains/Clang.cpp =================================================================== --- lib/Driver/ToolChains/Clang.cpp +++ lib/Driver/ToolChains/Clang.cpp @@ -4046,6 +4046,11 @@ CmdArgs.push_back(A->getValue()); } + if (Args.hasFlag(options::OPT_fdiagnostics_show_diag_ids, + options::OPT_fno_diagnostics_show_diag_ids, false)) { + CmdArgs.push_back("-fdiagnostics-show-diag-ids"); + } + if (Args.hasFlag(options::OPT_fdiagnostics_show_hotness, options::OPT_fno_diagnostics_show_hotness, false)) CmdArgs.push_back("-fdiagnostics-show-hotness"); Index: lib/Frontend/CompilerInvocation.cpp =================================================================== --- lib/Frontend/CompilerInvocation.cpp +++ lib/Frontend/CompilerInvocation.cpp @@ -1079,6 +1079,8 @@ << ShowCategory; } + Opts.ShowDiagIDs = Args.hasArg(OPT_fdiagnostics_show_diag_ids); + StringRef Format = Args.getLastArgValue(OPT_fdiagnostics_format, "clang"); if (Format == "clang") Index: lib/Frontend/TextDiagnosticPrinter.cpp =================================================================== --- lib/Frontend/TextDiagnosticPrinter.cpp +++ lib/Frontend/TextDiagnosticPrinter.cpp @@ -19,6 +19,7 @@ #include "llvm/ADT/SmallString.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/Support/Signals.h" #include using namespace clang; @@ -103,6 +104,13 @@ } } } + // If the user wants to see diagnostic ids, include it too. + if (DiagOpts.ShowDiagIDs) { + OS << (Started ? "," : " ["); + Started = true; + OS << DiagnosticIDs::getDiagIDName(Info.getID()) << ":" << Info.getID(); + } + if (Started) OS << ']'; }