hintonda created this revision.

This option helps locate the origin of a diagnostic message
by outputing the enum name and index associated with a specific
DiagID, allowing users to grep the code for the enum name directly
without having to find it in the td files first.

Additional ideas:

1. add another option to pass in the index (or enum) to force an assert or 
backtrace when a specific DiagID is seen.
2. capture __FILE__ and __LINE__ when a diagnostic is created and output it.  
This would make it easier to find the specific instance, and verify all 
instances are actually tested.  Currently, it's almost impossible to determine 
if all instances are actually tested.
3. keep track of the permutations and make sure each one is tested.


https://reviews.llvm.org/D35175

Files:
  include/clang/Basic/DiagnosticIDs.h
  include/clang/Basic/DiagnosticOptions.def
  include/clang/Driver/Options.td
  lib/Basic/DiagnosticIDs.cpp
  lib/Driver/ToolChains/Clang.cpp
  lib/Frontend/CompilerInvocation.cpp
  lib/Frontend/TextDiagnosticPrinter.cpp

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 <algorithm>
 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 << ']';
 }
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/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/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: include/clang/Driver/Options.td
===================================================================
--- include/clang/Driver/Options.td
+++ include/clang/Driver/Options.td
@@ -742,6 +742,10 @@
     Group<f_Group>,  Flags<[CC1Option]>, HelpText<"Display include stacks for diagnostic notes">;
 def fdiagnostics_format_EQ : Joined<["-"], "fdiagnostics-format=">, Group<f_clang_Group>;
 def fdiagnostics_show_category_EQ : Joined<["-"], "fdiagnostics-show-category=">, Group<f_clang_Group>;
+def fdiagnostics_show_diag_ids : Flag<["-"], "fdiagnostics-show-diag-ids">,
+    Group<f_clang_Group>, Flags<[CC1Option]>;
+def fno_diagnostics_show_diag_ids : Flag<["-"], "fno-diagnostics-show-diag-ids">,
+    Group<f_clang_Group>, Flags<[CC1Option]>;
 def fdiagnostics_show_template_tree : Flag<["-"], "fdiagnostics-show-template-tree">,
     Group<f_Group>, Flags<[CC1Option]>,
     HelpText<"Print a template comparison tree for differing templates">;
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/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();
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to