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