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