CIndexCodeCompletion.cpp revision 48601b32321496b07a18fb6631f8563275d8c5fb
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 "CIndexDiagnostic.h"
19#include "clang/Basic/SourceManager.h"
20#include "clang/Basic/FileManager.h"
21#include "clang/Frontend/ASTUnit.h"
22#include "clang/Frontend/CompilerInstance.h"
23#include "clang/Frontend/FrontendDiagnostic.h"
24#include "clang/Sema/CodeCompleteConsumer.h"
25#include "llvm/ADT/SmallString.h"
26#include "llvm/ADT/StringExtras.h"
27#include "llvm/Support/Atomic.h"
28#include "llvm/Support/CrashRecoveryContext.h"
29#include "llvm/Support/MemoryBuffer.h"
30#include "llvm/Support/Timer.h"
31#include "llvm/Support/raw_ostream.h"
32#include "llvm/Support/Program.h"
33#include <cstdlib>
34#include <cstdio>
35
36
37#ifdef UDP_CODE_COMPLETION_LOGGER
38#include "clang/Basic/Version.h"
39#include <arpa/inet.h>
40#include <sys/socket.h>
41#include <sys/types.h>
42#include <unistd.h>
43#endif
44
45using namespace clang;
46using namespace clang::cxstring;
47
48extern "C" {
49
50enum CXCompletionChunkKind
51clang_getCompletionChunkKind(CXCompletionString completion_string,
52                             unsigned chunk_number) {
53  CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
54  if (!CCStr || chunk_number >= CCStr->size())
55    return CXCompletionChunk_Text;
56
57  switch ((*CCStr)[chunk_number].Kind) {
58  case CodeCompletionString::CK_TypedText:
59    return CXCompletionChunk_TypedText;
60  case CodeCompletionString::CK_Text:
61    return CXCompletionChunk_Text;
62  case CodeCompletionString::CK_Optional:
63    return CXCompletionChunk_Optional;
64  case CodeCompletionString::CK_Placeholder:
65    return CXCompletionChunk_Placeholder;
66  case CodeCompletionString::CK_Informative:
67    return CXCompletionChunk_Informative;
68  case CodeCompletionString::CK_ResultType:
69    return CXCompletionChunk_ResultType;
70  case CodeCompletionString::CK_CurrentParameter:
71    return CXCompletionChunk_CurrentParameter;
72  case CodeCompletionString::CK_LeftParen:
73    return CXCompletionChunk_LeftParen;
74  case CodeCompletionString::CK_RightParen:
75    return CXCompletionChunk_RightParen;
76  case CodeCompletionString::CK_LeftBracket:
77    return CXCompletionChunk_LeftBracket;
78  case CodeCompletionString::CK_RightBracket:
79    return CXCompletionChunk_RightBracket;
80  case CodeCompletionString::CK_LeftBrace:
81    return CXCompletionChunk_LeftBrace;
82  case CodeCompletionString::CK_RightBrace:
83    return CXCompletionChunk_RightBrace;
84  case CodeCompletionString::CK_LeftAngle:
85    return CXCompletionChunk_LeftAngle;
86  case CodeCompletionString::CK_RightAngle:
87    return CXCompletionChunk_RightAngle;
88  case CodeCompletionString::CK_Comma:
89    return CXCompletionChunk_Comma;
90  case CodeCompletionString::CK_Colon:
91    return CXCompletionChunk_Colon;
92  case CodeCompletionString::CK_SemiColon:
93    return CXCompletionChunk_SemiColon;
94  case CodeCompletionString::CK_Equal:
95    return CXCompletionChunk_Equal;
96  case CodeCompletionString::CK_HorizontalSpace:
97    return CXCompletionChunk_HorizontalSpace;
98  case CodeCompletionString::CK_VerticalSpace:
99    return CXCompletionChunk_VerticalSpace;
100  }
101
102  // Should be unreachable, but let's be careful.
103  return CXCompletionChunk_Text;
104}
105
106CXString clang_getCompletionChunkText(CXCompletionString completion_string,
107                                      unsigned chunk_number) {
108  CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
109  if (!CCStr || chunk_number >= CCStr->size())
110    return createCXString((const char*)0);
111
112  switch ((*CCStr)[chunk_number].Kind) {
113  case CodeCompletionString::CK_TypedText:
114  case CodeCompletionString::CK_Text:
115  case CodeCompletionString::CK_Placeholder:
116  case CodeCompletionString::CK_CurrentParameter:
117  case CodeCompletionString::CK_Informative:
118  case CodeCompletionString::CK_LeftParen:
119  case CodeCompletionString::CK_RightParen:
120  case CodeCompletionString::CK_LeftBracket:
121  case CodeCompletionString::CK_RightBracket:
122  case CodeCompletionString::CK_LeftBrace:
123  case CodeCompletionString::CK_RightBrace:
124  case CodeCompletionString::CK_LeftAngle:
125  case CodeCompletionString::CK_RightAngle:
126  case CodeCompletionString::CK_Comma:
127  case CodeCompletionString::CK_ResultType:
128  case CodeCompletionString::CK_Colon:
129  case CodeCompletionString::CK_SemiColon:
130  case CodeCompletionString::CK_Equal:
131  case CodeCompletionString::CK_HorizontalSpace:
132  case CodeCompletionString::CK_VerticalSpace:
133    return createCXString((*CCStr)[chunk_number].Text, false);
134
135  case CodeCompletionString::CK_Optional:
136    // Note: treated as an empty text block.
137    return createCXString("");
138  }
139
140  // Should be unreachable, but let's be careful.
141  return createCXString((const char*)0);
142}
143
144
145CXCompletionString
146clang_getCompletionChunkCompletionString(CXCompletionString completion_string,
147                                         unsigned chunk_number) {
148  CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
149  if (!CCStr || chunk_number >= CCStr->size())
150    return 0;
151
152  switch ((*CCStr)[chunk_number].Kind) {
153  case CodeCompletionString::CK_TypedText:
154  case CodeCompletionString::CK_Text:
155  case CodeCompletionString::CK_Placeholder:
156  case CodeCompletionString::CK_CurrentParameter:
157  case CodeCompletionString::CK_Informative:
158  case CodeCompletionString::CK_LeftParen:
159  case CodeCompletionString::CK_RightParen:
160  case CodeCompletionString::CK_LeftBracket:
161  case CodeCompletionString::CK_RightBracket:
162  case CodeCompletionString::CK_LeftBrace:
163  case CodeCompletionString::CK_RightBrace:
164  case CodeCompletionString::CK_LeftAngle:
165  case CodeCompletionString::CK_RightAngle:
166  case CodeCompletionString::CK_Comma:
167  case CodeCompletionString::CK_ResultType:
168  case CodeCompletionString::CK_Colon:
169  case CodeCompletionString::CK_SemiColon:
170  case CodeCompletionString::CK_Equal:
171  case CodeCompletionString::CK_HorizontalSpace:
172  case CodeCompletionString::CK_VerticalSpace:
173    return 0;
174
175  case CodeCompletionString::CK_Optional:
176    // Note: treated as an empty text block.
177    return (*CCStr)[chunk_number].Optional;
178  }
179
180  // Should be unreachable, but let's be careful.
181  return 0;
182}
183
184unsigned clang_getNumCompletionChunks(CXCompletionString completion_string) {
185  CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
186  return CCStr? CCStr->size() : 0;
187}
188
189unsigned clang_getCompletionPriority(CXCompletionString completion_string) {
190  CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
191  return CCStr? CCStr->getPriority() : unsigned(CCP_Unlikely);
192}
193
194enum CXAvailabilityKind
195clang_getCompletionAvailability(CXCompletionString completion_string) {
196  CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
197  return CCStr? static_cast<CXAvailabilityKind>(CCStr->getAvailability())
198              : CXAvailability_Available;
199}
200
201/// \brief The CXCodeCompleteResults structure we allocate internally;
202/// the client only sees the initial CXCodeCompleteResults structure.
203struct AllocatedCXCodeCompleteResults : public CXCodeCompleteResults {
204  AllocatedCXCodeCompleteResults();
205  ~AllocatedCXCodeCompleteResults();
206
207  /// \brief Diagnostics produced while performing code completion.
208  llvm::SmallVector<StoredDiagnostic, 8> Diagnostics;
209
210  /// \brief Diag object
211  llvm::IntrusiveRefCntPtr<Diagnostic> Diag;
212
213  /// \brief Language options used to adjust source locations.
214  LangOptions LangOpts;
215
216  FileSystemOptions FileSystemOpts;
217
218  /// \brief File manager, used for diagnostics.
219  FileManager FileMgr;
220
221  /// \brief Source manager, used for diagnostics.
222  SourceManager SourceMgr;
223
224  /// \brief Temporary files that should be removed once we have finished
225  /// with the code-completion results.
226  std::vector<llvm::sys::Path> TemporaryFiles;
227
228  /// \brief Temporary buffers that will be deleted once we have finished with
229  /// the code-completion results.
230  llvm::SmallVector<const llvm::MemoryBuffer *, 1> TemporaryBuffers;
231
232  /// \brief Allocator used to store globally cached code-completion results.
233  llvm::IntrusiveRefCntPtr<clang::GlobalCodeCompletionAllocator>
234    CachedCompletionAllocator;
235
236  /// \brief Allocator used to store code completion results.
237  clang::CodeCompletionAllocator CodeCompletionAllocator;
238};
239
240/// \brief Tracks the number of code-completion result objects that are
241/// currently active.
242///
243/// Used for debugging purposes only.
244static llvm::sys::cas_flag CodeCompletionResultObjects;
245
246AllocatedCXCodeCompleteResults::AllocatedCXCodeCompleteResults()
247  : CXCodeCompleteResults(),
248    Diag(new Diagnostic(
249                   llvm::IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs))),
250    FileMgr(FileSystemOpts),
251    SourceMgr(*Diag, FileMgr) {
252  if (getenv("LIBCLANG_OBJTRACKING")) {
253    llvm::sys::AtomicIncrement(&CodeCompletionResultObjects);
254    fprintf(stderr, "+++ %d completion results\n", CodeCompletionResultObjects);
255  }
256}
257
258AllocatedCXCodeCompleteResults::~AllocatedCXCodeCompleteResults() {
259  delete [] Results;
260
261  for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I)
262    TemporaryFiles[I].eraseFromDisk();
263  for (unsigned I = 0, N = TemporaryBuffers.size(); I != N; ++I)
264    delete TemporaryBuffers[I];
265
266  if (getenv("LIBCLANG_OBJTRACKING")) {
267    llvm::sys::AtomicDecrement(&CodeCompletionResultObjects);
268    fprintf(stderr, "--- %d completion results\n", CodeCompletionResultObjects);
269  }
270}
271
272} // end extern "C"
273
274namespace {
275  class CaptureCompletionResults : public CodeCompleteConsumer {
276    AllocatedCXCodeCompleteResults &AllocatedResults;
277    llvm::SmallVector<CXCompletionResult, 16> StoredResults;
278  public:
279    CaptureCompletionResults(AllocatedCXCodeCompleteResults &Results)
280      : CodeCompleteConsumer(true, false, true, false),
281        AllocatedResults(Results) { }
282    ~CaptureCompletionResults() { Finish(); }
283
284    virtual void ProcessCodeCompleteResults(Sema &S,
285                                            CodeCompletionContext Context,
286                                            CodeCompletionResult *Results,
287                                            unsigned NumResults) {
288      StoredResults.reserve(StoredResults.size() + NumResults);
289      for (unsigned I = 0; I != NumResults; ++I) {
290        CodeCompletionString *StoredCompletion
291          = Results[I].CreateCodeCompletionString(S,
292                                      AllocatedResults.CodeCompletionAllocator);
293
294        CXCompletionResult R;
295        R.CursorKind = Results[I].CursorKind;
296        R.CompletionString = StoredCompletion;
297        StoredResults.push_back(R);
298      }
299    }
300
301    virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
302                                           OverloadCandidate *Candidates,
303                                           unsigned NumCandidates) {
304      StoredResults.reserve(StoredResults.size() + NumCandidates);
305      for (unsigned I = 0; I != NumCandidates; ++I) {
306        CodeCompletionString *StoredCompletion
307          = Candidates[I].CreateSignatureString(CurrentArg, S,
308                                      AllocatedResults.CodeCompletionAllocator);
309
310        CXCompletionResult R;
311        R.CursorKind = CXCursor_NotImplemented;
312        R.CompletionString = StoredCompletion;
313        StoredResults.push_back(R);
314      }
315    }
316
317    virtual CodeCompletionAllocator &getAllocator() {
318      return AllocatedResults.CodeCompletionAllocator;
319    }
320
321  private:
322    void Finish() {
323      AllocatedResults.Results = new CXCompletionResult [StoredResults.size()];
324      AllocatedResults.NumResults = StoredResults.size();
325      std::memcpy(AllocatedResults.Results, StoredResults.data(),
326                  StoredResults.size() * sizeof(CXCompletionResult));
327      StoredResults.clear();
328    }
329  };
330}
331
332extern "C" {
333struct CodeCompleteAtInfo {
334  CXTranslationUnit TU;
335  const char *complete_filename;
336  unsigned complete_line;
337  unsigned complete_column;
338  struct CXUnsavedFile *unsaved_files;
339  unsigned num_unsaved_files;
340  unsigned options;
341  CXCodeCompleteResults *result;
342};
343void clang_codeCompleteAt_Impl(void *UserData) {
344  CodeCompleteAtInfo *CCAI = static_cast<CodeCompleteAtInfo*>(UserData);
345  CXTranslationUnit TU = CCAI->TU;
346  const char *complete_filename = CCAI->complete_filename;
347  unsigned complete_line = CCAI->complete_line;
348  unsigned complete_column = CCAI->complete_column;
349  struct CXUnsavedFile *unsaved_files = CCAI->unsaved_files;
350  unsigned num_unsaved_files = CCAI->num_unsaved_files;
351  unsigned options = CCAI->options;
352  CCAI->result = 0;
353
354#ifdef UDP_CODE_COMPLETION_LOGGER
355#ifdef UDP_CODE_COMPLETION_LOGGER_PORT
356  const llvm::TimeRecord &StartTime =  llvm::TimeRecord::getCurrentTime();
357#endif
358#endif
359
360  bool EnableLogging = getenv("LIBCLANG_CODE_COMPLETION_LOGGING") != 0;
361
362  ASTUnit *AST = static_cast<ASTUnit *>(TU->TUData);
363  if (!AST)
364    return;
365
366  ASTUnit::ConcurrencyCheck Check(*AST);
367
368  // Perform the remapping of source files.
369  llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles;
370  for (unsigned I = 0; I != num_unsaved_files; ++I) {
371    llvm::StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length);
372    const llvm::MemoryBuffer *Buffer
373      = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename);
374    RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename,
375                                           Buffer));
376  }
377
378  if (EnableLogging) {
379    // FIXME: Add logging.
380  }
381
382  // Parse the resulting source file to find code-completion results.
383  AllocatedCXCodeCompleteResults *Results = new AllocatedCXCodeCompleteResults;
384  Results->Results = 0;
385  Results->NumResults = 0;
386
387  // Create a code-completion consumer to capture the results.
388  CaptureCompletionResults Capture(*Results);
389
390  // Perform completion.
391  AST->CodeComplete(complete_filename, complete_line, complete_column,
392                    RemappedFiles.data(), RemappedFiles.size(),
393                    (options & CXCodeComplete_IncludeMacros),
394                    (options & CXCodeComplete_IncludeCodePatterns),
395                    Capture,
396                    *Results->Diag, Results->LangOpts, Results->SourceMgr,
397                    Results->FileMgr, Results->Diagnostics,
398                    Results->TemporaryBuffers);
399
400  // Keep a reference to the allocator used for cached global completions, so
401  // that we can be sure that the memory used by our code completion strings
402  // doesn't get freed due to subsequent reparses (while the code completion
403  // results are still active).
404  Results->CachedCompletionAllocator = AST->getCachedCompletionAllocator();
405
406
407
408#ifdef UDP_CODE_COMPLETION_LOGGER
409#ifdef UDP_CODE_COMPLETION_LOGGER_PORT
410  const llvm::TimeRecord &EndTime =  llvm::TimeRecord::getCurrentTime();
411  llvm::SmallString<256> LogResult;
412  llvm::raw_svector_ostream os(LogResult);
413
414  // Figure out the language and whether or not it uses PCH.
415  const char *lang = 0;
416  bool usesPCH = false;
417
418  for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
419       I != E; ++I) {
420    if (*I == 0)
421      continue;
422    if (strcmp(*I, "-x") == 0) {
423      if (I + 1 != E) {
424        lang = *(++I);
425        continue;
426      }
427    }
428    else if (strcmp(*I, "-include") == 0) {
429      if (I+1 != E) {
430        const char *arg = *(++I);
431        llvm::SmallString<512> pchName;
432        {
433          llvm::raw_svector_ostream os(pchName);
434          os << arg << ".pth";
435        }
436        pchName.push_back('\0');
437        struct stat stat_results;
438        if (stat(pchName.data(), &stat_results) == 0)
439          usesPCH = true;
440        continue;
441      }
442    }
443  }
444
445  os << "{ ";
446  os << "\"wall\": " << (EndTime.getWallTime() - StartTime.getWallTime());
447  os << ", \"numRes\": " << Results->NumResults;
448  os << ", \"diags\": " << Results->Diagnostics.size();
449  os << ", \"pch\": " << (usesPCH ? "true" : "false");
450  os << ", \"lang\": \"" << (lang ? lang : "<unknown>") << '"';
451  const char *name = getlogin();
452  os << ", \"user\": \"" << (name ? name : "unknown") << '"';
453  os << ", \"clangVer\": \"" << getClangFullVersion() << '"';
454  os << " }";
455
456  llvm::StringRef res = os.str();
457  if (res.size() > 0) {
458    do {
459      // Setup the UDP socket.
460      struct sockaddr_in servaddr;
461      bzero(&servaddr, sizeof(servaddr));
462      servaddr.sin_family = AF_INET;
463      servaddr.sin_port = htons(UDP_CODE_COMPLETION_LOGGER_PORT);
464      if (inet_pton(AF_INET, UDP_CODE_COMPLETION_LOGGER,
465                    &servaddr.sin_addr) <= 0)
466        break;
467
468      int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
469      if (sockfd < 0)
470        break;
471
472      sendto(sockfd, res.data(), res.size(), 0,
473             (struct sockaddr *)&servaddr, sizeof(servaddr));
474      close(sockfd);
475    }
476    while (false);
477  }
478#endif
479#endif
480  CCAI->result = Results;
481}
482CXCodeCompleteResults *clang_codeCompleteAt(CXTranslationUnit TU,
483                                            const char *complete_filename,
484                                            unsigned complete_line,
485                                            unsigned complete_column,
486                                            struct CXUnsavedFile *unsaved_files,
487                                            unsigned num_unsaved_files,
488                                            unsigned options) {
489  CodeCompleteAtInfo CCAI = { TU, complete_filename, complete_line,
490                              complete_column, unsaved_files, num_unsaved_files,
491                              options, 0 };
492  llvm::CrashRecoveryContext CRC;
493
494  if (!RunSafely(CRC, clang_codeCompleteAt_Impl, &CCAI)) {
495    fprintf(stderr, "libclang: crash detected in code completion\n");
496    static_cast<ASTUnit *>(TU->TUData)->setUnsafeToFree(true);
497    return 0;
498  }
499
500  return CCAI.result;
501}
502
503unsigned clang_defaultCodeCompleteOptions(void) {
504  return CXCodeComplete_IncludeMacros;
505}
506
507void clang_disposeCodeCompleteResults(CXCodeCompleteResults *ResultsIn) {
508  if (!ResultsIn)
509    return;
510
511  AllocatedCXCodeCompleteResults *Results
512    = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn);
513  delete Results;
514}
515
516unsigned
517clang_codeCompleteGetNumDiagnostics(CXCodeCompleteResults *ResultsIn) {
518  AllocatedCXCodeCompleteResults *Results
519    = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn);
520  if (!Results)
521    return 0;
522
523  return Results->Diagnostics.size();
524}
525
526CXDiagnostic
527clang_codeCompleteGetDiagnostic(CXCodeCompleteResults *ResultsIn,
528                                unsigned Index) {
529  AllocatedCXCodeCompleteResults *Results
530    = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn);
531  if (!Results || Index >= Results->Diagnostics.size())
532    return 0;
533
534  return new CXStoredDiagnostic(Results->Diagnostics[Index], Results->LangOpts);
535}
536
537
538} // end extern "C"
539
540/// \brief Simple utility function that appends a \p New string to the given
541/// \p Old string, using the \p Buffer for storage.
542///
543/// \param Old The string to which we are appending. This parameter will be
544/// updated to reflect the complete string.
545///
546///
547/// \param New The string to append to \p Old.
548///
549/// \param Buffer A buffer that stores the actual, concatenated string. It will
550/// be used if the old string is already-non-empty.
551static void AppendToString(llvm::StringRef &Old, llvm::StringRef New,
552                           llvm::SmallString<256> &Buffer) {
553  if (Old.empty()) {
554    Old = New;
555    return;
556  }
557
558  if (Buffer.empty())
559    Buffer.append(Old.begin(), Old.end());
560  Buffer.append(New.begin(), New.end());
561  Old = Buffer.str();
562}
563
564/// \brief Get the typed-text blocks from the given code-completion string
565/// and return them as a single string.
566///
567/// \param String The code-completion string whose typed-text blocks will be
568/// concatenated.
569///
570/// \param Buffer A buffer used for storage of the completed name.
571static llvm::StringRef GetTypedName(CodeCompletionString *String,
572                                    llvm::SmallString<256> &Buffer) {
573  llvm::StringRef Result;
574  for (CodeCompletionString::iterator C = String->begin(), CEnd = String->end();
575       C != CEnd; ++C) {
576    if (C->Kind == CodeCompletionString::CK_TypedText)
577      AppendToString(Result, C->Text, Buffer);
578  }
579
580  return Result;
581}
582
583namespace {
584  struct OrderCompletionResults {
585    bool operator()(const CXCompletionResult &XR,
586                    const CXCompletionResult &YR) const {
587      CodeCompletionString *X
588        = (CodeCompletionString *)XR.CompletionString;
589      CodeCompletionString *Y
590        = (CodeCompletionString *)YR.CompletionString;
591
592      llvm::SmallString<256> XBuffer;
593      llvm::StringRef XText = GetTypedName(X, XBuffer);
594      llvm::SmallString<256> YBuffer;
595      llvm::StringRef YText = GetTypedName(Y, YBuffer);
596
597      if (XText.empty() || YText.empty())
598        return !XText.empty();
599
600      int result = XText.compare_lower(YText);
601      if (result < 0)
602        return true;
603      if (result > 0)
604        return false;
605
606      result = XText.compare(YText);
607      return result < 0;
608    }
609  };
610}
611
612extern "C" {
613  void clang_sortCodeCompletionResults(CXCompletionResult *Results,
614                                       unsigned NumResults) {
615    std::stable_sort(Results, Results + NumResults, OrderCompletionResults());
616  }
617}
618