[cfe-commits] r96592 - in /cfe/trunk: include/clang-c/Index.h include/clang/Basic/Diagnostic.h include/clang/Frontend/ASTUnit.h lib/Basic/Diagnostic.cpp lib/Frontend/ASTUnit.cpp lib/Frontend/CompilerInstance.cpp test/Index/cindex-on-invalid.m tools/CIndex/CIndex.cpp tools/CIndex/CIndex.exports tools/CIndex/CIndexCodeCompletion.cpp tools/CIndex/CIndexDiagnostic.cpp tools/CIndex/CIndexDiagnostic.h tools/c-index-test/c-index-test.c

Douglas Gregor dgregor at apple.com
Thu Feb 18 10:08:43 PST 2010


Author: dgregor
Date: Thu Feb 18 12:08:43 2010
New Revision: 96592

URL: http://llvm.org/viewvc/llvm-project?rev=96592&view=rev
Log:
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.


Modified:
    cfe/trunk/include/clang-c/Index.h
    cfe/trunk/include/clang/Basic/Diagnostic.h
    cfe/trunk/include/clang/Frontend/ASTUnit.h
    cfe/trunk/lib/Basic/Diagnostic.cpp
    cfe/trunk/lib/Frontend/ASTUnit.cpp
    cfe/trunk/lib/Frontend/CompilerInstance.cpp
    cfe/trunk/test/Index/cindex-on-invalid.m
    cfe/trunk/tools/CIndex/CIndex.cpp
    cfe/trunk/tools/CIndex/CIndex.exports
    cfe/trunk/tools/CIndex/CIndexCodeCompletion.cpp
    cfe/trunk/tools/CIndex/CIndexDiagnostic.cpp
    cfe/trunk/tools/CIndex/CIndexDiagnostic.h
    cfe/trunk/tools/c-index-test/c-index-test.c

Modified: cfe/trunk/include/clang-c/Index.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=96592&r1=96591&r2=96592&view=diff

==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Thu Feb 18 12:08:43 2010
@@ -416,17 +416,27 @@
 typedef void *CXDiagnostic;
 
 /**
- * \brief Callback function invoked for each diagnostic emitted during
- * translation.
+ * \brief Determine the number of diagnostics produced for the given
+ * translation unit.
+ */
+CINDEX_LINKAGE unsigned clang_getNumDiagnostics(CXTranslationUnit Unit);
+
+/**
+ * \brief Retrieve a diagnostic associated with the given translation unit.
  *
- * \param Diagnostic the diagnostic emitted during translation. This
- * diagnostic pointer is only valid during the execution of the
- * callback.
+ * \param Unit the translation unit to query.
+ * \param Index the zero-based diagnostic number to retrieve.
  *
- * \param ClientData the callback client data.
+ * \returns the requested diagnostic. This diagnostic must be freed
+ * via a call to \c clang_disposeDiagnostic().
  */
-typedef void (*CXDiagnosticCallback)(CXDiagnostic Diagnostic,
-                                     CXClientData ClientData);
+CINDEX_LINKAGE CXDiagnostic clang_getDiagnostic(CXTranslationUnit Unit,
+                                                unsigned Index);
+
+/**
+ * \brief Destroy a diagnostic.
+ */
+CINDEX_LINKAGE void clang_disposeDiagnostic(CXDiagnostic Diagnostic);
 
 /**
  * \brief Determine the severity of the given diagnostic.
@@ -600,17 +610,13 @@
                                          int num_clang_command_line_args,
                                          const char **clang_command_line_args,
                                          unsigned num_unsaved_files,
-                                         struct CXUnsavedFile *unsaved_files,
-                                         CXDiagnosticCallback diag_callback,
-                                         CXClientData diag_client_data);
+                                         struct CXUnsavedFile *unsaved_files);
  
 /**
  * \brief Create a translation unit from an AST file (-emit-ast).
  */
 CINDEX_LINKAGE CXTranslationUnit clang_createTranslationUnit(CXIndex, 
-                                             const char *ast_filename,
-                                             CXDiagnosticCallback diag_callback,
-                                             CXClientData diag_client_data);
+                                             const char *ast_filename);
 
 /**
  * \brief Destroy the specified CXTranslationUnit object.
@@ -1625,9 +1631,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);
 
 /**
  * \brief Free the given set of code-completion results.
@@ -1636,6 +1640,26 @@
 void clang_disposeCodeCompleteResults(CXCodeCompleteResults *Results);
 
 /**
+ * \brief Determine the number of diagnostics produced prior to the
+ * location where code completion was performed.
+ */
+CINDEX_LINKAGE 
+unsigned clang_codeCompleteGetNumDiagnostics(CXCodeCompleteResults *Results);
+
+/**
+ * \brief Retrieve a diagnostic associated with the given code completion.
+ *
+ * \param Result the code completion results to query.
+ * \param Index the zero-based diagnostic number to retrieve.
+ *
+ * \returns the requested diagnostic. This diagnostic must be freed
+ * via a call to \c clang_disposeDiagnostic().
+ */
+CINDEX_LINKAGE 
+CXDiagnostic clang_codeCompleteGetDiagnostic(CXCodeCompleteResults *Results,
+                                             unsigned Index);
+
+/**
  * @}
  */
 

