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