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