blob: 215929898909357d0547932f053e7fa91abd97d4 [file] [log] [blame]
Ted Kremenekab188932010-01-05 19:32:54 +00001//===- 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"
Ted Kremenek0a90d322010-11-17 23:24:11 +000016#include "CXTranslationUnit.h"
Ted Kremeneked122732010-11-16 01:56:27 +000017#include "CXString.h"
Douglas Gregore081a612011-07-21 01:05:26 +000018#include "CXCursor.h"
Ted Kremenekbaf82b02011-08-17 22:19:53 +000019#include "CXString.h"
Douglas Gregor936ea3b2010-01-28 00:56:43 +000020#include "CIndexDiagnostic.h"
Douglas Gregore081a612011-07-21 01:05:26 +000021#include "clang/AST/Type.h"
22#include "clang/AST/Decl.h"
23#include "clang/AST/DeclObjC.h"
Benjamin Kramerb846deb2010-04-12 19:45:50 +000024#include "clang/Basic/SourceManager.h"
25#include "clang/Basic/FileManager.h"
Douglas Gregor1abc6bc2010-08-04 16:47:14 +000026#include "clang/Frontend/ASTUnit.h"
Benjamin Kramerb846deb2010-04-12 19:45:50 +000027#include "clang/Frontend/CompilerInstance.h"
Douglas Gregor936ea3b2010-01-28 00:56:43 +000028#include "clang/Frontend/FrontendDiagnostic.h"
Ted Kremenekab188932010-01-05 19:32:54 +000029#include "clang/Sema/CodeCompleteConsumer.h"
Douglas Gregordf95a132010-08-09 20:45:32 +000030#include "llvm/ADT/SmallString.h"
Ted Kremenekab188932010-01-05 19:32:54 +000031#include "llvm/ADT/StringExtras.h"
Douglas Gregor1fd9e0d2010-12-07 00:05:48 +000032#include "llvm/Support/Atomic.h"
Daniel Dunbarb1fd3452010-08-19 23:44:10 +000033#include "llvm/Support/CrashRecoveryContext.h"
Ted Kremenekab188932010-01-05 19:32:54 +000034#include "llvm/Support/MemoryBuffer.h"
Douglas Gregordf95a132010-08-09 20:45:32 +000035#include "llvm/Support/Timer.h"
36#include "llvm/Support/raw_ostream.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000037#include "llvm/Support/Program.h"
Douglas Gregor3d398aa2010-07-26 16:29:14 +000038#include <cstdlib>
39#include <cstdio>
Ted Kremenekab188932010-01-05 19:32:54 +000040
Douglas Gregordf95a132010-08-09 20:45:32 +000041
Ted Kremenekda7af322010-04-15 01:02:28 +000042#ifdef UDP_CODE_COMPLETION_LOGGER
43#include "clang/Basic/Version.h"
Ted Kremenekda7af322010-04-15 01:02:28 +000044#include <arpa/inet.h>
45#include <sys/socket.h>
46#include <sys/types.h>
47#include <unistd.h>
48#endif
49
Ted Kremenekab188932010-01-05 19:32:54 +000050using namespace clang;
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +000051using namespace clang::cxstring;
Ted Kremenekab188932010-01-05 19:32:54 +000052
53extern "C" {
54
55enum CXCompletionChunkKind
56clang_getCompletionChunkKind(CXCompletionString completion_string,
57 unsigned chunk_number) {
Douglas Gregor218937c2011-02-01 19:23:04 +000058 CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
Ted Kremenekab188932010-01-05 19:32:54 +000059 if (!CCStr || chunk_number >= CCStr->size())
60 return CXCompletionChunk_Text;
61
62 switch ((*CCStr)[chunk_number].Kind) {
63 case CodeCompletionString::CK_TypedText:
64 return CXCompletionChunk_TypedText;
65 case CodeCompletionString::CK_Text:
66 return CXCompletionChunk_Text;
67 case CodeCompletionString::CK_Optional:
68 return CXCompletionChunk_Optional;
69 case CodeCompletionString::CK_Placeholder:
70 return CXCompletionChunk_Placeholder;
71 case CodeCompletionString::CK_Informative:
72 return CXCompletionChunk_Informative;
73 case CodeCompletionString::CK_ResultType:
74 return CXCompletionChunk_ResultType;
75 case CodeCompletionString::CK_CurrentParameter:
76 return CXCompletionChunk_CurrentParameter;
77 case CodeCompletionString::CK_LeftParen:
78 return CXCompletionChunk_LeftParen;
79 case CodeCompletionString::CK_RightParen:
80 return CXCompletionChunk_RightParen;
81 case CodeCompletionString::CK_LeftBracket:
82 return CXCompletionChunk_LeftBracket;
83 case CodeCompletionString::CK_RightBracket:
84 return CXCompletionChunk_RightBracket;
85 case CodeCompletionString::CK_LeftBrace:
86 return CXCompletionChunk_LeftBrace;
87 case CodeCompletionString::CK_RightBrace:
88 return CXCompletionChunk_RightBrace;
89 case CodeCompletionString::CK_LeftAngle:
90 return CXCompletionChunk_LeftAngle;
91 case CodeCompletionString::CK_RightAngle:
92 return CXCompletionChunk_RightAngle;
93 case CodeCompletionString::CK_Comma:
94 return CXCompletionChunk_Comma;
Douglas Gregor01dfea02010-01-10 23:08:15 +000095 case CodeCompletionString::CK_Colon:
96 return CXCompletionChunk_Colon;
97 case CodeCompletionString::CK_SemiColon:
98 return CXCompletionChunk_SemiColon;
99 case CodeCompletionString::CK_Equal:
100 return CXCompletionChunk_Equal;
101 case CodeCompletionString::CK_HorizontalSpace:
102 return CXCompletionChunk_HorizontalSpace;
103 case CodeCompletionString::CK_VerticalSpace:
104 return CXCompletionChunk_VerticalSpace;
Ted Kremenekab188932010-01-05 19:32:54 +0000105 }
106
107 // Should be unreachable, but let's be careful.
108 return CXCompletionChunk_Text;
109}
110
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000111CXString clang_getCompletionChunkText(CXCompletionString completion_string,
112 unsigned chunk_number) {
Douglas Gregor218937c2011-02-01 19:23:04 +0000113 CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
Ted Kremenekab188932010-01-05 19:32:54 +0000114 if (!CCStr || chunk_number >= CCStr->size())
Ted Kremeneka60ed472010-11-16 08:15:36 +0000115 return createCXString((const char*)0);
Ted Kremenekab188932010-01-05 19:32:54 +0000116
117 switch ((*CCStr)[chunk_number].Kind) {
118 case CodeCompletionString::CK_TypedText:
119 case CodeCompletionString::CK_Text:
120 case CodeCompletionString::CK_Placeholder:
121 case CodeCompletionString::CK_CurrentParameter:
122 case CodeCompletionString::CK_Informative:
123 case CodeCompletionString::CK_LeftParen:
124 case CodeCompletionString::CK_RightParen:
125 case CodeCompletionString::CK_LeftBracket:
126 case CodeCompletionString::CK_RightBracket:
127 case CodeCompletionString::CK_LeftBrace:
128 case CodeCompletionString::CK_RightBrace:
129 case CodeCompletionString::CK_LeftAngle:
130 case CodeCompletionString::CK_RightAngle:
131 case CodeCompletionString::CK_Comma:
132 case CodeCompletionString::CK_ResultType:
Douglas Gregor01dfea02010-01-10 23:08:15 +0000133 case CodeCompletionString::CK_Colon:
134 case CodeCompletionString::CK_SemiColon:
135 case CodeCompletionString::CK_Equal:
136 case CodeCompletionString::CK_HorizontalSpace:
Douglas Gregor21c241f2010-05-25 06:14:46 +0000137 case CodeCompletionString::CK_VerticalSpace:
Douglas Gregor5a9c0bc2010-10-08 20:39:29 +0000138 return createCXString((*CCStr)[chunk_number].Text, false);
Douglas Gregor21c241f2010-05-25 06:14:46 +0000139
Ted Kremenekab188932010-01-05 19:32:54 +0000140 case CodeCompletionString::CK_Optional:
141 // Note: treated as an empty text block.
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000142 return createCXString("");
Ted Kremenekab188932010-01-05 19:32:54 +0000143 }
144
145 // Should be unreachable, but let's be careful.
Ted Kremeneka60ed472010-11-16 08:15:36 +0000146 return createCXString((const char*)0);
Ted Kremenekab188932010-01-05 19:32:54 +0000147}
148
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000149
Ted Kremenekab188932010-01-05 19:32:54 +0000150CXCompletionString
151clang_getCompletionChunkCompletionString(CXCompletionString completion_string,
152 unsigned chunk_number) {
Douglas Gregor218937c2011-02-01 19:23:04 +0000153 CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
Ted Kremenekab188932010-01-05 19:32:54 +0000154 if (!CCStr || chunk_number >= CCStr->size())
155 return 0;
156
157 switch ((*CCStr)[chunk_number].Kind) {
158 case CodeCompletionString::CK_TypedText:
159 case CodeCompletionString::CK_Text:
160 case CodeCompletionString::CK_Placeholder:
161 case CodeCompletionString::CK_CurrentParameter:
162 case CodeCompletionString::CK_Informative:
163 case CodeCompletionString::CK_LeftParen:
164 case CodeCompletionString::CK_RightParen:
165 case CodeCompletionString::CK_LeftBracket:
166 case CodeCompletionString::CK_RightBracket:
167 case CodeCompletionString::CK_LeftBrace:
168 case CodeCompletionString::CK_RightBrace:
169 case CodeCompletionString::CK_LeftAngle:
170 case CodeCompletionString::CK_RightAngle:
171 case CodeCompletionString::CK_Comma:
172 case CodeCompletionString::CK_ResultType:
Douglas Gregor01dfea02010-01-10 23:08:15 +0000173 case CodeCompletionString::CK_Colon:
174 case CodeCompletionString::CK_SemiColon:
175 case CodeCompletionString::CK_Equal:
176 case CodeCompletionString::CK_HorizontalSpace:
177 case CodeCompletionString::CK_VerticalSpace:
Ted Kremenekab188932010-01-05 19:32:54 +0000178 return 0;
179
180 case CodeCompletionString::CK_Optional:
181 // Note: treated as an empty text block.
182 return (*CCStr)[chunk_number].Optional;
183 }
184
185 // Should be unreachable, but let's be careful.
186 return 0;
187}
188
189unsigned clang_getNumCompletionChunks(CXCompletionString completion_string) {
Douglas Gregor218937c2011-02-01 19:23:04 +0000190 CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
Ted Kremenekab188932010-01-05 19:32:54 +0000191 return CCStr? CCStr->size() : 0;
192}
193
Douglas Gregor12e13132010-05-26 22:00:08 +0000194unsigned clang_getCompletionPriority(CXCompletionString completion_string) {
Douglas Gregor218937c2011-02-01 19:23:04 +0000195 CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
Bill Wendlinga2ace582010-05-27 18:35:05 +0000196 return CCStr? CCStr->getPriority() : unsigned(CCP_Unlikely);
Douglas Gregor12e13132010-05-26 22:00:08 +0000197}
198
Douglas Gregor58ddb602010-08-23 23:00:57 +0000199enum CXAvailabilityKind
200clang_getCompletionAvailability(CXCompletionString completion_string) {
Douglas Gregor218937c2011-02-01 19:23:04 +0000201 CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
202 return CCStr? static_cast<CXAvailabilityKind>(CCStr->getAvailability())
203 : CXAvailability_Available;
Douglas Gregor58ddb602010-08-23 23:00:57 +0000204}
205
Ted Kremenekab188932010-01-05 19:32:54 +0000206/// \brief The CXCodeCompleteResults structure we allocate internally;
207/// the client only sees the initial CXCodeCompleteResults structure.
208struct AllocatedCXCodeCompleteResults : public CXCodeCompleteResults {
Anders Carlsson0d8d7e62011-03-18 18:22:40 +0000209 AllocatedCXCodeCompleteResults(const FileSystemOptions& FileSystemOpts);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000210 ~AllocatedCXCodeCompleteResults();
211
Douglas Gregora88084b2010-02-18 18:08:43 +0000212 /// \brief Diagnostics produced while performing code completion.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000213 SmallVector<StoredDiagnostic, 8> Diagnostics;
Douglas Gregora88084b2010-02-18 18:08:43 +0000214
Douglas Gregorf715ca12010-03-16 00:06:06 +0000215 /// \brief Diag object
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000216 llvm::IntrusiveRefCntPtr<Diagnostic> Diag;
Douglas Gregorf715ca12010-03-16 00:06:06 +0000217
Douglas Gregora88084b2010-02-18 18:08:43 +0000218 /// \brief Language options used to adjust source locations.
Daniel Dunbar35b84402010-01-30 23:31:40 +0000219 LangOptions LangOpts;
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000220
221 FileSystemOptions FileSystemOpts;
222
Chris Lattner39b49bc2010-11-23 08:35:12 +0000223 /// \brief File manager, used for diagnostics.
Ted Kremenek4f327862011-03-21 18:40:17 +0000224 llvm::IntrusiveRefCntPtr<FileManager> FileMgr;
Chris Lattner39b49bc2010-11-23 08:35:12 +0000225
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000226 /// \brief Source manager, used for diagnostics.
Ted Kremenek4f327862011-03-21 18:40:17 +0000227 llvm::IntrusiveRefCntPtr<SourceManager> SourceMgr;
Douglas Gregor313e26c2010-02-18 23:35:40 +0000228
229 /// \brief Temporary files that should be removed once we have finished
230 /// with the code-completion results.
231 std::vector<llvm::sys::Path> TemporaryFiles;
Douglas Gregorb75d3df2010-08-04 17:07:00 +0000232
Chris Lattner7ad97ff2010-11-23 07:51:02 +0000233 /// \brief Temporary buffers that will be deleted once we have finished with
234 /// the code-completion results.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000235 SmallVector<const llvm::MemoryBuffer *, 1> TemporaryBuffers;
Douglas Gregor218937c2011-02-01 19:23:04 +0000236
Douglas Gregor48601b32011-02-16 19:08:06 +0000237 /// \brief Allocator used to store globally cached code-completion results.
238 llvm::IntrusiveRefCntPtr<clang::GlobalCodeCompletionAllocator>
239 CachedCompletionAllocator;
240
Douglas Gregor218937c2011-02-01 19:23:04 +0000241 /// \brief Allocator used to store code completion results.
Douglas Gregor110a68e2011-02-01 23:14:23 +0000242 clang::CodeCompletionAllocator CodeCompletionAllocator;
Douglas Gregor3da626b2011-07-07 16:03:39 +0000243
244 /// \brief Context under which completion occurred.
245 enum clang::CodeCompletionContext::Kind ContextKind;
246
247 /// \brief A bitfield representing the acceptable completions for the
248 /// current context.
249 unsigned long long Contexts;
Douglas Gregore081a612011-07-21 01:05:26 +0000250
Douglas Gregor0a47d692011-07-26 15:24:30 +0000251 /// \brief The kind of the container for the current context for completions.
Douglas Gregore081a612011-07-21 01:05:26 +0000252 enum CXCursorKind ContainerKind;
Douglas Gregor0a47d692011-07-26 15:24:30 +0000253 /// \brief The USR of the container for the current context for completions.
Douglas Gregore081a612011-07-21 01:05:26 +0000254 CXString ContainerUSR;
Douglas Gregor0a47d692011-07-26 15:24:30 +0000255 /// \brief a boolean value indicating whether there is complete information
256 /// about the container
Douglas Gregore081a612011-07-21 01:05:26 +0000257 unsigned ContainerIsIncomplete;
Douglas Gregor0a47d692011-07-26 15:24:30 +0000258
259 /// \brief A string containing the Objective-C selector entered thus far for a
260 /// message send.
261 std::string Selector;
Ted Kremenekab188932010-01-05 19:32:54 +0000262};
263
Douglas Gregore3c60a72010-11-17 00:13:31 +0000264/// \brief Tracks the number of code-completion result objects that are
265/// currently active.
266///
267/// Used for debugging purposes only.
Douglas Gregor1fd9e0d2010-12-07 00:05:48 +0000268static llvm::sys::cas_flag CodeCompletionResultObjects;
Douglas Gregore3c60a72010-11-17 00:13:31 +0000269
Anders Carlsson0d8d7e62011-03-18 18:22:40 +0000270AllocatedCXCodeCompleteResults::AllocatedCXCodeCompleteResults(
271 const FileSystemOptions& FileSystemOpts)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000272 : CXCodeCompleteResults(),
273 Diag(new Diagnostic(
274 llvm::IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs))),
Anders Carlsson0d8d7e62011-03-18 18:22:40 +0000275 FileSystemOpts(FileSystemOpts),
Ted Kremenek4f327862011-03-21 18:40:17 +0000276 FileMgr(new FileManager(FileSystemOpts)),
277 SourceMgr(new SourceManager(*Diag, *FileMgr)) {
Douglas Gregore3c60a72010-11-17 00:13:31 +0000278 if (getenv("LIBCLANG_OBJTRACKING")) {
Douglas Gregor1fd9e0d2010-12-07 00:05:48 +0000279 llvm::sys::AtomicIncrement(&CodeCompletionResultObjects);
Douglas Gregore3c60a72010-11-17 00:13:31 +0000280 fprintf(stderr, "+++ %d completion results\n", CodeCompletionResultObjects);
281 }
282}
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000283
284AllocatedCXCodeCompleteResults::~AllocatedCXCodeCompleteResults() {
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000285 delete [] Results;
Douglas Gregor313e26c2010-02-18 23:35:40 +0000286
Douglas Gregore081a612011-07-21 01:05:26 +0000287 clang_disposeString(ContainerUSR);
288
Douglas Gregor313e26c2010-02-18 23:35:40 +0000289 for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I)
290 TemporaryFiles[I].eraseFromDisk();
Douglas Gregorb75d3df2010-08-04 17:07:00 +0000291 for (unsigned I = 0, N = TemporaryBuffers.size(); I != N; ++I)
292 delete TemporaryBuffers[I];
Douglas Gregore3c60a72010-11-17 00:13:31 +0000293
294 if (getenv("LIBCLANG_OBJTRACKING")) {
Douglas Gregor1fd9e0d2010-12-07 00:05:48 +0000295 llvm::sys::AtomicDecrement(&CodeCompletionResultObjects);
Douglas Gregore3c60a72010-11-17 00:13:31 +0000296 fprintf(stderr, "--- %d completion results\n", CodeCompletionResultObjects);
297 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000298}
299
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000300} // end extern "C"
301
Douglas Gregor3da626b2011-07-07 16:03:39 +0000302static unsigned long long getContextsForContextKind(
303 enum CodeCompletionContext::Kind kind,
304 Sema &S) {
305 unsigned long long contexts = 0;
306 switch (kind) {
307 case CodeCompletionContext::CCC_OtherWithMacros: {
308 //We can allow macros here, but we don't know what else is permissible
309 //So we'll say the only thing permissible are macros
310 contexts = CXCompletionContext_MacroName;
311 break;
312 }
313 case CodeCompletionContext::CCC_TopLevel:
314 case CodeCompletionContext::CCC_ObjCIvarList:
315 case CodeCompletionContext::CCC_ClassStructUnion:
316 case CodeCompletionContext::CCC_Type: {
317 contexts = CXCompletionContext_AnyType |
318 CXCompletionContext_ObjCInterface;
319 if (S.getLangOptions().CPlusPlus) {
320 contexts |= CXCompletionContext_EnumTag |
321 CXCompletionContext_UnionTag |
322 CXCompletionContext_StructTag |
323 CXCompletionContext_ClassTag |
324 CXCompletionContext_NestedNameSpecifier;
325 }
326 break;
327 }
328 case CodeCompletionContext::CCC_Statement: {
329 contexts = CXCompletionContext_AnyType |
330 CXCompletionContext_ObjCInterface |
331 CXCompletionContext_AnyValue;
332 if (S.getLangOptions().CPlusPlus) {
333 contexts |= CXCompletionContext_EnumTag |
334 CXCompletionContext_UnionTag |
335 CXCompletionContext_StructTag |
336 CXCompletionContext_ClassTag |
337 CXCompletionContext_NestedNameSpecifier;
338 }
339 break;
340 }
341 case CodeCompletionContext::CCC_Expression: {
342 contexts = CXCompletionContext_AnyValue;
343 if (S.getLangOptions().CPlusPlus) {
344 contexts |= CXCompletionContext_AnyType |
345 CXCompletionContext_ObjCInterface |
346 CXCompletionContext_EnumTag |
347 CXCompletionContext_UnionTag |
348 CXCompletionContext_StructTag |
349 CXCompletionContext_ClassTag |
350 CXCompletionContext_NestedNameSpecifier;
351 }
352 break;
353 }
354 case CodeCompletionContext::CCC_ObjCMessageReceiver: {
355 contexts = CXCompletionContext_ObjCObjectValue |
356 CXCompletionContext_ObjCSelectorValue |
357 CXCompletionContext_ObjCInterface;
358 if (S.getLangOptions().CPlusPlus) {
359 contexts |= CXCompletionContext_CXXClassTypeValue |
360 CXCompletionContext_AnyType |
361 CXCompletionContext_EnumTag |
362 CXCompletionContext_UnionTag |
363 CXCompletionContext_StructTag |
364 CXCompletionContext_ClassTag |
365 CXCompletionContext_NestedNameSpecifier;
366 }
367 break;
368 }
369 case CodeCompletionContext::CCC_DotMemberAccess: {
370 contexts = CXCompletionContext_DotMemberAccess;
371 break;
372 }
373 case CodeCompletionContext::CCC_ArrowMemberAccess: {
374 contexts = CXCompletionContext_ArrowMemberAccess;
375 break;
376 }
377 case CodeCompletionContext::CCC_ObjCPropertyAccess: {
378 contexts = CXCompletionContext_ObjCPropertyAccess;
379 break;
380 }
381 case CodeCompletionContext::CCC_EnumTag: {
382 contexts = CXCompletionContext_EnumTag |
383 CXCompletionContext_NestedNameSpecifier;
384 break;
385 }
386 case CodeCompletionContext::CCC_UnionTag: {
387 contexts = CXCompletionContext_UnionTag |
388 CXCompletionContext_NestedNameSpecifier;
389 break;
390 }
391 case CodeCompletionContext::CCC_ClassOrStructTag: {
392 contexts = CXCompletionContext_StructTag |
393 CXCompletionContext_ClassTag |
394 CXCompletionContext_NestedNameSpecifier;
395 break;
396 }
397 case CodeCompletionContext::CCC_ObjCProtocolName: {
398 contexts = CXCompletionContext_ObjCProtocol;
399 break;
400 }
401 case CodeCompletionContext::CCC_Namespace: {
402 contexts = CXCompletionContext_Namespace;
403 break;
404 }
405 case CodeCompletionContext::CCC_PotentiallyQualifiedName: {
406 contexts = CXCompletionContext_NestedNameSpecifier;
407 break;
408 }
409 case CodeCompletionContext::CCC_MacroNameUse: {
410 contexts = CXCompletionContext_MacroName;
411 break;
412 }
413 case CodeCompletionContext::CCC_NaturalLanguage: {
414 contexts = CXCompletionContext_NaturalLanguage;
415 break;
416 }
417 case CodeCompletionContext::CCC_SelectorName: {
418 contexts = CXCompletionContext_ObjCSelectorName;
419 break;
420 }
421 case CodeCompletionContext::CCC_ParenthesizedExpression: {
422 contexts = CXCompletionContext_AnyType |
423 CXCompletionContext_ObjCInterface |
424 CXCompletionContext_AnyValue;
425 if (S.getLangOptions().CPlusPlus) {
426 contexts |= CXCompletionContext_EnumTag |
427 CXCompletionContext_UnionTag |
428 CXCompletionContext_StructTag |
429 CXCompletionContext_ClassTag |
430 CXCompletionContext_NestedNameSpecifier;
431 }
432 break;
433 }
434 case CodeCompletionContext::CCC_ObjCInstanceMessage: {
435 contexts = CXCompletionContext_ObjCInstanceMessage;
436 break;
437 }
438 case CodeCompletionContext::CCC_ObjCClassMessage: {
439 contexts = CXCompletionContext_ObjCClassMessage;
440 break;
441 }
Douglas Gregor0f91c8c2011-07-30 06:55:39 +0000442 case CodeCompletionContext::CCC_ObjCInterfaceName: {
Douglas Gregor3da626b2011-07-07 16:03:39 +0000443 contexts = CXCompletionContext_ObjCInterface;
444 break;
445 }
446 case CodeCompletionContext::CCC_ObjCCategoryName: {
447 contexts = CXCompletionContext_ObjCCategory;
448 break;
449 }
450 case CodeCompletionContext::CCC_Other:
451 case CodeCompletionContext::CCC_ObjCInterface:
452 case CodeCompletionContext::CCC_ObjCImplementation:
453 case CodeCompletionContext::CCC_Name:
454 case CodeCompletionContext::CCC_MacroName:
455 case CodeCompletionContext::CCC_PreprocessorExpression:
456 case CodeCompletionContext::CCC_PreprocessorDirective:
457 case CodeCompletionContext::CCC_TypeQualifiers: {
458 //Only Clang results should be accepted, so we'll set all of the other
459 //context bits to 0 (i.e. the empty set)
460 contexts = CXCompletionContext_Unexposed;
461 break;
462 }
463 case CodeCompletionContext::CCC_Recovery: {
464 //We don't know what the current context is, so we'll return unknown
465 //This is the equivalent of setting all of the other context bits
466 contexts = CXCompletionContext_Unknown;
467 break;
468 }
469 }
470 return contexts;
471}
472
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000473namespace {
474 class CaptureCompletionResults : public CodeCompleteConsumer {
475 AllocatedCXCodeCompleteResults &AllocatedResults;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000476 SmallVector<CXCompletionResult, 16> StoredResults;
Douglas Gregore081a612011-07-21 01:05:26 +0000477 CXTranslationUnit *TU;
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000478 public:
Douglas Gregore081a612011-07-21 01:05:26 +0000479 CaptureCompletionResults(AllocatedCXCodeCompleteResults &Results,
480 CXTranslationUnit *TranslationUnit)
Douglas Gregor8071e422010-08-15 06:18:01 +0000481 : CodeCompleteConsumer(true, false, true, false),
Douglas Gregore081a612011-07-21 01:05:26 +0000482 AllocatedResults(Results), TU(TranslationUnit) { }
Douglas Gregor32be4a52010-10-11 21:37:58 +0000483 ~CaptureCompletionResults() { Finish(); }
484
Douglas Gregore6b1bb62010-08-11 21:23:17 +0000485 virtual void ProcessCodeCompleteResults(Sema &S,
486 CodeCompletionContext Context,
John McCall0a2c5e22010-08-25 06:19:51 +0000487 CodeCompletionResult *Results,
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000488 unsigned NumResults) {
Douglas Gregor32be4a52010-10-11 21:37:58 +0000489 StoredResults.reserve(StoredResults.size() + NumResults);
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000490 for (unsigned I = 0; I != NumResults; ++I) {
Douglas Gregor218937c2011-02-01 19:23:04 +0000491 CodeCompletionString *StoredCompletion
492 = Results[I].CreateCodeCompletionString(S,
493 AllocatedResults.CodeCompletionAllocator);
Douglas Gregor32be4a52010-10-11 21:37:58 +0000494
495 CXCompletionResult R;
496 R.CursorKind = Results[I].CursorKind;
497 R.CompletionString = StoredCompletion;
498 StoredResults.push_back(R);
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000499 }
Douglas Gregor3da626b2011-07-07 16:03:39 +0000500
Douglas Gregore081a612011-07-21 01:05:26 +0000501 enum CodeCompletionContext::Kind contextKind = Context.getKind();
Douglas Gregor3da626b2011-07-07 16:03:39 +0000502
Douglas Gregore081a612011-07-21 01:05:26 +0000503 AllocatedResults.ContextKind = contextKind;
504 AllocatedResults.Contexts = getContextsForContextKind(contextKind, S);
505
Douglas Gregor0a47d692011-07-26 15:24:30 +0000506 AllocatedResults.Selector = "";
507 if (Context.getNumSelIdents() > 0) {
508 for (unsigned i = 0; i < Context.getNumSelIdents(); i++) {
509 IdentifierInfo *selIdent = Context.getSelIdents()[i];
510 if (selIdent != NULL) {
511 StringRef selectorString = Context.getSelIdents()[i]->getName();
Benjamin Kramera0651c52011-07-26 16:59:25 +0000512 AllocatedResults.Selector += selectorString;
Douglas Gregor0a47d692011-07-26 15:24:30 +0000513 }
514 AllocatedResults.Selector += ":";
515 }
516 }
517
Douglas Gregore081a612011-07-21 01:05:26 +0000518 QualType baseType = Context.getBaseType();
519 NamedDecl *D = NULL;
520
521 if (!baseType.isNull()) {
522 // Get the declaration for a class/struct/union/enum type
523 if (const TagType *Tag = baseType->getAs<TagType>())
524 D = Tag->getDecl();
525 // Get the @interface declaration for a (possibly-qualified) Objective-C
526 // object pointer type, e.g., NSString*
527 else if (const ObjCObjectPointerType *ObjPtr =
528 baseType->getAs<ObjCObjectPointerType>())
529 D = ObjPtr->getInterfaceDecl();
530 // Get the @interface declaration for an Objective-C object type
531 else if (const ObjCObjectType *Obj = baseType->getAs<ObjCObjectType>())
532 D = Obj->getInterface();
533 // Get the class for a C++ injected-class-name
534 else if (const InjectedClassNameType *Injected =
535 baseType->getAs<InjectedClassNameType>())
536 D = Injected->getDecl();
537 }
538
539 if (D != NULL) {
540 CXCursor cursor = cxcursor::MakeCXCursor(D, *TU);
541
542 CXCursorKind cursorKind = clang_getCursorKind(cursor);
543 CXString cursorUSR = clang_getCursorUSR(cursor);
544
Ted Kremenekbaf82b02011-08-17 22:19:53 +0000545 // Normally, clients of CXString shouldn't care whether or not
546 // a CXString is managed by a pool or by explicitly malloc'ed memory.
547 // However, there are cases when AllocatedResults outlives the
548 // CXTranslationUnit. This is a workaround that failure mode.
549 if (cxstring::isManagedByPool(cursorUSR)) {
550 CXString heapStr =
551 cxstring::createCXString(clang_getCString(cursorUSR), true);
552 clang_disposeString(cursorUSR);
553 cursorUSR = heapStr;
554 }
555
Douglas Gregore081a612011-07-21 01:05:26 +0000556 AllocatedResults.ContainerKind = cursorKind;
557 AllocatedResults.ContainerUSR = cursorUSR;
Ted Kremenekbaf82b02011-08-17 22:19:53 +0000558
Douglas Gregore081a612011-07-21 01:05:26 +0000559 const Type *type = baseType.getTypePtrOrNull();
560 if (type != NULL) {
561 AllocatedResults.ContainerIsIncomplete = type->isIncompleteType();
562 }
563 else {
564 AllocatedResults.ContainerIsIncomplete = 1;
565 }
566 }
567 else {
568 AllocatedResults.ContainerKind = CXCursor_InvalidCode;
569 AllocatedResults.ContainerUSR = createCXString("");
570 AllocatedResults.ContainerIsIncomplete = 1;
571 }
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000572 }
Douglas Gregor1e5e6682010-08-26 13:48:20 +0000573
Douglas Gregor32be4a52010-10-11 21:37:58 +0000574 virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
575 OverloadCandidate *Candidates,
576 unsigned NumCandidates) {
577 StoredResults.reserve(StoredResults.size() + NumCandidates);
578 for (unsigned I = 0; I != NumCandidates; ++I) {
Douglas Gregor218937c2011-02-01 19:23:04 +0000579 CodeCompletionString *StoredCompletion
580 = Candidates[I].CreateSignatureString(CurrentArg, S,
581 AllocatedResults.CodeCompletionAllocator);
Douglas Gregor32be4a52010-10-11 21:37:58 +0000582
583 CXCompletionResult R;
584 R.CursorKind = CXCursor_NotImplemented;
585 R.CompletionString = StoredCompletion;
586 StoredResults.push_back(R);
587 }
588 }
589
Douglas Gregordae68752011-02-01 22:57:45 +0000590 virtual CodeCompletionAllocator &getAllocator() {
Douglas Gregor218937c2011-02-01 19:23:04 +0000591 return AllocatedResults.CodeCompletionAllocator;
592 }
593
Douglas Gregor32be4a52010-10-11 21:37:58 +0000594 private:
595 void Finish() {
596 AllocatedResults.Results = new CXCompletionResult [StoredResults.size()];
597 AllocatedResults.NumResults = StoredResults.size();
598 std::memcpy(AllocatedResults.Results, StoredResults.data(),
599 StoredResults.size() * sizeof(CXCompletionResult));
600 StoredResults.clear();
601 }
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000602 };
603}
604
605extern "C" {
Daniel Dunbarb1fd3452010-08-19 23:44:10 +0000606struct CodeCompleteAtInfo {
607 CXTranslationUnit TU;
608 const char *complete_filename;
609 unsigned complete_line;
610 unsigned complete_column;
611 struct CXUnsavedFile *unsaved_files;
612 unsigned num_unsaved_files;
613 unsigned options;
614 CXCodeCompleteResults *result;
615};
616void clang_codeCompleteAt_Impl(void *UserData) {
617 CodeCompleteAtInfo *CCAI = static_cast<CodeCompleteAtInfo*>(UserData);
618 CXTranslationUnit TU = CCAI->TU;
619 const char *complete_filename = CCAI->complete_filename;
620 unsigned complete_line = CCAI->complete_line;
621 unsigned complete_column = CCAI->complete_column;
622 struct CXUnsavedFile *unsaved_files = CCAI->unsaved_files;
623 unsigned num_unsaved_files = CCAI->num_unsaved_files;
624 unsigned options = CCAI->options;
625 CCAI->result = 0;
626
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000627#ifdef UDP_CODE_COMPLETION_LOGGER
628#ifdef UDP_CODE_COMPLETION_LOGGER_PORT
629 const llvm::TimeRecord &StartTime = llvm::TimeRecord::getCurrentTime();
630#endif
631#endif
632
633 bool EnableLogging = getenv("LIBCLANG_CODE_COMPLETION_LOGGING") != 0;
634
Ted Kremeneka60ed472010-11-16 08:15:36 +0000635 ASTUnit *AST = static_cast<ASTUnit *>(TU->TUData);
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000636 if (!AST)
Daniel Dunbarb1fd3452010-08-19 23:44:10 +0000637 return;
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000638
Douglas Gregor593b0c12010-09-23 18:47:53 +0000639 ASTUnit::ConcurrencyCheck Check(*AST);
640
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000641 // Perform the remapping of source files.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000642 SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles;
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000643 for (unsigned I = 0; I != num_unsaved_files; ++I) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000644 StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length);
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000645 const llvm::MemoryBuffer *Buffer
646 = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename);
647 RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename,
648 Buffer));
649 }
650
651 if (EnableLogging) {
652 // FIXME: Add logging.
653 }
654
655 // Parse the resulting source file to find code-completion results.
Anders Carlsson0d8d7e62011-03-18 18:22:40 +0000656 AllocatedCXCodeCompleteResults *Results =
657 new AllocatedCXCodeCompleteResults(AST->getFileSystemOpts());
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000658 Results->Results = 0;
659 Results->NumResults = 0;
Douglas Gregor48601b32011-02-16 19:08:06 +0000660
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000661 // Create a code-completion consumer to capture the results.
Douglas Gregore081a612011-07-21 01:05:26 +0000662 CaptureCompletionResults Capture(*Results, &TU);
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000663
664 // Perform completion.
665 AST->CodeComplete(complete_filename, complete_line, complete_column,
Douglas Gregorcee235c2010-08-05 09:09:23 +0000666 RemappedFiles.data(), RemappedFiles.size(),
667 (options & CXCodeComplete_IncludeMacros),
668 (options & CXCodeComplete_IncludeCodePatterns),
669 Capture,
Ted Kremenek4f327862011-03-21 18:40:17 +0000670 *Results->Diag, Results->LangOpts, *Results->SourceMgr,
671 *Results->FileMgr, Results->Diagnostics,
Douglas Gregor2283d792010-08-20 00:59:43 +0000672 Results->TemporaryBuffers);
Douglas Gregor48601b32011-02-16 19:08:06 +0000673
674 // Keep a reference to the allocator used for cached global completions, so
675 // that we can be sure that the memory used by our code completion strings
676 // doesn't get freed due to subsequent reparses (while the code completion
677 // results are still active).
678 Results->CachedCompletionAllocator = AST->getCachedCompletionAllocator();
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000679
680
681
682#ifdef UDP_CODE_COMPLETION_LOGGER
683#ifdef UDP_CODE_COMPLETION_LOGGER_PORT
684 const llvm::TimeRecord &EndTime = llvm::TimeRecord::getCurrentTime();
685 llvm::SmallString<256> LogResult;
686 llvm::raw_svector_ostream os(LogResult);
687
688 // Figure out the language and whether or not it uses PCH.
689 const char *lang = 0;
690 bool usesPCH = false;
691
692 for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
693 I != E; ++I) {
694 if (*I == 0)
695 continue;
696 if (strcmp(*I, "-x") == 0) {
697 if (I + 1 != E) {
698 lang = *(++I);
699 continue;
700 }
701 }
702 else if (strcmp(*I, "-include") == 0) {
703 if (I+1 != E) {
704 const char *arg = *(++I);
705 llvm::SmallString<512> pchName;
706 {
707 llvm::raw_svector_ostream os(pchName);
708 os << arg << ".pth";
709 }
710 pchName.push_back('\0');
711 struct stat stat_results;
Douglas Gregor0a47d692011-07-26 15:24:30 +0000712 if (stat(pchName.str().c_str(), &stat_results) == 0)
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000713 usesPCH = true;
714 continue;
715 }
716 }
717 }
718
719 os << "{ ";
720 os << "\"wall\": " << (EndTime.getWallTime() - StartTime.getWallTime());
721 os << ", \"numRes\": " << Results->NumResults;
722 os << ", \"diags\": " << Results->Diagnostics.size();
723 os << ", \"pch\": " << (usesPCH ? "true" : "false");
724 os << ", \"lang\": \"" << (lang ? lang : "<unknown>") << '"';
725 const char *name = getlogin();
726 os << ", \"user\": \"" << (name ? name : "unknown") << '"';
727 os << ", \"clangVer\": \"" << getClangFullVersion() << '"';
728 os << " }";
729
Chris Lattner5f9e2722011-07-23 10:55:15 +0000730 StringRef res = os.str();
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000731 if (res.size() > 0) {
732 do {
733 // Setup the UDP socket.
734 struct sockaddr_in servaddr;
735 bzero(&servaddr, sizeof(servaddr));
736 servaddr.sin_family = AF_INET;
737 servaddr.sin_port = htons(UDP_CODE_COMPLETION_LOGGER_PORT);
738 if (inet_pton(AF_INET, UDP_CODE_COMPLETION_LOGGER,
739 &servaddr.sin_addr) <= 0)
740 break;
741
742 int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
743 if (sockfd < 0)
744 break;
745
746 sendto(sockfd, res.data(), res.size(), 0,
747 (struct sockaddr *)&servaddr, sizeof(servaddr));
748 close(sockfd);
749 }
750 while (false);
751 }
752#endif
753#endif
Daniel Dunbarb1fd3452010-08-19 23:44:10 +0000754 CCAI->result = Results;
755}
756CXCodeCompleteResults *clang_codeCompleteAt(CXTranslationUnit TU,
757 const char *complete_filename,
758 unsigned complete_line,
759 unsigned complete_column,
760 struct CXUnsavedFile *unsaved_files,
761 unsigned num_unsaved_files,
762 unsigned options) {
763 CodeCompleteAtInfo CCAI = { TU, complete_filename, complete_line,
764 complete_column, unsaved_files, num_unsaved_files,
765 options, 0 };
766 llvm::CrashRecoveryContext CRC;
767
Daniel Dunbarbf44c3b2010-11-05 07:19:31 +0000768 if (!RunSafely(CRC, clang_codeCompleteAt_Impl, &CCAI)) {
Daniel Dunbarb1fd3452010-08-19 23:44:10 +0000769 fprintf(stderr, "libclang: crash detected in code completion\n");
Ted Kremeneka60ed472010-11-16 08:15:36 +0000770 static_cast<ASTUnit *>(TU->TUData)->setUnsafeToFree(true);
Daniel Dunbarb1fd3452010-08-19 23:44:10 +0000771 return 0;
Douglas Gregor6df78732011-05-05 20:27:22 +0000772 } else if (getenv("LIBCLANG_RESOURCE_USAGE"))
773 PrintLibclangResourceUsage(TU);
Daniel Dunbarb1fd3452010-08-19 23:44:10 +0000774
775 return CCAI.result;
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000776}
777
Douglas Gregorcee235c2010-08-05 09:09:23 +0000778unsigned clang_defaultCodeCompleteOptions(void) {
779 return CXCodeComplete_IncludeMacros;
780}
781
Ted Kremenekab188932010-01-05 19:32:54 +0000782void clang_disposeCodeCompleteResults(CXCodeCompleteResults *ResultsIn) {
783 if (!ResultsIn)
784 return;
785
786 AllocatedCXCodeCompleteResults *Results
787 = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn);
Ted Kremenekab188932010-01-05 19:32:54 +0000788 delete Results;
789}
Douglas Gregor58ddb602010-08-23 23:00:57 +0000790
Douglas Gregora88084b2010-02-18 18:08:43 +0000791unsigned
792clang_codeCompleteGetNumDiagnostics(CXCodeCompleteResults *ResultsIn) {
793 AllocatedCXCodeCompleteResults *Results
794 = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn);
795 if (!Results)
796 return 0;
797
798 return Results->Diagnostics.size();
799}
800
801CXDiagnostic
802clang_codeCompleteGetDiagnostic(CXCodeCompleteResults *ResultsIn,
803 unsigned Index) {
804 AllocatedCXCodeCompleteResults *Results
805 = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn);
806 if (!Results || Index >= Results->Diagnostics.size())
807 return 0;
808
809 return new CXStoredDiagnostic(Results->Diagnostics[Index], Results->LangOpts);
810}
811
Douglas Gregor3da626b2011-07-07 16:03:39 +0000812unsigned long long
813clang_codeCompleteGetContexts(CXCodeCompleteResults *ResultsIn) {
814 AllocatedCXCodeCompleteResults *Results
815 = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn);
816 if (!Results)
817 return 0;
818
819 return Results->Contexts;
820}
Douglas Gregora88084b2010-02-18 18:08:43 +0000821
Douglas Gregore081a612011-07-21 01:05:26 +0000822enum CXCursorKind clang_codeCompleteGetContainerKind(
823 CXCodeCompleteResults *ResultsIn,
824 unsigned *IsIncomplete) {
825 AllocatedCXCodeCompleteResults *Results =
826 static_cast<AllocatedCXCodeCompleteResults *>(ResultsIn);
827 if (!Results)
828 return CXCursor_InvalidCode;
829
830 if (IsIncomplete != NULL) {
831 *IsIncomplete = Results->ContainerIsIncomplete;
832 }
833
834 return Results->ContainerKind;
835}
836
837CXString clang_codeCompleteGetContainerUSR(CXCodeCompleteResults *ResultsIn) {
838 AllocatedCXCodeCompleteResults *Results =
839 static_cast<AllocatedCXCodeCompleteResults *>(ResultsIn);
840 if (!Results)
841 return createCXString("");
842
843 return createCXString(clang_getCString(Results->ContainerUSR));
844}
Douglas Gregor0a47d692011-07-26 15:24:30 +0000845
846
847CXString clang_codeCompleteGetObjCSelector(CXCodeCompleteResults *ResultsIn) {
848 AllocatedCXCodeCompleteResults *Results =
849 static_cast<AllocatedCXCodeCompleteResults *>(ResultsIn);
850 if (!Results)
851 return createCXString("");
852
853 return createCXString(Results->Selector);
854}
Douglas Gregore081a612011-07-21 01:05:26 +0000855
Ted Kremenekab188932010-01-05 19:32:54 +0000856} // end extern "C"
Douglas Gregor1e5e6682010-08-26 13:48:20 +0000857
Douglas Gregorc7b7b7a2010-10-18 21:05:04 +0000858/// \brief Simple utility function that appends a \p New string to the given
859/// \p Old string, using the \p Buffer for storage.
860///
861/// \param Old The string to which we are appending. This parameter will be
862/// updated to reflect the complete string.
863///
864///
865/// \param New The string to append to \p Old.
866///
867/// \param Buffer A buffer that stores the actual, concatenated string. It will
868/// be used if the old string is already-non-empty.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000869static void AppendToString(StringRef &Old, StringRef New,
Douglas Gregorc7b7b7a2010-10-18 21:05:04 +0000870 llvm::SmallString<256> &Buffer) {
871 if (Old.empty()) {
872 Old = New;
873 return;
874 }
875
876 if (Buffer.empty())
877 Buffer.append(Old.begin(), Old.end());
878 Buffer.append(New.begin(), New.end());
879 Old = Buffer.str();
880}
881
882/// \brief Get the typed-text blocks from the given code-completion string
883/// and return them as a single string.
884///
885/// \param String The code-completion string whose typed-text blocks will be
886/// concatenated.
887///
888/// \param Buffer A buffer used for storage of the completed name.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000889static StringRef GetTypedName(CodeCompletionString *String,
Douglas Gregorc7b7b7a2010-10-18 21:05:04 +0000890 llvm::SmallString<256> &Buffer) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000891 StringRef Result;
Douglas Gregorc7b7b7a2010-10-18 21:05:04 +0000892 for (CodeCompletionString::iterator C = String->begin(), CEnd = String->end();
893 C != CEnd; ++C) {
894 if (C->Kind == CodeCompletionString::CK_TypedText)
895 AppendToString(Result, C->Text, Buffer);
896 }
897
898 return Result;
899}
900
Douglas Gregor1e5e6682010-08-26 13:48:20 +0000901namespace {
902 struct OrderCompletionResults {
903 bool operator()(const CXCompletionResult &XR,
904 const CXCompletionResult &YR) const {
Douglas Gregor218937c2011-02-01 19:23:04 +0000905 CodeCompletionString *X
906 = (CodeCompletionString *)XR.CompletionString;
907 CodeCompletionString *Y
908 = (CodeCompletionString *)YR.CompletionString;
Douglas Gregor1e5e6682010-08-26 13:48:20 +0000909
Douglas Gregorc7b7b7a2010-10-18 21:05:04 +0000910 llvm::SmallString<256> XBuffer;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000911 StringRef XText = GetTypedName(X, XBuffer);
Douglas Gregorc7b7b7a2010-10-18 21:05:04 +0000912 llvm::SmallString<256> YBuffer;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000913 StringRef YText = GetTypedName(Y, YBuffer);
Douglas Gregor1e5e6682010-08-26 13:48:20 +0000914
Douglas Gregorc7b7b7a2010-10-18 21:05:04 +0000915 if (XText.empty() || YText.empty())
916 return !XText.empty();
917
918 int result = XText.compare_lower(YText);
Douglas Gregor1e5e6682010-08-26 13:48:20 +0000919 if (result < 0)
920 return true;
921 if (result > 0)
922 return false;
923
Douglas Gregorc7b7b7a2010-10-18 21:05:04 +0000924 result = XText.compare(YText);
Douglas Gregor1aad3402010-09-10 23:05:54 +0000925 return result < 0;
Douglas Gregor1e5e6682010-08-26 13:48:20 +0000926 }
927 };
928}
929
930extern "C" {
931 void clang_sortCodeCompletionResults(CXCompletionResult *Results,
932 unsigned NumResults) {
933 std::stable_sort(Results, Results + NumResults, OrderCompletionResults());
934 }
Douglas Gregor458433d2010-08-26 15:07:07 +0000935}