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