Rework how CIndex handles diagnostics. Rather than using a callback,
we attach diagnostics to translation units and code-completion
results, so they can be queried at any time.

To facilitate this, the new StoredDiagnostic class stores a diagnostic
in a serializable/deserializable form, and ASTUnit knows how to
capture diagnostics in this stored form. CIndex's CXDiagnostic is a
thin wrapper around StoredDiagnostic, providing a C interface to
stored or de-serialized diagnostics.

I've XFAIL'd one test case temporarily, because currently we end up
storing diagnostics in an ASTUnit that's never returned to the user
(because it contains errors). I'll introduce a temporary fix for this
soon; the real fix will be to allow us to return and query invalid ASTs.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@96592 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/tools/CIndex/CIndex.cpp b/tools/CIndex/CIndex.cpp
index 0b08874..cb81988 100644
--- a/tools/CIndex/CIndex.cpp
+++ b/tools/CIndex/CIndex.cpp
@@ -933,9 +933,7 @@
 }
 
 CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
-                                              const char *ast_filename,
-                                             CXDiagnosticCallback diag_callback,
-                                              CXClientData diag_client_data) {
+                                              const char *ast_filename) {
   if (!CIdx)
     return 0;
 
@@ -945,11 +943,9 @@
   DiagnosticOptions DiagOpts;
   llvm::OwningPtr<Diagnostic> Diags;
   Diags.reset(CompilerInstance::createDiagnostics(DiagOpts, 0, 0));
-  CIndexDiagnosticClient DiagClient(diag_callback, diag_client_data);
-  Diags->setClient(&DiagClient);
-
   return ASTUnit::LoadFromPCHFile(ast_filename, *Diags,
-                                  CXXIdx->getOnlyLocalDecls());
+                                  CXXIdx->getOnlyLocalDecls(),
+                                  0, 0, true);
 }
 
 CXTranslationUnit
@@ -958,9 +954,7 @@
                                           int num_command_line_args,
                                           const char **command_line_args,
                                           unsigned num_unsaved_files,
-                                          struct CXUnsavedFile *unsaved_files,
-                                          CXDiagnosticCallback diag_callback,
-                                          CXClientData diag_client_data) {
+                                          struct CXUnsavedFile *unsaved_files) {
   if (!CIdx)
     return 0;
 
@@ -970,8 +964,6 @@
   DiagnosticOptions DiagOpts;
   llvm::OwningPtr<Diagnostic> Diags;
   Diags.reset(CompilerInstance::createDiagnostics(DiagOpts, 0, 0));
-  CIndexDiagnosticClient DiagClient(diag_callback, diag_client_data);
-  Diags->setClient(&DiagClient);
 
   llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles;
   for (unsigned I = 0; I != num_unsaved_files; ++I) {
@@ -1006,7 +998,8 @@
                                    CXXIdx->getClangResourcesPath(),
                                    CXXIdx->getOnlyLocalDecls(),
                                    RemappedFiles.data(),
-                                   RemappedFiles.size()));
+                                   RemappedFiles.size(),
+                                   /*CaptureDiagnostics=*/true));
 
     // FIXME: Until we have broader testing, just drop the entire AST if we
     // encountered an error.
@@ -1097,20 +1090,22 @@
     Diags->Report(diag::err_fe_clang) << AllArgs << ErrMsg;
   }
 
-  // FIXME: Parse the (redirected) standard error to emit diagnostics.
-
   ASTUnit *ATU = ASTUnit::LoadFromPCHFile(astTmpFile, *Diags,
                                           CXXIdx->getOnlyLocalDecls(),
                                           RemappedFiles.data(),
-                                          RemappedFiles.size());
+                                          RemappedFiles.size(),
+                                          /*CaptureDiagnostics=*/true);
   if (ATU)
     ATU->unlinkTemporaryFile();
 
   // FIXME: Currently we don't report diagnostics on invalid ASTs.
