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