1 //===- CIndexCodeCompletion.cpp - Code Completion API hooks ---------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the Clang-C Source Indexing library hooks for
11 // code completion.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "CIndexer.h"
16 #include "CXTranslationUnit.h"
17 #include "CXString.h"
18 #include "CXCursor.h"
19 #include "CXString.h"
20 #include "CIndexDiagnostic.h"
21 #include "clang/AST/Type.h"
22 #include "clang/AST/Decl.h"
23 #include "clang/AST/DeclObjC.h"
24 #include "clang/Basic/SourceManager.h"
25 #include "clang/Basic/FileManager.h"
26 #include "clang/Frontend/ASTUnit.h"
27 #include "clang/Frontend/CompilerInstance.h"
28 #include "clang/Frontend/FrontendDiagnostic.h"
29 #include "clang/Sema/CodeCompleteConsumer.h"
30 #include "llvm/ADT/SmallString.h"
31 #include "llvm/ADT/StringExtras.h"
32 #include "llvm/Support/Atomic.h"
33 #include "llvm/Support/CrashRecoveryContext.h"
34 #include "llvm/Support/MemoryBuffer.h"
35 #include "llvm/Support/Timer.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include "llvm/Support/Program.h"
38 #include <cstdlib>
39 #include <cstdio>
40 
41 
42 #ifdef UDP_CODE_COMPLETION_LOGGER
43 #include "clang/Basic/Version.h"
44 #include <arpa/inet.h>
45 #include <sys/socket.h>
46 #include <sys/types.h>
47 #include <unistd.h>
48 #endif
49 
50 using namespace clang;
51 using namespace clang::cxstring;
52 
53 extern "C" {
54 
55 enum CXCompletionChunkKind
56 clang_getCompletionChunkKind(CXCompletionString completion_string,
57                              unsigned chunk_number) {
58   CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
59   if (!CCStr || chunk_number >= CCStr->size())
60     return CXCompletionChunk_Text;
61 
62   switch ((*CCStr)[chunk_number].Kind) {
63   case CodeCompletionString::CK_TypedText:
64     return CXCompletionChunk_TypedText;
65   case CodeCompletionString::CK_Text:
66     return CXCompletionChunk_Text;
67   case CodeCompletionString::CK_Optional:
68     return CXCompletionChunk_Optional;
69   case CodeCompletionString::CK_Placeholder:
70     return CXCompletionChunk_Placeholder;
71   case CodeCompletionString::CK_Informative:
72     return CXCompletionChunk_Informative;
73   case CodeCompletionString::CK_ResultType:
74     return CXCompletionChunk_ResultType;
75   case CodeCompletionString::CK_CurrentParameter:
76     return CXCompletionChunk_CurrentParameter;
77   case CodeCompletionString::CK_LeftParen:
78     return CXCompletionChunk_LeftParen;
79   case CodeCompletionString::CK_RightParen:
80     return CXCompletionChunk_RightParen;
81   case CodeCompletionString::CK_LeftBracket:
82     return CXCompletionChunk_LeftBracket;
83   case CodeCompletionString::CK_RightBracket:
84     return CXCompletionChunk_RightBracket;
85   case CodeCompletionString::CK_LeftBrace:
86     return CXCompletionChunk_LeftBrace;
87   case CodeCompletionString::CK_RightBrace:
88     return CXCompletionChunk_RightBrace;
89   case CodeCompletionString::CK_LeftAngle:
90     return CXCompletionChunk_LeftAngle;
91   case CodeCompletionString::CK_RightAngle:
92     return CXCompletionChunk_RightAngle;
93   case CodeCompletionString::CK_Comma:
94     return CXCompletionChunk_Comma;
95   case CodeCompletionString::CK_Colon:
96     return CXCompletionChunk_Colon;
97   case CodeCompletionString::CK_SemiColon:
98     return CXCompletionChunk_SemiColon;
99   case CodeCompletionString::CK_Equal:
100     return CXCompletionChunk_Equal;
101   case CodeCompletionString::CK_HorizontalSpace:
102     return CXCompletionChunk_HorizontalSpace;
103   case CodeCompletionString::CK_VerticalSpace:
104     return CXCompletionChunk_VerticalSpace;
105   }
106 
107   // Should be unreachable, but let's be careful.
108   return CXCompletionChunk_Text;
109 }
110 
111 CXString clang_getCompletionChunkText(CXCompletionString completion_string,
112                                       unsigned chunk_number) {
113   CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
114   if (!CCStr || chunk_number >= CCStr->size())
115     return createCXString((const char*)0);
116 
117   switch ((*CCStr)[chunk_number].Kind) {
118   case CodeCompletionString::CK_TypedText:
119   case CodeCompletionString::CK_Text:
120   case CodeCompletionString::CK_Placeholder:
121   case CodeCompletionString::CK_CurrentParameter:
122   case CodeCompletionString::CK_Informative:
123   case CodeCompletionString::CK_LeftParen:
124   case CodeCompletionString::CK_RightParen:
125   case CodeCompletionString::CK_LeftBracket:
126   case CodeCompletionString::CK_RightBracket:
127   case CodeCompletionString::CK_LeftBrace:
128   case CodeCompletionString::CK_RightBrace:
129   case CodeCompletionString::CK_LeftAngle:
130   case CodeCompletionString::CK_RightAngle:
131   case CodeCompletionString::CK_Comma:
132   case CodeCompletionString::CK_ResultType:
133   case CodeCompletionString::CK_Colon:
134   case CodeCompletionString::CK_SemiColon:
135   case CodeCompletionString::CK_Equal:
136   case CodeCompletionString::CK_HorizontalSpace:
137   case CodeCompletionString::CK_VerticalSpace:
138     return createCXString((*CCStr)[chunk_number].Text, false);
139 
140   case CodeCompletionString::CK_Optional:
141     // Note: treated as an empty text block.
142     return createCXString("");
143   }
144 
145   // Should be unreachable, but let's be careful.
146   return createCXString((const char*)0);
147 }
148 
149 
150 CXCompletionString
151 clang_getCompletionChunkCompletionString(CXCompletionString completion_string,
152                                          unsigned chunk_number) {
153   CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
154   if (!CCStr || chunk_number >= CCStr->size())
155     return 0;
156 
157   switch ((*CCStr)[chunk_number].Kind) {
158   case CodeCompletionString::CK_TypedText:
159   case CodeCompletionString::CK_Text:
160   case CodeCompletionString::CK_Placeholder:
161   case CodeCompletionString::CK_CurrentParameter:
162   case CodeCompletionString::CK_Informative:
163   case CodeCompletionString::CK_LeftParen:
164   case CodeCompletionString::CK_RightParen:
165   case CodeCompletionString::CK_LeftBracket:
166   case CodeCompletionString::CK_RightBracket:
167   case CodeCompletionString::CK_LeftBrace:
168   case CodeCompletionString::CK_RightBrace:
169   case CodeCompletionString::CK_LeftAngle:
170   case CodeCompletionString::CK_RightAngle:
171   case CodeCompletionString::CK_Comma:
172   case CodeCompletionString::CK_ResultType:
173   case CodeCompletionString::CK_Colon:
174   case CodeCompletionString::CK_SemiColon:
175   case CodeCompletionString::CK_Equal:
176   case CodeCompletionString::CK_HorizontalSpace:
177   case CodeCompletionString::CK_VerticalSpace:
178     return 0;
179 
180   case CodeCompletionString::CK_Optional:
181     // Note: treated as an empty text block.
182     return (*CCStr)[chunk_number].Optional;
183   }
184 
185   // Should be unreachable, but let's be careful.
186   return 0;
187 }
188 
189 unsigned clang_getNumCompletionChunks(CXCompletionString completion_string) {
190   CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
191   return CCStr? CCStr->size() : 0;
192 }
193 
194 unsigned clang_getCompletionPriority(CXCompletionString completion_string) {
195   CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
196   return CCStr? CCStr->getPriority() : unsigned(CCP_Unlikely);
197 }
198 
199 enum CXAvailabilityKind
200 clang_getCompletionAvailability(CXCompletionString completion_string) {
201   CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
202   return CCStr? static_cast<CXAvailabilityKind>(CCStr->getAvailability())
203               : CXAvailability_Available;
204 }
205 
206 /// \brief The CXCodeCompleteResults structure we allocate internally;
207 /// the client only sees the initial CXCodeCompleteResults structure.
208 struct AllocatedCXCodeCompleteResults : public CXCodeCompleteResults {
209   AllocatedCXCodeCompleteResults(const FileSystemOptions& FileSystemOpts);
210   ~AllocatedCXCodeCompleteResults();
211 
212   /// \brief Diagnostics produced while performing code completion.
213   SmallVector<StoredDiagnostic, 8> Diagnostics;
214 
215   /// \brief Diag object
216   llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diag;
217 
218   /// \brief Language options used to adjust source locations.
219   LangOptions LangOpts;
220 
221   FileSystemOptions FileSystemOpts;
222 
223   /// \brief File manager, used for diagnostics.
224   llvm::IntrusiveRefCntPtr<FileManager> FileMgr;
225 
226   /// \brief Source manager, used for diagnostics.
227   llvm::IntrusiveRefCntPtr<SourceManager> SourceMgr;
228 
229   /// \brief Temporary files that should be removed once we have finished
230   /// with the code-completion results.
231   std::vector<llvm::sys::Path> TemporaryFiles;
232 
233   /// \brief Temporary buffers that will be deleted once we have finished with
234   /// the code-completion results.
235   SmallVector<const llvm::MemoryBuffer *, 1> TemporaryBuffers;
236 
237   /// \brief Allocator used to store globally cached code-completion results.
238   llvm::IntrusiveRefCntPtr<clang::GlobalCodeCompletionAllocator>
239     CachedCompletionAllocator;
240 
241   /// \brief Allocator used to store code completion results.
242   clang::CodeCompletionAllocator CodeCompletionAllocator;
243 
244   /// \brief Context under which completion occurred.
245   enum clang::CodeCompletionContext::Kind ContextKind;
246 
247   /// \brief A bitfield representing the acceptable completions for the
248   /// current context.
249   unsigned long long Contexts;
250 
251   /// \brief The kind of the container for the current context for completions.
252   enum CXCursorKind ContainerKind;
253   /// \brief The USR of the container for the current context for completions.
254   CXString ContainerUSR;
255   /// \brief a boolean value indicating whether there is complete information
256   /// about the container
257   unsigned ContainerIsIncomplete;
258 
259   /// \brief A string containing the Objective-C selector entered thus far for a
260   /// message send.
261   std::string Selector;
262 };
263 
264 /// \brief Tracks the number of code-completion result objects that are
265 /// currently active.
266 ///
267 /// Used for debugging purposes only.
268 static llvm::sys::cas_flag CodeCompletionResultObjects;
269 
270 AllocatedCXCodeCompleteResults::AllocatedCXCodeCompleteResults(
271                                       const FileSystemOptions& FileSystemOpts)
272   : CXCodeCompleteResults(),
273     Diag(new DiagnosticsEngine(
274                    llvm::IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs))),
275     FileSystemOpts(FileSystemOpts),
276     FileMgr(new FileManager(FileSystemOpts)),
277     SourceMgr(new SourceManager(*Diag, *FileMgr)),
278     Contexts(CXCompletionContext_Unknown),
279     ContainerKind(CXCursor_InvalidCode),
280     ContainerUSR(createCXString("")),
281     ContainerIsIncomplete(1)
282 {
283   if (getenv("LIBCLANG_OBJTRACKING")) {
284     llvm::sys::AtomicIncrement(&CodeCompletionResultObjects);
285     fprintf(stderr, "+++ %d completion results\n", CodeCompletionResultObjects);
286   }
287 }
288 
289 AllocatedCXCodeCompleteResults::~AllocatedCXCodeCompleteResults() {
290   delete [] Results;
291 
292   clang_disposeString(ContainerUSR);
293 
294   for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I)
295     TemporaryFiles[I].eraseFromDisk();
296   for (unsigned I = 0, N = TemporaryBuffers.size(); I != N; ++I)
297     delete TemporaryBuffers[I];
298 
299   if (getenv("LIBCLANG_OBJTRACKING")) {
300     llvm::sys::AtomicDecrement(&CodeCompletionResultObjects);
301     fprintf(stderr, "--- %d completion results\n", CodeCompletionResultObjects);
302   }
303 }
304 
305 } // end extern "C"
306 
307 static unsigned long long getContextsForContextKind(
308                                           enum CodeCompletionContext::Kind kind,
309                                                     Sema &S) {
310   unsigned long long contexts = 0;
311   switch (kind) {
312     case CodeCompletionContext::CCC_OtherWithMacros: {
313       //We can allow macros here, but we don't know what else is permissible
314       //So we'll say the only thing permissible are macros
315       contexts = CXCompletionContext_MacroName;
316       break;
317     }
318     case CodeCompletionContext::CCC_TopLevel:
319     case CodeCompletionContext::CCC_ObjCIvarList:
320     case CodeCompletionContext::CCC_ClassStructUnion:
321     case CodeCompletionContext::CCC_Type: {
322       contexts = CXCompletionContext_AnyType |
323                  CXCompletionContext_ObjCInterface;
324       if (S.getLangOptions().CPlusPlus) {
325         contexts |= CXCompletionContext_EnumTag |
326                     CXCompletionContext_UnionTag |
327                     CXCompletionContext_StructTag |
328                     CXCompletionContext_ClassTag |
329                     CXCompletionContext_NestedNameSpecifier;
330       }
331       break;
332     }
333     case CodeCompletionContext::CCC_Statement: {
334       contexts = CXCompletionContext_AnyType |
335                  CXCompletionContext_ObjCInterface |
336                  CXCompletionContext_AnyValue;
337       if (S.getLangOptions().CPlusPlus) {
338         contexts |= CXCompletionContext_EnumTag |
339                     CXCompletionContext_UnionTag |
340                     CXCompletionContext_StructTag |
341                     CXCompletionContext_ClassTag |
342                     CXCompletionContext_NestedNameSpecifier;
343       }
344       break;
345     }
346     case CodeCompletionContext::CCC_Expression: {
347       contexts = CXCompletionContext_AnyValue;
348       if (S.getLangOptions().CPlusPlus) {
349         contexts |= CXCompletionContext_AnyType |
350                     CXCompletionContext_ObjCInterface |
351                     CXCompletionContext_EnumTag |
352                     CXCompletionContext_UnionTag |
353                     CXCompletionContext_StructTag |
354                     CXCompletionContext_ClassTag |
355                     CXCompletionContext_NestedNameSpecifier;
356       }
357       break;
358     }
359     case CodeCompletionContext::CCC_ObjCMessageReceiver: {
360       contexts = CXCompletionContext_ObjCObjectValue |
361                  CXCompletionContext_ObjCSelectorValue |
362                  CXCompletionContext_ObjCInterface;
363       if (S.getLangOptions().CPlusPlus) {
364         contexts |= CXCompletionContext_CXXClassTypeValue |
365                     CXCompletionContext_AnyType |
366                     CXCompletionContext_EnumTag |
367                     CXCompletionContext_UnionTag |
368                     CXCompletionContext_StructTag |
369                     CXCompletionContext_ClassTag |
370                     CXCompletionContext_NestedNameSpecifier;
371       }
372       break;
373     }
374     case CodeCompletionContext::CCC_DotMemberAccess: {
375       contexts = CXCompletionContext_DotMemberAccess;
376       break;
377     }
378     case CodeCompletionContext::CCC_ArrowMemberAccess: {
379       contexts = CXCompletionContext_ArrowMemberAccess;
380       break;
381     }
382     case CodeCompletionContext::CCC_ObjCPropertyAccess: {
383       contexts = CXCompletionContext_ObjCPropertyAccess;
384       break;
385     }
386     case CodeCompletionContext::CCC_EnumTag: {
387       contexts = CXCompletionContext_EnumTag |
388                  CXCompletionContext_NestedNameSpecifier;
389       break;
390     }
391     case CodeCompletionContext::CCC_UnionTag: {
392       contexts = CXCompletionContext_UnionTag |
393                  CXCompletionContext_NestedNameSpecifier;
394       break;
395     }
396     case CodeCompletionContext::CCC_ClassOrStructTag: {
397       contexts = CXCompletionContext_StructTag |
398                  CXCompletionContext_ClassTag |
399                  CXCompletionContext_NestedNameSpecifier;
400       break;
401     }
402     case CodeCompletionContext::CCC_ObjCProtocolName: {
403       contexts = CXCompletionContext_ObjCProtocol;
404       break;
405     }
406     case CodeCompletionContext::CCC_Namespace: {
407       contexts = CXCompletionContext_Namespace;
408       break;
409     }
410     case CodeCompletionContext::CCC_PotentiallyQualifiedName: {
411       contexts = CXCompletionContext_NestedNameSpecifier;
412       break;
413     }
414     case CodeCompletionContext::CCC_MacroNameUse: {
415       contexts = CXCompletionContext_MacroName;
416       break;
417     }
418     case CodeCompletionContext::CCC_NaturalLanguage: {
419       contexts = CXCompletionContext_NaturalLanguage;
420       break;
421     }
422     case CodeCompletionContext::CCC_SelectorName: {
423       contexts = CXCompletionContext_ObjCSelectorName;
424       break;
425     }
426     case CodeCompletionContext::CCC_ParenthesizedExpression: {
427       contexts = CXCompletionContext_AnyType |
428                  CXCompletionContext_ObjCInterface |
429                  CXCompletionContext_AnyValue;
430       if (S.getLangOptions().CPlusPlus) {
431         contexts |= CXCompletionContext_EnumTag |
432                     CXCompletionContext_UnionTag |
433                     CXCompletionContext_StructTag |
434                     CXCompletionContext_ClassTag |
435                     CXCompletionContext_NestedNameSpecifier;
436       }
437       break;
438     }
439     case CodeCompletionContext::CCC_ObjCInstanceMessage: {
440       contexts = CXCompletionContext_ObjCInstanceMessage;
441       break;
442     }
443     case CodeCompletionContext::CCC_ObjCClassMessage: {
444       contexts = CXCompletionContext_ObjCClassMessage;
445       break;
446     }
447     case CodeCompletionContext::CCC_ObjCInterfaceName: {
448       contexts = CXCompletionContext_ObjCInterface;
449       break;
450     }
451     case CodeCompletionContext::CCC_ObjCCategoryName: {
452       contexts = CXCompletionContext_ObjCCategory;
453       break;
454     }
455     case CodeCompletionContext::CCC_Other:
456     case CodeCompletionContext::CCC_ObjCInterface:
457     case CodeCompletionContext::CCC_ObjCImplementation:
458     case CodeCompletionContext::CCC_Name:
459     case CodeCompletionContext::CCC_MacroName:
460     case CodeCompletionContext::CCC_PreprocessorExpression:
461     case CodeCompletionContext::CCC_PreprocessorDirective:
462     case CodeCompletionContext::CCC_TypeQualifiers: {
463       //Only Clang results should be accepted, so we'll set all of the other
464       //context bits to 0 (i.e. the empty set)
465       contexts = CXCompletionContext_Unexposed;
466       break;
467     }
468     case CodeCompletionContext::CCC_Recovery: {
469       //We don't know what the current context is, so we'll return unknown
470       //This is the equivalent of setting all of the other context bits
471       contexts = CXCompletionContext_Unknown;
472       break;
473     }
474   }
475   return contexts;
476 }
477 
478 namespace {
479   class CaptureCompletionResults : public CodeCompleteConsumer {
480     AllocatedCXCodeCompleteResults &AllocatedResults;
481     SmallVector<CXCompletionResult, 16> StoredResults;
482     CXTranslationUnit *TU;
483   public:
484     CaptureCompletionResults(AllocatedCXCodeCompleteResults &Results,
485                              CXTranslationUnit *TranslationUnit)
486       : CodeCompleteConsumer(true, false, true, false),
487         AllocatedResults(Results), TU(TranslationUnit) { }
488     ~CaptureCompletionResults() { Finish(); }
489 
490     virtual void ProcessCodeCompleteResults(Sema &S,
491                                             CodeCompletionContext Context,
492                                             CodeCompletionResult *Results,
493                                             unsigned NumResults) {
494       StoredResults.reserve(StoredResults.size() + NumResults);
495       for (unsigned I = 0; I != NumResults; ++I) {
496         CodeCompletionString *StoredCompletion
497           = Results[I].CreateCodeCompletionString(S,
498                                       AllocatedResults.CodeCompletionAllocator);
499 
500         CXCompletionResult R;
501         R.CursorKind = Results[I].CursorKind;
502         R.CompletionString = StoredCompletion;
503         StoredResults.push_back(R);
504       }
505 
506       enum CodeCompletionContext::Kind contextKind = Context.getKind();
507 
508       AllocatedResults.ContextKind = contextKind;
509       AllocatedResults.Contexts = getContextsForContextKind(contextKind, S);
510 
511       AllocatedResults.Selector = "";
512       if (Context.getNumSelIdents() > 0) {
513         for (unsigned i = 0; i < Context.getNumSelIdents(); i++) {
514           IdentifierInfo *selIdent = Context.getSelIdents()[i];
515           if (selIdent != NULL) {
516             StringRef selectorString = Context.getSelIdents()[i]->getName();
517             AllocatedResults.Selector += selectorString;
518           }
519           AllocatedResults.Selector += ":";
520         }
521       }
522 
523       QualType baseType = Context.getBaseType();
524       NamedDecl *D = NULL;
525 
526       if (!baseType.isNull()) {
527         // Get the declaration for a class/struct/union/enum type
528         if (const TagType *Tag = baseType->getAs<TagType>())
529           D = Tag->getDecl();
530         // Get the @interface declaration for a (possibly-qualified) Objective-C
531         // object pointer type, e.g., NSString*
532         else if (const ObjCObjectPointerType *ObjPtr =
533                  baseType->getAs<ObjCObjectPointerType>())
534           D = ObjPtr->getInterfaceDecl();
535         // Get the @interface declaration for an Objective-C object type
536         else if (const ObjCObjectType *Obj = baseType->getAs<ObjCObjectType>())
537           D = Obj->getInterface();
538         // Get the class for a C++ injected-class-name
539         else if (const InjectedClassNameType *Injected =
540                  baseType->getAs<InjectedClassNameType>())
541           D = Injected->getDecl();
542       }
543 
544       if (D != NULL) {
545         CXCursor cursor = cxcursor::MakeCXCursor(D, *TU);
546 
547         CXCursorKind cursorKind = clang_getCursorKind(cursor);
548         CXString cursorUSR = clang_getCursorUSR(cursor);
549 
550         // Normally, clients of CXString shouldn't care whether or not
551         // a CXString is managed by a pool or by explicitly malloc'ed memory.
552         // However, there are cases when AllocatedResults outlives the
553         // CXTranslationUnit.  This is a workaround that failure mode.
554         if (cxstring::isManagedByPool(cursorUSR)) {
555           CXString heapStr =
556             cxstring::createCXString(clang_getCString(cursorUSR), true);
557           clang_disposeString(cursorUSR);
558           cursorUSR = heapStr;
559         }
560 
561         AllocatedResults.ContainerKind = cursorKind;
562         AllocatedResults.ContainerUSR = cursorUSR;
563 
564         const Type *type = baseType.getTypePtrOrNull();
565         if (type != NULL) {
566           AllocatedResults.ContainerIsIncomplete = type->isIncompleteType();
567         }
568         else {
569           AllocatedResults.ContainerIsIncomplete = 1;
570         }
571       }
572       else {
573         AllocatedResults.ContainerKind = CXCursor_InvalidCode;
574         AllocatedResults.ContainerUSR = createCXString("");
575         AllocatedResults.ContainerIsIncomplete = 1;
576       }
577     }
578 
579     virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
580                                            OverloadCandidate *Candidates,
581                                            unsigned NumCandidates) {
582       StoredResults.reserve(StoredResults.size() + NumCandidates);
583       for (unsigned I = 0; I != NumCandidates; ++I) {
584         CodeCompletionString *StoredCompletion
585           = Candidates[I].CreateSignatureString(CurrentArg, S,
586                                       AllocatedResults.CodeCompletionAllocator);
587 
588         CXCompletionResult R;
589         R.CursorKind = CXCursor_NotImplemented;
590         R.CompletionString = StoredCompletion;
591         StoredResults.push_back(R);
592       }
593     }
594 
595     virtual CodeCompletionAllocator &getAllocator() {
596       return AllocatedResults.CodeCompletionAllocator;
597     }
598 
599   private:
600     void Finish() {
601       AllocatedResults.Results = new CXCompletionResult [StoredResults.size()];
602       AllocatedResults.NumResults = StoredResults.size();
603       std::memcpy(AllocatedResults.Results, StoredResults.data(),
604                   StoredResults.size() * sizeof(CXCompletionResult));
605       StoredResults.clear();
606     }
607   };
608 }
609 
610 extern "C" {
611 struct CodeCompleteAtInfo {
612   CXTranslationUnit TU;
613   const char *complete_filename;
614   unsigned complete_line;
615   unsigned complete_column;
616   struct CXUnsavedFile *unsaved_files;
617   unsigned num_unsaved_files;
618   unsigned options;
619   CXCodeCompleteResults *result;
620 };
621 void clang_codeCompleteAt_Impl(void *UserData) {
622   CodeCompleteAtInfo *CCAI = static_cast<CodeCompleteAtInfo*>(UserData);
623   CXTranslationUnit TU = CCAI->TU;
624   const char *complete_filename = CCAI->complete_filename;
625   unsigned complete_line = CCAI->complete_line;
626   unsigned complete_column = CCAI->complete_column;
627   struct CXUnsavedFile *unsaved_files = CCAI->unsaved_files;
628   unsigned num_unsaved_files = CCAI->num_unsaved_files;
629   unsigned options = CCAI->options;
630   CCAI->result = 0;
631 
632 #ifdef UDP_CODE_COMPLETION_LOGGER
633 #ifdef UDP_CODE_COMPLETION_LOGGER_PORT
634   const llvm::TimeRecord &StartTime =  llvm::TimeRecord::getCurrentTime();
635 #endif
636 #endif
637 
638   bool EnableLogging = getenv("LIBCLANG_CODE_COMPLETION_LOGGING") != 0;
639 
640   ASTUnit *AST = static_cast<ASTUnit *>(TU->TUData);
641   if (!AST)
642     return;
643 
644   ASTUnit::ConcurrencyCheck Check(*AST);
645 
646   // Perform the remapping of source files.
647   SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles;
648   for (unsigned I = 0; I != num_unsaved_files; ++I) {
649     StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length);
650     const llvm::MemoryBuffer *Buffer
651       = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename);
652     RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename,
653                                            Buffer));
654   }
655 
656   if (EnableLogging) {
657     // FIXME: Add logging.
658   }
659 
660   // Parse the resulting source file to find code-completion results.
661   AllocatedCXCodeCompleteResults *Results =
662         new AllocatedCXCodeCompleteResults(AST->getFileSystemOpts());
663   Results->Results = 0;
664   Results->NumResults = 0;
665 
666   // Create a code-completion consumer to capture the results.
667   CaptureCompletionResults Capture(*Results, &TU);
668 
669   // Perform completion.
670   AST->CodeComplete(complete_filename, complete_line, complete_column,
671                     RemappedFiles.data(), RemappedFiles.size(),
672                     (options & CXCodeComplete_IncludeMacros),
673                     (options & CXCodeComplete_IncludeCodePatterns),
674                     Capture,
675                     *Results->Diag, Results->LangOpts, *Results->SourceMgr,
676                     *Results->FileMgr, Results->Diagnostics,
677                     Results->TemporaryBuffers);
678 
679   // Keep a reference to the allocator used for cached global completions, so
680   // that we can be sure that the memory used by our code completion strings
681   // doesn't get freed due to subsequent reparses (while the code completion
682   // results are still active).
683   Results->CachedCompletionAllocator = AST->getCachedCompletionAllocator();
684 
685 
686 
687 #ifdef UDP_CODE_COMPLETION_LOGGER
688 #ifdef UDP_CODE_COMPLETION_LOGGER_PORT
689   const llvm::TimeRecord &EndTime =  llvm::TimeRecord::getCurrentTime();
690   llvm::SmallString<256> LogResult;
691   llvm::raw_svector_ostream os(LogResult);
692 
693   // Figure out the language and whether or not it uses PCH.
694   const char *lang = 0;
695   bool usesPCH = false;
696 
697   for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
698        I != E; ++I) {
699     if (*I == 0)
700       continue;
701     if (strcmp(*I, "-x") == 0) {
702       if (I + 1 != E) {
703         lang = *(++I);
704         continue;
705       }
706     }
707     else if (strcmp(*I, "-include") == 0) {
708       if (I+1 != E) {
709         const char *arg = *(++I);
710         llvm::SmallString<512> pchName;
711         {
712           llvm::raw_svector_ostream os(pchName);
713           os << arg << ".pth";
714         }
715         pchName.push_back('\0');
716         struct stat stat_results;
717         if (stat(pchName.str().c_str(), &stat_results) == 0)
718           usesPCH = true;
719         continue;
720       }
721     }
722   }
723 
724   os << "{ ";
725   os << "\"wall\": " << (EndTime.getWallTime() - StartTime.getWallTime());
726   os << ", \"numRes\": " << Results->NumResults;
727   os << ", \"diags\": " << Results->Diagnostics.size();
728   os << ", \"pch\": " << (usesPCH ? "true" : "false");
729   os << ", \"lang\": \"" << (lang ? lang : "<unknown>") << '"';
730   const char *name = getlogin();
731   os << ", \"user\": \"" << (name ? name : "unknown") << '"';
732   os << ", \"clangVer\": \"" << getClangFullVersion() << '"';
733   os << " }";
734 
735   StringRef res = os.str();
736   if (res.size() > 0) {
737     do {
738       // Setup the UDP socket.
739       struct sockaddr_in servaddr;
740       bzero(&servaddr, sizeof(servaddr));
741       servaddr.sin_family = AF_INET;
742       servaddr.sin_port = htons(UDP_CODE_COMPLETION_LOGGER_PORT);
743       if (inet_pton(AF_INET, UDP_CODE_COMPLETION_LOGGER,
744                     &servaddr.sin_addr) <= 0)
745         break;
746 
747       int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
748       if (sockfd < 0)
749         break;
750 
751       sendto(sockfd, res.data(), res.size(), 0,
752              (struct sockaddr *)&servaddr, sizeof(servaddr));
753       close(sockfd);
754     }
755     while (false);
756   }
757 #endif
758 #endif
759   CCAI->result = Results;
760 }
761 CXCodeCompleteResults *clang_codeCompleteAt(CXTranslationUnit TU,
762                                             const char *complete_filename,
763                                             unsigned complete_line,
764                                             unsigned complete_column,
765                                             struct CXUnsavedFile *unsaved_files,
766                                             unsigned num_unsaved_files,
767                                             unsigned options) {
768   CodeCompleteAtInfo CCAI = { TU, complete_filename, complete_line,
769                               complete_column, unsaved_files, num_unsaved_files,
770                               options, 0 };
771   llvm::CrashRecoveryContext CRC;
772 
773   if (!RunSafely(CRC, clang_codeCompleteAt_Impl, &CCAI)) {
774     fprintf(stderr, "libclang: crash detected in code completion\n");
775     static_cast<ASTUnit *>(TU->TUData)->setUnsafeToFree(true);
776     return 0;
777   } else if (getenv("LIBCLANG_RESOURCE_USAGE"))
778     PrintLibclangResourceUsage(TU);
779 
780   return CCAI.result;
781 }
782 
783 unsigned clang_defaultCodeCompleteOptions(void) {
784   return CXCodeComplete_IncludeMacros;
785 }
786 
787 void clang_disposeCodeCompleteResults(CXCodeCompleteResults *ResultsIn) {
788   if (!ResultsIn)
789     return;
790 
791   AllocatedCXCodeCompleteResults *Results
792     = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn);
793   delete Results;
794 }
795 
796 unsigned
797 clang_codeCompleteGetNumDiagnostics(CXCodeCompleteResults *ResultsIn) {
798   AllocatedCXCodeCompleteResults *Results
799     = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn);
800   if (!Results)
801     return 0;
802 
803   return Results->Diagnostics.size();
804 }
805 
806 CXDiagnostic
807 clang_codeCompleteGetDiagnostic(CXCodeCompleteResults *ResultsIn,
808                                 unsigned Index) {
809   AllocatedCXCodeCompleteResults *Results
810     = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn);
811   if (!Results || Index >= Results->Diagnostics.size())
812     return 0;
813 
814   return new CXStoredDiagnostic(Results->Diagnostics[Index], Results->LangOpts);
815 }
816 
817 unsigned long long
818 clang_codeCompleteGetContexts(CXCodeCompleteResults *ResultsIn) {
819   AllocatedCXCodeCompleteResults *Results
820     = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn);
821   if (!Results)
822     return 0;
823 
824   return Results->Contexts;
825 }
826 
827 enum CXCursorKind clang_codeCompleteGetContainerKind(
828                                                CXCodeCompleteResults *ResultsIn,
829                                                      unsigned *IsIncomplete) {
830   AllocatedCXCodeCompleteResults *Results =
831     static_cast<AllocatedCXCodeCompleteResults *>(ResultsIn);
832   if (!Results)
833     return CXCursor_InvalidCode;
834 
835   if (IsIncomplete != NULL) {
836     *IsIncomplete = Results->ContainerIsIncomplete;
837   }
838 
839   return Results->ContainerKind;
840 }
841 
842 CXString clang_codeCompleteGetContainerUSR(CXCodeCompleteResults *ResultsIn) {
843   AllocatedCXCodeCompleteResults *Results =
844     static_cast<AllocatedCXCodeCompleteResults *>(ResultsIn);
845   if (!Results)
846     return createCXString("");
847 
848   return createCXString(clang_getCString(Results->ContainerUSR));
849 }
850 
851 
852 CXString clang_codeCompleteGetObjCSelector(CXCodeCompleteResults *ResultsIn) {
853   AllocatedCXCodeCompleteResults *Results =
854     static_cast<AllocatedCXCodeCompleteResults *>(ResultsIn);
855   if (!Results)
856     return createCXString("");
857 
858   return createCXString(Results->Selector);
859 }
860 
861 } // end extern "C"
862 
863 /// \brief Simple utility function that appends a \p New string to the given
864 /// \p Old string, using the \p Buffer for storage.
865 ///
866 /// \param Old The string to which we are appending. This parameter will be
867 /// updated to reflect the complete string.
868 ///
869 ///
870 /// \param New The string to append to \p Old.
871 ///
872 /// \param Buffer A buffer that stores the actual, concatenated string. It will
873 /// be used if the old string is already-non-empty.
874 static void AppendToString(StringRef &Old, StringRef New,
875                            llvm::SmallString<256> &Buffer) {
876   if (Old.empty()) {
877     Old = New;
878     return;
879   }
880 
881   if (Buffer.empty())
882     Buffer.append(Old.begin(), Old.end());
883   Buffer.append(New.begin(), New.end());
884   Old = Buffer.str();
885 }
886 
887 /// \brief Get the typed-text blocks from the given code-completion string
888 /// and return them as a single string.
889 ///
890 /// \param String The code-completion string whose typed-text blocks will be
891 /// concatenated.
892 ///
893 /// \param Buffer A buffer used for storage of the completed name.
894 static StringRef GetTypedName(CodeCompletionString *String,
895                                     llvm::SmallString<256> &Buffer) {
896   StringRef Result;
897   for (CodeCompletionString::iterator C = String->begin(), CEnd = String->end();
898        C != CEnd; ++C) {
899     if (C->Kind == CodeCompletionString::CK_TypedText)
900       AppendToString(Result, C->Text, Buffer);
901   }
902 
903   return Result;
904 }
905 
906 namespace {
907   struct OrderCompletionResults {
908     bool operator()(const CXCompletionResult &XR,
909                     const CXCompletionResult &YR) const {
910       CodeCompletionString *X
911         = (CodeCompletionString *)XR.CompletionString;
912       CodeCompletionString *Y
913         = (CodeCompletionString *)YR.CompletionString;
914 
915       llvm::SmallString<256> XBuffer;
916       StringRef XText = GetTypedName(X, XBuffer);
917       llvm::SmallString<256> YBuffer;
918       StringRef YText = GetTypedName(Y, YBuffer);
919 
920       if (XText.empty() || YText.empty())
921         return !XText.empty();
922 
923       int result = XText.compare_lower(YText);
924       if (result < 0)
925         return true;
926       if (result > 0)
927         return false;
928 
929       result = XText.compare(YText);
930       return result < 0;
931     }
932   };
933 }
934 
935 extern "C" {
936   void clang_sortCodeCompletionResults(CXCompletionResult *Results,
937                                        unsigned NumResults) {
938     std::stable_sort(Results, Results + NumResults, OrderCompletionResults());
939   }
940 }
941