Modified: cfe/trunk/include/clang/Basic/Diagnostic.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/Diagnostic.h?rev=96592&r1=96591&r2=96592&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/Diagnostic.h (original)
+++ cfe/trunk/include/clang/Basic/Diagnostic.h Thu Feb 18 12:08:43 2010
@@ -403,13 +403,6 @@
   /// \brief Clear out the current diagnostic.
   void Clear() { CurDiagID = ~0U; }
 
-  /// Deserialize - Deserialize the first diagnostic within the memory
-  /// [Memory, MemoryEnd), producing a new diagnostic builder describing the
-  /// deserialized diagnostic. If the memory does not contain a
-  /// diagnostic, returns a diagnostic builder with no diagnostic ID.
-  DiagnosticBuilder Deserialize(FileManager &FM, SourceManager &SM, 
-                                const char *&Memory, const char *MemoryEnd);
-
 private:
   /// getDiagnosticMappingInfo - Return the mapping info currently set for the
   /// specified builtin diagnostic.  This returns the high bit encoding, or zero
@@ -799,12 +792,54 @@
   /// output buffer using the arguments stored in this diagnostic.
   void FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
                         llvm::SmallVectorImpl<char> &OutStr) const;
+};
+
+/**
+ * \brief Represents a diagnostic in a form that can be serialized and
+ * deserialized.
+ */
+class StoredDiagnostic {
+  Diagnostic::Level Level;
+  FullSourceLoc Loc;
+  std::string Message;
+  std::vector<SourceRange> Ranges;
+  std::vector<CodeModificationHint> FixIts;
+
+public:
+  StoredDiagnostic();
+  StoredDiagnostic(Diagnostic::Level Level, const DiagnosticInfo &Info);
+  StoredDiagnostic(Diagnostic::Level Level, llvm::StringRef Message);
+  ~StoredDiagnostic();
+
+  /// \brief Evaluates true when this object stores a diagnostic.
+  operator bool() const { return Message.size() > 0; }
+
+  Diagnostic::Level getLevel() const { return Level; }
+  const FullSourceLoc &getLocation() const { return Loc; }
+  llvm::StringRef getMessage() const { return Message; }
+  
+  typedef std::vector<SourceRange>::const_iterator range_iterator;
+  range_iterator range_begin() const { return Ranges.begin(); }
+  range_iterator range_end() const { return Ranges.end(); }
+  unsigned range_size() const { return Ranges.size(); }
+
+  typedef std::vector<CodeModificationHint>::const_iterator fixit_iterator;
+  fixit_iterator fixit_begin() const { return FixIts.begin(); }
+  fixit_iterator fixit_end() const { return FixIts.end(); }
+  unsigned fixit_size() const { return FixIts.size(); }
 
   /// Serialize - Serialize the given diagnostic (with its diagnostic
   /// level) to the given stream. Serialization is a lossy operation,
   /// since the specific diagnostic ID and any macro-instantiation
   /// information is lost.
-  void Serialize(Diagnostic::Level DiagLevel, llvm::raw_ostream &OS) const;
+  void Serialize(llvm::raw_ostream &OS) const;
+
+  /// Deserialize - Deserialize the first diagnostic within the memory
+  /// [Memory, MemoryEnd), producing a new diagnostic builder describing the
+  /// deserialized diagnostic. If the memory does not contain a
+  /// diagnostic, returns a diagnostic builder with no diagnostic ID.
+  static StoredDiagnostic Deserialize(FileManager &FM, SourceManager &SM, 
+                                   const char *&Memory, const char *MemoryEnd);
 };
 
 /// DiagnosticClient - This is an abstract interface implemented by clients of

Modified: cfe/trunk/include/clang/Frontend/ASTUnit.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/ASTUnit.h?rev=96592&r1=96591&r2=96592&view=diff

==============================================================================
--- cfe/trunk/include/clang/Frontend/ASTUnit.h (original)
+++ cfe/trunk/include/clang/Frontend/ASTUnit.h Thu Feb 18 12:08:43 2010
@@ -18,6 +18,7 @@
 #include "llvm/ADT/OwningPtr.h"
 #include "clang/Basic/FileManager.h"
 #include "clang/Index/ASTLocation.h"
+#include "llvm/ADT/SmallVector.h"
 #include <string>
 #include <vector>
 #include <cassert>
@@ -80,6 +81,10 @@
   // Critical optimization when using clang_getCursor().
   ASTLocation LastLoc;
 
