Implement clang_getDiagnosticCategoryText() to provide a way for a client of libclang to accurately
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
diff --git a/tools/c-index-test/c-index-test.c b/tools/c-index-test/c-index-test.c
index a81de27..04e7a50 100644
--- a/tools/c-index-test/c-index-test.c
+++ b/tools/c-index-test/c-index-test.c
@@ -2586,9 +2586,9 @@
     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 @@
       fprintf(stderr, " [%s]", DiagOptionStr);
     }
     
+    DiagCat = clang_getDiagnosticCategoryText(D);
+    DiagCatStr = clang_getCString(DiagCat);
+    if (DiagCatStr) {
+      fprintf(stderr, " [%s]", DiagCatStr);
+    }
+    
     fprintf(stderr, "\n");
     
     printRanges(D, indent);
diff --git a/tools/libclang/CIndexDiagnostic.cpp b/tools/libclang/CIndexDiagnostic.cpp
index d8a2b05..8fbe3d8 100644
--- a/tools/libclang/CIndexDiagnostic.cpp
+++ b/tools/libclang/CIndexDiagnostic.cpp
@@ -72,6 +72,8 @@
   }
   
   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 @@
         }
         
         if (Options & CXDiagnostic_DisplayCategoryName) {
-          CXString CategoryName = clang_getDiagnosticCategoryName(CategoryID);
+          CXString CategoryName = clang_getDiagnosticCategoryText(Diagnostic);
           if (NeedBracket)
             Out << " [";
           if (NeedComma)
@@ -385,9 +387,16 @@
 }
   
 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();
diff --git a/tools/libclang/CIndexDiagnostic.h b/tools/libclang/CIndexDiagnostic.h
index 3066998..b1c3978 100644
--- a/tools/libclang/CIndexDiagnostic.h
+++ b/tools/libclang/CIndexDiagnostic.h
@@ -73,7 +73,10 @@
   
   /// \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 @@
   /// \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;
   
diff --git a/tools/libclang/CXLoadedDiagnostic.cpp b/tools/libclang/CXLoadedDiagnostic.cpp
index d52c183..e5b6ccc 100644
--- a/tools/libclang/CXLoadedDiagnostic.cpp
+++ b/tools/libclang/CXLoadedDiagnostic.cpp
@@ -119,6 +119,10 @@
   return category;
 }
 
+CXString CXLoadedDiagnostic::getCategoryText() const {
+  return cxstring::createCXString(CategoryText);
+}
+
 unsigned CXLoadedDiagnostic::getNumRanges() const {
   return Ranges.size();
 }
@@ -650,6 +654,7 @@
         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;
       }
diff --git a/tools/libclang/CXLoadedDiagnostic.h b/tools/libclang/CXLoadedDiagnostic.h
index 9eff144..d4a321e 100644
--- a/tools/libclang/CXLoadedDiagnostic.h
+++ b/tools/libclang/CXLoadedDiagnostic.h
@@ -43,6 +43,9 @@
   /// \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 @@
   std::vector<std::pair<CXSourceRange, CXString> > FixIts;
   llvm::StringRef Spelling;
   llvm::StringRef DiagOption;
+  llvm::StringRef CategoryText;
   unsigned severity;
   unsigned category;
 };
diff --git a/tools/libclang/CXStoredDiagnostic.cpp b/tools/libclang/CXStoredDiagnostic.cpp
index c1bcd74..8284dc9 100644
--- a/tools/libclang/CXStoredDiagnostic.cpp
+++ b/tools/libclang/CXStoredDiagnostic.cpp
@@ -79,6 +79,11 @@
   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;
diff --git a/tools/libclang/libclang.exports b/tools/libclang/libclang.exports
index 07bdece..d3b64db 100644
--- a/tools/libclang/libclang.exports
+++ b/tools/libclang/libclang.exports
@@ -6,9 +6,9 @@
 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_getDiagnostic
 clang_getDiagnosticCategory
 clang_getDiagnosticCategoryName
+clang_getDiagnosticCategoryText
 clang_getDiagnosticFixIt
 clang_getDiagnosticInSet
 clang_getDiagnosticLocation