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
53using namespace clang;
54using namespace clang::cxindex;
55
56extern "C" {
57
58enum CXCompletionChunkKind
59clang_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
113CXString 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
151CXCompletionString
152clang_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
189unsigned clang_getNumCompletionChunks(CXCompletionString completion_string) {
190  CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
191  return CCStr? CCStr->size() : 0;
192}
193
194unsigned clang_getCompletionPriority(CXCompletionString completion_string) {
195  CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
196  return CCStr? CCStr->getPriority() : unsigned(CCP_Unlikely);
197}
198
199enum CXAvailabilityKind
200clang_getCompletionAvailability(CXCompletionString completion_string) {
201  CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
202  return CCStr? static_cast<CXAvailabilityKind>(CCStr->getAvailability())
203              : CXAvailability_Available;
204}
205
206unsigned clang_getCompletionNumAnnotations(CXCompletionString completion_string)
207{
208  CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
209  return CCStr ? CCStr->getAnnotationCount() : 0;
210}
211
212CXString 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
219CXString
220clang_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
232CXString
233clang_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
242namespace {
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.
251struct 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.
319static std::atomic<unsigned> CodeCompletionResultObjects;
320
321AllocatedCXCodeCompleteResults::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
336AllocatedCXCodeCompleteResults::~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
352static 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
523namespace {
524  class CaptureCompletionResults : public CodeCompleteConsumer {
525    AllocatedCXCodeCompleteResults &AllocatedResults;
526    CodeCompletionTUInfo CCTUInfo;
527    SmallVector<CXCompletionResult, 16> StoredResults;
528    CXTranslationUnit *TU;
529  public:
530    CaptureCompletionResults(const CodeCompleteOptions &Opts,
531                             AllocatedCXCodeCompleteResults &Results,
532                             CXTranslationUnit *TranslationUnit)
533      : CodeCompleteConsumer(Opts, false),
534        AllocatedResults(Results), CCTUInfo(Results.CodeCompletionAllocator),
535        TU(TranslationUnit) { }
536    ~CaptureCompletionResults() override { Finish(); }
537
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, 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
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
632    CodeCompletionAllocator &getAllocator() override {
633      return *AllocatedResults.CodeCompletionAllocator;
634    }
635
636    CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo;}
637
638  private:
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
649extern "C" {
650struct CodeCompleteAtInfo {
651  CXTranslationUnit TU;
652  const char *complete_filename;
653  unsigned complete_line;
654  unsigned complete_column;
655  ArrayRef<CXUnsavedFile> unsaved_files;
656  unsigned options;
657  CXCodeCompleteResults *result;
658};
659static void clang_codeCompleteAt_Impl(void *UserData) {
660  CodeCompleteAtInfo *CCAI = static_cast<CodeCompleteAtInfo*>(UserData);
661  CXTranslationUnit TU = CCAI->TU;
662  const char *complete_filename = CCAI->complete_filename;
663  unsigned complete_line = CCAI->complete_line;
664  unsigned complete_column = CCAI->complete_column;
665  unsigned options = CCAI->options;
666  bool IncludeBriefComments = options & CXCodeComplete_IncludeBriefComments;
667  CCAI->result = nullptr;
668
669#ifdef UDP_CODE_COMPLETION_LOGGER
670#ifdef UDP_CODE_COMPLETION_LOGGER_PORT
671  const llvm::TimeRecord &StartTime =  llvm::TimeRecord::getCurrentTime();
672#endif
673#endif
674
675  bool EnableLogging = getenv("LIBCLANG_CODE_COMPLETION_LOGGING") != nullptr;
676
677  if (cxtu::isNotUsableTU(TU)) {
678    LOG_BAD_TU(TU);
679    return;
680  }
681
682  ASTUnit *AST = cxtu::getASTUnit(TU);
683  if (!AST)
684    return;
685
686  CIndexer *CXXIdx = TU->CIdx;
687  if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForEditing))
688    setThreadBackgroundPriority();
689
690  ASTUnit::ConcurrencyCheck Check(*AST);
691
692  // Perform the remapping of source files.
693  SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles;
694
695  for (auto &UF : CCAI->unsaved_files) {
696    std::unique_ptr<llvm::MemoryBuffer> MB =
697        llvm::MemoryBuffer::getMemBufferCopy(getContents(UF), UF.Filename);
698    RemappedFiles.push_back(std::make_pair(UF.Filename, MB.release()));
699  }
700
701  if (EnableLogging) {
702    // FIXME: Add logging.
703  }
704
705  // Parse the resulting source file to find code-completion results.
706  AllocatedCXCodeCompleteResults *Results = new AllocatedCXCodeCompleteResults(
707      &AST->getFileManager());
708  Results->Results = nullptr;
709  Results->NumResults = 0;
710
711  // Create a code-completion consumer to capture the results.
712  CodeCompleteOptions Opts;
713  Opts.IncludeBriefComments = IncludeBriefComments;
714  CaptureCompletionResults Capture(Opts, *Results, &TU);
715
716  // Perform completion.
717  AST->CodeComplete(complete_filename, complete_line, complete_column,
718                    RemappedFiles,
719                    (options & CXCodeComplete_IncludeMacros),
720                    (options & CXCodeComplete_IncludeCodePatterns),
721                    IncludeBriefComments,
722                    Capture,
723                    *Results->Diag, Results->LangOpts, *Results->SourceMgr,
724                    *Results->FileMgr, Results->Diagnostics,
725                    Results->TemporaryBuffers);
726
727  Results->DiagnosticsWrappers.resize(Results->Diagnostics.size());
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}
811CXCodeCompleteResults *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  LOG_FUNC_SECTION {
819    *Log << TU << ' '
820         << complete_filename << ':' << complete_line << ':' << complete_column;
821  }
822
823  if (num_unsaved_files && !unsaved_files)
824    return nullptr;
825
826  CodeCompleteAtInfo CCAI = {TU, complete_filename, complete_line,
827    complete_column, llvm::makeArrayRef(unsaved_files, num_unsaved_files),
828    options, nullptr};
829
830  if (getenv("LIBCLANG_NOTHREADS")) {
831    clang_codeCompleteAt_Impl(&CCAI);
832    return CCAI.result;
833  }
834
835  llvm::CrashRecoveryContext CRC;
836
837  if (!RunSafely(CRC, clang_codeCompleteAt_Impl, &CCAI)) {
838    fprintf(stderr, "libclang: crash detected in code completion\n");
839    cxtu::getASTUnit(TU)->setUnsafeToFree(true);
840    return nullptr;
841  } else if (getenv("LIBCLANG_RESOURCE_USAGE"))
842    PrintLibclangResourceUsage(TU);
843
844  return CCAI.result;
845}
846
847unsigned clang_defaultCodeCompleteOptions(void) {
848  return CXCodeComplete_IncludeMacros;
849}
850
851void clang_disposeCodeCompleteResults(CXCodeCompleteResults *ResultsIn) {
852  if (!ResultsIn)
853    return;
854
855  AllocatedCXCodeCompleteResults *Results
856    = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn);
857  delete Results;
858}
859
860unsigned
861clang_codeCompleteGetNumDiagnostics(CXCodeCompleteResults *ResultsIn) {
862  AllocatedCXCodeCompleteResults *Results
863    = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn);
864  if (!Results)
865    return 0;
866
867  return Results->Diagnostics.size();
868}
869
870CXDiagnostic
871clang_codeCompleteGetDiagnostic(CXCodeCompleteResults *ResultsIn,
872                                unsigned Index) {
873  AllocatedCXCodeCompleteResults *Results
874    = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn);
875  if (!Results || Index >= Results->Diagnostics.size())
876    return nullptr;
877
878  CXStoredDiagnostic *Diag = Results->DiagnosticsWrappers[Index];
879  if (!Diag)
880    Results->DiagnosticsWrappers[Index] = Diag =
881        new CXStoredDiagnostic(Results->Diagnostics[Index], Results->LangOpts);
882  return Diag;
883}
884
885unsigned long long
886clang_codeCompleteGetContexts(CXCodeCompleteResults *ResultsIn) {
887  AllocatedCXCodeCompleteResults *Results
888    = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn);
889  if (!Results)
890    return 0;
891
892  return Results->Contexts;
893}
894
895enum CXCursorKind clang_codeCompleteGetContainerKind(
896                                               CXCodeCompleteResults *ResultsIn,
897                                                     unsigned *IsIncomplete) {
898  AllocatedCXCodeCompleteResults *Results =
899    static_cast<AllocatedCXCodeCompleteResults *>(ResultsIn);
900  if (!Results)
901    return CXCursor_InvalidCode;
902
903  if (IsIncomplete != nullptr) {
904    *IsIncomplete = Results->ContainerIsIncomplete;
905  }
906
907  return Results->ContainerKind;
908}
909
910CXString clang_codeCompleteGetContainerUSR(CXCodeCompleteResults *ResultsIn) {
911  AllocatedCXCodeCompleteResults *Results =
912    static_cast<AllocatedCXCodeCompleteResults *>(ResultsIn);
913  if (!Results)
914    return cxstring::createEmpty();
915
916  return cxstring::createRef(Results->ContainerUSR.c_str());
917}
918
919
920CXString clang_codeCompleteGetObjCSelector(CXCodeCompleteResults *ResultsIn) {
921  AllocatedCXCodeCompleteResults *Results =
922    static_cast<AllocatedCXCodeCompleteResults *>(ResultsIn);
923  if (!Results)
924    return cxstring::createEmpty();
925
926  return cxstring::createDup(Results->Selector);
927}
928
929} // end extern "C"
930
931/// \brief Simple utility function that appends a \p New string to the given
932/// \p Old string, using the \p Buffer for storage.
933///
934/// \param Old The string to which we are appending. This parameter will be
935/// updated to reflect the complete string.
936///
937///
938/// \param New The string to append to \p Old.
939///
940/// \param Buffer A buffer that stores the actual, concatenated string. It will
941/// be used if the old string is already-non-empty.
942static void AppendToString(StringRef &Old, StringRef New,
943                           SmallString<256> &Buffer) {
944  if (Old.empty()) {
945    Old = New;
946    return;
947  }
948
949  if (Buffer.empty())
950    Buffer.append(Old.begin(), Old.end());
951  Buffer.append(New.begin(), New.end());
952  Old = Buffer.str();
953}
954
955/// \brief Get the typed-text blocks from the given code-completion string
956/// and return them as a single string.
957///
958/// \param String The code-completion string whose typed-text blocks will be
959/// concatenated.
960///
961/// \param Buffer A buffer used for storage of the completed name.
962static StringRef GetTypedName(CodeCompletionString *String,
963                                    SmallString<256> &Buffer) {
964  StringRef Result;
965  for (CodeCompletionString::iterator C = String->begin(), CEnd = String->end();
966       C != CEnd; ++C) {
967    if (C->Kind == CodeCompletionString::CK_TypedText)
968      AppendToString(Result, C->Text, Buffer);
969  }
970
971  return Result;
972}
973
974namespace {
975  struct OrderCompletionResults {
976    bool operator()(const CXCompletionResult &XR,
977                    const CXCompletionResult &YR) const {
978      CodeCompletionString *X
979        = (CodeCompletionString *)XR.CompletionString;
980      CodeCompletionString *Y
981        = (CodeCompletionString *)YR.CompletionString;
982
983      SmallString<256> XBuffer;
984      StringRef XText = GetTypedName(X, XBuffer);
985      SmallString<256> YBuffer;
986      StringRef YText = GetTypedName(Y, YBuffer);
987
988      if (XText.empty() || YText.empty())
989        return !XText.empty();
990
991      int result = XText.compare_lower(YText);
992      if (result < 0)
993        return true;
994      if (result > 0)
995        return false;
996
997      result = XText.compare(YText);
998      return result < 0;
999    }
1000  };
1001}
1002
1003extern "C" {
1004  void clang_sortCodeCompletionResults(CXCompletionResult *Results,
1005                                       unsigned NumResults) {
1006    std::stable_sort(Results, Results + NumResults, OrderCompletionResults());
1007  }
1008}
1009