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