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