-  if (ATU)
-    ReportSerializedDiagnostics(DiagnosticsFile, *Diags,
-                                num_unsaved_files, unsaved_files,
-                                ATU->getASTContext().getLangOptions());
+  if (ATU) {
+    LoadSerializedDiagnostics(DiagnosticsFile, 
+                              num_unsaved_files, unsaved_files,
+                              ATU->getFileManager(),
+                              ATU->getSourceManager(),
+                              ATU->getDiagnostics());
+  }
 
   for (unsigned i = 0, e = TemporaryFiles.size(); i != e; ++i)
     TemporaryFiles[i].eraseFromDisk();
diff --git a/tools/CIndex/CIndex.exports b/tools/CIndex/CIndex.exports
index 2c73819..1c445b7 100644
--- a/tools/CIndex/CIndex.exports
+++ b/tools/CIndex/CIndex.exports
@@ -1,9 +1,12 @@
 _clang_annotateTokens
 _clang_codeComplete
+_clang_codeCompleteGetDiagnostic
+_clang_codeCompleteGetNumDiagnostics
 _clang_createIndex
 _clang_createTranslationUnit
 _clang_createTranslationUnitFromSourceFile
 _clang_disposeCodeCompleteResults
+_clang_disposeDiagnostic
 _clang_disposeIndex
 _clang_disposeString
 _clang_disposeTokens
@@ -25,6 +28,7 @@
 _clang_getCursorSpelling
 _clang_getCursorUSR
 _clang_getDefinitionSpellingAndExtent
+_clang_getDiagnostic
 _clang_getDiagnosticFixItInsertion
 _clang_getDiagnosticFixItKind
 _clang_getDiagnosticFixItRemoval
@@ -45,6 +49,7 @@
 _clang_getNullLocation
 _clang_getNullRange
 _clang_getNumCompletionChunks
+_clang_getNumDiagnostics
 _clang_getRange
 _clang_getRangeEnd
 _clang_getRangeStart
diff --git a/tools/CIndex/CIndexCodeCompletion.cpp b/tools/CIndex/CIndexCodeCompletion.cpp
index 4912fef..9e89463 100644
--- a/tools/CIndex/CIndexCodeCompletion.cpp
+++ b/tools/CIndex/CIndexCodeCompletion.cpp
@@ -181,7 +181,17 @@
   /// retain this buffer because the completion strings point into it.
   llvm::MemoryBuffer *Buffer;
 
+  /// \brief Diagnostics produced while performing code completion.
+  llvm::SmallVector<StoredDiagnostic, 8> Diagnostics;
+
+  /// \brief Language options used to adjust source locations.
   LangOptions LangOpts;
