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