+  /// \brief The set of diagnostics produced when creating this
+  /// translation unit.
+  llvm::SmallVector<StoredDiagnostic, 4> Diagnostics;
+
   ASTUnit(const ASTUnit&); // DO NOT IMPLEMENT
   ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT
 
@@ -120,6 +125,15 @@
     return TopLevelDecls;
   }
 
+  // Retrieve the diagnostics associated with this AST
+  typedef const StoredDiagnostic * diag_iterator;
+  diag_iterator diag_begin() const { return Diagnostics.begin(); }
+  diag_iterator diag_end() const { return Diagnostics.end(); }
+  unsigned diag_size() const { return Diagnostics.size(); }
+  llvm::SmallVector<StoredDiagnostic, 4> &getDiagnostics() { 
+    return Diagnostics; 
+  }
+
   /// \brief A mapping from a file name to the memory buffer that stores the
   /// remapped contents of that file.
   typedef std::pair<std::string, const llvm::MemoryBuffer *> RemappedFile;
@@ -136,7 +150,8 @@
                                   Diagnostic &Diags,
                                   bool OnlyLocalDecls = false,
                                   RemappedFile *RemappedFiles = 0,
-                                  unsigned NumRemappedFiles = 0);
+                                  unsigned NumRemappedFiles = 0,
+                                  bool CaptureDiagnostics = false);
 
   /// LoadFromCompilerInvocation - Create an ASTUnit from a source file, via a
   /// CompilerInvocation object.
@@ -151,7 +166,8 @@
   // shouldn't need to specify them at construction time.
   static ASTUnit *LoadFromCompilerInvocation(CompilerInvocation *CI,
                                              Diagnostic &Diags,
-                                             bool OnlyLocalDecls = false);
+                                             bool OnlyLocalDecls = false,
+                                             bool CaptureDiagnostics = false);
 
   /// LoadFromCommandLine - Create an ASTUnit from a vector of command line
   /// arguments, which must specify exactly one source file.
@@ -173,7 +189,8 @@
                                       llvm::StringRef ResourceFilesPath,
                                       bool OnlyLocalDecls = false,
                                       RemappedFile *RemappedFiles = 0,
-                                      unsigned NumRemappedFiles = 0);
+                                      unsigned NumRemappedFiles = 0,
+                                      bool CaptureDiagnostics = false);
 };
 
 } // namespace clang

Modified: cfe/trunk/lib/Basic/Diagnostic.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/Diagnostic.cpp?rev=96592&r1=96591&r2=96592&view=diff

==============================================================================
--- cfe/trunk/lib/Basic/Diagnostic.cpp (original)
+++ cfe/trunk/lib/Basic/Diagnostic.cpp Thu Feb 18 12:08:43 2010
@@ -387,123 +387,6 @@
   return Result;
 }
 
