[cfe-commits] r157932 - in /cfe/trunk: test/Misc/warning-flags-enabled.c tools/diagtool/CMakeLists.txt tools/diagtool/DiagnosticNames.cpp tools/diagtool/DiagnosticNames.h tools/diagtool/ListWarnings.cpp tools/diagtool/Makefile tools/diagtool/ShowEnabledWarnings.cpp

Jordan Rose jordan_rose at apple.com
Mon Jun 4 09:57:51 PDT 2012


Author: jrose
Date: Mon Jun  4 11:57:50 2012
New Revision: 157932

URL: http://llvm.org/viewvc/llvm-project?rev=157932&view=rev
Log:
[diagtool] Re-add show-enabled, minimizing the code pulled in from Frontend.

Now correctly builds with both GNU make and CMake.

Added:
    cfe/trunk/test/Misc/warning-flags-enabled.c
    cfe/trunk/tools/diagtool/DiagnosticNames.cpp
    cfe/trunk/tools/diagtool/DiagnosticNames.h
    cfe/trunk/tools/diagtool/ShowEnabledWarnings.cpp
Modified:
    cfe/trunk/tools/diagtool/CMakeLists.txt
    cfe/trunk/tools/diagtool/ListWarnings.cpp
    cfe/trunk/tools/diagtool/Makefile

Added: cfe/trunk/test/Misc/warning-flags-enabled.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Misc/warning-flags-enabled.c?rev=157932&view=auto
==============================================================================
--- cfe/trunk/test/Misc/warning-flags-enabled.c (added)
+++ cfe/trunk/test/Misc/warning-flags-enabled.c Mon Jun  4 11:57:50 2012
@@ -0,0 +1,27 @@
+// RUN: diagtool show-enabled %s 2>&1 | FileCheck %s
+//
+// This shows warnings which are on by default.
+// We just check a few to make sure it's doing something sensible.
+//
+// CHECK: warn_condition_is_assignment
+// CHECK: warn_null_arg
+// CHECK: warn_unterminated_string
+
+
+// RUN: diagtool show-enabled -Wno-everything %s 2>&1 | count 0
+
+
+// RUN: diagtool show-enabled -Wno-everything -Wobjc-root-class %s 2>&1 | FileCheck -check-prefix CHECK-WARN %s
+// RUN: diagtool show-enabled -Wno-everything -Werror=objc-root-class %s 2>&1 | FileCheck -check-prefix CHECK-ERROR %s
+// RUN: diagtool show-enabled -Wno-everything -Wfatal-errors=objc-root-class %s 2>&1 | FileCheck -check-prefix CHECK-FATAL %s
+//
+// CHECK-WARN:  W  warn_objc_root_class_missing [-Wobjc-root-class]
+// CHECK-ERROR: E  warn_objc_root_class_missing [-Wobjc-root-class]
+// CHECK-FATAL: F  warn_objc_root_class_missing [-Wobjc-root-class]
+
+// RUN: diagtool show-enabled --no-levels -Wno-everything -Wobjc-root-class %s 2>&1 | FileCheck -check-prefix CHECK-NO-LEVELS %s
+//
+// CHECK-NO-LEVELS-NOT: W
+// CHECK-NO-LEVELS-NOT: E
+// CHECK-NO-LEVELS-NOT: F
+// CHECK-NO-LEVELS: warn_objc_root_class_missing [-Wobjc-root-class]

Modified: cfe/trunk/tools/diagtool/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/diagtool/CMakeLists.txt?rev=157932&r1=157931&r2=157932&view=diff
==============================================================================
--- cfe/trunk/tools/diagtool/CMakeLists.txt (original)
+++ cfe/trunk/tools/diagtool/CMakeLists.txt Mon Jun  4 11:57:50 2012
@@ -6,12 +6,15 @@
   clangBasic
   clangLex
   clangSema