+
+  /// \brief Source manager, used for diagnostics.
+  SourceManager SourceMgr;
+  
+  /// \brief File manager, used for diagnostics.
+  FileManager FileMgr;
 };
 
 CXCodeCompleteResults *clang_codeComplete(CXIndex CIdx,
@@ -192,9 +202,7 @@
                                           struct CXUnsavedFile *unsaved_files,
                                           const char *complete_filename,
                                           unsigned complete_line,
-                                          unsigned complete_column,
-                                          CXDiagnosticCallback diag_callback,
-                                          CXClientData diag_client_data) {
+                                          unsigned complete_column) {
   // The indexer, which is mainly used to determine where diagnostics go.
   CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
 
@@ -202,8 +210,6 @@
   DiagnosticOptions DiagOpts;
   llvm::OwningPtr<Diagnostic> Diags;
   Diags.reset(CompilerInstance::createDiagnostics(DiagOpts, 0, 0));
-  CIndexDiagnosticClient DiagClient(diag_callback, diag_client_data);
-  Diags->setClient(&DiagClient);
   
   // The set of temporary files that we've built.
   std::vector<llvm::sys::Path> TemporaryFiles;
@@ -310,7 +316,11 @@
   // Parse the resulting source file to find code-completion results.
   using llvm::MemoryBuffer;
   using llvm::StringRef;
-  AllocatedCXCodeCompleteResults *Results = 0;
+  AllocatedCXCodeCompleteResults *Results = new AllocatedCXCodeCompleteResults;
+  Results->Results = 0;
+  Results->NumResults = 0;
+  Results->Buffer = 0;
+  // FIXME: Set Results->LangOpts!
   if (MemoryBuffer *F = MemoryBuffer::getFile(ResultsFile.c_str())) {
     llvm::SmallVector<CXCompletionResult, 4> CompletionResults;
     StringRef Buffer = F->getBuffer();
@@ -335,7 +345,6 @@
     };
 
     // Allocate the results.
-    Results = new AllocatedCXCodeCompleteResults;
     Results->Results = new CXCompletionResult [CompletionResults.size()];
     Results->NumResults = CompletionResults.size();
     memcpy(Results->Results, CompletionResults.data(),
@@ -343,13 +352,10 @@
     Results->Buffer = F;
   }
 
-  // FIXME: The LangOptions we are passing here are not at all correct. However,
-  // in the current design we must pass something in so the SourceLocations have
-  // a LangOptions object to refer to.
-  ReportSerializedDiagnostics(DiagnosticsFile, *Diags, 
-                              num_unsaved_files, unsaved_files,
-                              Results->LangOpts);
-  
+  LoadSerializedDiagnostics(DiagnosticsFile, num_unsaved_files, unsaved_files,
+                            Results->FileMgr, Results->SourceMgr, 
+                            Results->Diagnostics);
+
   for (unsigned i = 0, e = TemporaryFiles.size(); i != e; ++i)
     TemporaryFiles[i].eraseFromDisk();
 
@@ -374,4 +380,26 @@
   delete Results;
 }
 
+unsigned 
+clang_codeCompleteGetNumDiagnostics(CXCodeCompleteResults *ResultsIn) {
+  AllocatedCXCodeCompleteResults *Results
+    = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn);
+  if (!Results)
+    return 0;
+
+  return Results->Diagnostics.size();
+}
+
+CXDiagnostic 
+clang_codeCompleteGetDiagnostic(CXCodeCompleteResults *ResultsIn,
+                                unsigned Index) {
+  AllocatedCXCodeCompleteResults *Results
+    = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn);
+  if (!Results || Index >= Results->Diagnostics.size())
+    return 0;
+
+  return new CXStoredDiagnostic(Results->Diagnostics[Index], Results->LangOpts);
+}
+
+
 } // end extern "C"