-static bool ReadUnsigned(const char *&Memory, const char *MemoryEnd,
-                         unsigned &Value) {
-  if (Memory + sizeof(unsigned) > MemoryEnd)
-    return true;
-
-  memmove(&Value, Memory, sizeof(unsigned));
-  Memory += sizeof(unsigned);
-  return false;
-}
-
-static bool ReadSourceLocation(FileManager &FM, SourceManager &SM,
-                               const char *&Memory, const char *MemoryEnd,
-                               SourceLocation &Location) {
-  // Read the filename.
-  unsigned FileNameLen = 0;
-  if (ReadUnsigned(Memory, MemoryEnd, FileNameLen) || 
-      Memory + FileNameLen > MemoryEnd)
-    return true;
-
-  llvm::StringRef FileName(Memory, FileNameLen);
-  Memory += FileNameLen;
-
-  // Read the line, column.
-  unsigned Line = 0, Column = 0;
-  if (ReadUnsigned(Memory, MemoryEnd, Line) ||
-      ReadUnsigned(Memory, MemoryEnd, Column))
-    return true;
-
-  if (FileName.empty()) {
-    Location = SourceLocation();
-    return false;
-  }
-
-  const FileEntry *File = FM.getFile(FileName);
-  if (!File)
-    return true;
-
-  // Make sure that this file has an entry in the source manager.
-  if (!SM.hasFileInfo(File))
-    SM.createFileID(File, SourceLocation(), SrcMgr::C_User);
-
-  Location = SM.getLocation(File, Line, Column);
-  return false;
-}
-
-DiagnosticBuilder Diagnostic::Deserialize(FileManager &FM, SourceManager &SM, 
-                                          const char *&Memory, 
-                                          const char *MemoryEnd) {
-  if (Memory == MemoryEnd)
-    return DiagnosticBuilder(0);
-
-  // Read the severity level.
-  unsigned Level = 0;
-  if (ReadUnsigned(Memory, MemoryEnd, Level) || Level > Fatal)
-    return DiagnosticBuilder(0);
-
-  // Read the source location.
-  SourceLocation Location;
-  if (ReadSourceLocation(FM, SM, Memory, MemoryEnd, Location))
-    return DiagnosticBuilder(0);
-
-  // Read the diagnostic text.
-  if (Memory == MemoryEnd)
-    return DiagnosticBuilder(0);
-
-  unsigned MessageLen = 0;
-  if (ReadUnsigned(Memory, MemoryEnd, MessageLen) ||
-      Memory + MessageLen > MemoryEnd)
-    return DiagnosticBuilder(0);
-  
-  llvm::StringRef Message(Memory, MessageLen);
-  Memory += MessageLen;
-
-  // At this point, we have enough information to form a diagnostic. Do so.
-  unsigned DiagID = getCustomDiagID((enum Level)Level, Message);
-  DiagnosticBuilder DB = Report(FullSourceLoc(Location, SM), DiagID);
-  if (Memory == MemoryEnd)
-    return DB;
-
-  // Read the source ranges.
-  unsigned NumSourceRanges = 0;
-  if (ReadUnsigned(Memory, MemoryEnd, NumSourceRanges))
-    return DB;
-  for (unsigned I = 0; I != NumSourceRanges; ++I) {
-    SourceLocation Begin, End;
-    if (ReadSourceLocation(FM, SM, Memory, MemoryEnd, Begin) ||
-        ReadSourceLocation(FM, SM, Memory, MemoryEnd, End))
-      return DB;
-
-    DB << SourceRange(Begin, End);
-  }
-
-  // Read the fix-it hints.
-  unsigned NumFixIts = 0;
-  if (ReadUnsigned(Memory, MemoryEnd, NumFixIts))
-    return DB;
-  for (unsigned I = 0; I != NumFixIts; ++I) {
-    SourceLocation RemoveBegin, RemoveEnd, InsertionLoc;
-    unsigned InsertLen = 0;
-    if (ReadSourceLocation(FM, SM, Memory, MemoryEnd, RemoveBegin) ||
-        ReadSourceLocation(FM, SM, Memory, MemoryEnd, RemoveEnd) ||
-        ReadSourceLocation(FM, SM, Memory, MemoryEnd, InsertionLoc) ||
-        ReadUnsigned(Memory, MemoryEnd, InsertLen) ||
-        Memory + InsertLen > MemoryEnd)
-      return DB;
-
-    CodeModificationHint Hint;
-    Hint.RemoveRange = SourceRange(RemoveBegin, RemoveEnd);
-    Hint.InsertionLoc = InsertionLoc;
-    Hint.CodeToInsert.assign(Memory, Memory + InsertLen);
-    Memory += InsertLen;
-    DB << Hint;
-  }
-
-  return DB;
-}
-
 struct WarningOption {
   const char  *Name;
   const short *Members;
@@ -1036,6 +919,31 @@
   }
 }
 
+StoredDiagnostic::StoredDiagnostic() { }
+
+StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level, 
+                                   llvm::StringRef Message)
+  : Level(Level), Message(Message) { }
+
+StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level, 
+                                   const DiagnosticInfo &Info)
+  : Level(Level), Loc(Info.getLocation()) 
+{
+  llvm::SmallString<64> Message;
+  Info.FormatDiagnostic(Message);
+  this->Message.assign(Message.begin(), Message.end());
+
+  Ranges.reserve(Info.getNumRanges());
+  for (unsigned I = 0, N = Info.getNumRanges(); I != N; ++I)
+    Ranges.push_back(Info.getRange(I));
+
+  FixIts.reserve(Info.getNumCodeModificationHints());
+  for (unsigned I = 0, N = Info.getNumCodeModificationHints(); I != N; ++I)
+    FixIts.push_back(Info.getCodeModificationHint(I));
+}
+
+StoredDiagnostic::~StoredDiagnostic() { }
+
 static void WriteUnsigned(llvm::raw_ostream &OS, unsigned Value) {
   OS.write((const char *)&Value, sizeof(unsigned));
 }
@@ -1065,27 +973,24 @@
   WriteUnsigned(OS, SM->getColumnNumber(Decomposed.first, Decomposed.second));
 }
 