+  clangFrontend
   )
 
 add_clang_executable(diagtool
   diagtool_main.cpp
   DiagTool.cpp
+  DiagnosticNames.cpp
   ListWarnings.cpp
+  ShowEnabledWarnings.cpp
 )
 
 if(UNIX)

Added: cfe/trunk/tools/diagtool/DiagnosticNames.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/diagtool/DiagnosticNames.cpp?rev=157932&view=auto
==============================================================================
--- cfe/trunk/tools/diagtool/DiagnosticNames.cpp (added)
+++ cfe/trunk/tools/diagtool/DiagnosticNames.cpp Mon Jun  4 11:57:50 2012
@@ -0,0 +1,25 @@
+//===- DiagnosticNames.cpp - Defines a table of all builtin diagnostics ----==//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "DiagnosticNames.h"
+#include "clang/Basic/AllDiagnostics.h"
+
+using namespace clang;
+
+const diagtool::DiagnosticRecord diagtool::BuiltinDiagnostics[] = {
+#define DIAG_NAME_INDEX(ENUM) { #ENUM, diag::ENUM, STR_SIZE(#ENUM, uint8_t) },
+#include "clang/Basic/DiagnosticIndexName.inc"
+#undef DIAG_NAME_INDEX
+  { 0, 0, 0 }
+};
+
+const size_t diagtool::BuiltinDiagnosticsCount =
+  sizeof(diagtool::BuiltinDiagnostics) /
+  sizeof(diagtool::BuiltinDiagnostics[0]) - 1;
+

Added: cfe/trunk/tools/diagtool/DiagnosticNames.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/diagtool/DiagnosticNames.h?rev=157932&view=auto
==============================================================================
--- cfe/trunk/tools/diagtool/DiagnosticNames.h (added)
+++ cfe/trunk/tools/diagtool/DiagnosticNames.h Mon Jun  4 11:57:50 2012
@@ -0,0 +1,28 @@
+//===- DiagnosticNames.h - Defines a table of all builtin diagnostics ------==//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/DataTypes.h"
+
+namespace diagtool {
+  struct DiagnosticRecord {
+    const char *NameStr;
+    unsigned short DiagID;
+    uint8_t NameLen;
+    
+    llvm::StringRef getName() const {
+      return llvm::StringRef(NameStr, NameLen);
+    }
+  };
+
+  extern const DiagnosticRecord BuiltinDiagnostics[];
+  extern const size_t BuiltinDiagnosticsCount;
+
+} // end namespace diagtool
+

Modified: cfe/trunk/tools/diagtool/ListWarnings.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/diagtool/ListWarnings.cpp?rev=157932&r1=157931&r2=157932&view=diff
==============================================================================
--- cfe/trunk/tools/diagtool/ListWarnings.cpp (original)
+++ cfe/trunk/tools/diagtool/ListWarnings.cpp Mon Jun  4 11:57:50 2012
@@ -13,6 +13,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "DiagTool.h"
+#include "DiagnosticNames.h"
 #include "clang/Basic/Diagnostic.h"
 #include "llvm/Support/Format.h"
 #include "llvm/ADT/StringMap.h"
