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