[cfe-commits] r119802 - in /cfe/trunk: include/clang-c/Index.h include/clang/Basic/Diagnostic.h 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

Douglas Gregor dgregor at apple.com
Fri Nov 19 08:18:17 PST 2010


Author: dgregor
Date: Fri Nov 19 10:18:16 2010
New Revision: 119802

URL: http://llvm.org/viewvc/llvm-project?rev=119802&view=rev
Log:
Extend the libclang diagnostic API to provide information about the
option name, category ID, and category name corresponding to a diagnostic.

Modified:
    cfe/trunk/include/clang-c/Index.h
    cfe/trunk/include/clang/Basic/Diagnostic.h
    cfe/trunk/lib/Basic/Diagnostic.cpp
    cfe/trunk/test/Index/code-complete-errors.c
    cfe/trunk/tools/c-index-test/c-index-test.c
    cfe/trunk/tools/libclang/CIndexDiagnostic.cpp
    cfe/trunk/tools/libclang/libclang.darwin.exports
    cfe/trunk/tools/libclang/libclang.exports

Modified: cfe/trunk/include/clang-c/Index.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=119802&r1=119801&r2=119802&view=diff
==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Fri Nov 19 10:18:16 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/trunk/include/clang/Basic/Diagnostic.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/Diagnostic.h?rev=119802&r1=119801&r2=119802&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/Diagnostic.h (original)
+++ cfe/trunk/include/clang/Basic/Diagnostic.h Fri Nov 19 10:18:16 2010
@@ -876,6 +876,7 @@
  * corresponding source manager is destroyed. 
  */
 class StoredDiagnostic {
+  unsigned ID;
   Diagnostic::Level Level;
   FullSourceLoc Loc;
   std::string Message;
@@ -885,12 +886,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/trunk/lib/Basic/Diagnostic.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/Diagnostic.cpp?rev=119802&r1=119801&r2=119802&view=diff
==============================================================================
--- cfe/trunk/lib/Basic/Diagnostic.cpp (original)
+++ cfe/trunk/lib/Basic/Diagnostic.cpp Fri Nov 19 10:18:16 2010
@@ -547,13 +547,14 @@
 
 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) {
+  : 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())

Modified: cfe/trunk/test/Index/code-complete-errors.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/code-complete-errors.c?rev=119802&r1=119801&r2=119802&view=diff
==============================================================================
--- cfe/trunk/test/Index/code-complete-errors.c (original)
+++ cfe/trunk/test/Index/code-complete-errors.c Fri Nov 19 10:18:16 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/trunk/tools/c-index-test/c-index-test.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/c-index-test/c-index-test.c?rev=119802&r1=119801&r2=119802&view=diff
==============================================================================
--- cfe/trunk/tools/c-index-test/c-index-test.c (original)
+++ cfe/trunk/tools/c-index-test/c-index-test.c Fri Nov 19 10:18:16 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/trunk/tools/libclang/CIndexDiagnostic.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndexDiagnostic.cpp?rev=119802&r1=119801&r2=119802&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndexDiagnostic.cpp (original)
+++ cfe/trunk/tools/libclang/CIndexDiagnostic.cpp Fri Nov 19 10:18:16 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 = DiagnosticIDs::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 (DiagnosticIDs::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 DiagnosticIDs::getCategoryNumberForDiag(StoredDiag->Diag.getID());
+}
+  
+CXString clang_getDiagnosticCategoryName(unsigned Category) {
+  return createCXString(DiagnosticIDs::getCategoryNameFromID(Category));
+}
+  
 unsigned clang_getDiagnosticNumRanges(CXDiagnostic Diag) {
   CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
   if (!StoredDiag || StoredDiag->Diag.getLocation().isInvalid())

Modified: cfe/trunk/tools/libclang/libclang.darwin.exports
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/libclang.darwin.exports?rev=119802&r1=119801&r2=119802&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/libclang.darwin.exports (original)
+++ cfe/trunk/tools/libclang/libclang.darwin.exports Fri Nov 19 10:18:16 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/trunk/tools/libclang/libclang.exports
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/libclang.exports?rev=119802&r1=119801&r2=119802&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/libclang.exports (original)
+++ cfe/trunk/tools/libclang/libclang.exports Fri Nov 19 10:18:16 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 cfe-commits mailing list