@@ -26,28 +27,6 @@
 using namespace clang;
 
 namespace {
-struct StaticDiagNameIndexRec {
-  const char *NameStr;
-  unsigned short DiagID;
-  uint8_t NameLen;
-
-  StringRef getName() const {
-    return StringRef(NameStr, NameLen);
-  }
-};
-}
-
-static const StaticDiagNameIndexRec StaticDiagNameIndex[] = {
-#define DIAG_NAME_INDEX(ENUM) { #ENUM, diag::ENUM, STR_SIZE(#ENUM, uint8_t) },
-#include "clang/Basic/DiagnosticIndexName.inc"
-#undef DIAG_NAME_INDEX
-  { 0, 0, 0 }
-};
-
-static const unsigned StaticDiagNameIndexSize =
-  sizeof(StaticDiagNameIndex)/sizeof(StaticDiagNameIndex[0])-1;
-
-namespace {
 struct Entry {
   llvm::StringRef DiagName;
   llvm::StringRef Flag;
@@ -73,8 +52,8 @@
   std::vector<Entry> Flagged, Unflagged;
   llvm::StringMap<std::vector<unsigned> > flagHistogram;
   
-  for (const StaticDiagNameIndexRec *di = StaticDiagNameIndex, *de = StaticDiagNameIndex + StaticDiagNameIndexSize;
-       di != de; ++di) {
+  for (const diagtool::DiagnosticRecord *di = diagtool::BuiltinDiagnostics,
+       *de = di + diagtool::BuiltinDiagnosticsCount; di != de; ++di) {
     
     unsigned diagID = di->DiagID;
     

Modified: cfe/trunk/tools/diagtool/Makefile
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/diagtool/Makefile?rev=157932&r1=157931&r2=157932&view=diff
==============================================================================
--- cfe/trunk/tools/diagtool/Makefile (original)
+++ cfe/trunk/tools/diagtool/Makefile Mon Jun  4 11:57:50 2012
@@ -1,4 +1,4 @@
-##===- tools/driver/Makefile -------------------------------*- Makefile -*-===##
+##===- tools/diagtool/Makefile -----------------------------*- Makefile -*-===##
 #
 #                     The LLVM Compiler Infrastructure
 #
@@ -16,9 +16,11 @@
 # Don't install this.
 NO_INSTALL = 1
 
-LINK_COMPONENTS := support
+LINK_COMPONENTS := support mc
 
-USEDLIBS = clangBasic.a
+USEDLIBS = clangAnalysis.a clangAST.a clangBasic.a clangDriver.a clangEdit.a \
+           clangFrontend.a clangLex.a clangParse.a clangSema.a \
+           clangSerialization.a
 
 include $(CLANG_LEVEL)/Makefile
 

Added: cfe/trunk/tools/diagtool/ShowEnabledWarnings.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/diagtool/ShowEnabledWarnings.cpp?rev=157932&view=auto
==============================================================================
--- cfe/trunk/tools/diagtool/ShowEnabledWarnings.cpp (added)
+++ cfe/trunk/tools/diagtool/ShowEnabledWarnings.cpp Mon Jun  4 11:57:50 2012
@@ -0,0 +1,147 @@
+//===- ShowEnabledWarnings - diagtool tool for printing enabled flags -----===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "DiagTool.h"
+#include "DiagnosticNames.h"
+#include "clang/Basic/LLVM.h"
+#include "clang/Frontend/CompilerInstance.h"
+#include "clang/Frontend/TextDiagnosticBuffer.h"
+#include "clang/Frontend/TextDiagnosticPrinter.h"
+#include "clang/Frontend/Utils.h"
+#include "llvm/Support/TargetSelect.h"
+
+DEF_DIAGTOOL("show-enabled",
+             "Show which warnings are enabled for a given command line",
+             ShowEnabledWarnings)
+
+using namespace clang;
+
+namespace {
+  struct PrettyDiag {
+    StringRef Name;
+    StringRef Flag;
+    DiagnosticsEngine::Level Level;
+
+    PrettyDiag(StringRef name, StringRef flag, DiagnosticsEngine::Level level)
+    : Name(name), Flag(flag), Level(level) {}
+
+    bool operator<(const PrettyDiag &x) const { return Name < x.Name; }
+  };
+}
+
+static void printUsage() {
+  llvm::errs() << "Usage: diagtool show-enabled [<flags>] <single-input.c>\n";
+}
+
+static char getCharForLevel(DiagnosticsEngine::Level Level) {
+  switch (Level) {
+  case DiagnosticsEngine::Ignored: return ' ';
+  case DiagnosticsEngine::Note:    return '-';
+  case DiagnosticsEngine::Warning: return 'W';
+  case DiagnosticsEngine::Error:   return 'E';
+  case DiagnosticsEngine::Fatal:   return 'F';
+  }
+
+  llvm_unreachable("Unknown diagnostic level");
+}
+
+static IntrusiveRefCntPtr<DiagnosticsEngine>
+createDiagnostics(unsigned int argc, char **argv) {
+  IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(new DiagnosticIDs());
+
+  // Buffer diagnostics from argument parsing so that we can output them using a
+  // well formed diagnostic object.
+  TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer;
+  IntrusiveRefCntPtr<DiagnosticsEngine> InterimDiags(
+    new DiagnosticsEngine(DiagIDs, DiagsBuffer));
+
+  // Try to build a CompilerInvocation.
+  OwningPtr<CompilerInvocation> Invocation(
+    createInvocationFromCommandLine(ArrayRef<const char *>(argv, argc),
+                                    InterimDiags));
+  if (!Invocation)
+    return NULL;
+
+  // Build the diagnostics parser
+  IntrusiveRefCntPtr<DiagnosticsEngine> FinalDiags =
+    CompilerInstance::createDiagnostics(Invocation->getDiagnosticOpts(),
+                                        argc, argv);
+  if (!FinalDiags)
+    return NULL;
+  
+  // Flush any errors created when initializing everything. This could happen
+  // for invalid command lines, which will probably give non-sensical results.
+  DiagsBuffer->FlushDiagnostics(*FinalDiags);
+
+  return FinalDiags;
+}
+
+int ShowEnabledWarnings::run(unsigned int argc, char **argv, raw_ostream &Out) {
+  // First check our one flag (--levels).
+  bool ShouldShowLevels = true;
+  if (argc > 0) {
+    StringRef FirstArg(*argv);
+    if (FirstArg.equals("--no-levels")) {
+      ShouldShowLevels = false;
+      --argc;
+      ++argv;
+    } else if (FirstArg.equals("--levels")) {
+      ShouldShowLevels = true;
+      --argc;
+      ++argv;
+    }
+  }
+
+  // Create the diagnostic engine.
+  IntrusiveRefCntPtr<DiagnosticsEngine> Diags = createDiagnostics(argc, argv);
+  if (!Diags) {
+    printUsage();
+    return EXIT_FAILURE;
+  }
+
+  // Now we have our diagnostics. Iterate through EVERY diagnostic and see
+  // which ones are turned on.
+  // FIXME: It would be very nice to print which flags are turning on which
+  // diagnostics, but this can be done with a diff.
+  std::vector<PrettyDiag> Active;
+
+  for (const diagtool::DiagnosticRecord *I = diagtool::BuiltinDiagnostics,
+       *E = I + diagtool::BuiltinDiagnosticsCount; I != E; ++I) {
+    unsigned DiagID = I->DiagID;
+    
+    if (DiagnosticIDs::isBuiltinNote(DiagID))
+      continue;
+    
+    if (!DiagnosticIDs::isBuiltinWarningOrExtension(DiagID))
+      continue;
+
+    DiagnosticsEngine::Level DiagLevel =
+      Diags->getDiagnosticLevel(DiagID, SourceLocation());
+    if (DiagLevel == DiagnosticsEngine::Ignored)
+      continue;
+
+    StringRef WarningOpt = DiagnosticIDs::getWarningOptionForDiag(DiagID);
+    Active.push_back(PrettyDiag(I->getName(), WarningOpt, DiagLevel));
+  }
+
+  std::sort(Active.begin(), Active.end());
+
+  // Print them all out.
+  for (std::vector<PrettyDiag>::const_iterator I = Active.begin(),
+       E = Active.end(); I != E; ++I) {
+    if (ShouldShowLevels)
+      Out << getCharForLevel(I->Level) << "  ";
+    Out << I->Name;
+    if (!I->Flag.empty())
+      Out << " [-W" << I->Flag << "]";
+    Out << '\n';
+  }
+
+  return EXIT_SUCCESS;
+}





More information about the cfe-commits mailing list