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