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