diff --git a/tools/CIndex/CIndexDiagnostic.cpp b/tools/CIndex/CIndexDiagnostic.cpp
index 5c18c95..70676f0 100644
--- a/tools/CIndex/CIndexDiagnostic.cpp
+++ b/tools/CIndex/CIndexDiagnostic.cpp
@@ -1,4 +1,4 @@
-/*===-- CIndexDiagnostics.cpp - Diagnostics C Interface -----------*- C -*-===*\
+/*===-- CIndexDiagnostics.cpp - Diagnostics C Interface ---------*- C++ -*-===*\
 |*                                                                            *|
 |*                     The LLVM Compiler Infrastructure                       *|
 |*                                                                            *|
@@ -15,67 +15,44 @@
 #include "CXSourceLocation.h"
 
 #include "clang/Frontend/FrontendDiagnostic.h"
+#include "llvm/ADT/Twine.h"
 #include "llvm/Support/MemoryBuffer.h"
 
 using namespace clang;
 using namespace clang::cxloc;
 using namespace clang::cxstring;
-
-//-----------------------------------------------------------------------------
-// Opaque data structures
-//-----------------------------------------------------------------------------
-namespace {
-  /// \brief The storage behind a CXDiagnostic
-  struct CXStoredDiagnostic {
-    /// \brief The translation unit this diagnostic came from.
-    const LangOptions *LangOptsPtr;
-
-    /// \brief The severity level of this diagnostic.
-    Diagnostic::Level Level;
-
-    /// \brief A reference to the diagnostic information.
-    const DiagnosticInfo &Info;
-  };
-}
-
-//-----------------------------------------------------------------------------
-// CIndex Diagnostic Client
-//-----------------------------------------------------------------------------
-CIndexDiagnosticClient::~CIndexDiagnosticClient() { }
-
-void CIndexDiagnosticClient::BeginSourceFile(const LangOptions &LangOpts,
-                                             const Preprocessor *PP) {
-  assert(!LangOptsPtr && "Invalid state!");
-  LangOptsPtr = &LangOpts;
-}
-
-void CIndexDiagnosticClient::EndSourceFile() {
-  assert(LangOptsPtr && "Invalid state!");
-  LangOptsPtr = 0;
-}
-
-void CIndexDiagnosticClient::HandleDiagnostic(Diagnostic::Level DiagLevel,
-                                              const DiagnosticInfo &Info) {
-  if (!Callback)
-    return;
-
-  assert((LangOptsPtr || Info.getLocation().isInvalid()) &&
-         "Missing language options with located diagnostic!");
-  CXStoredDiagnostic Stored = { this->LangOptsPtr, DiagLevel, Info };
-  Callback(&Stored, ClientData);
-}
+using namespace llvm;
 
 //-----------------------------------------------------------------------------
 // C Interface Routines
 //-----------------------------------------------------------------------------
 extern "C" {
 
+unsigned clang_getNumDiagnostics(CXTranslationUnit Unit) {
+  ASTUnit *CXXUnit = static_cast<ASTUnit *>(Unit);
+  return CXXUnit? CXXUnit->diag_size() : 0;
+}
+
+CXDiagnostic clang_getDiagnostic(CXTranslationUnit Unit, unsigned Index) {
+  ASTUnit *CXXUnit = static_cast<ASTUnit *>(Unit);
+  if (!CXXUnit || Index >= CXXUnit->diag_size())
+    return 0;
+
+  return new CXStoredDiagnostic(CXXUnit->diag_begin()[Index],
+                                CXXUnit->getASTContext().getLangOptions());
+}
+
+void clang_disposeDiagnostic(CXDiagnostic Diagnostic) {
+  CXStoredDiagnostic *Stored = static_cast<CXStoredDiagnostic *>(Diagnostic);
+  delete Stored;
+}
+
 enum CXDiagnosticSeverity clang_getDiagnosticSeverity(CXDiagnostic Diag) {
   CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
   if (!StoredDiag)
     return CXDiagnostic_Ignored;
 
-  switch (StoredDiag->Level) {
+  switch (StoredDiag->Diag.getLevel()) {
   case Diagnostic::Ignored: return CXDiagnostic_Ignored;
   case Diagnostic::Note:    return CXDiagnostic_Note;
   case Diagnostic::Warning: return CXDiagnostic_Warning;
@@ -89,12 +66,12 @@
 
 CXSourceLocation clang_getDiagnosticLocation(CXDiagnostic Diag) {
   CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
-  if (!StoredDiag || StoredDiag->Info.getLocation().isInvalid())
+  if (!StoredDiag || StoredDiag->Diag.getLocation().isInvalid())
     return clang_getNullLocation();
 
-  return translateSourceLocation(StoredDiag->Info.getLocation().getManager(),
-                                 *StoredDiag->LangOptsPtr,
-                                 StoredDiag->Info.getLocation());
+  return translateSourceLocation(StoredDiag->Diag.getLocation().getManager(),
+                                 StoredDiag->LangOpts,
+                                 StoredDiag->Diag.getLocation());
 }
 
 CXString clang_getDiagnosticSpelling(CXDiagnostic Diag) {
@@ -102,28 +79,26 @@
   if (!StoredDiag)
     return createCXString("");
 
-  llvm::SmallString<64> Spelling;
-  StoredDiag->Info.FormatDiagnostic(Spelling);
-  return createCXString(Spelling.str(), true);
+  return createCXString(StoredDiag->Diag.getMessage(), false);
 }
 
 unsigned clang_getDiagnosticNumRanges(CXDiagnostic Diag) {
   CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
-  if (!StoredDiag || StoredDiag->Info.getLocation().isInvalid())
+  if (!StoredDiag || StoredDiag->Diag.getLocation().isInvalid())
     return 0;
 
-  return StoredDiag->Info.getNumRanges();
+  return StoredDiag->Diag.range_size();
 }
 
 CXSourceRange clang_getDiagnosticRange(CXDiagnostic Diag, unsigned Range) {
   CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
-  if (!StoredDiag || Range >= StoredDiag->Info.getNumRanges() ||
-      StoredDiag->Info.getLocation().isInvalid())
+  if (!StoredDiag || Range >= StoredDiag->Diag.range_size() ||
+      StoredDiag->Diag.getLocation().isInvalid())
     return clang_getNullRange();
 
-  return translateSourceRange(StoredDiag->Info.getLocation().getManager(),
-                              *StoredDiag->LangOptsPtr,
-                              StoredDiag->Info.getRange(Range));
+  return translateSourceRange(StoredDiag->Diag.getLocation().getManager(),
+                              StoredDiag->LangOpts,
+                              StoredDiag->Diag.range_begin()[Range]);
 }
 
 unsigned clang_getDiagnosticNumFixIts(CXDiagnostic Diag) {
@@ -131,17 +106,16 @@
   if (!StoredDiag)
     return 0;
 
-  return StoredDiag->Info.getNumCodeModificationHints();
+  return StoredDiag->Diag.fixit_size();
 }
 
 enum CXFixItKind clang_getDiagnosticFixItKind(CXDiagnostic Diag,
                                               unsigned FixIt) {
   CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
-  if (!StoredDiag || FixIt >= StoredDiag->Info.getNumCodeModificationHints())
+  if (!StoredDiag || FixIt >= StoredDiag->Diag.fixit_size())
     return CXFixIt_Insertion;
 
-  const CodeModificationHint &Hint
-    = StoredDiag->Info.getCodeModificationHint(FixIt);
+  const CodeModificationHint &Hint = StoredDiag->Diag.fixit_begin()[FixIt];
   if (Hint.RemoveRange.isInvalid())
     return CXFixIt_Insertion;
   if (Hint.InsertionLoc.isInvalid())
@@ -157,16 +131,15 @@
     *Location = clang_getNullLocation();
 
   CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
-  if (!StoredDiag || FixIt >= StoredDiag->Info.getNumCodeModificationHints())
+  if (!StoredDiag || FixIt >= StoredDiag->Diag.fixit_size())
     return createCXString("");
 
-  const CodeModificationHint &Hint
-    = StoredDiag->Info.getCodeModificationHint(FixIt);
+  const CodeModificationHint &Hint = StoredDiag->Diag.fixit_begin()[FixIt];
 
-  if (Location && StoredDiag->Info.getLocation().isValid())
+  if (Location && StoredDiag->Diag.getLocation().isValid())
     *Location = translateSourceLocation(
-                                    StoredDiag->Info.getLocation().getManager(),
-                                        *StoredDiag->LangOptsPtr,
+                                    StoredDiag->Diag.getLocation().getManager(),
+                                        StoredDiag->LangOpts,
                                         Hint.InsertionLoc);
   return createCXString(Hint.CodeToInsert);
 }
@@ -174,14 +147,13 @@
 CXSourceRange clang_getDiagnosticFixItRemoval(CXDiagnostic Diag,
                                               unsigned FixIt) {
   CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
-  if (!StoredDiag || FixIt >= StoredDiag->Info.getNumCodeModificationHints() ||
-      StoredDiag->Info.getLocation().isInvalid())
+  if (!StoredDiag || FixIt >= StoredDiag->Diag.fixit_size() ||
+      StoredDiag->Diag.getLocation().isInvalid())
     return clang_getNullRange();
 
-  const CodeModificationHint &Hint
-    = StoredDiag->Info.getCodeModificationHint(FixIt);
-  return translateSourceRange(StoredDiag->Info.getLocation().getManager(),
-                              *StoredDiag->LangOptsPtr,
+  const CodeModificationHint &Hint = StoredDiag->Diag.fixit_begin()[FixIt];
+  return translateSourceRange(StoredDiag->Diag.getLocation().getManager(),
+                              StoredDiag->LangOpts,
                               Hint.RemoveRange);
 }
 
@@ -192,30 +164,30 @@
     *Range = clang_getNullRange();
 
   CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
-  if (!StoredDiag || FixIt >= StoredDiag->Info.getNumCodeModificationHints() ||
-      StoredDiag->Info.getLocation().isInvalid()) {
+  if (!StoredDiag || FixIt >= StoredDiag->Diag.fixit_size() ||
+      StoredDiag->Diag.getLocation().isInvalid()) {
     if (Range)
       *Range = clang_getNullRange();
 
     return createCXString("");
   }
 
-  const CodeModificationHint &Hint
-    = StoredDiag->Info.getCodeModificationHint(FixIt);
+  const CodeModificationHint &Hint = StoredDiag->Diag.fixit_begin()[FixIt];
   if (Range)
-    *Range = translateSourceRange(StoredDiag->Info.getLocation().getManager(),
-                                  *StoredDiag->LangOptsPtr,
+    *Range = translateSourceRange(StoredDiag->Diag.getLocation().getManager(),
+                                  StoredDiag->LangOpts,
                                   Hint.RemoveRange);
   return createCXString(Hint.CodeToInsert);
 }
 
 } // end extern "C"
 
-void clang::ReportSerializedDiagnostics(const llvm::sys::Path &DiagnosticsPath,
-                                        Diagnostic &Diags,
-                                        unsigned num_unsaved_files,
-                                        struct CXUnsavedFile *unsaved_files,
-                                        const LangOptions &LangOpts) {
+void clang::LoadSerializedDiagnostics(const llvm::sys::Path &DiagnosticsPath,
+                                      unsigned num_unsaved_files,
+                                      struct CXUnsavedFile *unsaved_files,
+                                      FileManager &FileMgr,
+                                      SourceManager &SourceMgr,
+                                     SmallVectorImpl<StoredDiagnostic> &Diags) {
   using llvm::MemoryBuffer;
   using llvm::StringRef;
   MemoryBuffer *F = MemoryBuffer::getFile(DiagnosticsPath.c_str());
@@ -223,15 +195,15 @@
     return;
 
   // Enter the unsaved files into the file manager.
-  SourceManager SourceMgr;
-  FileManager FileMgr;
   for (unsigned I = 0; I != num_unsaved_files; ++I) {
     const FileEntry *File = FileMgr.getVirtualFile(unsaved_files[I].Filename,
                                                    unsaved_files[I].Length,
                                                    0);
     if (!File) {
-      Diags.Report(diag::err_fe_remap_missing_from_file)
-        << unsaved_files[I].Filename;
+      // FIXME: Hard to localize when we have no diagnostics engine!
+      Diags.push_back(StoredDiagnostic(Diagnostic::Fatal,
+                            (Twine("could not remap from missing file ") +
+                                   unsaved_files[I].Filename).str()));
       return;
     }
 
@@ -244,18 +216,16 @@
     SourceMgr.overrideFileContents(File, Buffer);
   }
 
-  Diags.getClient()->BeginSourceFile(LangOpts, 0);
-
   // Parse the diagnostics, emitting them one by one until we've
   // exhausted the data.
   StringRef Buffer = F->getBuffer();
   const char *Memory = Buffer.data(), *MemoryEnd = Memory + Buffer.size();
   while (Memory != MemoryEnd) {
-    DiagnosticBuilder DB = Diags.Deserialize(FileMgr, SourceMgr,
-                                             Memory, MemoryEnd);
-    if (!DB.isActive())
+    StoredDiagnostic Stored = StoredDiagnostic::Deserialize(FileMgr, SourceMgr,
+                                                            Memory, MemoryEnd);
+    if (!Stored)
       return;
-  }
 
-  Diags.getClient()->EndSourceFile();
+    Diags.push_back(Stored);
+  }
 }
diff --git a/tools/CIndex/CIndexDiagnostic.h b/tools/CIndex/CIndexDiagnostic.h
index 9f7ae51..79a5df0 100644
--- a/tools/CIndex/CIndexDiagnostic.h
+++ b/tools/CIndex/CIndexDiagnostic.h
@@ -16,6 +16,7 @@
 #include "clang-c/Index.h"
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/LangOptions.h"
+#include "llvm/ADT/SmallVector.h"
 
 namespace llvm { namespace sys {
 class Path;
@@ -26,40 +27,25 @@
 class Diagnostic;
 class LangOptions;
 class Preprocessor;
-  
-/**
- * \brief Diagnostic client that translates Clang diagnostics into diagnostics
- * for the C interface to Clang.
- */
-class CIndexDiagnosticClient : public DiagnosticClient {
-  CXDiagnosticCallback Callback;
-  CXClientData ClientData;
-  const LangOptions *LangOptsPtr;
-  
-public:
-  CIndexDiagnosticClient(CXDiagnosticCallback Callback,
-                         CXClientData ClientData)
-    : Callback(Callback), ClientData(ClientData), LangOptsPtr(0) { }
-  
-  virtual ~CIndexDiagnosticClient();
-  
-  virtual void BeginSourceFile(const LangOptions &LangOpts,
-                               const Preprocessor *PP);
-  
-  virtual void EndSourceFile();
 
-  virtual void HandleDiagnostic(Diagnostic::Level DiagLevel,
-                                const DiagnosticInfo &Info);
+/// \brief The storage behind a CXDiagnostic
+struct CXStoredDiagnostic {
+  const StoredDiagnostic &Diag;
+  const LangOptions &LangOpts;
+  
+  CXStoredDiagnostic(const StoredDiagnostic &Diag,
+                     const LangOptions &LangOpts)
+    : Diag(Diag), LangOpts(LangOpts) { }
 };
-
+  
 /// \brief Given the path to a file that contains binary, serialized
-/// diagnostics produced by Clang, emit those diagnostics via the
-/// given diagnostic engine.
-void ReportSerializedDiagnostics(const llvm::sys::Path &DiagnosticsPath,
-                                 Diagnostic &Diags,
-                                 unsigned num_unsaved_files,
-                                 struct CXUnsavedFile *unsaved_files,
-                                 const LangOptions &LangOpts);
+/// diagnostics produced by Clang, load those diagnostics.
+void LoadSerializedDiagnostics(const llvm::sys::Path &DiagnosticsPath,
+                               unsigned num_unsaved_files,
+                               struct CXUnsavedFile *unsaved_files,
+                               FileManager &FileMgr,
+                               SourceManager &SourceMgr,
+                               llvm::SmallVectorImpl<StoredDiagnostic> &Diags);
 
 } // end namespace clang
 
