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