blob: 58a1627b478aa87efed53fa09c499666b2031711 [file] [log] [blame]
Douglas Gregor3545ff42009-09-21 16:56:56 +00001//===--- CodeCompleteConsumer.cpp - Code Completion Interface ---*- C++ -*-===//
Douglas Gregor2436e712009-09-17 21:32:03 +00002//
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 CodeCompleteConsumer class.
11//
12//===----------------------------------------------------------------------===//
13#include "clang/Sema/CodeCompleteConsumer.h"
John McCall8b0666c2010-08-20 18:27:03 +000014#include "clang/Sema/Scope.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000015#include "clang/Sema/Sema.h"
Douglas Gregor56c2dbc2009-09-18 17:54:00 +000016#include "clang/AST/DeclCXX.h"
John McCallde6836a2010-08-24 07:21:54 +000017#include "clang/AST/DeclObjC.h"
John McCall19c1bfd2010-08-25 05:32:35 +000018#include "clang/AST/DeclTemplate.h"
Douglas Gregor2436e712009-09-17 21:32:03 +000019#include "clang/Lex/Preprocessor.h"
Douglas Gregorf09935f2009-12-01 05:55:20 +000020#include "clang-c/Index.h"
Douglas Gregor2436e712009-09-17 21:32:03 +000021#include "llvm/ADT/STLExtras.h"
Douglas Gregor2436e712009-09-17 21:32:03 +000022#include "llvm/Support/raw_ostream.h"
23#include <algorithm>
Douglas Gregorfedc3282009-09-18 22:15:54 +000024#include <cstring>
25#include <functional>
Douglas Gregorab6ccb52009-11-17 16:43:05 +000026
Douglas Gregor2436e712009-09-17 21:32:03 +000027using namespace clang;
Douglas Gregorab6ccb52009-11-17 16:43:05 +000028using llvm::StringRef;
Douglas Gregor2436e712009-09-17 21:32:03 +000029
Douglas Gregorfedc3282009-09-18 22:15:54 +000030//===----------------------------------------------------------------------===//
31// Code completion string implementation
32//===----------------------------------------------------------------------===//
Douglas Gregor9eb77012009-11-07 00:00:49 +000033CodeCompletionString::Chunk::Chunk(ChunkKind Kind, llvm::StringRef Text)
Daniel Dunbarb0a19422009-11-12 18:40:12 +000034 : Kind(Kind), Text("")
Douglas Gregor5bf52692009-09-22 23:15:58 +000035{
Douglas Gregor9eb77012009-11-07 00:00:49 +000036 switch (Kind) {
37 case CK_TypedText:
38 case CK_Text:
39 case CK_Placeholder:
40 case CK_Informative:
Douglas Gregorb3fa9192009-12-18 18:53:37 +000041 case CK_ResultType:
Douglas Gregor9eb77012009-11-07 00:00:49 +000042 case CK_CurrentParameter: {
43 char *New = new char [Text.size() + 1];
44 std::memcpy(New, Text.data(), Text.size());
45 New[Text.size()] = '\0';
46 this->Text = New;
47 break;
48 }
49
50 case CK_Optional:
Jeffrey Yasskin1615d452009-12-12 05:05:38 +000051 llvm_unreachable("Optional strings cannot be created from text");
Douglas Gregor9eb77012009-11-07 00:00:49 +000052 break;
53
54 case CK_LeftParen:
55 this->Text = "(";
56 break;
57
58 case CK_RightParen:
59 this->Text = ")";
60 break;
61
62 case CK_LeftBracket:
63 this->Text = "[";
64 break;
65
66 case CK_RightBracket:
67 this->Text = "]";
68 break;
69
70 case CK_LeftBrace:
71 this->Text = "{";
72 break;
73
74 case CK_RightBrace:
75 this->Text = "}";
76 break;
77
78 case CK_LeftAngle:
79 this->Text = "<";
80 break;
81
82 case CK_RightAngle:
83 this->Text = ">";
84 break;
85
86 case CK_Comma:
87 this->Text = ", ";
88 break;
Douglas Gregor504a6ae2010-01-10 23:08:15 +000089
90 case CK_Colon:
Douglas Gregor636a61e2010-04-07 00:21:17 +000091 this->Text = ":";
Douglas Gregor504a6ae2010-01-10 23:08:15 +000092 break;
93
94 case CK_SemiColon:
95 this->Text = ";";
96 break;
97
98 case CK_Equal:
99 this->Text = " = ";
100 break;
101
102 case CK_HorizontalSpace:
103 this->Text = " ";
104 break;
105
106 case CK_VerticalSpace:
107 this->Text = "\n";
108 break;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000109 }
Douglas Gregor5bf52692009-09-22 23:15:58 +0000110}
111
112CodeCompletionString::Chunk
Douglas Gregorab6ccb52009-11-17 16:43:05 +0000113CodeCompletionString::Chunk::CreateText(StringRef Text) {
Douglas Gregor5bf52692009-09-22 23:15:58 +0000114 return Chunk(CK_Text, Text);
Douglas Gregorfedc3282009-09-18 22:15:54 +0000115}
116
117CodeCompletionString::Chunk
118CodeCompletionString::Chunk::CreateOptional(
119 std::auto_ptr<CodeCompletionString> Optional) {
120 Chunk Result;
121 Result.Kind = CK_Optional;
122 Result.Optional = Optional.release();
123 return Result;
124}
125
126CodeCompletionString::Chunk
Douglas Gregorab6ccb52009-11-17 16:43:05 +0000127CodeCompletionString::Chunk::CreatePlaceholder(StringRef Placeholder) {
Douglas Gregor5bf52692009-09-22 23:15:58 +0000128 return Chunk(CK_Placeholder, Placeholder);
129}
130
131CodeCompletionString::Chunk
Douglas Gregorab6ccb52009-11-17 16:43:05 +0000132CodeCompletionString::Chunk::CreateInformative(StringRef Informative) {
Douglas Gregor5bf52692009-09-22 23:15:58 +0000133 return Chunk(CK_Informative, Informative);
Douglas Gregorfedc3282009-09-18 22:15:54 +0000134}
135
Douglas Gregor9eb77012009-11-07 00:00:49 +0000136CodeCompletionString::Chunk
Douglas Gregorb3fa9192009-12-18 18:53:37 +0000137CodeCompletionString::Chunk::CreateResultType(StringRef ResultType) {
138 return Chunk(CK_ResultType, ResultType);
139}
140
141CodeCompletionString::Chunk
Douglas Gregor9eb77012009-11-07 00:00:49 +0000142CodeCompletionString::Chunk::CreateCurrentParameter(
Douglas Gregorab6ccb52009-11-17 16:43:05 +0000143 StringRef CurrentParameter) {
Douglas Gregor9eb77012009-11-07 00:00:49 +0000144 return Chunk(CK_CurrentParameter, CurrentParameter);
145}
146
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000147CodeCompletionString::Chunk CodeCompletionString::Chunk::Clone() const {
148 switch (Kind) {
149 case CK_TypedText:
150 case CK_Text:
151 case CK_Placeholder:
152 case CK_Informative:
Douglas Gregorb3fa9192009-12-18 18:53:37 +0000153 case CK_ResultType:
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000154 case CK_CurrentParameter:
155 case CK_LeftParen:
156 case CK_RightParen:
157 case CK_LeftBracket:
158 case CK_RightBracket:
159 case CK_LeftBrace:
160 case CK_RightBrace:
161 case CK_LeftAngle:
162 case CK_RightAngle:
163 case CK_Comma:
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000164 case CK_Colon:
165 case CK_SemiColon:
166 case CK_Equal:
167 case CK_HorizontalSpace:
168 case CK_VerticalSpace:
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000169 return Chunk(Kind, Text);
170
171 case CK_Optional: {
172 std::auto_ptr<CodeCompletionString> Opt(Optional->Clone());
173 return CreateOptional(Opt);
174 }
175 }
176
177 // Silence GCC warning.
178 return Chunk();
179}
Douglas Gregor9eb77012009-11-07 00:00:49 +0000180
Douglas Gregorfedc3282009-09-18 22:15:54 +0000181void
182CodeCompletionString::Chunk::Destroy() {
183 switch (Kind) {
Douglas Gregor5bf52692009-09-22 23:15:58 +0000184 case CK_Optional:
185 delete Optional;
186 break;
187
Douglas Gregor9eb77012009-11-07 00:00:49 +0000188 case CK_TypedText:
Douglas Gregor5bf52692009-09-22 23:15:58 +0000189 case CK_Text:
190 case CK_Placeholder:
191 case CK_Informative:
Douglas Gregorb3fa9192009-12-18 18:53:37 +0000192 case CK_ResultType:
Douglas Gregor9eb77012009-11-07 00:00:49 +0000193 case CK_CurrentParameter:
194 delete [] Text;
195 break;
196
197 case CK_LeftParen:
198 case CK_RightParen:
199 case CK_LeftBracket:
200 case CK_RightBracket:
201 case CK_LeftBrace:
202 case CK_RightBrace:
203 case CK_LeftAngle:
204 case CK_RightAngle:
205 case CK_Comma:
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000206 case CK_Colon:
207 case CK_SemiColon:
208 case CK_Equal:
209 case CK_HorizontalSpace:
210 case CK_VerticalSpace:
Douglas Gregor5bf52692009-09-22 23:15:58 +0000211 break;
Douglas Gregorfedc3282009-09-18 22:15:54 +0000212 }
213}
214
Douglas Gregora2db7932010-05-26 22:00:08 +0000215void CodeCompletionString::clear() {
Douglas Gregorfedc3282009-09-18 22:15:54 +0000216 std::for_each(Chunks.begin(), Chunks.end(),
217 std::mem_fun_ref(&Chunk::Destroy));
Douglas Gregora2db7932010-05-26 22:00:08 +0000218 Chunks.clear();
Douglas Gregorfedc3282009-09-18 22:15:54 +0000219}
220
221std::string CodeCompletionString::getAsString() const {
222 std::string Result;
223 llvm::raw_string_ostream OS(Result);
224
225 for (iterator C = begin(), CEnd = end(); C != CEnd; ++C) {
226 switch (C->Kind) {
Douglas Gregorfedc3282009-09-18 22:15:54 +0000227 case CK_Optional: OS << "{#" << C->Optional->getAsString() << "#}"; break;
Douglas Gregor5bf52692009-09-22 23:15:58 +0000228 case CK_Placeholder: OS << "<#" << C->Text << "#>"; break;
Douglas Gregorb3fa9192009-12-18 18:53:37 +0000229
230 case CK_Informative:
231 case CK_ResultType:
232 OS << "[#" << C->Text << "#]";
233 break;
234
Douglas Gregor9eb77012009-11-07 00:00:49 +0000235 case CK_CurrentParameter: OS << "<#" << C->Text << "#>"; break;
236 default: OS << C->Text; break;
Douglas Gregorfedc3282009-09-18 22:15:54 +0000237 }
238 }
Dan Gohman4888f1a2010-07-26 21:33:22 +0000239 return OS.str();
Douglas Gregorfedc3282009-09-18 22:15:54 +0000240}
241
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000242const char *CodeCompletionString::getTypedText() const {
243 for (iterator C = begin(), CEnd = end(); C != CEnd; ++C)
244 if (C->Kind == CK_TypedText)
245 return C->Text;
246
247 return 0;
248}
249
Douglas Gregor8e984da2010-08-04 16:47:14 +0000250CodeCompletionString *
251CodeCompletionString::Clone(CodeCompletionString *Result) const {
252 if (!Result)
253 Result = new CodeCompletionString;
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000254 for (iterator C = begin(), CEnd = end(); C != CEnd; ++C)
255 Result->AddChunk(C->Clone());
256 return Result;
257}
Douglas Gregor9eb77012009-11-07 00:00:49 +0000258
Douglas Gregorf09935f2009-12-01 05:55:20 +0000259static void WriteUnsigned(llvm::raw_ostream &OS, unsigned Value) {
260 OS.write((const char *)&Value, sizeof(unsigned));
261}
Douglas Gregor9eb77012009-11-07 00:00:49 +0000262
Douglas Gregorf09935f2009-12-01 05:55:20 +0000263static bool ReadUnsigned(const char *&Memory, const char *MemoryEnd,
264 unsigned &Value) {
265 if (Memory + sizeof(unsigned) > MemoryEnd)
266 return true;
267
268 memmove(&Value, Memory, sizeof(unsigned));
269 Memory += sizeof(unsigned);
270 return false;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000271}
272
273void CodeCompletionString::Serialize(llvm::raw_ostream &OS) const {
Douglas Gregorf09935f2009-12-01 05:55:20 +0000274 // Write the number of chunks.
275 WriteUnsigned(OS, size());
276
Douglas Gregor9eb77012009-11-07 00:00:49 +0000277 for (iterator C = begin(), CEnd = end(); C != CEnd; ++C) {
Douglas Gregorf09935f2009-12-01 05:55:20 +0000278 WriteUnsigned(OS, C->Kind);
279
Douglas Gregor9eb77012009-11-07 00:00:49 +0000280 switch (C->Kind) {
281 case CK_TypedText:
Douglas Gregor9eb77012009-11-07 00:00:49 +0000282 case CK_Text:
Douglas Gregor9eb77012009-11-07 00:00:49 +0000283 case CK_Placeholder:
Douglas Gregor9eb77012009-11-07 00:00:49 +0000284 case CK_Informative:
Douglas Gregorb3fa9192009-12-18 18:53:37 +0000285 case CK_ResultType:
Douglas Gregorf09935f2009-12-01 05:55:20 +0000286 case CK_CurrentParameter: {
287 const char *Text = C->Text;
288 unsigned StrLen = strlen(Text);
289 WriteUnsigned(OS, StrLen);
290 OS.write(Text, StrLen);
Douglas Gregor9eb77012009-11-07 00:00:49 +0000291 break;
Douglas Gregorf09935f2009-12-01 05:55:20 +0000292 }
293
294 case CK_Optional:
295 C->Optional->Serialize(OS);
Douglas Gregor9eb77012009-11-07 00:00:49 +0000296 break;
Douglas Gregorf09935f2009-12-01 05:55:20 +0000297
Douglas Gregor9eb77012009-11-07 00:00:49 +0000298 case CK_LeftParen:
Douglas Gregor9eb77012009-11-07 00:00:49 +0000299 case CK_RightParen:
Douglas Gregor9eb77012009-11-07 00:00:49 +0000300 case CK_LeftBracket:
Douglas Gregor9eb77012009-11-07 00:00:49 +0000301 case CK_RightBracket:
Douglas Gregor9eb77012009-11-07 00:00:49 +0000302 case CK_LeftBrace:
Douglas Gregor9eb77012009-11-07 00:00:49 +0000303 case CK_RightBrace:
Douglas Gregor9eb77012009-11-07 00:00:49 +0000304 case CK_LeftAngle:
Douglas Gregor9eb77012009-11-07 00:00:49 +0000305 case CK_RightAngle:
Douglas Gregor9eb77012009-11-07 00:00:49 +0000306 case CK_Comma:
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000307 case CK_Colon:
308 case CK_SemiColon:
309 case CK_Equal:
310 case CK_HorizontalSpace:
311 case CK_VerticalSpace:
Douglas Gregor9eb77012009-11-07 00:00:49 +0000312 break;
Douglas Gregorf09935f2009-12-01 05:55:20 +0000313 }
Douglas Gregor9eb77012009-11-07 00:00:49 +0000314 }
315}
316
Douglas Gregora2db7932010-05-26 22:00:08 +0000317bool CodeCompletionString::Deserialize(const char *&Str, const char *StrEnd) {
Douglas Gregorf09935f2009-12-01 05:55:20 +0000318 if (Str == StrEnd || *Str == 0)
Douglas Gregora2db7932010-05-26 22:00:08 +0000319 return false;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000320
Douglas Gregorf09935f2009-12-01 05:55:20 +0000321 unsigned NumBlocks;
322 if (ReadUnsigned(Str, StrEnd, NumBlocks))
Douglas Gregora2db7932010-05-26 22:00:08 +0000323 return false;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000324
Douglas Gregorf09935f2009-12-01 05:55:20 +0000325 for (unsigned I = 0; I != NumBlocks; ++I) {
326 if (Str + 1 >= StrEnd)
327 break;
328
329 // Parse the next kind.
330 unsigned KindValue;
331 if (ReadUnsigned(Str, StrEnd, KindValue))
Douglas Gregora2db7932010-05-26 22:00:08 +0000332 return false;
Douglas Gregorf09935f2009-12-01 05:55:20 +0000333
334 switch (ChunkKind Kind = (ChunkKind)KindValue) {
335 case CK_TypedText:
336 case CK_Text:
337 case CK_Placeholder:
338 case CK_Informative:
Douglas Gregorb3fa9192009-12-18 18:53:37 +0000339 case CK_ResultType:
Douglas Gregorf09935f2009-12-01 05:55:20 +0000340 case CK_CurrentParameter: {
341 unsigned StrLen;
342 if (ReadUnsigned(Str, StrEnd, StrLen) || (Str + StrLen > StrEnd))
Douglas Gregora2db7932010-05-26 22:00:08 +0000343 return false;
Douglas Gregorf09935f2009-12-01 05:55:20 +0000344
Douglas Gregora2db7932010-05-26 22:00:08 +0000345 AddChunk(Chunk(Kind, StringRef(Str, StrLen)));
Douglas Gregorf09935f2009-12-01 05:55:20 +0000346 Str += StrLen;
347 break;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000348 }
Douglas Gregorf09935f2009-12-01 05:55:20 +0000349
350 case CK_Optional: {
Douglas Gregora2db7932010-05-26 22:00:08 +0000351 std::auto_ptr<CodeCompletionString> Optional(new CodeCompletionString());
352 if (Optional->Deserialize(Str, StrEnd))
353 AddOptionalChunk(Optional);
Douglas Gregorf09935f2009-12-01 05:55:20 +0000354 break;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000355 }
Douglas Gregorf09935f2009-12-01 05:55:20 +0000356
357 case CK_LeftParen:
358 case CK_RightParen:
359 case CK_LeftBracket:
360 case CK_RightBracket:
361 case CK_LeftBrace:
362 case CK_RightBrace:
363 case CK_LeftAngle:
364 case CK_RightAngle:
365 case CK_Comma:
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000366 case CK_Colon:
367 case CK_SemiColon:
368 case CK_Equal:
369 case CK_HorizontalSpace:
370 case CK_VerticalSpace:
Douglas Gregora2db7932010-05-26 22:00:08 +0000371 AddChunk(Chunk(Kind));
Douglas Gregorf09935f2009-12-01 05:55:20 +0000372 break;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000373 }
Douglas Gregorf09935f2009-12-01 05:55:20 +0000374 };
Douglas Gregor9eb77012009-11-07 00:00:49 +0000375
Douglas Gregora2db7932010-05-26 22:00:08 +0000376 return true;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000377}
378
John McCall276321a2010-08-25 06:19:51 +0000379void CodeCompletionResult::Destroy() {
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000380 if (Kind == RK_Pattern) {
381 delete Pattern;
382 Pattern = 0;
383 }
384}
385
John McCall276321a2010-08-25 06:19:51 +0000386unsigned CodeCompletionResult::getPriorityFromDecl(NamedDecl *ND) {
Douglas Gregora2db7932010-05-26 22:00:08 +0000387 if (!ND)
388 return CCP_Unlikely;
389
390 // Context-based decisions.
Sebastian Redl50c68252010-08-31 00:36:30 +0000391 DeclContext *DC = ND->getDeclContext()->getRedeclContext();
Douglas Gregora2db7932010-05-26 22:00:08 +0000392 if (DC->isFunctionOrMethod() || isa<BlockDecl>(DC))
393 return CCP_LocalDeclaration;
394 if (DC->isRecord() || isa<ObjCContainerDecl>(DC))
395 return CCP_MemberDeclaration;
396
397 // Content-based decisions.
398 if (isa<EnumConstantDecl>(ND))
399 return CCP_Constant;
400 if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND))
401 return CCP_Type;
402 return CCP_Declaration;
403}
404
Douglas Gregorfedc3282009-09-18 22:15:54 +0000405//===----------------------------------------------------------------------===//
Douglas Gregor05f477c2009-09-23 00:16:58 +0000406// Code completion overload candidate implementation
407//===----------------------------------------------------------------------===//
408FunctionDecl *
409CodeCompleteConsumer::OverloadCandidate::getFunction() const {
410 if (getKind() == CK_Function)
411 return Function;
412 else if (getKind() == CK_FunctionTemplate)
413 return FunctionTemplate->getTemplatedDecl();
414 else
415 return 0;
416}
417
418const FunctionType *
419CodeCompleteConsumer::OverloadCandidate::getFunctionType() const {
420 switch (Kind) {
421 case CK_Function:
422 return Function->getType()->getAs<FunctionType>();
423
424 case CK_FunctionTemplate:
425 return FunctionTemplate->getTemplatedDecl()->getType()
426 ->getAs<FunctionType>();
427
428 case CK_FunctionType:
429 return Type;
430 }
431
432 return 0;
433}
434
435//===----------------------------------------------------------------------===//
Douglas Gregorfedc3282009-09-18 22:15:54 +0000436// Code completion consumer implementation
437//===----------------------------------------------------------------------===//
438
Douglas Gregor3545ff42009-09-21 16:56:56 +0000439CodeCompleteConsumer::~CodeCompleteConsumer() { }
Douglas Gregorfedc3282009-09-18 22:15:54 +0000440
Douglas Gregor2436e712009-09-17 21:32:03 +0000441void
Daniel Dunbar242ea9a2009-11-13 08:58:20 +0000442PrintingCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &SemaRef,
Douglas Gregor00c37ef2010-08-11 21:23:17 +0000443 CodeCompletionContext Context,
John McCall276321a2010-08-25 06:19:51 +0000444 CodeCompletionResult *Results,
Douglas Gregor2436e712009-09-17 21:32:03 +0000445 unsigned NumResults) {
Douglas Gregor49f67ce2010-08-26 13:48:20 +0000446 std::stable_sort(Results, Results + NumResults);
447
Douglas Gregor2436e712009-09-17 21:32:03 +0000448 // Print the results.
449 for (unsigned I = 0; I != NumResults; ++I) {
Douglas Gregor58acf322009-10-09 22:16:47 +0000450 OS << "COMPLETION: ";
Douglas Gregor2436e712009-09-17 21:32:03 +0000451 switch (Results[I].Kind) {
John McCall276321a2010-08-25 06:19:51 +0000452 case CodeCompletionResult::RK_Declaration:
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000453 OS << Results[I].Declaration;
Douglas Gregor2436e712009-09-17 21:32:03 +0000454 if (Results[I].Hidden)
455 OS << " (Hidden)";
Douglas Gregor3545ff42009-09-21 16:56:56 +0000456 if (CodeCompletionString *CCS
457 = Results[I].CreateCodeCompletionString(SemaRef)) {
Douglas Gregorfedc3282009-09-18 22:15:54 +0000458 OS << " : " << CCS->getAsString();
459 delete CCS;
460 }
461
Douglas Gregor2436e712009-09-17 21:32:03 +0000462 OS << '\n';
463 break;
464
John McCall276321a2010-08-25 06:19:51 +0000465 case CodeCompletionResult::RK_Keyword:
Douglas Gregor52ce62f2010-01-13 23:24:38 +0000466 OS << Results[I].Keyword << '\n';
Douglas Gregor2436e712009-09-17 21:32:03 +0000467 break;
Douglas Gregorf329c7c2009-10-30 16:50:04 +0000468
John McCall276321a2010-08-25 06:19:51 +0000469 case CodeCompletionResult::RK_Macro: {
Douglas Gregor52ce62f2010-01-13 23:24:38 +0000470 OS << Results[I].Macro->getName();
Douglas Gregorf329c7c2009-10-30 16:50:04 +0000471 if (CodeCompletionString *CCS
Douglas Gregor52ce62f2010-01-13 23:24:38 +0000472 = Results[I].CreateCodeCompletionString(SemaRef)) {
Douglas Gregorf329c7c2009-10-30 16:50:04 +0000473 OS << " : " << CCS->getAsString();
474 delete CCS;
475 }
476 OS << '\n';
477 break;
478 }
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000479
John McCall276321a2010-08-25 06:19:51 +0000480 case CodeCompletionResult::RK_Pattern: {
Douglas Gregor52ce62f2010-01-13 23:24:38 +0000481 OS << "Pattern : "
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000482 << Results[I].Pattern->getAsString() << '\n';
483 break;
484 }
Douglas Gregor2436e712009-09-17 21:32:03 +0000485 }
486 }
Douglas Gregor2436e712009-09-17 21:32:03 +0000487}
Douglas Gregor05f477c2009-09-23 00:16:58 +0000488
489void
Daniel Dunbar242ea9a2009-11-13 08:58:20 +0000490PrintingCodeCompleteConsumer::ProcessOverloadCandidates(Sema &SemaRef,
491 unsigned CurrentArg,
Douglas Gregor05f477c2009-09-23 00:16:58 +0000492 OverloadCandidate *Candidates,
493 unsigned NumCandidates) {
494 for (unsigned I = 0; I != NumCandidates; ++I) {
Douglas Gregorf0f51982009-09-23 00:34:09 +0000495 if (CodeCompletionString *CCS
496 = Candidates[I].CreateSignatureString(CurrentArg, SemaRef)) {
Douglas Gregor58acf322009-10-09 22:16:47 +0000497 OS << "OVERLOAD: " << CCS->getAsString() << "\n";
Douglas Gregorf0f51982009-09-23 00:34:09 +0000498 delete CCS;
Douglas Gregor05f477c2009-09-23 00:16:58 +0000499 }
500 }
Douglas Gregor05f477c2009-09-23 00:16:58 +0000501}
Douglas Gregor9eb77012009-11-07 00:00:49 +0000502
John McCall276321a2010-08-25 06:19:51 +0000503void CodeCompletionResult::computeCursorKindAndAvailability() {
Douglas Gregorb14904c2010-08-13 22:48:40 +0000504 switch (Kind) {
505 case RK_Declaration:
Douglas Gregorf757a122010-08-23 23:00:57 +0000506 // Set the availability based on attributes.
507 Availability = CXAvailability_Available;
508 if (Declaration->getAttr<UnavailableAttr>())
509 Availability = CXAvailability_NotAvailable;
510 else if (Declaration->getAttr<DeprecatedAttr>())
511 Availability = CXAvailability_Deprecated;
512
Douglas Gregor09c0eb12010-09-03 23:30:36 +0000513 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Declaration))
514 if (Function->isDeleted())
Douglas Gregorf757a122010-08-23 23:00:57 +0000515 Availability = CXAvailability_NotAvailable;
Douglas Gregor09c0eb12010-09-03 23:30:36 +0000516
517 CursorKind = getCursorKindForDecl(Declaration);
518 if (CursorKind == CXCursor_UnexposedDecl)
Douglas Gregorb14904c2010-08-13 22:48:40 +0000519 CursorKind = CXCursor_NotImplemented;
Douglas Gregorb14904c2010-08-13 22:48:40 +0000520 break;
521
John McCall276321a2010-08-25 06:19:51 +0000522 case RK_Macro:
Douglas Gregorf757a122010-08-23 23:00:57 +0000523 Availability = CXAvailability_Available;
Douglas Gregorb14904c2010-08-13 22:48:40 +0000524 CursorKind = CXCursor_MacroDefinition;
525 break;
526
John McCall276321a2010-08-25 06:19:51 +0000527 case RK_Keyword:
Douglas Gregorf757a122010-08-23 23:00:57 +0000528 Availability = CXAvailability_Available;
Douglas Gregorb14904c2010-08-13 22:48:40 +0000529 CursorKind = CXCursor_NotImplemented;
530 break;
531
John McCall276321a2010-08-25 06:19:51 +0000532 case RK_Pattern:
Douglas Gregorb14904c2010-08-13 22:48:40 +0000533 // Do nothing: Patterns can come with cursor kinds!
534 break;
Douglas Gregor8e984da2010-08-04 16:47:14 +0000535 }
536}
Douglas Gregorf09935f2009-12-01 05:55:20 +0000537
Douglas Gregor0de55ce2010-08-25 18:41:16 +0000538/// \brief Retrieve the name that should be used to order a result.
539///
540/// If the name needs to be constructed as a string, that string will be
541/// saved into Saved and the returned StringRef will refer to it.
542static llvm::StringRef getOrderedName(const CodeCompletionResult &R,
543 std::string &Saved) {
544 switch (R.Kind) {
545 case CodeCompletionResult::RK_Keyword:
546 return R.Keyword;
547
548 case CodeCompletionResult::RK_Pattern:
549 return R.Pattern->getTypedText();
550
551 case CodeCompletionResult::RK_Macro:
552 return R.Macro->getName();
553
554 case CodeCompletionResult::RK_Declaration:
555 // Handle declarations below.
556 break;
557 }
558
559 DeclarationName Name = R.Declaration->getDeclName();
560
561 // If the name is a simple identifier (by far the common case), or a
562 // zero-argument selector, just return a reference to that identifier.
563 if (IdentifierInfo *Id = Name.getAsIdentifierInfo())
564 return Id->getName();
565 if (Name.isObjCZeroArgSelector())
566 if (IdentifierInfo *Id
567 = Name.getObjCSelector().getIdentifierInfoForSlot(0))
568 return Id->getName();
569
570 Saved = Name.getAsString();
571 return Saved;
572}
573
574bool clang::operator<(const CodeCompletionResult &X,
575 const CodeCompletionResult &Y) {
576 std::string XSaved, YSaved;
577 llvm::StringRef XStr = getOrderedName(X, XSaved);
578 llvm::StringRef YStr = getOrderedName(Y, YSaved);
579 int cmp = XStr.compare_lower(YStr);
580 if (cmp)
581 return cmp < 0;
582
Douglas Gregor49f67ce2010-08-26 13:48:20 +0000583 // If case-insensitive comparison fails, try case-sensitive comparison.
584 cmp = XStr.compare(YStr);
585 if (cmp)
586 return cmp < 0;
Douglas Gregor0de55ce2010-08-25 18:41:16 +0000587
588 return false;
589}
590
Douglas Gregor8e984da2010-08-04 16:47:14 +0000591void
592CIndexCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &SemaRef,
Douglas Gregor00c37ef2010-08-11 21:23:17 +0000593 CodeCompletionContext Context,
John McCall276321a2010-08-25 06:19:51 +0000594 CodeCompletionResult *Results,
Douglas Gregor8e984da2010-08-04 16:47:14 +0000595 unsigned NumResults) {
596 // Print the results.
597 for (unsigned I = 0; I != NumResults; ++I) {
Douglas Gregorb14904c2010-08-13 22:48:40 +0000598 WriteUnsigned(OS, Results[I].CursorKind);
Douglas Gregora2db7932010-05-26 22:00:08 +0000599 WriteUnsigned(OS, Results[I].Priority);
Douglas Gregorf757a122010-08-23 23:00:57 +0000600 WriteUnsigned(OS, Results[I].Availability);
Douglas Gregorf09935f2009-12-01 05:55:20 +0000601 CodeCompletionString *CCS = Results[I].CreateCodeCompletionString(SemaRef);
602 assert(CCS && "No code-completion string?");
603 CCS->Serialize(OS);
604 delete CCS;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000605 }
Douglas Gregor9eb77012009-11-07 00:00:49 +0000606}
607
608void
Daniel Dunbar242ea9a2009-11-13 08:58:20 +0000609CIndexCodeCompleteConsumer::ProcessOverloadCandidates(Sema &SemaRef,
610 unsigned CurrentArg,
Douglas Gregor9eb77012009-11-07 00:00:49 +0000611 OverloadCandidate *Candidates,
612 unsigned NumCandidates) {
613 for (unsigned I = 0; I != NumCandidates; ++I) {
Douglas Gregorf09935f2009-12-01 05:55:20 +0000614 WriteUnsigned(OS, CXCursor_NotImplemented);
Douglas Gregor49f67ce2010-08-26 13:48:20 +0000615 WriteUnsigned(OS, /*Priority=*/I);
Douglas Gregorf757a122010-08-23 23:00:57 +0000616 WriteUnsigned(OS, /*Availability=*/CXAvailability_Available);
Douglas Gregorf09935f2009-12-01 05:55:20 +0000617 CodeCompletionString *CCS
618 = Candidates[I].CreateSignatureString(CurrentArg, SemaRef);
619 assert(CCS && "No code-completion string?");
620 CCS->Serialize(OS);
621 delete CCS;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000622 }
Douglas Gregor9eb77012009-11-07 00:00:49 +0000623}