]> granicus.if.org Git - clang/commitdiff
Implement clang_getDiagnosticCategoryText() to provide a way for a client of libclang...
authorTed Kremenek <kremenek@apple.com>
Thu, 12 Apr 2012 00:03:31 +0000 (00:03 +0000)
committerTed Kremenek <kremenek@apple.com>
Thu, 12 Apr 2012 00:03:31 +0000 (00:03 +0000)
get the diagnostic category name from a serialized diagnostic when the version of libclang used
to read the diagnostic file is newer than the clang that emitted the diagnostic file.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@154567 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang-c/Index.h
test/Misc/serialized-diags-single-issue.c
tools/c-index-test/c-index-test.c
tools/libclang/CIndexDiagnostic.cpp
tools/libclang/CIndexDiagnostic.h
tools/libclang/CXLoadedDiagnostic.cpp
tools/libclang/CXLoadedDiagnostic.h
tools/libclang/CXStoredDiagnostic.cpp
tools/libclang/libclang.exports

index 18316b4d33c136d86742bc09a9ceb03f43e73d92..b297e545122993de91edc10f31fea486e7993c2e 100644 (file)
@@ -35,6 +35,16 @@ extern "C" {
   #define CINDEX_LINKAGE
 #endif
 
+#ifdef __GNUC__
+  #define CINDEX_DEPRECATED __attribute__((deprecated))
+#else
+  #ifdef _MSC_VER
+    #define CINDEX_DEPRECATED __declspec(deprecated)
+  #else
+    #define CINDEX_DEPRECATED
+  #endif
+#endif
+
 /** \defgroup CINDEX libclang: C Interface to Clang
  *
  * The C Interface to Clang provides a relatively small API that exposes
@@ -830,14 +840,25 @@ CINDEX_LINKAGE CXString clang_getDiagnosticOption(CXDiagnostic Diag,
 CINDEX_LINKAGE unsigned clang_getDiagnosticCategory(CXDiagnostic);
 
 /**
- * \brief Retrieve the name of a particular diagnostic category.
+ * \brief Retrieve the name of a particular diagnostic category.  This
+ *  is now deprecated.  Use clang_getDiagnosticCategoryText()
+ *  instead.
  *
  * \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);
+CINDEX_DEPRECATED CINDEX_LINKAGE
+CXString clang_getDiagnosticCategoryName(unsigned Category);
+
+/**
+ * \brief Retrieve the diagnostic category text for a given diagnostic.
+ *
+ *
+ * \returns The text of the given diagnostic category.
+ */
+CINDEX_LINKAGE CXString clang_getDiagnosticCategoryText(CXDiagnostic);
   
 /**
  * \brief Determine the number of source ranges associated with the given
index a6af836e386c138a7c4bc88af4e34d09c3c18e05..938dda3f0de77d2d63ded07fb455b939149bf624 100644 (file)
@@ -10,7 +10,7 @@ void foo() {
 // NOTE: it is important that this test case only contain a single issue.  This test case checks
 // if we can handle serialized diagnostics that contain only one diagnostic.
 
-// CHECK: {{.*}}serialized-diags-single-issue.c:3:12: warning: variable 'voodoo' is uninitialized when used here [-Wuninitialized]
+// CHECK: {{.*}}serialized-diags-single-issue.c:3:12: warning: variable 'voodoo' is uninitialized when used here [-Wuninitialized] [Semantic Issue]
 // CHECK: Range: {{.*}}serialized-diags-single-issue.c:3:12 {{.*}}serialized-diags-single-issue.c:3:18
 // CHECK: +-{{.*}}serialized-diags-single-issue.c:2:13: note: initialize the variable 'voodoo' to silence this warning []
 // CHECK: +-Range: {{.*}}serialized-diags-single-issue.c:2:13 {{.*}}serialized-diags-single-issue.c:2:13
index a81de2717f7fee6e5a16da4c65b119ac1f06b59d..04e7a506e2386ec13df6c066f6c6b47ae6bb71d0 100644 (file)
@@ -2586,9 +2586,9 @@ static void printDiagnosticSet(CXDiagnosticSet Diags, unsigned indent) {
     CXSourceLocation DiagLoc;
     CXDiagnostic D;
     CXFile File;
-    CXString FileName, DiagSpelling, DiagOption;
+    CXString FileName, DiagSpelling, DiagOption, DiagCat;
     unsigned line, column, offset;
-    const char *DiagOptionStr = 0;
+    const char *DiagOptionStr = 0, *DiagCatStr = 0;
     
     D = clang_getDiagnosticInSet(Diags, i);
     DiagLoc = clang_getDiagnosticLocation(D);
@@ -2611,6 +2611,12 @@ static void printDiagnosticSet(CXDiagnosticSet Diags, unsigned indent) {
       fprintf(stderr, " [%s]", DiagOptionStr);
     }
     
+    DiagCat = clang_getDiagnosticCategoryText(D);
+    DiagCatStr = clang_getCString(DiagCat);
+    if (DiagCatStr) {
+      fprintf(stderr, " [%s]", DiagCatStr);
+    }
+    
     fprintf(stderr, "\n");
     
     printRanges(D, indent);
index d8a2b05b206bac9c6557e7bb1e8ef4980dbc86eb..8fbe3d8c3d38240616753f74f9987725c6040cca 100644 (file)
@@ -72,6 +72,8 @@ public:
   }
   
   unsigned getCategory() const { return 0; }
+  CXString getCategoryText() const { return createCXString(""); }
+
   unsigned getNumRanges() const { return 0; }
   CXSourceRange getRange(unsigned Range) const { return clang_getNullRange(); }
   unsigned getNumFixIts() const { return 0; }
@@ -324,7 +326,7 @@ CXString clang_formatDiagnostic(CXDiagnostic Diagnostic, unsigned Options) {
         }
         
         if (Options & CXDiagnostic_DisplayCategoryName) {
-          CXString CategoryName = clang_getDiagnosticCategoryName(CategoryID);
+          CXString CategoryName = clang_getDiagnosticCategoryText(Diagnostic);
           if (NeedBracket)
             Out << " [";
           if (NeedComma)
@@ -385,9 +387,16 @@ unsigned clang_getDiagnosticCategory(CXDiagnostic Diag) {
 }
   
 CXString clang_getDiagnosticCategoryName(unsigned Category) {
+  // Kept for backwards compatibility.
   return createCXString(DiagnosticIDs::getCategoryNameFromID(Category));
 }
   
+CXString clang_getDiagnosticCategoryText(CXDiagnostic Diag) {
+  if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag))
+    return D->getCategoryText();
+  return createCXString("");
+}
+  
 unsigned clang_getDiagnosticNumRanges(CXDiagnostic Diag) {
   if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag))
     return D->getNumRanges();
index 30669982370fdf7212dd16f63656ce731b6d0591..b1c3978e0ae8a7535b4eb15fd924d6e662a788ec 100644 (file)
@@ -73,7 +73,10 @@ public:
   
   /// \brief Return the category of the diagnostic.
   virtual unsigned getCategory() const = 0;
-  
+
+  /// \brief Return the category string of the diagnostic.
+  virtual CXString getCategoryText() const = 0;
+
   /// \brief Return the number of source ranges for the diagnostic.
   virtual unsigned getNumRanges() const = 0;
   
@@ -132,6 +135,9 @@ struct CXStoredDiagnostic : public CXDiagnosticImpl {
   /// \brief Return the category of the diagnostic.
   virtual unsigned getCategory() const;
   
+  /// \brief Return the category string of the diagnostic.
+  virtual CXString getCategoryText() const;
+
   /// \brief Return the number of source ranges for the diagnostic.
   virtual unsigned getNumRanges() const;
   
index d52c18319faa7d02e80b6b4625941fbd91d7c0fd..e5b6ccc5b7e81ea32b6d04c2193f2190adfd0eeb 100644 (file)
@@ -119,6 +119,10 @@ unsigned CXLoadedDiagnostic::getCategory() const {
   return category;
 }
 
+CXString CXLoadedDiagnostic::getCategoryText() const {
+  return cxstring::createCXString(CategoryText);
+}
+
 unsigned CXLoadedDiagnostic::getNumRanges() const {
   return Ranges.size();
 }
@@ -650,6 +654,7 @@ LoadResult DiagLoader::readDiagnosticBlock(llvm::BitstreamCursor &Stream,
         D->category = Record[offset++];
         unsigned diagFlag = Record[offset++];
         D->DiagOption = diagFlag ? TopDiags.WarningFlags[diagFlag] : "";
+        D->CategoryText = D->category ? TopDiags.Categories[D->category] : "";
         D->Spelling = TopDiags.makeString(BlobStart, BlobLen);
         continue;
       }
index 9eff144901a436726aa4a2a9b72918eaafc4596b..d4a321e0e1cdf3be5ba1a67d6e75eaf65a494313 100644 (file)
@@ -43,6 +43,9 @@ public:
   /// \brief Return the category of the diagnostic.
   virtual unsigned getCategory() const;
   
+  /// \brief Return the category string of the diagnostic.
+  virtual CXString getCategoryText() const;
+  
   /// \brief Return the number of source ranges for the diagnostic.
   virtual unsigned getNumRanges() const;
   
@@ -82,6 +85,7 @@ public:
   std::vector<std::pair<CXSourceRange, CXString> > FixIts;
   llvm::StringRef Spelling;
   llvm::StringRef DiagOption;
+  llvm::StringRef CategoryText;
   unsigned severity;
   unsigned category;
 };
index c1bcd74387a776852d46d24407b028aa10cce4fd..8284dc96180bc1e0da9fe40734c3e8e09e4205fd 100644 (file)
@@ -79,6 +79,11 @@ unsigned CXStoredDiagnostic::getCategory() const {
   return DiagnosticIDs::getCategoryNumberForDiag(Diag.getID());
 }
 
+CXString CXStoredDiagnostic::getCategoryText() const {
+  unsigned catID = DiagnosticIDs::getCategoryNumberForDiag(Diag.getID());
+  return createCXString(DiagnosticIDs::getCategoryNameFromID(catID));
+}
+
 unsigned CXStoredDiagnostic::getNumRanges() const {
   if (Diag.getLocation().isInvalid())
     return 0;
index 07bdece8eb98576891e1bd4fa5150c435529833b..d3b64dbd0f50f3da307d1414e144ce94a89144c1 100644 (file)
@@ -6,9 +6,9 @@ clang_CXXMethod_isStatic
 clang_CXXMethod_isVirtual
 clang_Cursor_getArgument
 clang_Cursor_getNumArguments
+clang_Cursor_getObjCSelectorIndex
 clang_Cursor_getSpellingNameRange
 clang_Cursor_getTranslationUnit
-clang_Cursor_getObjCSelectorIndex
 clang_Cursor_isNull
 clang_IndexAction_create
 clang_IndexAction_dispose
@@ -97,6 +97,7 @@ clang_getDefinitionSpellingAndExtent
 clang_getDiagnostic
 clang_getDiagnosticCategory
 clang_getDiagnosticCategoryName
+clang_getDiagnosticCategoryText
 clang_getDiagnosticFixIt
 clang_getDiagnosticInSet
 clang_getDiagnosticLocation