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