-void DiagnosticInfo::Serialize(Diagnostic::Level DiagLevel, 
-                               llvm::raw_ostream &OS) const {
+void StoredDiagnostic::Serialize(llvm::raw_ostream &OS) const {
   SourceManager *SM = 0;
   if (getLocation().isValid())
     SM = &const_cast<SourceManager &>(getLocation().getManager());
 
   // Write the diagnostic level and location.
-  WriteUnsigned(OS, (unsigned)DiagLevel);
+  WriteUnsigned(OS, (unsigned)Level);
   WriteSourceLocation(OS, SM, getLocation());
 
   // Write the diagnostic message.
   llvm::SmallString<64> Message;
-  FormatDiagnostic(Message);
-  WriteString(OS, Message);
+  WriteString(OS, getMessage());
   
   // Count the number of ranges that don't point into macros, since
   // only simple file ranges serialize well.
   unsigned NumNonMacroRanges = 0;
-  for (unsigned I = 0, N = getNumRanges(); I != N; ++I) {
-    SourceRange R = getRange(I);
-    if (R.getBegin().isMacroID() || R.getEnd().isMacroID())
+  for (range_iterator R = range_begin(), REnd = range_end(); R != REnd; ++R) {
+    if (R->getBegin().isMacroID() || R->getEnd().isMacroID())
       continue;
 
     ++NumNonMacroRanges;
@@ -1094,44 +999,165 @@
   // Write the ranges.
   WriteUnsigned(OS, NumNonMacroRanges);
   if (NumNonMacroRanges) {
-    for (unsigned I = 0, N = getNumRanges(); I != N; ++I) {
-      SourceRange R = getRange(I);
-      if (R.getBegin().isMacroID() || R.getEnd().isMacroID())
+    for (range_iterator R = range_begin(), REnd = range_end(); R != REnd; ++R) {
+      if (R->getBegin().isMacroID() || R->getEnd().isMacroID())
         continue;
       
-      WriteSourceLocation(OS, SM, R.getBegin());
-      WriteSourceLocation(OS, SM, R.getEnd());
+      WriteSourceLocation(OS, SM, R->getBegin());
+      WriteSourceLocation(OS, SM, R->getEnd());
     }
   }
 
   // Determine if all of the fix-its involve rewrites with simple file
   // locations (not in macro instantiations). If so, we can write
   // fix-it information.
-  unsigned NumFixIts = getNumCodeModificationHints();
-  for (unsigned I = 0; I != NumFixIts; ++I) {
-    const CodeModificationHint &Hint = getCodeModificationHint(I);
-    if (Hint.RemoveRange.isValid() &&
-        (Hint.RemoveRange.getBegin().isMacroID() ||
-         Hint.RemoveRange.getEnd().isMacroID())) {
+  unsigned NumFixIts = 0;
+  for (fixit_iterator F = fixit_begin(), FEnd = fixit_end(); F != FEnd; ++F) {
+    if (F->RemoveRange.isValid() &&
+        (F->RemoveRange.getBegin().isMacroID() ||
+         F->RemoveRange.getEnd().isMacroID())) {
       NumFixIts = 0;
       break;
     }
 
-    if (Hint.InsertionLoc.isValid() && Hint.InsertionLoc.isMacroID()) {
+    if (F->InsertionLoc.isValid() && F->InsertionLoc.isMacroID()) {
       NumFixIts = 0;
       break;
     }
+
+    ++NumFixIts;
   }
 
   // Write the fix-its.
   WriteUnsigned(OS, NumFixIts);
+  for (fixit_iterator F = fixit_begin(), FEnd = fixit_end(); F != FEnd; ++F) {
+    WriteSourceLocation(OS, SM, F->RemoveRange.getBegin());
+    WriteSourceLocation(OS, SM, F->RemoveRange.getEnd());
+    WriteSourceLocation(OS, SM, F->InsertionLoc);
+    WriteString(OS, F->CodeToInsert);
+  }
+}
+
+static bool ReadUnsigned(const char *&Memory, const char *MemoryEnd,
+                         unsigned &Value) {
+  if (Memory + sizeof(unsigned) > MemoryEnd)
+    return true;
+
+  memmove(&Value, Memory, sizeof(unsigned));
+  Memory += sizeof(unsigned);
+  return false;
+}
+
+static bool ReadSourceLocation(FileManager &FM, SourceManager &SM,
+                               const char *&Memory, const char *MemoryEnd,
+                               SourceLocation &Location) {
+  // Read the filename.
+  unsigned FileNameLen = 0;
+  if (ReadUnsigned(Memory, MemoryEnd, FileNameLen) || 
+      Memory + FileNameLen > MemoryEnd)
+    return true;
+
+  llvm::StringRef FileName(Memory, FileNameLen);
+  Memory += FileNameLen;
+
+  // Read the line, column.
+  unsigned Line = 0, Column = 0;
+  if (ReadUnsigned(Memory, MemoryEnd, Line) ||
+      ReadUnsigned(Memory, MemoryEnd, Column))
+    return true;
+
+  if (FileName.empty()) {
+    Location = SourceLocation();
+    return false;
+  }
+
+  const FileEntry *File = FM.getFile(FileName);
+  if (!File)
+    return true;
+
+  // Make sure that this file has an entry in the source manager.
+  if (!SM.hasFileInfo(File))
+    SM.createFileID(File, SourceLocation(), SrcMgr::C_User);
+
+  Location = SM.getLocation(File, Line, Column);
+  return false;
+}
+
+StoredDiagnostic 
+StoredDiagnostic::Deserialize(FileManager &FM, SourceManager &SM, 
+                              const char *&Memory, const char *MemoryEnd) {
+  if (Memory == MemoryEnd)
+    return StoredDiagnostic();
+
+  // Read the severity level.
+  unsigned Level = 0;
+  if (ReadUnsigned(Memory, MemoryEnd, Level) || Level > Diagnostic::Fatal)
+    return StoredDiagnostic();
+
+  // Read the source location.
+  SourceLocation Location;
+  if (ReadSourceLocation(FM, SM, Memory, MemoryEnd, Location))
+    return StoredDiagnostic();
+
+  // Read the diagnostic text.
+  if (Memory == MemoryEnd)
+    return StoredDiagnostic();
+
+  unsigned MessageLen = 0;
+  if (ReadUnsigned(Memory, MemoryEnd, MessageLen) ||
+      Memory + MessageLen > MemoryEnd)
+    return StoredDiagnostic();
+  
+  llvm::StringRef Message(Memory, MessageLen);
+  Memory += MessageLen;
+
+
+  // At this point, we have enough information to form a diagnostic. Do so.
+  StoredDiagnostic Diag;
+  Diag.Level = (Diagnostic::Level)Level;
+  Diag.Loc = FullSourceLoc(Location, SM);
+  Diag.Message = Message;
+  if (Memory == MemoryEnd)
+    return Diag;
+
+  // Read the source ranges.
+  unsigned NumSourceRanges = 0;
+  if (ReadUnsigned(Memory, MemoryEnd, NumSourceRanges))
+    return Diag;
+  for (unsigned I = 0; I != NumSourceRanges; ++I) {
+    SourceLocation Begin, End;
+    if (ReadSourceLocation(FM, SM, Memory, MemoryEnd, Begin) ||
+        ReadSourceLocation(FM, SM, Memory, MemoryEnd, End))
+      return Diag;
+
+    Diag.Ranges.push_back(SourceRange(Begin, End));
+  }
+
+  // Read the fix-it hints.
+  unsigned NumFixIts = 0;
+  if (ReadUnsigned(Memory, MemoryEnd, NumFixIts))
+    return Diag;
   for (unsigned I = 0; I != NumFixIts; ++I) {
-    const CodeModificationHint &Hint = getCodeModificationHint(I);
-    WriteSourceLocation(OS, SM, Hint.RemoveRange.getBegin());
-    WriteSourceLocation(OS, SM, Hint.RemoveRange.getEnd());
-    WriteSourceLocation(OS, SM, Hint.InsertionLoc);
-    WriteString(OS, Hint.CodeToInsert);
+    SourceLocation RemoveBegin, RemoveEnd, InsertionLoc;
+    unsigned InsertLen = 0;
+    if (ReadSourceLocation(FM, SM, Memory, MemoryEnd, RemoveBegin) ||
+        ReadSourceLocation(FM, SM, Memory, MemoryEnd, RemoveEnd) ||
+        ReadSourceLocation(FM, SM, Memory, MemoryEnd, InsertionLoc) ||
+        ReadUnsigned(Memory, MemoryEnd, InsertLen) ||
+        Memory + InsertLen > MemoryEnd) {
+      Diag.FixIts.clear();
+      return Diag;
+    }
+
+    CodeModificationHint Hint;
+    Hint.RemoveRange = SourceRange(RemoveBegin, RemoveEnd);
+    Hint.InsertionLoc = InsertionLoc;
+    Hint.CodeToInsert.assign(Memory, Memory + InsertLen);
+    Memory += InsertLen;
+    Diag.FixIts.push_back(Hint);
   }
+
+  return Diag;
 }
 
 /// IncludeInDiagnosticCounts - This method (whose default implementation

Modified: cfe/trunk/lib/Frontend/ASTUnit.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/ASTUnit.cpp?rev=96592&r1=96591&r2=96592&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/ASTUnit.cpp (original)
+++ cfe/trunk/lib/Frontend/ASTUnit.cpp Thu Feb 18 12:08:43 2010
@@ -90,8 +90,46 @@
   }
 };
 
+class StoredDiagnosticClient : public DiagnosticClient {
+  llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags;
+  
+public:
+  explicit StoredDiagnosticClient(
+                          llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags)
+    : StoredDiags(StoredDiags) { }
+  
+  virtual void HandleDiagnostic(Diagnostic::Level Level,
+                                const DiagnosticInfo &Info);
+};
+
+/// \brief RAII object that optionally captures diagnostics, if
+/// there is no diagnostic client to capture them already.
+class CaptureDroppedDiagnostics {
+  Diagnostic &Diags;
+  StoredDiagnosticClient Client;
+  DiagnosticClient *PreviousClient;
+
+public:
+  CaptureDroppedDiagnostics(bool RequestCapture, Diagnostic &Diags, 
+                           llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags)
+    : Diags(Diags), Client(StoredDiags), PreviousClient(Diags.getClient()) 
+  {
+    if (RequestCapture || Diags.getClient() == 0)
+      Diags.setClient(&Client);
+  }
+
+  ~CaptureDroppedDiagnostics() {
+    Diags.setClient(PreviousClient);
+  }
+};
+
 } // anonymous namespace
 
+void StoredDiagnosticClient::HandleDiagnostic(Diagnostic::Level Level,
+                                              const DiagnosticInfo &Info) {
+  StoredDiags.push_back(StoredDiagnostic(Level, Info));
+}
+
 const std::string &ASTUnit::getOriginalSourceFileName() {
   return OriginalSourceFile;
 }
@@ -105,11 +143,16 @@
                                   Diagnostic &Diags,
                                   bool OnlyLocalDecls,
                                   RemappedFile *RemappedFiles,
-                                  unsigned NumRemappedFiles) {
+                                  unsigned NumRemappedFiles,
+                                  bool CaptureDiagnostics) {
   llvm::OwningPtr<ASTUnit> AST(new ASTUnit(true));
   AST->OnlyLocalDecls = OnlyLocalDecls;
   AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager()));
 
+  // If requested, capture diagnostics in the ASTUnit.
+  CaptureDroppedDiagnostics Capture(CaptureDiagnostics, Diags, 
+                                    AST->Diagnostics);
+
   for (unsigned I = 0; I != NumRemappedFiles; ++I) {
     // Create the file entry for the file that we're mapping from.
     const FileEntry *FromFile
@@ -231,7 +274,8 @@
 
 ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI,
                                              Diagnostic &Diags,
-                                             bool OnlyLocalDecls) {
+                                             bool OnlyLocalDecls,
+                                             bool CaptureDiagnostics) {
   // Create the compiler instance to use for building the AST.
   CompilerInstance Clang;
   llvm::OwningPtr<ASTUnit> AST;
@@ -245,8 +289,13 @@
   // Create the target instance.
   Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(),
                                                Clang.getTargetOpts()));
-  if (!Clang.hasTarget())
-    goto error;
+  if (!Clang.hasTarget()) {
+    Clang.takeSourceManager();
+    Clang.takeFileManager();
+    Clang.takeDiagnosticClient();
+    Clang.takeDiagnostics();
+    return 0;
+  }
 
   // Inform the target of the language options.
   //
@@ -261,10 +310,14 @@
 
   // Create the AST unit.
   AST.reset(new ASTUnit(false));
-
   AST->OnlyLocalDecls = OnlyLocalDecls;
   AST->OriginalSourceFile = Clang.getFrontendOpts().Inputs[0].second;
 
+  // Capture any diagnostics that would otherwise be dropped.
+  CaptureDroppedDiagnostics Capture(CaptureDiagnostics, 
+                                    Clang.getDiagnostics(),
+                                    AST->Diagnostics);
+
   // Create a file manager object to provide access to and cache the filesystem.
   Clang.setFileManager(&AST->getFileManager());
 
@@ -312,7 +365,8 @@
                                       llvm::StringRef ResourceFilesPath,
                                       bool OnlyLocalDecls,
                                       RemappedFile *RemappedFiles,
-                                      unsigned NumRemappedFiles) {
+                                      unsigned NumRemappedFiles,
+                                      bool CaptureDiagnostics) {
   llvm::SmallVector<const char *, 16> Args;
   Args.push_back("<clang>"); // FIXME: Remove dummy argument.
   Args.insert(Args.end(), ArgBegin, ArgEnd);
@@ -363,5 +417,6 @@
   CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
 
   CI->getFrontendOpts().DisableFree = true;
-  return LoadFromCompilerInvocation(CI.take(), Diags, OnlyLocalDecls);
+  return LoadFromCompilerInvocation(CI.take(), Diags, OnlyLocalDecls,
+                                    CaptureDiagnostics);
 }

