JDevlieghere created this revision.
JDevlieghere added a project: clang.
Herald added a subscriber: mgorny.

This patch adds a new tool to diagnostic tool called `list-warning-flags` to 
display only warnings that have a corresponding -W flag.

While we already have `list-warnings`, the output contains all internal 
diagnostic names. This is an implementation detail of the compiler and usually 
not what the users wants. I've rephrased the description of the exiting tool to 
emphasize its respective purpose.

Because the limited amount of code and complexity, as well as the absence of 
arguments in the other tools and the existing duplication in features between 
the them, it seemed reasonable to me to add this as something separate. However 
I don't know too much about diagtool and its usage so this is very much open 
for discussion.


Repository:
  rL LLVM

https://reviews.llvm.org/D37390

Files:
  tools/diagtool/CMakeLists.txt
  tools/diagtool/ListWarningFlags.cpp
  tools/diagtool/ListWarnings.cpp

Index: tools/diagtool/ListWarnings.cpp
===================================================================
--- tools/diagtool/ListWarnings.cpp
+++ tools/diagtool/ListWarnings.cpp
@@ -1,13 +1,13 @@
-//===- ListWarnings.h - diagtool tool for printing warning flags ----------===//
+//===- ListWarnings.h - diagtool tool for printing warnings ---------------===//
 //
 //                     The LLVM Compiler Infrastructure
 //
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
 //===----------------------------------------------------------------------===//
 //
-// This file provides a diagtool tool that displays warning flags for
+// This file provides a diagtool tool that displays all warning for
 // diagnostics.
 //
 //===----------------------------------------------------------------------===//
@@ -21,20 +21,20 @@
 #include "llvm/Support/Format.h"
 
 DEF_DIAGTOOL("list-warnings",
-             "List warnings and their corresponding flags",
+             "List all (internal) warnings",
              ListWarnings)
-  
+
 using namespace clang;
 using namespace diagtool;
 
 namespace {
 struct Entry {
   llvm::StringRef DiagName;
   llvm::StringRef Flag;
-  
+
   Entry(llvm::StringRef diagN, llvm::StringRef flag)
     : DiagName(diagN), Flag(flag) {}
-  
+
   bool operator<(const Entry &x) const { return DiagName < x.DiagName; }
 };
 }
@@ -52,57 +52,57 @@
 int ListWarnings::run(unsigned int argc, char **argv, llvm::raw_ostream &out) {
   std::vector<Entry> Flagged, Unflagged;
   llvm::StringMap<std::vector<unsigned> > flagHistogram;
-  
+
   ArrayRef<DiagnosticRecord> AllDiagnostics = getBuiltinDiagnosticsByName();
 
   for (ArrayRef<DiagnosticRecord>::iterator di = AllDiagnostics.begin(),
                                             de = AllDiagnostics.end();
        di != de; ++di) {
     unsigned diagID = di->DiagID;
-    
+
     if (DiagnosticIDs::isBuiltinNote(diagID))
       continue;
-        
+
     if (!DiagnosticIDs::isBuiltinWarningOrExtension(diagID))
       continue;
-  
+
     Entry entry(di->getName(),
                 DiagnosticIDs::getWarningOptionForDiag(diagID));
-    
+
     if (entry.Flag.empty())
       Unflagged.push_back(entry);
     else {
       Flagged.push_back(entry);
       flagHistogram[entry.Flag].push_back(diagID);
     }
   }
-  
+
   out << "Warnings with flags (" << Flagged.size() << "):\n";
   printEntries(Flagged, out);
-  
+
   out << "Warnings without flags (" << Unflagged.size() << "):\n";
   printEntries(Unflagged, out);
 
   out << "\nSTATISTICS:\n\n";
 
-  double percentFlagged = ((double) Flagged.size()) 
+  double percentFlagged = ((double) Flagged.size())
     / (Flagged.size() + Unflagged.size()) * 100.0;
-  
-  out << "  Percentage of warnings with flags: " 
+
+  out << "  Percentage of warnings with flags: "
       << llvm::format("%.4g",percentFlagged) << "%\n";
-  
+
   out << "  Number of unique flags: "
       << flagHistogram.size() << '\n';
-  
+
   double avgDiagsPerFlag = (double) Flagged.size() / flagHistogram.size();
   out << "  Average number of diagnostics per flag: "
       << llvm::format("%.4g", avgDiagsPerFlag) << '\n';
 
   out << "  Number in -Wpedantic (not covered by other -W flags): "
       << flagHistogram["pedantic"].size() << '\n';
 
   out << '\n';
-  
+
   return 0;
 }
 
Index: tools/diagtool/ListWarningFlags.cpp
===================================================================
--- /dev/null
+++ tools/diagtool/ListWarningFlags.cpp
@@ -0,0 +1,55 @@
+//===- ListWarningFlags.h - diagtool tool for printing warning flags ------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file provides a diagtool tool that displays only warnings that have a
+// corresponding flags for diagnostics.
+//
+//===----------------------------------------------------------------------===//
+
+#include "DiagTool.h"
+#include "DiagnosticNames.h"
+#include "clang/AST/ASTDiagnostic.h"
+#include "clang/Basic/AllDiagnostics.h"
+#include "clang/Basic/Diagnostic.h"
+#include "llvm/Support/Format.h"
+#include <algorithm>
+#include <vector>
+
+DEF_DIAGTOOL("list-warning-flags", "List only warnings with flags",
+             ListWarningFlags)
+
+using namespace clang;
+using namespace diagtool;
+
+int ListWarningFlags::run(unsigned int argc, char **argv,
+                          llvm::raw_ostream &out) {
+  std::vector<llvm::StringRef> flags;
+
+  ArrayRef<DiagnosticRecord> AllDiagnostics = getBuiltinDiagnosticsByName();
+  for (ArrayRef<DiagnosticRecord>::iterator di = AllDiagnostics.begin(),
+                                            de = AllDiagnostics.end();
+       di != de; ++di) {
+    unsigned diagID = di->DiagID;
+    llvm::StringRef flag = DiagnosticIDs::getWarningOptionForDiag(diagID);
+
+    if (flag.empty())
+      continue;
+
+    flags.push_back(flag);
+  }
+
+  std::sort(flags.begin(), flags.end());
+  flags.erase(std::unique(flags.begin(), flags.end()), flags.end());
+
+  for (auto &flag : flags) {
+    out << "-W" << flag << '\n';
+  }
+
+  return 0;
+}
Index: tools/diagtool/CMakeLists.txt
===================================================================
--- tools/diagtool/CMakeLists.txt
+++ tools/diagtool/CMakeLists.txt
@@ -8,6 +8,7 @@
   DiagnosticNames.cpp
   FindDiagnosticID.cpp
   ListWarnings.cpp
+  ListWarningFlags.cpp
   ShowEnabledWarnings.cpp
   TreeView.cpp
 )
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to