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 extern "C" {
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 0;
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 0;
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 /// \brief 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(const FileSystemOptions& FileSystemOpts);
253   ~AllocatedCXCodeCompleteResults();
254 
255   /// \brief Diagnostics produced while performing code completion.
256   SmallVector<StoredDiagnostic, 8> Diagnostics;
257 
258   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
259 
260   /// \brief Diag object
261   IntrusiveRefCntPtr<DiagnosticsEngine> Diag;
262 
263   /// \brief Language options used to adjust source locations.
264   LangOptions LangOpts;
265 
266   FileSystemOptions FileSystemOpts;
267 
268   /// \brief File manager, used for diagnostics.
269   IntrusiveRefCntPtr<FileManager> FileMgr;
270 
271   /// \brief Source manager, used for diagnostics.
272   IntrusiveRefCntPtr<SourceManager> SourceMgr;
273 
274   /// \brief Temporary files that should be removed once we have finished
275   /// with the code-completion results.
276   std::vector<std::string> TemporaryFiles;
277 
278   /// \brief Temporary buffers that will be deleted once we have finished with
279   /// the code-completion results.
280   SmallVector<const llvm::MemoryBuffer *, 1> TemporaryBuffers;
281 
282   /// \brief Allocator used to store globally cached code-completion results.
283   IntrusiveRefCntPtr<clang::GlobalCodeCompletionAllocator>
284     CachedCompletionAllocator;
285 
286   /// \brief Allocator used to store code completion results.
287   IntrusiveRefCntPtr<clang::GlobalCodeCompletionAllocator>
288     CodeCompletionAllocator;
289 
290   /// \brief Context under which completion occurred.
291   enum clang::CodeCompletionContext::Kind ContextKind;
292 
293   /// \brief A bitfield representing the acceptable completions for the
294   /// current context.
295   unsigned long long Contexts;
296 
297   /// \brief The kind of the container for the current context for completions.
298   enum CXCursorKind ContainerKind;
299 
300   /// \brief The USR of the container for the current context for completions.
301   std::string ContainerUSR;
302 
303   /// \brief a boolean value indicating whether there is complete information
304   /// about the container
305   unsigned ContainerIsIncomplete;
306 
307   /// \brief A string containing the Objective-C selector entered thus far for a
308   /// message send.
309   std::string Selector;
310 };
311 
312 } // end anonymous namespace
313 
314 /// \brief Tracks the number of code-completion result objects that are
315 /// currently active.
316 ///
317 /// Used for debugging purposes only.
318 static std::atomic<unsigned> CodeCompletionResultObjects;
319 
320 AllocatedCXCodeCompleteResults::AllocatedCXCodeCompleteResults(
321                                       const FileSystemOptions& FileSystemOpts)
322   : CXCodeCompleteResults(),
323     DiagOpts(new DiagnosticOptions),
324     Diag(new DiagnosticsEngine(
325                    IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
326                    &*DiagOpts)),
327     FileSystemOpts(FileSystemOpts),
328     FileMgr(new FileManager(FileSystemOpts)),
329     SourceMgr(new SourceManager(*Diag, *FileMgr)),
330     CodeCompletionAllocator(new clang::GlobalCodeCompletionAllocator),
331     Contexts(CXCompletionContext_Unknown),
332     ContainerKind(CXCursor_InvalidCode),
333     ContainerIsIncomplete(1)
334 {
335   if (getenv("LIBCLANG_OBJTRACKING"))
336     fprintf(stderr, "+++ %u completion results\n",
337             ++CodeCompletionResultObjects);
338 }
339 
340 AllocatedCXCodeCompleteResults::~AllocatedCXCodeCompleteResults() {
341   delete [] Results;
342 
343   for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I)
344     llvm::sys::fs::remove(TemporaryFiles[I]);
345   for (unsigned I = 0, N = TemporaryBuffers.size(); I != N; ++I)
346     delete TemporaryBuffers[I];
347 
348   if (getenv("LIBCLANG_OBJTRACKING"))
349     fprintf(stderr, "--- %u completion results\n",
350             --CodeCompletionResultObjects);
351 }
352 
353 } // end extern "C"
354 
355 static unsigned long long getContextsForContextKind(
356                                           enum CodeCompletionContext::Kind kind,
357                                                     Sema &S) {
358   unsigned long long contexts = 0;
359   switch (kind) {
360     case CodeCompletionContext::CCC_OtherWithMacros: {
361       //We can allow macros here, but we don't know what else is permissible
362       //So we'll say the only thing permissible are macros
363       contexts = CXCompletionContext_MacroName;
364       break;
365     }
366     case CodeCompletionContext::CCC_TopLevel:
367     case CodeCompletionContext::CCC_ObjCIvarList:
368     case CodeCompletionContext::CCC_ClassStructUnion:
369     case CodeCompletionContext::CCC_Type: {
370       contexts = CXCompletionContext_AnyType |
371                  CXCompletionContext_ObjCInterface;
372       if (S.getLangOpts().CPlusPlus) {
373         contexts |= CXCompletionContext_EnumTag |
374                     CXCompletionContext_UnionTag |
375                     CXCompletionContext_StructTag |
376                     CXCompletionContext_ClassTag |
377                     CXCompletionContext_NestedNameSpecifier;
378       }
379       break;
380     }
381     case CodeCompletionContext::CCC_Statement: {
382       contexts = CXCompletionContext_AnyType |
383                  CXCompletionContext_ObjCInterface |
384                  CXCompletionContext_AnyValue;
385       if (S.getLangOpts().CPlusPlus) {
386         contexts |= CXCompletionContext_EnumTag |
387                     CXCompletionContext_UnionTag |
388                     CXCompletionContext_StructTag |
389                     CXCompletionContext_ClassTag |
390                     CXCompletionContext_NestedNameSpecifier;
391       }
392       break;
393     }
394     case CodeCompletionContext::CCC_Expression: {
395       contexts = CXCompletionContext_AnyValue;
396       if (S.getLangOpts().CPlusPlus) {
397         contexts |= CXCompletionContext_AnyType |
398                     CXCompletionContext_ObjCInterface |
399                     CXCompletionContext_EnumTag |
400                     CXCompletionContext_UnionTag |
401                     CXCompletionContext_StructTag |
402                     CXCompletionContext_ClassTag |
403                     CXCompletionContext_NestedNameSpecifier;
404       }
405       break;
406     }
407     case CodeCompletionContext::CCC_ObjCMessageReceiver: {
408       contexts = CXCompletionContext_ObjCObjectValue |
409                  CXCompletionContext_ObjCSelectorValue |
410                  CXCompletionContext_ObjCInterface;
411       if (S.getLangOpts().CPlusPlus) {
412         contexts |= CXCompletionContext_CXXClassTypeValue |
413                     CXCompletionContext_AnyType |
414                     CXCompletionContext_EnumTag |
415                     CXCompletionContext_UnionTag |
416                     CXCompletionContext_StructTag |
417                     CXCompletionContext_ClassTag |
418                     CXCompletionContext_NestedNameSpecifier;
419       }
420       break;
421     }
422     case CodeCompletionContext::CCC_DotMemberAccess: {
423       contexts = CXCompletionContext_DotMemberAccess;
424       break;
425     }
426     case CodeCompletionContext::CCC_ArrowMemberAccess: {
427       contexts = CXCompletionContext_ArrowMemberAccess;
428       break;
429     }
430     case CodeCompletionContext::CCC_ObjCPropertyAccess: {
431       contexts = CXCompletionContext_ObjCPropertyAccess;
432       break;
433     }
434     case CodeCompletionContext::CCC_EnumTag: {
435       contexts = CXCompletionContext_EnumTag |
436                  CXCompletionContext_NestedNameSpecifier;
437       break;
438     }
439     case CodeCompletionContext::CCC_UnionTag: {
440       contexts = CXCompletionContext_UnionTag |
441                  CXCompletionContext_NestedNameSpecifier;
442       break;
443     }
444     case CodeCompletionContext::CCC_ClassOrStructTag: {
445       contexts = CXCompletionContext_StructTag |
446                  CXCompletionContext_ClassTag |
447                  CXCompletionContext_NestedNameSpecifier;
448       break;
449     }
450     case CodeCompletionContext::CCC_ObjCProtocolName: {
451       contexts = CXCompletionContext_ObjCProtocol;
452       break;
453     }
454     case CodeCompletionContext::CCC_Namespace: {
455       contexts = CXCompletionContext_Namespace;
456       break;
457     }
458     case CodeCompletionContext::CCC_PotentiallyQualifiedName: {
459       contexts = CXCompletionContext_NestedNameSpecifier;
460       break;
461     }
462     case CodeCompletionContext::CCC_MacroNameUse: {
463       contexts = CXCompletionContext_MacroName;
464       break;
465     }
466     case CodeCompletionContext::CCC_NaturalLanguage: {
467       contexts = CXCompletionContext_NaturalLanguage;
468       break;
469     }
470     case CodeCompletionContext::CCC_SelectorName: {
471       contexts = CXCompletionContext_ObjCSelectorName;
472       break;
473     }
474     case CodeCompletionContext::CCC_ParenthesizedExpression: {
475       contexts = CXCompletionContext_AnyType |
476                  CXCompletionContext_ObjCInterface |
477                  CXCompletionContext_AnyValue;
478       if (S.getLangOpts().CPlusPlus) {
479         contexts |= CXCompletionContext_EnumTag |
480                     CXCompletionContext_UnionTag |
481                     CXCompletionContext_StructTag |
482                     CXCompletionContext_ClassTag |
483                     CXCompletionContext_NestedNameSpecifier;
484       }
485       break;
486     }
487     case CodeCompletionContext::CCC_ObjCInstanceMessage: {
488       contexts = CXCompletionContext_ObjCInstanceMessage;
489       break;
490     }
491     case CodeCompletionContext::CCC_ObjCClassMessage: {
492       contexts = CXCompletionContext_ObjCClassMessage;
493       break;
494     }
495     case CodeCompletionContext::CCC_ObjCInterfaceName: {
496       contexts = CXCompletionContext_ObjCInterface;
497       break;
498     }
499     case CodeCompletionContext::CCC_ObjCCategoryName: {
500       contexts = CXCompletionContext_ObjCCategory;
501       break;
502     }
503     case CodeCompletionContext::CCC_Other:
504     case CodeCompletionContext::CCC_ObjCInterface:
505     case CodeCompletionContext::CCC_ObjCImplementation:
506     case CodeCompletionContext::CCC_Name:
507     case CodeCompletionContext::CCC_MacroName:
508     case CodeCompletionContext::CCC_PreprocessorExpression:
509     case CodeCompletionContext::CCC_PreprocessorDirective:
510     case CodeCompletionContext::CCC_TypeQualifiers: {
511       //Only Clang results should be accepted, so we'll set all of the other
512       //context bits to 0 (i.e. the empty set)
513       contexts = CXCompletionContext_Unexposed;
514       break;
515     }
516     case CodeCompletionContext::CCC_Recovery: {
517       //We don't know what the current context is, so we'll return unknown
518       //This is the equivalent of setting all of the other context bits
519       contexts = CXCompletionContext_Unknown;
520       break;
521     }
522   }
523   return contexts;
524 }
525 
526 namespace {
527   class CaptureCompletionResults : public CodeCompleteConsumer {
528     AllocatedCXCodeCompleteResults &AllocatedResults;
529     CodeCompletionTUInfo CCTUInfo;
530     SmallVector<CXCompletionResult, 16> StoredResults;
531     CXTranslationUnit *TU;
532   public:
533     CaptureCompletionResults(const CodeCompleteOptions &Opts,
534                              AllocatedCXCodeCompleteResults &Results,
535                              CXTranslationUnit *TranslationUnit)
536       : CodeCompleteConsumer(Opts, false),
537         AllocatedResults(Results), CCTUInfo(Results.CodeCompletionAllocator),
538         TU(TranslationUnit) { }
539     ~CaptureCompletionResults() { Finish(); }
540 
541     void ProcessCodeCompleteResults(Sema &S,
542                                     CodeCompletionContext Context,
543                                     CodeCompletionResult *Results,
544                                     unsigned NumResults) override {
545       StoredResults.reserve(StoredResults.size() + NumResults);
546       for (unsigned I = 0; I != NumResults; ++I) {
547         CodeCompletionString *StoredCompletion
548           = Results[I].CreateCodeCompletionString(S, getAllocator(),
549                                                   getCodeCompletionTUInfo(),
550                                                   includeBriefComments());
551 
552         CXCompletionResult R;
553         R.CursorKind = Results[I].CursorKind;
554         R.CompletionString = StoredCompletion;
555         StoredResults.push_back(R);
556       }
557 
558       enum CodeCompletionContext::Kind contextKind = Context.getKind();
559 
560       AllocatedResults.ContextKind = contextKind;
561       AllocatedResults.Contexts = getContextsForContextKind(contextKind, S);
562 
563       AllocatedResults.Selector = "";
564       ArrayRef<IdentifierInfo *> SelIdents = Context.getSelIdents();
565       for (ArrayRef<IdentifierInfo *>::iterator I = SelIdents.begin(),
566                                                 E = SelIdents.end();
567            I != E; ++I) {
568         if (IdentifierInfo *selIdent = *I)
569           AllocatedResults.Selector += selIdent->getName();
570         AllocatedResults.Selector += ":";
571       }
572 
573       QualType baseType = Context.getBaseType();
574       NamedDecl *D = NULL;
575 
576       if (!baseType.isNull()) {
577         // Get the declaration for a class/struct/union/enum type
578         if (const TagType *Tag = baseType->getAs<TagType>())
579           D = Tag->getDecl();
580         // Get the @interface declaration for a (possibly-qualified) Objective-C
581         // object pointer type, e.g., NSString*
582         else if (const ObjCObjectPointerType *ObjPtr =
583                  baseType->getAs<ObjCObjectPointerType>())
584           D = ObjPtr->getInterfaceDecl();
585         // Get the @interface declaration for an Objective-C object type
586         else if (const ObjCObjectType *Obj = baseType->getAs<ObjCObjectType>())
587           D = Obj->getInterface();
588         // Get the class for a C++ injected-class-name
589         else if (const InjectedClassNameType *Injected =
590                  baseType->getAs<InjectedClassNameType>())
591           D = Injected->getDecl();
592       }
593 
594       if (D != NULL) {
595         CXCursor cursor = cxcursor::MakeCXCursor(D, *TU);
596 
597         AllocatedResults.ContainerKind = clang_getCursorKind(cursor);
598 
599         CXString CursorUSR = clang_getCursorUSR(cursor);
600         AllocatedResults.ContainerUSR = clang_getCString(CursorUSR);
601         clang_disposeString(CursorUSR);
602 
603         const Type *type = baseType.getTypePtrOrNull();
604         if (type != NULL) {
605           AllocatedResults.ContainerIsIncomplete = type->isIncompleteType();
606         }
607         else {
608           AllocatedResults.ContainerIsIncomplete = 1;
609         }
610       }
611       else {
612         AllocatedResults.ContainerKind = CXCursor_InvalidCode;
613         AllocatedResults.ContainerUSR.clear();
614         AllocatedResults.ContainerIsIncomplete = 1;
615       }
616     }
617 
618     void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
619                                    OverloadCandidate *Candidates,
620                                    unsigned NumCandidates) override {
621       StoredResults.reserve(StoredResults.size() + NumCandidates);
622       for (unsigned I = 0; I != NumCandidates; ++I) {
623         CodeCompletionString *StoredCompletion
624           = Candidates[I].CreateSignatureString(CurrentArg, S, getAllocator(),
625                                                 getCodeCompletionTUInfo());
626 
627         CXCompletionResult R;
628         R.CursorKind = CXCursor_NotImplemented;
629         R.CompletionString = StoredCompletion;
630         StoredResults.push_back(R);
631       }
632     }
633 
634     CodeCompletionAllocator &getAllocator() override {
635       return *AllocatedResults.CodeCompletionAllocator;
636     }
637 
638     CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo;}
639 
640   private:
641     void Finish() {
642       AllocatedResults.Results = new CXCompletionResult [StoredResults.size()];
643       AllocatedResults.NumResults = StoredResults.size();
644       std::memcpy(AllocatedResults.Results, StoredResults.data(),
645                   StoredResults.size() * sizeof(CXCompletionResult));
646       StoredResults.clear();
647     }
648   };
649 }
650 
651 extern "C" {
652 struct CodeCompleteAtInfo {
653   CXTranslationUnit TU;
654   const char *complete_filename;
655   unsigned complete_line;
656   unsigned complete_column;
657   struct CXUnsavedFile *unsaved_files;
658   unsigned num_unsaved_files;
659   unsigned options;
660   CXCodeCompleteResults *result;
661 };
662 void clang_codeCompleteAt_Impl(void *UserData) {
663   CodeCompleteAtInfo *CCAI = static_cast<CodeCompleteAtInfo*>(UserData);
664   CXTranslationUnit TU = CCAI->TU;
665   const char *complete_filename = CCAI->complete_filename;
666   unsigned complete_line = CCAI->complete_line;
667   unsigned complete_column = CCAI->complete_column;
668   struct CXUnsavedFile *unsaved_files = CCAI->unsaved_files;
669   unsigned num_unsaved_files = CCAI->num_unsaved_files;
670   unsigned options = CCAI->options;
671   bool IncludeBriefComments = options & CXCodeComplete_IncludeBriefComments;
672   CCAI->result = 0;
673 
674 #ifdef UDP_CODE_COMPLETION_LOGGER
675 #ifdef UDP_CODE_COMPLETION_LOGGER_PORT
676   const llvm::TimeRecord &StartTime =  llvm::TimeRecord::getCurrentTime();
677 #endif
678 #endif
679 
680   bool EnableLogging = getenv("LIBCLANG_CODE_COMPLETION_LOGGING") != 0;
681 
682   if (cxtu::isNotUsableTU(TU)) {
683     LOG_BAD_TU(TU);
684     return;
685   }
686 
687   ASTUnit *AST = cxtu::getASTUnit(TU);
688   if (!AST)
689     return;
690 
691   CIndexer *CXXIdx = TU->CIdx;
692   if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForEditing))
693     setThreadBackgroundPriority();
694 
695   ASTUnit::ConcurrencyCheck Check(*AST);
696 
697   // Perform the remapping of source files.
698   SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles;
699   for (unsigned I = 0; I != num_unsaved_files; ++I) {
700     StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length);
701     const llvm::MemoryBuffer *Buffer
702       = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename);
703     RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename,
704                                            Buffer));
705   }
706 
707   if (EnableLogging) {
708     // FIXME: Add logging.
709   }
710 
711   // Parse the resulting source file to find code-completion results.
712   AllocatedCXCodeCompleteResults *Results =
713         new AllocatedCXCodeCompleteResults(AST->getFileSystemOpts());
714   Results->Results = 0;
715   Results->NumResults = 0;
716 
717   // Create a code-completion consumer to capture the results.
718   CodeCompleteOptions Opts;
719   Opts.IncludeBriefComments = IncludeBriefComments;
720   CaptureCompletionResults Capture(Opts, *Results, &TU);
721 
722   // Perform completion.
723   AST->CodeComplete(complete_filename, complete_line, complete_column,
724                     RemappedFiles,
725                     (options & CXCodeComplete_IncludeMacros),
726                     (options & CXCodeComplete_IncludeCodePatterns),
727                     IncludeBriefComments,
728                     Capture,
729                     *Results->Diag, Results->LangOpts, *Results->SourceMgr,
730                     *Results->FileMgr, Results->Diagnostics,
731                     Results->TemporaryBuffers);
732 
733   // Keep a reference to the allocator used for cached global completions, so
734   // that we can be sure that the memory used by our code completion strings
735   // doesn't get freed due to subsequent reparses (while the code completion
736   // results are still active).
737   Results->CachedCompletionAllocator = AST->getCachedCompletionAllocator();
738 
739 
740 
741 #ifdef UDP_CODE_COMPLETION_LOGGER
742 #ifdef UDP_CODE_COMPLETION_LOGGER_PORT
743   const llvm::TimeRecord &EndTime =  llvm::TimeRecord::getCurrentTime();
744   SmallString<256> LogResult;
745   llvm::raw_svector_ostream os(LogResult);
746 
747   // Figure out the language and whether or not it uses PCH.
748   const char *lang = 0;
749   bool usesPCH = false;
750 
751   for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
752        I != E; ++I) {
753     if (*I == 0)
754       continue;
755     if (strcmp(*I, "-x") == 0) {
756       if (I + 1 != E) {
757         lang = *(++I);
758         continue;
759       }
760     }
761     else if (strcmp(*I, "-include") == 0) {
762       if (I+1 != E) {
763         const char *arg = *(++I);
764         SmallString<512> pchName;
765         {
766           llvm::raw_svector_ostream os(pchName);
767           os << arg << ".pth";
768         }
769         pchName.push_back('\0');
770         struct stat stat_results;
771         if (stat(pchName.str().c_str(), &stat_results) == 0)
772           usesPCH = true;
773         continue;
774       }
775     }
776   }
777 
778   os << "{ ";
779   os << "\"wall\": " << (EndTime.getWallTime() - StartTime.getWallTime());
780   os << ", \"numRes\": " << Results->NumResults;
781   os << ", \"diags\": " << Results->Diagnostics.size();
782   os << ", \"pch\": " << (usesPCH ? "true" : "false");
783   os << ", \"lang\": \"" << (lang ? lang : "<unknown>") << '"';
784   const char *name = getlogin();
785   os << ", \"user\": \"" << (name ? name : "unknown") << '"';
786   os << ", \"clangVer\": \"" << getClangFullVersion() << '"';
787   os << " }";
788 
789   StringRef res = os.str();
790   if (res.size() > 0) {
791     do {
792       // Setup the UDP socket.
793       struct sockaddr_in servaddr;
794       bzero(&servaddr, sizeof(servaddr));
795       servaddr.sin_family = AF_INET;
796       servaddr.sin_port = htons(UDP_CODE_COMPLETION_LOGGER_PORT);
797       if (inet_pton(AF_INET, UDP_CODE_COMPLETION_LOGGER,
798                     &servaddr.sin_addr) <= 0)
799         break;
800 
801       int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
802       if (sockfd < 0)
803         break;
804 
805       sendto(sockfd, res.data(), res.size(), 0,
806              (struct sockaddr *)&servaddr, sizeof(servaddr));
807       close(sockfd);
808     }
809     while (false);
810   }
811 #endif
812 #endif
813   CCAI->result = Results;
814 }
815 CXCodeCompleteResults *clang_codeCompleteAt(CXTranslationUnit TU,
816                                             const char *complete_filename,
817                                             unsigned complete_line,
818                                             unsigned complete_column,
819                                             struct CXUnsavedFile *unsaved_files,
820                                             unsigned num_unsaved_files,
821                                             unsigned options) {
822   LOG_FUNC_SECTION {
823     *Log << TU << ' '
824          << complete_filename << ':' << complete_line << ':' << complete_column;
825   }
826 
827   CodeCompleteAtInfo CCAI = { TU, complete_filename, complete_line,
828                               complete_column, unsaved_files, num_unsaved_files,
829                               options, 0 };
830 
831   if (getenv("LIBCLANG_NOTHREADS")) {
832     clang_codeCompleteAt_Impl(&CCAI);
833     return CCAI.result;
834   }
835 
836   llvm::CrashRecoveryContext CRC;
837 
838   if (!RunSafely(CRC, clang_codeCompleteAt_Impl, &CCAI)) {
839     fprintf(stderr, "libclang: crash detected in code completion\n");
840     cxtu::getASTUnit(TU)->setUnsafeToFree(true);
841     return 0;
842   } else if (getenv("LIBCLANG_RESOURCE_USAGE"))
843     PrintLibclangResourceUsage(TU);
844 
845   return CCAI.result;
846 }
847 
848 unsigned clang_defaultCodeCompleteOptions(void) {
849   return CXCodeComplete_IncludeMacros;
850 }
851 
852 void clang_disposeCodeCompleteResults(CXCodeCompleteResults *ResultsIn) {
853   if (!ResultsIn)
854     return;
855 
856   AllocatedCXCodeCompleteResults *Results
857     = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn);
858   delete Results;
859 }
860 
861 unsigned
862 clang_codeCompleteGetNumDiagnostics(CXCodeCompleteResults *ResultsIn) {
863   AllocatedCXCodeCompleteResults *Results
864     = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn);
865   if (!Results)
866     return 0;
867 
868   return Results->Diagnostics.size();
869 }
870 
871 CXDiagnostic
872 clang_codeCompleteGetDiagnostic(CXCodeCompleteResults *ResultsIn,
873                                 unsigned Index) {
874   AllocatedCXCodeCompleteResults *Results
875     = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn);
876   if (!Results || Index >= Results->Diagnostics.size())
877     return 0;
878 
879   return new CXStoredDiagnostic(Results->Diagnostics[Index], Results->LangOpts);
880 }
881 
882 unsigned long long
883 clang_codeCompleteGetContexts(CXCodeCompleteResults *ResultsIn) {
884   AllocatedCXCodeCompleteResults *Results
885     = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn);
886   if (!Results)
887     return 0;
888 
889   return Results->Contexts;
890 }
891 
892 enum CXCursorKind clang_codeCompleteGetContainerKind(
893                                                CXCodeCompleteResults *ResultsIn,
894                                                      unsigned *IsIncomplete) {
895   AllocatedCXCodeCompleteResults *Results =
896     static_cast<AllocatedCXCodeCompleteResults *>(ResultsIn);
897   if (!Results)
898     return CXCursor_InvalidCode;
899 
900   if (IsIncomplete != NULL) {
901     *IsIncomplete = Results->ContainerIsIncomplete;
902   }
903 
904   return Results->ContainerKind;
905 }
906 
907 CXString clang_codeCompleteGetContainerUSR(CXCodeCompleteResults *ResultsIn) {
908   AllocatedCXCodeCompleteResults *Results =
909     static_cast<AllocatedCXCodeCompleteResults *>(ResultsIn);
910   if (!Results)
911     return cxstring::createEmpty();
912 
913   return cxstring::createRef(Results->ContainerUSR.c_str());
914 }
915 
916 
917 CXString clang_codeCompleteGetObjCSelector(CXCodeCompleteResults *ResultsIn) {
918   AllocatedCXCodeCompleteResults *Results =
919     static_cast<AllocatedCXCodeCompleteResults *>(ResultsIn);
920   if (!Results)
921     return cxstring::createEmpty();
922 
923   return cxstring::createDup(Results->Selector);
924 }
925 
926 } // end extern "C"
927 
928 /// \brief Simple utility function that appends a \p New string to the given
929 /// \p Old string, using the \p Buffer for storage.
930 ///
931 /// \param Old The string to which we are appending. This parameter will be
932 /// updated to reflect the complete string.
933 ///
934 ///
935 /// \param New The string to append to \p Old.
936 ///
937 /// \param Buffer A buffer that stores the actual, concatenated string. It will
938 /// be used if the old string is already-non-empty.
939 static void AppendToString(StringRef &Old, StringRef New,
940                            SmallString<256> &Buffer) {
941   if (Old.empty()) {
942     Old = New;
943     return;
944   }
945 
946   if (Buffer.empty())
947     Buffer.append(Old.begin(), Old.end());
948   Buffer.append(New.begin(), New.end());
949   Old = Buffer.str();
950 }
951 
952 /// \brief Get the typed-text blocks from the given code-completion string
953 /// and return them as a single string.
954 ///
955 /// \param String The code-completion string whose typed-text blocks will be
956 /// concatenated.
957 ///
958 /// \param Buffer A buffer used for storage of the completed name.
959 static StringRef GetTypedName(CodeCompletionString *String,
960                                     SmallString<256> &Buffer) {
961   StringRef Result;
962   for (CodeCompletionString::iterator C = String->begin(), CEnd = String->end();
963        C != CEnd; ++C) {
964     if (C->Kind == CodeCompletionString::CK_TypedText)
965       AppendToString(Result, C->Text, Buffer);
966   }
967 
968   return Result;
969 }
970 
971 namespace {
972   struct OrderCompletionResults {
973     bool operator()(const CXCompletionResult &XR,
974                     const CXCompletionResult &YR) const {
975       CodeCompletionString *X
976         = (CodeCompletionString *)XR.CompletionString;
977       CodeCompletionString *Y
978         = (CodeCompletionString *)YR.CompletionString;
979 
980       SmallString<256> XBuffer;
981       StringRef XText = GetTypedName(X, XBuffer);
982       SmallString<256> YBuffer;
983       StringRef YText = GetTypedName(Y, YBuffer);
984 
985       if (XText.empty() || YText.empty())
986         return !XText.empty();
987 
988       int result = XText.compare_lower(YText);
989       if (result < 0)
990         return true;
991       if (result > 0)
992         return false;
993 
994       result = XText.compare(YText);
995       return result < 0;
996     }
997   };
998 }
999 
1000 extern "C" {
1001   void clang_sortCodeCompletionResults(CXCompletionResult *Results,
1002                                        unsigned NumResults) {
1003     std::stable_sort(Results, Results + NumResults, OrderCompletionResults());
1004   }
1005 }
1006