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