Change clang_codeComplete API to return the results in a structure on
the heap, so that clients are not forced to copy the results during
the initial iteration. A separate clang_disposeCodeCompleteResults
function frees the returned results.

llvm-svn: 91690
diff --git a/clang/tools/CIndex/CIndex.cpp b/clang/tools/CIndex/CIndex.cpp
index fe3aa37..c4a6166 100644
--- a/clang/tools/CIndex/CIndex.cpp
+++ b/clang/tools/CIndex/CIndex.cpp
@@ -1220,17 +1220,23 @@
   return false;
 }
 
-void clang_codeComplete(CXIndex CIdx,
-                        const char *source_filename,
-                        int num_command_line_args,
-                        const char **command_line_args,
-                        unsigned num_unsaved_files,
-                        struct CXUnsavedFile *unsaved_files,
-                        const char *complete_filename,
-                        unsigned complete_line,
-                        unsigned complete_column,
-                        CXCompletionIterator completion_iterator,
-                        CXClientData client_data)  {
+/// \brief The CXCodeCompleteResults structure we allocate internally;
+/// the client only sees the initial CXCodeCompleteResults structure.
+struct AllocatedCXCodeCompleteResults : public CXCodeCompleteResults {
+  /// \brief The memory buffer from which we parsed the results. We
+  /// retain this buffer because the completion strings point into it.
+  llvm::MemoryBuffer *Buffer;
+};
+
+CXCodeCompleteResults *clang_codeComplete(CXIndex CIdx,
+                                          const char *source_filename,
+                                          int num_command_line_args,
+                                          const char **command_line_args,
+                                          unsigned num_unsaved_files,
+                                          struct CXUnsavedFile *unsaved_files,
+                                          const char *complete_filename,
+                                          unsigned complete_line,
+                                          unsigned complete_column) {
   // The indexer, which is mainly used to determine where diagnostics go.
   CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
 
@@ -1353,7 +1359,9 @@
   // Parse the resulting source file to find code-completion results.
   using llvm::MemoryBuffer;
   using llvm::StringRef;
+  AllocatedCXCodeCompleteResults *Results = 0;
   if (MemoryBuffer *F = MemoryBuffer::getFile(ResultsFile.c_str())) {
+    llvm::SmallVector<CXCompletionResult, 4> CompletionResults;
     StringRef Buffer = F->getBuffer();
     for (const char *Str = Buffer.data(), *StrEnd = Str + Buffer.size();
          Str < StrEnd;) {
@@ -1371,17 +1379,41 @@
         CXCompletionResult Result;
         Result.CursorKind = (CXCursorKind)KindValue;
         Result.CompletionString = CCStr;
-        if (completion_iterator)
-          completion_iterator(&Result, client_data);
+        CompletionResults.push_back(Result);
       }
-
-      delete CCStr;
     };
-    delete F;
+
+    // Allocate the results.
+    Results = new AllocatedCXCodeCompleteResults;
+    Results->Results = new CXCompletionResult [CompletionResults.size()];
+    Results->NumResults = CompletionResults.size();
+    memcpy(Results->Results, CompletionResults.data(),
+           CompletionResults.size() * sizeof(CXCompletionResult));
+    Results->Buffer = F;
   }
 
   for (unsigned i = 0, e = TemporaryFiles.size(); i != e; ++i)
     TemporaryFiles[i].eraseFromDisk();
+
+  return Results;
+}
+
+void clang_disposeCodeCompleteResults(CXCodeCompleteResults *ResultsIn) {
+  if (!ResultsIn)
+    return;
+
+  AllocatedCXCodeCompleteResults *Results
+    = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn);
+
+  for (unsigned I = 0, N = Results->NumResults; I != N; ++I)
+    delete (CXCompletionString *)Results->Results[I].CompletionString;
+  delete [] Results->Results;
+
+  Results->Results = 0;
+  Results->NumResults = 0;
+  delete Results->Buffer;
+  Results->Buffer = 0;
+  delete Results;
 }
 
 } // end extern "C"
diff --git a/clang/tools/CIndex/CIndex.exports b/clang/tools/CIndex/CIndex.exports
index 458ad10..e925df9 100644
--- a/clang/tools/CIndex/CIndex.exports
+++ b/clang/tools/CIndex/CIndex.exports
@@ -2,6 +2,7 @@
 _clang_createIndex
 _clang_createTranslationUnit
 _clang_createTranslationUnitFromSourceFile
+_clang_disposeCodeCompleteResults
 _clang_disposeIndex
 _clang_disposeString
 _clang_disposeTranslationUnit