Add CodeCompletion consumer to CompilerInvocation.

llvm-svn: 87100
diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp
index e7c3611..ce7c54f 100644
--- a/clang/lib/Frontend/CompilerInstance.cpp
+++ b/clang/lib/Frontend/CompilerInstance.cpp
@@ -18,9 +18,11 @@
 #include "clang/Lex/PTHManager.h"
 #include "clang/Frontend/ChainedDiagnosticClient.h"
 #include "clang/Frontend/PCHReader.h"
+#include "clang/Frontend/FrontendDiagnostic.h"
 #include "clang/Frontend/TextDiagnosticBuffer.h"
 #include "clang/Frontend/TextDiagnosticPrinter.h"
 #include "clang/Frontend/Utils.h"
+#include "clang/Sema/CodeCompleteConsumer.h"
 #include "llvm/LLVMContext.h"
 #include "llvm/Support/raw_ostream.h"
 using namespace clang;
@@ -29,7 +31,7 @@
                                    bool _OwnsLLVMContext)
   : LLVMContext(_LLVMContext),
     OwnsLLVMContext(_OwnsLLVMContext) {
-}
+    }
 
 CompilerInstance::~CompilerInstance() {
   if (OwnsLLVMContext)
@@ -202,3 +204,42 @@
 
   return 0;
 }
+
+// Code Completion
+
+void CompilerInstance::createCodeCompletionConsumer() {
+  const ParsedSourceLocation &Loc = getFrontendOpts().CodeCompletionAt;
+  CompletionConsumer.reset(
+    createCodeCompletionConsumer(getPreprocessor(),
+                                 Loc.FileName, Loc.Line, Loc.Column,
+                                 getFrontendOpts().DebugCodeCompletionPrinter,
+                                 getFrontendOpts().ShowMacrosInCodeCompletion,
+                                 llvm::outs()));
+}
+
+CodeCompleteConsumer *
+CompilerInstance::createCodeCompletionConsumer(Preprocessor &PP,
+                                               const std::string &Filename,
+                                               unsigned Line,
+                                               unsigned Column,
+                                               bool UseDebugPrinter,
+                                               bool ShowMacros,
+                                               llvm::raw_ostream &OS) {
+  // Tell the source manager to chop off the given file at a specific
+  // line and column.
+  const FileEntry *Entry = PP.getFileManager().getFile(Filename);
+  if (!Entry) {
+    PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file)
+      << Filename;
+    return 0;
+  }
+
+  // Truncate the named file at the given line/column.
+  PP.getSourceManager().truncateFileAt(Entry, Line, Column);
+
+  // Set up the creation routine for code-completion.
+  if (UseDebugPrinter)
+    return new PrintingCodeCompleteConsumer(ShowMacros, OS);
+  else
+    return new CIndexCodeCompleteConsumer(ShowMacros, OS);
+}