diff --git a/tools/c-index-test/c-index-test.c b/tools/c-index-test/c-index-test.c
index 640375a..b983d3a 100644
--- a/tools/c-index-test/c-index-test.c
+++ b/tools/c-index-test/c-index-test.c
@@ -28,10 +28,6 @@
 extern char *basename(const char *);
 #endif
 
-static void PrintDiagnosticCallback(CXDiagnostic Diagnostic,
-                                    CXClientData ClientData);
-
-
 static void PrintExtent(FILE *out, unsigned begin_line, unsigned begin_column,
                         unsigned end_line, unsigned end_column) {
   fprintf(out, "[%d:%d - %d:%d]", begin_line, begin_column,
@@ -41,7 +37,7 @@
 static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
                                       CXTranslationUnit *TU) {
 
-  *TU = clang_createTranslationUnit(Idx, file, PrintDiagnosticCallback, stderr);
+  *TU = clang_createTranslationUnit(Idx, file);
   if (!TU) {
     fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
     return 0;
@@ -199,9 +195,8 @@
 
 typedef void (*PostVisitTU)(CXTranslationUnit);
 
-static void PrintDiagnosticCallback(CXDiagnostic Diagnostic,
-                                    CXClientData ClientData) {
-  FILE *out = (FILE *)ClientData;
+void PrintDiagnostic(CXDiagnostic Diagnostic) {
+  FILE *out = stderr;
   CXFile file;
   unsigned line, column;
   CXString text;
@@ -321,6 +316,15 @@
   }
 }
 
+void PrintDiagnostics(CXTranslationUnit TU) {
+  int i, n = clang_getNumDiagnostics(TU);
+  for (i = 0; i != n; ++i) {
+    CXDiagnostic Diag = clang_getDiagnostic(TU, i);
+    PrintDiagnostic(Diag);
+    clang_disposeDiagnostic(Diag);
+  }
+}
+
 /******************************************************************************/
 /* Logic for testing traversal.                                               */
 /******************************************************************************/
@@ -518,6 +522,7 @@
   if (PV)
     PV(TU);
 
+  PrintDiagnostics(TU);
   clang_disposeTranslationUnit(TU);
   return 0;
 }
@@ -567,9 +572,7 @@
                                                  argc - num_unsaved_files,
                                                  argv + num_unsaved_files,
                                                  num_unsaved_files,
-                                                 unsaved_files,
-                                                 PrintDiagnosticCallback,
-                                                 stderr);
+                                                 unsaved_files);
   if (!TU) {
     fprintf(stderr, "Unable to load translation unit!\n");
     clang_disposeIndex(Idx);
@@ -815,13 +818,18 @@
                                argv[argc - 1], argc - num_unsaved_files - 3,
                                argv + num_unsaved_files + 2,
                                num_unsaved_files, unsaved_files,
-                               filename, line, column,
-                               PrintDiagnosticCallback, stderr);
+                               filename, line, column);
 
   if (results) {
     unsigned i, n = results->NumResults;
     for (i = 0; i != n; ++i)
       print_completion_result(results->Results + i, stdout);
+    n = clang_codeCompleteGetNumDiagnostics(results);
+    for (i = 0; i != n; ++i) {
+      CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
+      PrintDiagnostic(diag);
+      clang_disposeDiagnostic(diag);
+    }
     clang_disposeCodeCompleteResults(results);
   }
 
