[llvm-branch-commits] [cfe-branch] r119824 - in /cfe/branches/Apple/whitney: include/clang-c/Index.h include/clang/Basic/Diagnostic.h lib/Basic/Diagnostic.cpp lib/Basic/Diagnostic.cpp.rej lib/Basic/Diagnostic.cpp~ test/Index/code-complete-errors.c tools/c-index-test/c-index-test.c tools/libclang/CIndexDiagnostic.cpp tools/libclang/libclang.darwin.exports tools/libclang/libclang.exports

Daniel Dunbar daniel at zuster.org
Fri Nov 19 11:49:43 PST 2010


Author: ddunbar
Date: Fri Nov 19 13:49:43 2010
New Revision: 119824

URL: http://llvm.org/viewvc/llvm-project?rev=119824&view=rev
Log:
Merge r119802:
--
Author: Douglas Gregor <dgregor at apple.com>
Date:   Fri Nov 19 16:18:16 2010 +0000

    Extend the libclang diagnostic API to provide information about the
    option name, category ID, and category name corresponding to a diagnostic.

*** MANUAL MERGE ***

Added:
    cfe/branches/Apple/whitney/lib/Basic/Diagnostic.cpp.rej
    cfe/branches/Apple/whitney/lib/Basic/Diagnostic.cpp~
      - copied, changed from r119823, cfe/branches/Apple/whitney/lib/Basic/Diagnostic.cpp
Modified:
    cfe/branches/Apple/whitney/include/clang-c/Index.h
    cfe/branches/Apple/whitney/include/clang/Basic/Diagnostic.h
    cfe/branches/Apple/whitney/lib/Basic/Diagnostic.cpp
    cfe/branches/Apple/whitney/test/Index/code-complete-errors.c
    cfe/branches/Apple/whitney/tools/c-index-test/c-index-test.c
    cfe/branches/Apple/whitney/tools/libclang/CIndexDiagnostic.cpp
    cfe/branches/Apple/whitney/tools/libclang/libclang.darwin.exports
    cfe/branches/Apple/whitney/tools/libclang/libclang.exports

Modified: cfe/branches/Apple/whitney/include/clang-c/Index.h
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/whitney/include/clang-c/Index.h?rev=119824&r1=119823&r2=119824&view=diff
==============================================================================
--- cfe/branches/Apple/whitney/include/clang-c/Index.h (original)
+++ cfe/branches/Apple/whitney/include/clang-c/Index.h Fri Nov 19 13:49:43 2010
@@ -499,7 +499,34 @@
    * This option corresponds to the clang flag
    * \c -fdiagnostics-print-source-range-info.
    */
-  CXDiagnostic_DisplaySourceRanges = 0x04
+  CXDiagnostic_DisplaySourceRanges = 0x04,
+  
+  /**
+   * \brief Display the option name associated with this diagnostic, if any.
+   *
+   * The option name displayed (e.g., -Wconversion) will be placed in brackets
+   * after the diagnostic text. This option corresponds to the clang flag
+   * \c -fdiagnostics-show-option.
+   */
+  CXDiagnostic_DisplayOption = 0x08,
+  
+  /**
+   * \brief Display the category number associated with this diagnostic, if any.
+   *
+   * The category number is displayed within brackets after the diagnostic text.
+   * This option corresponds to the clang flag 
+   * \c -fdiagnostics-show-category=id.
+   */
+  CXDiagnostic_DisplayCategoryId = 0x10,
+
+  /**
+   * \brief Display the category name associated with this diagnostic, if any.
+   *
+   * The category name is displayed within brackets after the diagnostic text.
+   * This option corresponds to the clang flag 
+   * \c -fdiagnostics-show-category=name.
+   */
+  CXDiagnostic_DisplayCategoryName = 0x20
 };
 
 /**
@@ -530,10 +557,6 @@
 CINDEX_LINKAGE unsigned clang_defaultDiagnosticDisplayOptions(void);
 
 /**
- * \brief Print a diagnostic to the given file.
- */
-
-/**
  * \brief Determine the severity of the given diagnostic.
  */
 CINDEX_LINKAGE enum CXDiagnosticSeverity
