blob: 9f0ee5cde737129574a3ef0542785d3151af1fc6 [file] [log] [blame]
Douglas Gregor86d9a522009-09-21 16:56:56 +00001//===--- CodeCompleteConsumer.cpp - Code Completion Interface ---*- C++ -*-===//
Douglas Gregor81b747b2009-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"
Douglas Gregor75b71282009-09-18 17:54:00 +000014#include "clang/AST/DeclCXX.h"
Douglas Gregor374929f2009-09-18 15:37:17 +000015#include "clang/Parse/Scope.h"
Douglas Gregor81b747b2009-09-17 21:32:03 +000016#include "clang/Lex/Preprocessor.h"
Douglas Gregor2b4074f2009-12-01 05:55:20 +000017#include "clang-c/Index.h"
Douglas Gregor81b747b2009-09-17 21:32:03 +000018#include "Sema.h"
19#include "llvm/ADT/STLExtras.h"
Douglas Gregor81b747b2009-09-17 21:32:03 +000020#include "llvm/Support/raw_ostream.h"
21#include <algorithm>
Douglas Gregore6e03612009-09-18 22:15:54 +000022#include <cstring>
23#include <functional>
Douglas Gregor92eff462009-11-17 16:43:05 +000024
Douglas Gregor81b747b2009-09-17 21:32:03 +000025using namespace clang;
Douglas Gregor92eff462009-11-17 16:43:05 +000026using llvm::StringRef;
Douglas Gregor81b747b2009-09-17 21:32:03 +000027
Douglas Gregore6e03612009-09-18 22:15:54 +000028//===----------------------------------------------------------------------===//
29// Code completion string implementation
30//===----------------------------------------------------------------------===//
Douglas Gregor0c8296d2009-11-07 00:00:49 +000031CodeCompletionString::Chunk::Chunk(ChunkKind Kind, llvm::StringRef Text)
Daniel Dunbarad5757f2009-11-12 18:40:12 +000032 : Kind(Kind), Text("")
Douglas Gregor0563c262009-09-22 23:15:58 +000033{
Douglas Gregor0c8296d2009-11-07 00:00:49 +000034 switch (Kind) {
35 case CK_TypedText:
36 case CK_Text:
37 case CK_Placeholder:
38 case CK_Informative:
Douglas Gregorff5ce6e2009-12-18 18:53:37 +000039 case CK_ResultType:
Douglas Gregor0c8296d2009-11-07 00:00:49 +000040 case CK_CurrentParameter: {
41 char *New = new char [Text.size() + 1];
42 std::memcpy(New, Text.data(), Text.size());
43 New[Text.size()] = '\0';
44 this->Text = New;
45 break;
46 }
47
48 case CK_Optional:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +000049 llvm_unreachable("Optional strings cannot be created from text");
Douglas Gregor0c8296d2009-11-07 00:00:49 +000050 break;
51
52 case CK_LeftParen:
53 this->Text = "(";
54 break;
55
56 case CK_RightParen:
57 this->Text = ")";
58 break;
59
60 case CK_LeftBracket:
61 this->Text = "[";
62 break;
63
64 case CK_RightBracket:
65 this->Text = "]";
66 break;
67
68 case CK_LeftBrace:
69 this->Text = "{";
70 break;
71
72 case CK_RightBrace:
73 this->Text = "}";
74 break;
75
76 case CK_LeftAngle:
77 this->Text = "<";
78 break;
79
80 case CK_RightAngle:
81 this->Text = ">";
82 break;
83
84 case CK_Comma:
85 this->Text = ", ";
86 break;
Douglas Gregor01dfea02010-01-10 23:08:15 +000087
88 case CK_Colon:
Douglas Gregore8f5a172010-04-07 00:21:17 +000089 this->Text = ":";
Douglas Gregor01dfea02010-01-10 23:08:15 +000090 break;
91
92 case CK_SemiColon:
93 this->Text = ";";
94 break;
95
96 case CK_Equal:
97 this->Text = " = ";
98 break;
99
100 case CK_HorizontalSpace:
101 this->Text = " ";
102 break;
103
104 case CK_VerticalSpace:
105 this->Text = "\n";
106 break;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000107 }
Douglas Gregor0563c262009-09-22 23:15:58 +0000108}
109
110CodeCompletionString::Chunk
Douglas Gregor92eff462009-11-17 16:43:05 +0000111CodeCompletionString::Chunk::CreateText(StringRef Text) {
Douglas Gregor0563c262009-09-22 23:15:58 +0000112 return Chunk(CK_Text, Text);
Douglas Gregore6e03612009-09-18 22:15:54 +0000113}
114
115CodeCompletionString::Chunk
116CodeCompletionString::Chunk::CreateOptional(
117 std::auto_ptr<CodeCompletionString> Optional) {
118 Chunk Result;
119 Result.Kind = CK_Optional;
120 Result.Optional = Optional.release();
121 return Result;
122}
123
124CodeCompletionString::Chunk
Douglas Gregor92eff462009-11-17 16:43:05 +0000125CodeCompletionString::Chunk::CreatePlaceholder(StringRef Placeholder) {
Douglas Gregor0563c262009-09-22 23:15:58 +0000126 return Chunk(CK_Placeholder, Placeholder);
127}
128
129CodeCompletionString::Chunk
Douglas Gregor92eff462009-11-17 16:43:05 +0000130CodeCompletionString::Chunk::CreateInformative(StringRef Informative) {
Douglas Gregor0563c262009-09-22 23:15:58 +0000131 return Chunk(CK_Informative, Informative);
Douglas Gregore6e03612009-09-18 22:15:54 +0000132}
133
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000134CodeCompletionString::Chunk
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000135CodeCompletionString::Chunk::CreateResultType(StringRef ResultType) {
136 return Chunk(CK_ResultType, ResultType);
137}
138
139CodeCompletionString::Chunk
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000140CodeCompletionString::Chunk::CreateCurrentParameter(
Douglas Gregor92eff462009-11-17 16:43:05 +0000141 StringRef CurrentParameter) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000142 return Chunk(CK_CurrentParameter, CurrentParameter);
143}
144
Douglas Gregor54f01612009-11-19 00:01:57 +0000145CodeCompletionString::Chunk CodeCompletionString::Chunk::Clone() const {
146 switch (Kind) {
147 case CK_TypedText:
148 case CK_Text:
149 case CK_Placeholder:
150 case CK_Informative:
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000151 case CK_ResultType:
Douglas Gregor54f01612009-11-19 00:01:57 +0000152 case CK_CurrentParameter:
153 case CK_LeftParen:
154 case CK_RightParen:
155 case CK_LeftBracket:
156 case CK_RightBracket:
157 case CK_LeftBrace:
158 case CK_RightBrace:
159 case CK_LeftAngle:
160 case CK_RightAngle:
161 case CK_Comma:
Douglas Gregor01dfea02010-01-10 23:08:15 +0000162 case CK_Colon:
163 case CK_SemiColon:
164 case CK_Equal:
165 case CK_HorizontalSpace:
166 case CK_VerticalSpace:
Douglas Gregor54f01612009-11-19 00:01:57 +0000167 return Chunk(Kind, Text);
168
169 case CK_Optional: {
170 std::auto_ptr<CodeCompletionString> Opt(Optional->Clone());
171 return CreateOptional(Opt);
172 }
173 }
174
175 // Silence GCC warning.
176 return Chunk();
177}
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000178
Douglas Gregore6e03612009-09-18 22:15:54 +0000179void
180CodeCompletionString::Chunk::Destroy() {
181 switch (Kind) {
Douglas Gregor0563c262009-09-22 23:15:58 +0000182 case CK_Optional:
183 delete Optional;
184 break;
185
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000186 case CK_TypedText:
Douglas Gregor0563c262009-09-22 23:15:58 +0000187 case CK_Text:
188 case CK_Placeholder:
189 case CK_Informative:
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000190 case CK_ResultType:
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000191 case CK_CurrentParameter:
192 delete [] Text;
193 break;
194
195 case CK_LeftParen:
196 case CK_RightParen:
197 case CK_LeftBracket:
198 case CK_RightBracket:
199 case CK_LeftBrace:
200 case CK_RightBrace:
201 case CK_LeftAngle:
202 case CK_RightAngle:
203 case CK_Comma:
Douglas Gregor01dfea02010-01-10 23:08:15 +0000204 case CK_Colon:
205 case CK_SemiColon:
206 case CK_Equal:
207 case CK_HorizontalSpace:
208 case CK_VerticalSpace:
Douglas Gregor0563c262009-09-22 23:15:58 +0000209 break;
Douglas Gregore6e03612009-09-18 22:15:54 +0000210 }
211}
212
213CodeCompletionString::~CodeCompletionString() {
214 std::for_each(Chunks.begin(), Chunks.end(),
215 std::mem_fun_ref(&Chunk::Destroy));
216}
217
218std::string CodeCompletionString::getAsString() const {
219 std::string Result;
220 llvm::raw_string_ostream OS(Result);
221
222 for (iterator C = begin(), CEnd = end(); C != CEnd; ++C) {
223 switch (C->Kind) {
Douglas Gregore6e03612009-09-18 22:15:54 +0000224 case CK_Optional: OS << "{#" << C->Optional->getAsString() << "#}"; break;
Douglas Gregor0563c262009-09-22 23:15:58 +0000225 case CK_Placeholder: OS << "<#" << C->Text << "#>"; break;
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000226
227 case CK_Informative:
228 case CK_ResultType:
229 OS << "[#" << C->Text << "#]";
230 break;
231
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000232 case CK_CurrentParameter: OS << "<#" << C->Text << "#>"; break;
233 default: OS << C->Text; break;
Douglas Gregore6e03612009-09-18 22:15:54 +0000234 }
235 }
Douglas Gregore2b7eea2009-09-29 15:13:39 +0000236 OS.flush();
Douglas Gregore6e03612009-09-18 22:15:54 +0000237 return Result;
238}
239
Douglas Gregor54f01612009-11-19 00:01:57 +0000240const char *CodeCompletionString::getTypedText() const {
241 for (iterator C = begin(), CEnd = end(); C != CEnd; ++C)
242 if (C->Kind == CK_TypedText)
243 return C->Text;
244
245 return 0;
246}
247
248CodeCompletionString *CodeCompletionString::Clone() const {
249 CodeCompletionString *Result = new CodeCompletionString;
250 for (iterator C = begin(), CEnd = end(); C != CEnd; ++C)
251 Result->AddChunk(C->Clone());
252 return Result;
253}
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000254
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000255static void WriteUnsigned(llvm::raw_ostream &OS, unsigned Value) {
256 OS.write((const char *)&Value, sizeof(unsigned));
257}
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000258
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000259static bool ReadUnsigned(const char *&Memory, const char *MemoryEnd,
260 unsigned &Value) {
261 if (Memory + sizeof(unsigned) > MemoryEnd)
262 return true;
263
264 memmove(&Value, Memory, sizeof(unsigned));
265 Memory += sizeof(unsigned);
266 return false;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000267}
268
269void CodeCompletionString::Serialize(llvm::raw_ostream &OS) const {
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000270 // Write the number of chunks.
271 WriteUnsigned(OS, size());
272
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000273 for (iterator C = begin(), CEnd = end(); C != CEnd; ++C) {
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000274 WriteUnsigned(OS, C->Kind);
275
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000276 switch (C->Kind) {
277 case CK_TypedText:
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000278 case CK_Text:
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000279 case CK_Placeholder:
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000280 case CK_Informative:
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000281 case CK_ResultType:
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000282 case CK_CurrentParameter: {
283 const char *Text = C->Text;
284 unsigned StrLen = strlen(Text);
285 WriteUnsigned(OS, StrLen);
286 OS.write(Text, StrLen);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000287 break;
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000288 }
289
290 case CK_Optional:
291 C->Optional->Serialize(OS);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000292 break;
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000293
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000294 case CK_LeftParen:
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000295 case CK_RightParen:
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000296 case CK_LeftBracket:
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000297 case CK_RightBracket:
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000298 case CK_LeftBrace:
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000299 case CK_RightBrace:
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000300 case CK_LeftAngle:
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000301 case CK_RightAngle:
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000302 case CK_Comma:
Douglas Gregor01dfea02010-01-10 23:08:15 +0000303 case CK_Colon:
304 case CK_SemiColon:
305 case CK_Equal:
306 case CK_HorizontalSpace:
307 case CK_VerticalSpace:
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000308 break;
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000309 }
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000310 }
311}
312
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000313CodeCompletionString *CodeCompletionString::Deserialize(const char *&Str,
314 const char *StrEnd) {
315 if (Str == StrEnd || *Str == 0)
316 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000317
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000318 CodeCompletionString *Result = new CodeCompletionString;
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000319 unsigned NumBlocks;
320 if (ReadUnsigned(Str, StrEnd, NumBlocks))
321 return Result;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000322
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000323 for (unsigned I = 0; I != NumBlocks; ++I) {
324 if (Str + 1 >= StrEnd)
325 break;
326
327 // Parse the next kind.
328 unsigned KindValue;
329 if (ReadUnsigned(Str, StrEnd, KindValue))
330 return Result;
331
332 switch (ChunkKind Kind = (ChunkKind)KindValue) {
333 case CK_TypedText:
334 case CK_Text:
335 case CK_Placeholder:
336 case CK_Informative:
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000337 case CK_ResultType:
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000338 case CK_CurrentParameter: {
339 unsigned StrLen;
340 if (ReadUnsigned(Str, StrEnd, StrLen) || (Str + StrLen > StrEnd))
341 return Result;
342
343 Result->AddChunk(Chunk(Kind, StringRef(Str, StrLen)));
344 Str += StrLen;
345 break;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000346 }
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000347
348 case CK_Optional: {
349 std::auto_ptr<CodeCompletionString> Optional(Deserialize(Str, StrEnd));
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000350 Result->AddOptionalChunk(Optional);
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000351 break;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000352 }
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000353
354 case CK_LeftParen:
355 case CK_RightParen:
356 case CK_LeftBracket:
357 case CK_RightBracket:
358 case CK_LeftBrace:
359 case CK_RightBrace:
360 case CK_LeftAngle:
361 case CK_RightAngle:
362 case CK_Comma:
Douglas Gregor01dfea02010-01-10 23:08:15 +0000363 case CK_Colon:
364 case CK_SemiColon:
365 case CK_Equal:
366 case CK_HorizontalSpace:
367 case CK_VerticalSpace:
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000368 Result->AddChunk(Chunk(Kind));
369 break;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000370 }
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000371 };
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000372
373 return Result;
374}
375
Douglas Gregor54f01612009-11-19 00:01:57 +0000376void CodeCompleteConsumer::Result::Destroy() {
377 if (Kind == RK_Pattern) {
378 delete Pattern;
379 Pattern = 0;
380 }
381}
382
Douglas Gregore6e03612009-09-18 22:15:54 +0000383//===----------------------------------------------------------------------===//
Douglas Gregor05944382009-09-23 00:16:58 +0000384// Code completion overload candidate implementation
385//===----------------------------------------------------------------------===//
386FunctionDecl *
387CodeCompleteConsumer::OverloadCandidate::getFunction() const {
388 if (getKind() == CK_Function)
389 return Function;
390 else if (getKind() == CK_FunctionTemplate)
391 return FunctionTemplate->getTemplatedDecl();
392 else
393 return 0;
394}
395
396const FunctionType *
397CodeCompleteConsumer::OverloadCandidate::getFunctionType() const {
398 switch (Kind) {
399 case CK_Function:
400 return Function->getType()->getAs<FunctionType>();
401
402 case CK_FunctionTemplate:
403 return FunctionTemplate->getTemplatedDecl()->getType()
404 ->getAs<FunctionType>();
405
406 case CK_FunctionType:
407 return Type;
408 }
409
410 return 0;
411}
412
413//===----------------------------------------------------------------------===//
Douglas Gregore6e03612009-09-18 22:15:54 +0000414// Code completion consumer implementation
415//===----------------------------------------------------------------------===//
416
Douglas Gregor86d9a522009-09-21 16:56:56 +0000417CodeCompleteConsumer::~CodeCompleteConsumer() { }
Douglas Gregore6e03612009-09-18 22:15:54 +0000418
Douglas Gregor81b747b2009-09-17 21:32:03 +0000419void
Daniel Dunbar3a2838d2009-11-13 08:58:20 +0000420PrintingCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &SemaRef,
421 Result *Results,
Douglas Gregor81b747b2009-09-17 21:32:03 +0000422 unsigned NumResults) {
Douglas Gregor81b747b2009-09-17 21:32:03 +0000423 // Print the results.
424 for (unsigned I = 0; I != NumResults; ++I) {
Douglas Gregorf52cede2009-10-09 22:16:47 +0000425 OS << "COMPLETION: ";
Douglas Gregor81b747b2009-09-17 21:32:03 +0000426 switch (Results[I].Kind) {
427 case Result::RK_Declaration:
Douglas Gregorab0b4f12010-01-13 23:24:38 +0000428 OS << Results[I].Declaration->getNameAsString() ;
Douglas Gregor81b747b2009-09-17 21:32:03 +0000429 if (Results[I].Hidden)
430 OS << " (Hidden)";
Douglas Gregor86d9a522009-09-21 16:56:56 +0000431 if (CodeCompletionString *CCS
432 = Results[I].CreateCodeCompletionString(SemaRef)) {
Douglas Gregore6e03612009-09-18 22:15:54 +0000433 OS << " : " << CCS->getAsString();
434 delete CCS;
435 }
436
Douglas Gregor81b747b2009-09-17 21:32:03 +0000437 OS << '\n';
438 break;
439
440 case Result::RK_Keyword:
Douglas Gregorab0b4f12010-01-13 23:24:38 +0000441 OS << Results[I].Keyword << '\n';
Douglas Gregor81b747b2009-09-17 21:32:03 +0000442 break;
Douglas Gregor3f7c7f42009-10-30 16:50:04 +0000443
444 case Result::RK_Macro: {
Douglas Gregorab0b4f12010-01-13 23:24:38 +0000445 OS << Results[I].Macro->getName();
Douglas Gregor3f7c7f42009-10-30 16:50:04 +0000446 if (CodeCompletionString *CCS
Douglas Gregorab0b4f12010-01-13 23:24:38 +0000447 = Results[I].CreateCodeCompletionString(SemaRef)) {
Douglas Gregor3f7c7f42009-10-30 16:50:04 +0000448 OS << " : " << CCS->getAsString();
449 delete CCS;
450 }
451 OS << '\n';
452 break;
453 }
Douglas Gregor54f01612009-11-19 00:01:57 +0000454
455 case Result::RK_Pattern: {
Douglas Gregorab0b4f12010-01-13 23:24:38 +0000456 OS << "Pattern : "
Douglas Gregor54f01612009-11-19 00:01:57 +0000457 << Results[I].Pattern->getAsString() << '\n';
458 break;
459 }
Douglas Gregor81b747b2009-09-17 21:32:03 +0000460 }
461 }
462
463 // Once we've printed the code-completion results, suppress remaining
464 // diagnostics.
465 // FIXME: Move this somewhere else!
Douglas Gregor86d9a522009-09-21 16:56:56 +0000466 SemaRef.PP.getDiagnostics().setSuppressAllDiagnostics();
Douglas Gregor81b747b2009-09-17 21:32:03 +0000467}
Douglas Gregor05944382009-09-23 00:16:58 +0000468
469void
Daniel Dunbar3a2838d2009-11-13 08:58:20 +0000470PrintingCodeCompleteConsumer::ProcessOverloadCandidates(Sema &SemaRef,
471 unsigned CurrentArg,
Douglas Gregor05944382009-09-23 00:16:58 +0000472 OverloadCandidate *Candidates,
473 unsigned NumCandidates) {
474 for (unsigned I = 0; I != NumCandidates; ++I) {
Douglas Gregor86d802e2009-09-23 00:34:09 +0000475 if (CodeCompletionString *CCS
476 = Candidates[I].CreateSignatureString(CurrentArg, SemaRef)) {
Douglas Gregorf52cede2009-10-09 22:16:47 +0000477 OS << "OVERLOAD: " << CCS->getAsString() << "\n";
Douglas Gregor86d802e2009-09-23 00:34:09 +0000478 delete CCS;
Douglas Gregor05944382009-09-23 00:16:58 +0000479 }
480 }
481
482 // Once we've printed the code-completion results, suppress remaining
483 // diagnostics.
484 // FIXME: Move this somewhere else!
485 SemaRef.PP.getDiagnostics().setSuppressAllDiagnostics();
486}
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000487
488void
Daniel Dunbar3a2838d2009-11-13 08:58:20 +0000489CIndexCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &SemaRef,
490 Result *Results,
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000491 unsigned NumResults) {
492 // Print the results.
493 for (unsigned I = 0; I != NumResults; ++I) {
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000494 CXCursorKind Kind = CXCursor_NotImplemented;
495
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000496 switch (Results[I].Kind) {
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000497 case Result::RK_Declaration:
498 switch (Results[I].Declaration->getKind()) {
499 case Decl::Record:
500 case Decl::CXXRecord:
501 case Decl::ClassTemplateSpecialization: {
502 RecordDecl *Record = cast<RecordDecl>(Results[I].Declaration);
503 if (Record->isStruct())
504 Kind = CXCursor_StructDecl;
505 else if (Record->isUnion())
506 Kind = CXCursor_UnionDecl;
507 else
508 Kind = CXCursor_ClassDecl;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000509 break;
510 }
Douglas Gregor54f01612009-11-19 00:01:57 +0000511
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000512 case Decl::ObjCMethod: {
513 ObjCMethodDecl *Method = cast<ObjCMethodDecl>(Results[I].Declaration);
514 if (Method->isInstanceMethod())
515 Kind = CXCursor_ObjCInstanceMethodDecl;
516 else
517 Kind = CXCursor_ObjCClassMethodDecl;
Douglas Gregor54f01612009-11-19 00:01:57 +0000518 break;
519 }
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000520
521 case Decl::Typedef:
522 Kind = CXCursor_TypedefDecl;
523 break;
524
525 case Decl::Enum:
526 Kind = CXCursor_EnumDecl;
527 break;
528
529 case Decl::Field:
530 Kind = CXCursor_FieldDecl;
531 break;
532
533 case Decl::EnumConstant:
534 Kind = CXCursor_EnumConstantDecl;
535 break;
536
537 case Decl::Function:
538 case Decl::CXXMethod:
539 case Decl::CXXConstructor:
540 case Decl::CXXDestructor:
541 case Decl::CXXConversion:
542 Kind = CXCursor_FunctionDecl;
543 break;
544
545 case Decl::Var:
546 Kind = CXCursor_VarDecl;
547 break;
548
549 case Decl::ParmVar:
550 Kind = CXCursor_ParmDecl;
551 break;
552
553 case Decl::ObjCInterface:
554 Kind = CXCursor_ObjCInterfaceDecl;
555 break;
556
557 case Decl::ObjCCategory:
558 Kind = CXCursor_ObjCCategoryDecl;
559 break;
560
561 case Decl::ObjCProtocol:
562 Kind = CXCursor_ObjCProtocolDecl;
563 break;
564
565 case Decl::ObjCProperty:
566 Kind = CXCursor_ObjCPropertyDecl;
567 break;
568
569 case Decl::ObjCIvar:
570 Kind = CXCursor_ObjCIvarDecl;
571 break;
572
573 case Decl::ObjCImplementation:
Douglas Gregorb6998662010-01-19 19:34:47 +0000574 Kind = CXCursor_ObjCImplementationDecl;
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000575 break;
576
577 case Decl::ObjCCategoryImpl:
Douglas Gregorb6998662010-01-19 19:34:47 +0000578 Kind = CXCursor_ObjCCategoryImplDecl;
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000579 break;
580
581 default:
582 break;
583 }
584 break;
Douglas Gregor09d9fa12010-04-05 16:10:30 +0000585
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000586 case Result::RK_Macro:
Douglas Gregor09d9fa12010-04-05 16:10:30 +0000587 Kind = CXCursor_MacroDefinition;
588 break;
589
590 case Result::RK_Keyword:
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000591 case Result::RK_Pattern:
592 Kind = CXCursor_NotImplemented;
593 break;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000594 }
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000595
596 WriteUnsigned(OS, Kind);
597 CodeCompletionString *CCS = Results[I].CreateCodeCompletionString(SemaRef);
598 assert(CCS && "No code-completion string?");
599 CCS->Serialize(OS);
600 delete CCS;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000601 }
602
603 // Once we've printed the code-completion results, suppress remaining
604 // diagnostics.
605 // FIXME: Move this somewhere else!
606 SemaRef.PP.getDiagnostics().setSuppressAllDiagnostics();
607}
608
609void
Daniel Dunbar3a2838d2009-11-13 08:58:20 +0000610CIndexCodeCompleteConsumer::ProcessOverloadCandidates(Sema &SemaRef,
611 unsigned CurrentArg,
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000612 OverloadCandidate *Candidates,
613 unsigned NumCandidates) {
614 for (unsigned I = 0; I != NumCandidates; ++I) {
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000615 WriteUnsigned(OS, CXCursor_NotImplemented);
616 CodeCompletionString *CCS
617 = Candidates[I].CreateSignatureString(CurrentArg, SemaRef);
618 assert(CCS && "No code-completion string?");
619 CCS->Serialize(OS);
620 delete CCS;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000621 }
622
623 // Once we've printed the code-completion results, suppress remaining
624 // diagnostics.
625 // FIXME: Move this somewhere else!
626 SemaRef.PP.getDiagnostics().setSuppressAllDiagnostics();
627}