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