@@ -553,6 +576,43 @@
 CINDEX_LINKAGE CXString clang_getDiagnosticSpelling(CXDiagnostic);
 
 /**
+ * \brief Retrieve the name of the command-line option that enabled this
+ * diagnostic.
+ *
+ * \param Diag The diagnostic to be queried.
+ *
+ * \param Disable If non-NULL, will be set to the option that disables this
+ * diagnostic (if any).
+ *
+ * \returns A string that contains the command-line option used to enable this
+ * warning, such as "-Wconversion" or "-pedantic". 
+ */
+CINDEX_LINKAGE CXString clang_getDiagnosticOption(CXDiagnostic Diag,
+                                                  CXString *Disable);
+
+/**
+ * \brief Retrieve the category number for this diagnostic.
+ *
+ * Diagnostics can be categorized into groups along with other, related
+ * diagnostics (e.g., diagnostics under the same warning flag). This routine 
+ * retrieves the category number for the given diagnostic.
+ *
+ * \returns The number of the category that contains this diagnostic, or zero
+ * if this diagnostic is uncategorized.
+ */
+CINDEX_LINKAGE unsigned clang_getDiagnosticCategory(CXDiagnostic);
+
+/**
+ * \brief Retrieve the name of a particular diagnostic category.
+ *
+ * \param Category A diagnostic category number, as returned by 
+ * \c clang_getDiagnosticCategory().
+ *
+ * \returns The name of the given diagnostic category.
+ */
+CINDEX_LINKAGE CXString clang_getDiagnosticCategoryName(unsigned Category);
+  
+/**
  * \brief Determine the number of source ranges associated with the given
  * diagnostic.
  */

Modified: cfe/branches/Apple/whitney/include/clang/Basic/Diagnostic.h
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/whitney/include/clang/Basic/Diagnostic.h?rev=119824&r1=119823&r2=119824&view=diff
==============================================================================
--- cfe/branches/Apple/whitney/include/clang/Basic/Diagnostic.h (original)
+++ cfe/branches/Apple/whitney/include/clang/Basic/Diagnostic.h Fri Nov 19 13:49:43 2010
@@ -954,6 +954,7 @@
  * corresponding source manager is destroyed. 
  */
 class StoredDiagnostic {
+  unsigned ID;
   Diagnostic::Level Level;
   FullSourceLoc Loc;
   std::string Message;
@@ -963,12 +964,14 @@
 public:
   StoredDiagnostic();
   StoredDiagnostic(Diagnostic::Level Level, const DiagnosticInfo &Info);
-  StoredDiagnostic(Diagnostic::Level Level, llvm::StringRef Message);
+  StoredDiagnostic(Diagnostic::Level Level, unsigned ID, 
+                   llvm::StringRef Message);
   ~StoredDiagnostic();
 
   /// \brief Evaluates true when this object stores a diagnostic.
   operator bool() const { return Message.size() > 0; }
 
+  unsigned getID() const { return ID; }
   Diagnostic::Level getLevel() const { return Level; }
   const FullSourceLoc &getLocation() const { return Loc; }
   llvm::StringRef getMessage() const { return Message; }

Modified: cfe/branches/Apple/whitney/lib/Basic/Diagnostic.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/whitney/lib/Basic/Diagnostic.cpp?rev=119824&r1=119823&r2=119824&view=diff
==============================================================================
--- cfe/branches/Apple/whitney/lib/Basic/Diagnostic.cpp (original)
+++ cfe/branches/Apple/whitney/lib/Basic/Diagnostic.cpp Fri Nov 19 13:49:43 2010
@@ -1053,13 +1053,13 @@
 
 StoredDiagnostic::StoredDiagnostic() { }
 
-StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level, 
+StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level, unsigned ID, 
                                    llvm::StringRef Message)
-  : Level(Level), Loc(), Message(Message) { }
+  : Level(Level), ID(ID), Loc(), Message(Message) { }
 
 StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level, 
                                    const DiagnosticInfo &Info)
-  : Level(Level), Loc(Info.getLocation()) {
+  : ID(Info.getID()), Level(Level), Loc(Info.getLocation()) {
   llvm::SmallString<64> Message;
   Info.FormatDiagnostic(Message);
   this->Message.assign(Message.begin(), Message.end());

Added: cfe/branches/Apple/whitney/lib/Basic/Diagnostic.cpp.rej
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/whitney/lib/Basic/Diagnostic.cpp.rej?rev=119824&view=auto
==============================================================================
--- cfe/branches/Apple/whitney/lib/Basic/Diagnostic.cpp.rej (added)
+++ cfe/branches/Apple/whitney/lib/Basic/Diagnostic.cpp.rej Fri Nov 19 13:49:43 2010
@@ -0,0 +1,30 @@
+***************
+*** 547,559 ****
+  
+  StoredDiagnostic::StoredDiagnostic() { }
+  
+- StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level, 
+                                     llvm::StringRef Message)
+-   : Level(Level), Loc(), Message(Message) { }
+  
+  StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level, 
+                                     const DiagnosticInfo &Info)
+-   : Level(Level) {
+    assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) &&
+         "Valid source location without setting a source manager for diagnostic");
+    if (Info.getLocation().isValid())
+--- 547,560 ----
+  
+  StoredDiagnostic::StoredDiagnostic() { }
+  
++ StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level, unsigned ID,
+                                     llvm::StringRef Message)
++   : Level(Level), ID(ID), Loc(), Message(Message) { }
+  
+  StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level, 
+                                     const DiagnosticInfo &Info)
++   : ID(Info.getID()), Level(Level) 
++ {
+    assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) &&
+         "Valid source location without setting a source manager for diagnostic");
+    if (Info.getLocation().isValid())

