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