@@ -874,9 +882,7 @@
                                   argc - num_unsaved_files - 2 - NumLocations,
                                    argv + num_unsaved_files + 1 + NumLocations,
                                                  num_unsaved_files,
-                                                 unsaved_files,
-                                                 PrintDiagnosticCallback,
-                                                 stderr);
+                                                 unsaved_files);
   if (!TU) {
     fprintf(stderr, "unable to parse input\n");
     return -1;
@@ -895,6 +901,7 @@
     free(Locations[Loc].filename);
   }
 
+  PrintDiagnostics(TU);
   clang_disposeTranslationUnit(TU);
   clang_disposeIndex(CIdx);
   free(Locations);
@@ -933,9 +940,7 @@
                                                  argc - num_unsaved_files - 3,
                                                  argv + num_unsaved_files + 2,
                                                  num_unsaved_files,
-                                                 unsaved_files,
-                                                 PrintDiagnosticCallback,
-                                                 stderr);
+                                                 unsaved_files);
   if (!TU) {
     fprintf(stderr, "unable to parse input\n");
     clang_disposeIndex(CIdx);
@@ -1000,6 +1005,7 @@
   free(cursors);
 
  teardown:
+  PrintDiagnostics(TU);
   clang_disposeTranslationUnit(TU);
   clang_disposeIndex(CIdx);
   free(filename);