Copied: cfe/branches/Apple/whitney/lib/Basic/Diagnostic.cpp~ (from r119823, cfe/branches/Apple/whitney/lib/Basic/Diagnostic.cpp)
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/whitney/lib/Basic/Diagnostic.cpp%7E?p2=cfe/branches/Apple/whitney/lib/Basic/Diagnostic.cpp%7E&p1=cfe/branches/Apple/whitney/lib/Basic/Diagnostic.cpp&r1=119823&r2=119824&rev=119824&view=diff
==============================================================================
    (empty)

Modified: cfe/branches/Apple/whitney/test/Index/code-complete-errors.c
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/whitney/test/Index/code-complete-errors.c?rev=119824&r1=119823&r2=119824&view=diff
==============================================================================
--- cfe/branches/Apple/whitney/test/Index/code-complete-errors.c (original)
+++ cfe/branches/Apple/whitney/test/Index/code-complete-errors.c Fri Nov 19 13:49:43 2010
@@ -13,11 +13,13 @@
 #define expand_to_binary_function(ret, name, parm1, parm2, code) ret name(parm1, parm2) code
 
 expand_to_binary_function(int, g, int *ip, float *fp, {
-// CHECK: code-complete-errors.c:17:15:{17:12-17:14}{17:18-17:20}: warning: comparison of distinct pointer types ('int *' and 'float *')
+ // CHECK: code-complete-errors.c:17:12:{17:9-17:24}: warning: using the result of an assignment as a condition without parentheses [-Wparentheses]
+    if (ip = (float*)fp) ;
+// CHECK: code-complete-errors.c:19:15:{19:12-19:14}{19:18-19:20}: warning: comparison of distinct pointer types ('int *' and 'float *')
     return ip == fp;
   })
 
 void g() {  }
 
-// RUN: c-index-test -code-completion-at=%s:19:12 -pedantic %s 2> %t
+// RUN: c-index-test -code-completion-at=%s:21:12 -pedantic %s 2> %t
 // RUN: FileCheck -check-prefix=CHECK %s < %t

Modified: cfe/branches/Apple/whitney/tools/c-index-test/c-index-test.c
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/whitney/tools/c-index-test/c-index-test.c?rev=119824&r1=119823&r2=119824&view=diff
==============================================================================
--- cfe/branches/Apple/whitney/tools/c-index-test/c-index-test.c (original)
+++ cfe/branches/Apple/whitney/tools/c-index-test/c-index-test.c Fri Nov 19 13:49:43 2010
@@ -309,7 +309,8 @@
   CXFile file;
   CXString Msg;
   unsigned display_opts = CXDiagnostic_DisplaySourceLocation
-    | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges;
+    | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges
+    | CXDiagnostic_DisplayOption;
   unsigned i, num_fixits;
 
   if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)

Modified: cfe/branches/Apple/whitney/tools/libclang/CIndexDiagnostic.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/whitney/tools/libclang/CIndexDiagnostic.cpp?rev=119824&r1=119823&r2=119824&view=diff
==============================================================================
--- cfe/branches/Apple/whitney/tools/libclang/CIndexDiagnostic.cpp (original)
+++ cfe/branches/Apple/whitney/tools/libclang/CIndexDiagnostic.cpp Fri Nov 19 13:49:43 2010
@@ -119,11 +119,61 @@
   else
     Out << "<no diagnostic text>";
   clang_disposeString(Text);
+  
+  if (Options & (CXDiagnostic_DisplayOption | CXDiagnostic_DisplayCategoryId |
+                 CXDiagnostic_DisplayCategoryName)) {
+    bool NeedBracket = true;
+    bool NeedComma = false;
+
+    if (Options & CXDiagnostic_DisplayOption) {
+      CXString OptionName = clang_getDiagnosticOption(Diagnostic, 0);
+      if (const char *OptionText = clang_getCString(OptionName)) {
+        if (OptionText[0]) {
+          Out << " [" << OptionText;
+          NeedBracket = false;
+          NeedComma = true;
+        }
+      }
+      clang_disposeString(OptionName);
+    }
+    
+    if (Options & (CXDiagnostic_DisplayCategoryId | 
+                   CXDiagnostic_DisplayCategoryName)) {
+      if (unsigned CategoryID = clang_getDiagnosticCategory(Diagnostic)) {
+        if (Options & CXDiagnostic_DisplayCategoryId) {
+          if (NeedBracket)
+            Out << " [";
+          if (NeedComma)
+            Out << ", ";
+          Out << CategoryID;
+          NeedBracket = false;
+          NeedComma = true;
+        }
+        
+        if (Options & CXDiagnostic_DisplayCategoryName) {
+          CXString CategoryName = clang_getDiagnosticCategoryName(CategoryID);
+          if (NeedBracket)
+            Out << " [";
+          if (NeedComma)
+            Out << ", ";
+          Out << clang_getCString(CategoryName);
+          NeedBracket = false;
+          NeedComma = true;
+          clang_disposeString(CategoryName);
+        }
+      }
+    }
+    
+    if (!NeedBracket)
+      Out << "]";
+  }
+  
   return createCXString(Out.str(), true);
 }
 
 unsigned clang_defaultDiagnosticDisplayOptions() {
-  return CXDiagnostic_DisplaySourceLocation | CXDiagnostic_DisplayColumn;
+  return CXDiagnostic_DisplaySourceLocation | CXDiagnostic_DisplayColumn |
+         CXDiagnostic_DisplayOption;
 }
 
 enum CXDiagnosticSeverity clang_getDiagnosticSeverity(CXDiagnostic Diag) {
@@ -161,6 +211,47 @@
   return createCXString(StoredDiag->Diag.getMessage(), false);
 }
 