Modified: cfe/trunk/lib/Frontend/CompilerInstance.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/CompilerInstance.cpp?rev=96592&r1=96591&r2=96592&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/CompilerInstance.cpp (original)
+++ cfe/trunk/lib/Frontend/CompilerInstance.cpp Thu Feb 18 12:08:43 2010
@@ -102,7 +102,7 @@
 
 void BinaryDiagnosticSerializer::HandleDiagnostic(Diagnostic::Level DiagLevel,
                                                   const DiagnosticInfo &Info) {
-  Info.Serialize(DiagLevel, OS);
+  StoredDiagnostic(DiagLevel, Info).Serialize(OS);
 }
 
 static void SetUpBuildDumpLog(const DiagnosticOptions &DiagOpts,

Modified: cfe/trunk/test/Index/cindex-on-invalid.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/cindex-on-invalid.m?rev=96592&r1=96591&r2=96592&view=diff

==============================================================================
--- cfe/trunk/test/Index/cindex-on-invalid.m (original)
+++ cfe/trunk/test/Index/cindex-on-invalid.m Thu Feb 18 12:08:43 2010
@@ -1,5 +1,6 @@
 // RUN: not c-index-test -test-load-source local %s > %t 2> %t.err
 // RUN: FileCheck %s < %t.err
+// XFAIL: *
 
 // CHECK: error: expected identifier or '('
 // CHECK: Unable to load translation unit!

Modified: cfe/trunk/tools/CIndex/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndex.cpp?rev=96592&r1=96591&r2=96592&view=diff

==============================================================================
--- cfe/trunk/tools/CIndex/CIndex.cpp (original)
+++ cfe/trunk/tools/CIndex/CIndex.cpp Thu Feb 18 12:08:43 2010
@@ -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();

Modified: cfe/trunk/tools/CIndex/CIndex.exports
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndex.exports?rev=96592&r1=96591&r2=96592&view=diff

==============================================================================
--- cfe/trunk/tools/CIndex/CIndex.exports (original)
+++ cfe/trunk/tools/CIndex/CIndex.exports Thu Feb 18 12:08:43 2010
@@ -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

Modified: cfe/trunk/tools/CIndex/CIndexCodeCompletion.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndexCodeCompletion.cpp?rev=96592&r1=96591&r2=96592&view=diff

==============================================================================
--- cfe/trunk/tools/CIndex/CIndexCodeCompletion.cpp (original)
+++ cfe/trunk/tools/CIndex/CIndexCodeCompletion.cpp Thu Feb 18 12:08:43 2010
@@ -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"

Modified: cfe/trunk/tools/CIndex/CIndexDiagnostic.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndexDiagnostic.cpp?rev=96592&r1=96591&r2=96592&view=diff

==============================================================================
--- cfe/trunk/tools/CIndex/CIndexDiagnostic.cpp (original)
+++ cfe/trunk/tools/CIndex/CIndexDiagnostic.cpp Thu Feb 18 12:08:43 2010
@@ -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;
+using namespace llvm;
 
 //-----------------------------------------------------------------------------
-// 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
+// C Interface Routines
 //-----------------------------------------------------------------------------
-CIndexDiagnosticClient::~CIndexDiagnosticClient() { }
-
-void CIndexDiagnosticClient::BeginSourceFile(const LangOptions &LangOpts,
-                                             const Preprocessor *PP) {
-  assert(!LangOptsPtr && "Invalid state!");
-  LangOptsPtr = &LangOpts;
-}
+extern "C" {
 
-void CIndexDiagnosticClient::EndSourceFile() {
-  assert(LangOptsPtr && "Invalid state!");
-  LangOptsPtr = 0;
+unsigned clang_getNumDiagnostics(CXTranslationUnit Unit) {
+  ASTUnit *CXXUnit = static_cast<ASTUnit *>(Unit);
+  return CXXUnit? CXXUnit->diag_size() : 0;
 }
 
-void CIndexDiagnosticClient::HandleDiagnostic(Diagnostic::Level DiagLevel,
-                                              const DiagnosticInfo &Info) {
-  if (!Callback)
-    return;
+CXDiagnostic clang_getDiagnostic(CXTranslationUnit Unit, unsigned Index) {
+  ASTUnit *CXXUnit = static_cast<ASTUnit *>(Unit);
+  if (!CXXUnit || Index >= CXXUnit->diag_size())
+    return 0;
 
-  assert((LangOptsPtr || Info.getLocation().isInvalid()) &&
-         "Missing language options with located diagnostic!");
-  CXStoredDiagnostic Stored = { this->LangOptsPtr, DiagLevel, Info };
-  Callback(&Stored, ClientData);
+  return new CXStoredDiagnostic(CXXUnit->diag_begin()[Index],
+                                CXXUnit->getASTContext().getLangOptions());
 }
 
-//-----------------------------------------------------------------------------
-// C Interface Routines
-//-----------------------------------------------------------------------------
-extern "C" {
+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);
+  }
 }

Modified: cfe/trunk/tools/CIndex/CIndexDiagnostic.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndexDiagnostic.h?rev=96592&r1=96591&r2=96592&view=diff

==============================================================================
--- cfe/trunk/tools/CIndex/CIndexDiagnostic.h (original)
+++ cfe/trunk/tools/CIndex/CIndexDiagnostic.h Thu Feb 18 12:08:43 2010
@@ -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
 

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=96592&r1=96591&r2=96592&view=diff

==============================================================================
--- cfe/trunk/tools/c-index-test/c-index-test.c (original)
+++ cfe/trunk/tools/c-index-test/c-index-test.c Thu Feb 18 12:08:43 2010
@@ -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);





More information about the cfe-commits mailing list