+CXString clang_getDiagnosticOption(CXDiagnostic Diag, CXString *Disable) {
+  if (Disable)
+    *Disable = createCXString("");
+  
+  CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
+  if (!StoredDiag)
+    return createCXString("");
+  
+  unsigned ID = StoredDiag->Diag.getID();
+  if (const char *Option = Diagnostic::getWarningOptionForDiag(ID)) {
+    if (Disable)
+      *Disable = createCXString((llvm::Twine("-Wno-") + Option).str());
+    return createCXString((llvm::Twine("-W") + Option).str());
+  }
+  
+  if (ID == diag::fatal_too_many_errors) {
+    if (Disable)
+      *Disable = createCXString("-ferror-limit=0");
+    return createCXString("-ferror-limit=");
+  }
+  
+  bool EnabledByDefault;
+  if (Diagnostic::isBuiltinExtensionDiag(ID, EnabledByDefault) &&
+      !EnabledByDefault)
+    return createCXString("-pedantic");
+
+  return createCXString("");
+}
+
+unsigned clang_getDiagnosticCategory(CXDiagnostic Diag) {
+  CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
+  if (!StoredDiag)
+    return 0;
+
+  return Diagnostic::getCategoryNumberForDiag(StoredDiag->Diag.getID());
+}
+  
+CXString clang_getDiagnosticCategoryName(unsigned Category) {
+  return createCXString(Diagnostic::getCategoryNameFromID(Category));
+}
+  
 unsigned clang_getDiagnosticNumRanges(CXDiagnostic Diag) {
   CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
   if (!StoredDiag || StoredDiag->Diag.getLocation().isInvalid())

Modified: cfe/branches/Apple/whitney/tools/libclang/libclang.darwin.exports
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/whitney/tools/libclang/libclang.darwin.exports?rev=119824&r1=119823&r2=119824&view=diff
==============================================================================
--- cfe/branches/Apple/whitney/tools/libclang/libclang.darwin.exports (original)
+++ cfe/branches/Apple/whitney/tools/libclang/libclang.darwin.exports Fri Nov 19 13:49:43 2010
@@ -58,10 +58,13 @@
 _clang_getCursorUSR
 _clang_getDefinitionSpellingAndExtent
 _clang_getDiagnostic
+_clang_getDiagnosticCategory
+_clang_getDiagnosticCategoryName
 _clang_getDiagnosticFixIt
 _clang_getDiagnosticLocation
 _clang_getDiagnosticNumFixIts
 _clang_getDiagnosticNumRanges
+_clang_getDiagnosticOption
 _clang_getDiagnosticRange
 _clang_getDiagnosticSeverity
 _clang_getDiagnosticSpelling

Modified: cfe/branches/Apple/whitney/tools/libclang/libclang.exports
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/whitney/tools/libclang/libclang.exports?rev=119824&r1=119823&r2=119824&view=diff
==============================================================================
--- cfe/branches/Apple/whitney/tools/libclang/libclang.exports (original)
+++ cfe/branches/Apple/whitney/tools/libclang/libclang.exports Fri Nov 19 13:49:43 2010
@@ -58,10 +58,13 @@
 clang_getCursorUSR
 clang_getDefinitionSpellingAndExtent
 clang_getDiagnostic
+clang_getDiagnosticCategory
+clang_getDiagnosticCategoryName
 clang_getDiagnosticFixIt
 clang_getDiagnosticLocation
 clang_getDiagnosticNumFixIts
 clang_getDiagnosticNumRanges
+clang_getDiagnosticOption
 clang_getDiagnosticRange
 clang_getDiagnosticSeverity
 clang_getDiagnosticSpelling





More information about the llvm-branch-commits mailing list