blob: 6a96e626d9022486f8b6cacde881b16cfdb3d52f [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"
Chandler Carruth3a022472012-12-04 09:13:33 +000014#include "clang-c/Index.h"
Douglas Gregor56c2dbc2009-09-18 17:54:00 +000015#include "clang/AST/DeclCXX.h"
John McCallde6836a2010-08-24 07:21:54 +000016#include "clang/AST/DeclObjC.h"
John McCall19c1bfd2010-08-25 05:32:35 +000017#include "clang/AST/DeclTemplate.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/Sema/Scope.h"
19#include "clang/Sema/Sema.h"
Vassil Vassilev644ea612016-07-27 14:56:59 +000020#include "clang/Lex/Preprocessor.h"
Douglas Gregor2436e712009-09-17 21:32:03 +000021#include "llvm/ADT/STLExtras.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "llvm/ADT/SmallString.h"
Douglas Gregor669a25a2011-02-17 00:22:45 +000023#include "llvm/ADT/Twine.h"
Douglas Gregor2436e712009-09-17 21:32:03 +000024#include "llvm/Support/raw_ostream.h"
25#include <algorithm>
Douglas Gregorfedc3282009-09-18 22:15:54 +000026#include <cstring>
27#include <functional>
Douglas Gregorab6ccb52009-11-17 16:43:05 +000028
Douglas Gregor2436e712009-09-17 21:32:03 +000029using namespace clang;
30
Douglas Gregorfedc3282009-09-18 22:15:54 +000031//===----------------------------------------------------------------------===//
Douglas Gregor0212fd72010-09-21 16:06:22 +000032// Code completion context implementation
33//===----------------------------------------------------------------------===//
34
35bool CodeCompletionContext::wantConstructorResults() const {
36 switch (Kind) {
Douglas Gregor0ac41382010-09-23 23:01:17 +000037 case CCC_Recovery:
Douglas Gregor0212fd72010-09-21 16:06:22 +000038 case CCC_Statement:
39 case CCC_Expression:
40 case CCC_ObjCMessageReceiver:
41 case CCC_ParenthesizedExpression:
42 return true;
43
44 case CCC_TopLevel:
45 case CCC_ObjCInterface:
46 case CCC_ObjCImplementation:
47 case CCC_ObjCIvarList:
48 case CCC_ClassStructUnion:
Douglas Gregor21325842011-07-07 16:03:39 +000049 case CCC_DotMemberAccess:
50 case CCC_ArrowMemberAccess:
51 case CCC_ObjCPropertyAccess:
Douglas Gregor0212fd72010-09-21 16:06:22 +000052 case CCC_EnumTag:
53 case CCC_UnionTag:
54 case CCC_ClassOrStructTag:
55 case CCC_ObjCProtocolName:
56 case CCC_Namespace:
57 case CCC_Type:
58 case CCC_Name:
59 case CCC_PotentiallyQualifiedName:
60 case CCC_MacroName:
61 case CCC_MacroNameUse:
62 case CCC_PreprocessorExpression:
63 case CCC_PreprocessorDirective:
64 case CCC_NaturalLanguage:
65 case CCC_SelectorName:
66 case CCC_TypeQualifiers:
Douglas Gregor0ac41382010-09-23 23:01:17 +000067 case CCC_Other:
Douglas Gregor3a69eaf2011-02-18 23:30:37 +000068 case CCC_OtherWithMacros:
Douglas Gregor21325842011-07-07 16:03:39 +000069 case CCC_ObjCInstanceMessage:
70 case CCC_ObjCClassMessage:
Douglas Gregor2c595ad2011-07-30 06:55:39 +000071 case CCC_ObjCInterfaceName:
Douglas Gregor21325842011-07-07 16:03:39 +000072 case CCC_ObjCCategoryName:
Douglas Gregor0212fd72010-09-21 16:06:22 +000073 return false;
74 }
David Blaikie8a40f702012-01-17 06:56:22 +000075
76 llvm_unreachable("Invalid CodeCompletionContext::Kind!");
Douglas Gregor0212fd72010-09-21 16:06:22 +000077}
78
Ilya Biryukov27d82582018-02-19 12:35:33 +000079StringRef clang::getCompletionKindString(enum CodeCompletionContext::Kind Kind) {
80 using CCKind = enum CodeCompletionContext::Kind;
81 switch (Kind) {
82 case CCKind::CCC_Other:
83 return "Other";
84 case CCKind::CCC_OtherWithMacros:
85 return "OtherWithMacros";
86 case CCKind::CCC_TopLevel:
87 return "TopLevel";
88 case CCKind::CCC_ObjCInterface:
89 return "ObjCInterface";
90 case CCKind::CCC_ObjCImplementation:
91 return "ObjCImplementation";
92 case CCKind::CCC_ObjCIvarList:
93 return "ObjCIvarList";
94 case CCKind::CCC_ClassStructUnion:
95 return "ClassStructUnion";
96 case CCKind::CCC_Statement:
97 return "Statement";
98 case CCKind::CCC_Expression:
99 return "Expression";
100 case CCKind::CCC_ObjCMessageReceiver:
101 return "ObjCMessageReceiver";
102 case CCKind::CCC_DotMemberAccess:
103 return "DotMemberAccess";
104 case CCKind::CCC_ArrowMemberAccess:
105 return "ArrowMemberAccess";
106 case CCKind::CCC_ObjCPropertyAccess:
107 return "ObjCPropertyAccess";
108 case CCKind::CCC_EnumTag:
109 return "EnumTag";
110 case CCKind::CCC_UnionTag:
111 return "UnionTag";
112 case CCKind::CCC_ClassOrStructTag:
113 return "ClassOrStructTag";
114 case CCKind::CCC_ObjCProtocolName:
115 return "ObjCProtocolName";
116 case CCKind::CCC_Namespace:
117 return "Namespace";
118 case CCKind::CCC_Type:
119 return "Type";
120 case CCKind::CCC_Name:
121 return "Name";
122 case CCKind::CCC_PotentiallyQualifiedName:
123 return "PotentiallyQualifiedName";
124 case CCKind::CCC_MacroName:
125 return "MacroName";
126 case CCKind::CCC_MacroNameUse:
127 return "MacroNameUse";
128 case CCKind::CCC_PreprocessorExpression:
129 return "PreprocessorExpression";
130 case CCKind::CCC_PreprocessorDirective:
131 return "PreprocessorDirective";
132 case CCKind::CCC_NaturalLanguage:
133 return "NaturalLanguage";
134 case CCKind::CCC_SelectorName:
135 return "SelectorName";
136 case CCKind::CCC_TypeQualifiers:
137 return "TypeQualifiers";
138 case CCKind::CCC_ParenthesizedExpression:
139 return "ParenthesizedExpression";
140 case CCKind::CCC_ObjCInstanceMessage:
141 return "ObjCInstanceMessage";
142 case CCKind::CCC_ObjCClassMessage:
143 return "ObjCClassMessage";
144 case CCKind::CCC_ObjCInterfaceName:
145 return "ObjCInterfaceName";
146 case CCKind::CCC_ObjCCategoryName:
147 return "ObjCCategoryName";
148 case CCKind::CCC_Recovery:
149 return "Recovery";
150 }
151 llvm_unreachable("Invalid CodeCompletionContext::Kind!");
152}
153
Douglas Gregor0212fd72010-09-21 16:06:22 +0000154//===----------------------------------------------------------------------===//
Douglas Gregorfedc3282009-09-18 22:15:54 +0000155// Code completion string implementation
156//===----------------------------------------------------------------------===//
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000157CodeCompletionString::Chunk::Chunk(ChunkKind Kind, const char *Text)
Daniel Dunbarb0a19422009-11-12 18:40:12 +0000158 : Kind(Kind), Text("")
Douglas Gregor5bf52692009-09-22 23:15:58 +0000159{
Douglas Gregor9eb77012009-11-07 00:00:49 +0000160 switch (Kind) {
161 case CK_TypedText:
162 case CK_Text:
163 case CK_Placeholder:
164 case CK_Informative:
Douglas Gregorb3fa9192009-12-18 18:53:37 +0000165 case CK_ResultType:
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000166 case CK_CurrentParameter:
167 this->Text = Text;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000168 break;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000169
170 case CK_Optional:
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000171 llvm_unreachable("Optional strings cannot be created from text");
Douglas Gregor9eb77012009-11-07 00:00:49 +0000172
173 case CK_LeftParen:
174 this->Text = "(";
175 break;
176
177 case CK_RightParen:
178 this->Text = ")";
179 break;
180
181 case CK_LeftBracket:
182 this->Text = "[";
183 break;
184
185 case CK_RightBracket:
186 this->Text = "]";
187 break;
188
189 case CK_LeftBrace:
190 this->Text = "{";
191 break;
192
193 case CK_RightBrace:
194 this->Text = "}";
195 break;
196
197 case CK_LeftAngle:
198 this->Text = "<";
199 break;
200
201 case CK_RightAngle:
202 this->Text = ">";
203 break;
204
205 case CK_Comma:
206 this->Text = ", ";
207 break;
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000208
209 case CK_Colon:
Douglas Gregor636a61e2010-04-07 00:21:17 +0000210 this->Text = ":";
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000211 break;
212
213 case CK_SemiColon:
214 this->Text = ";";
215 break;
216
217 case CK_Equal:
218 this->Text = " = ";
219 break;
220
221 case CK_HorizontalSpace:
222 this->Text = " ";
223 break;
224
225 case CK_VerticalSpace:
226 this->Text = "\n";
227 break;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000228 }
Douglas Gregor5bf52692009-09-22 23:15:58 +0000229}
230
231CodeCompletionString::Chunk
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000232CodeCompletionString::Chunk::CreateText(const char *Text) {
Douglas Gregor5bf52692009-09-22 23:15:58 +0000233 return Chunk(CK_Text, Text);
Douglas Gregorfedc3282009-09-18 22:15:54 +0000234}
235
236CodeCompletionString::Chunk
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000237CodeCompletionString::Chunk::CreateOptional(CodeCompletionString *Optional) {
Douglas Gregorfedc3282009-09-18 22:15:54 +0000238 Chunk Result;
239 Result.Kind = CK_Optional;
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000240 Result.Optional = Optional;
Douglas Gregorfedc3282009-09-18 22:15:54 +0000241 return Result;
242}
243
244CodeCompletionString::Chunk
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000245CodeCompletionString::Chunk::CreatePlaceholder(const char *Placeholder) {
Douglas Gregor5bf52692009-09-22 23:15:58 +0000246 return Chunk(CK_Placeholder, Placeholder);
247}
248
249CodeCompletionString::Chunk
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000250CodeCompletionString::Chunk::CreateInformative(const char *Informative) {
Douglas Gregor5bf52692009-09-22 23:15:58 +0000251 return Chunk(CK_Informative, Informative);
Douglas Gregorfedc3282009-09-18 22:15:54 +0000252}
253
Douglas Gregor9eb77012009-11-07 00:00:49 +0000254CodeCompletionString::Chunk
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000255CodeCompletionString::Chunk::CreateResultType(const char *ResultType) {
Douglas Gregorb3fa9192009-12-18 18:53:37 +0000256 return Chunk(CK_ResultType, ResultType);
257}
258
259CodeCompletionString::Chunk
Douglas Gregor9eb77012009-11-07 00:00:49 +0000260CodeCompletionString::Chunk::CreateCurrentParameter(
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000261 const char *CurrentParameter) {
Douglas Gregor9eb77012009-11-07 00:00:49 +0000262 return Chunk(CK_CurrentParameter, CurrentParameter);
263}
264
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000265CodeCompletionString::CodeCompletionString(const Chunk *Chunks,
266 unsigned NumChunks,
267 unsigned Priority,
Erik Verbruggen98ea7f62011-10-14 15:31:08 +0000268 CXAvailabilityKind Availability,
269 const char **Annotations,
Douglas Gregor78254c82012-03-27 23:34:16 +0000270 unsigned NumAnnotations,
Dmitri Gribenko3292d062012-07-02 17:35:10 +0000271 StringRef ParentName,
272 const char *BriefComment)
Douglas Gregor78254c82012-03-27 23:34:16 +0000273 : NumChunks(NumChunks), NumAnnotations(NumAnnotations),
Argyrios Kyrtzidis9ae39562012-09-26 16:39:56 +0000274 Priority(Priority), Availability(Availability),
Dmitri Gribenko3292d062012-07-02 17:35:10 +0000275 ParentName(ParentName), BriefComment(BriefComment)
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000276{
Erik Verbruggen98ea7f62011-10-14 15:31:08 +0000277 assert(NumChunks <= 0xffff);
278 assert(NumAnnotations <= 0xffff);
279
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000280 Chunk *StoredChunks = reinterpret_cast<Chunk *>(this + 1);
281 for (unsigned I = 0; I != NumChunks; ++I)
282 StoredChunks[I] = Chunks[I];
Erik Verbruggen98ea7f62011-10-14 15:31:08 +0000283
284 const char **StoredAnnotations = reinterpret_cast<const char **>(StoredChunks + NumChunks);
285 for (unsigned I = 0; I != NumAnnotations; ++I)
286 StoredAnnotations[I] = Annotations[I];
Douglas Gregorfedc3282009-09-18 22:15:54 +0000287}
288
Erik Verbruggen98ea7f62011-10-14 15:31:08 +0000289unsigned CodeCompletionString::getAnnotationCount() const {
290 return NumAnnotations;
291}
292
293const char *CodeCompletionString::getAnnotation(unsigned AnnotationNr) const {
294 if (AnnotationNr < NumAnnotations)
295 return reinterpret_cast<const char * const*>(end())[AnnotationNr];
296 else
Craig Topperc3ec1492014-05-26 06:22:03 +0000297 return nullptr;
Erik Verbruggen98ea7f62011-10-14 15:31:08 +0000298}
299
300
Douglas Gregorfedc3282009-09-18 22:15:54 +0000301std::string CodeCompletionString::getAsString() const {
302 std::string Result;
303 llvm::raw_string_ostream OS(Result);
304
305 for (iterator C = begin(), CEnd = end(); C != CEnd; ++C) {
306 switch (C->Kind) {
Douglas Gregorfedc3282009-09-18 22:15:54 +0000307 case CK_Optional: OS << "{#" << C->Optional->getAsString() << "#}"; break;
Douglas Gregor5bf52692009-09-22 23:15:58 +0000308 case CK_Placeholder: OS << "<#" << C->Text << "#>"; break;
Douglas Gregorb3fa9192009-12-18 18:53:37 +0000309
310 case CK_Informative:
311 case CK_ResultType:
312 OS << "[#" << C->Text << "#]";
313 break;
314
Douglas Gregor9eb77012009-11-07 00:00:49 +0000315 case CK_CurrentParameter: OS << "<#" << C->Text << "#>"; break;
316 default: OS << C->Text; break;
Douglas Gregorfedc3282009-09-18 22:15:54 +0000317 }
318 }
Dan Gohman4888f1a2010-07-26 21:33:22 +0000319 return OS.str();
Douglas Gregorfedc3282009-09-18 22:15:54 +0000320}
321
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000322const char *CodeCompletionString::getTypedText() const {
323 for (iterator C = begin(), CEnd = end(); C != CEnd; ++C)
324 if (C->Kind == CK_TypedText)
325 return C->Text;
Craig Topperc3ec1492014-05-26 06:22:03 +0000326
327 return nullptr;
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000328}
329
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000330const char *CodeCompletionAllocator::CopyString(const Twine &String) {
331 SmallString<128> Data;
332 StringRef Ref = String.toStringRef(Data);
Douglas Gregor669a25a2011-02-17 00:22:45 +0000333 // FIXME: It would be more efficient to teach Twine to tell us its size and
334 // then add a routine there to fill in an allocated char* with the contents
335 // of the string.
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000336 char *Mem = (char *)Allocate(Ref.size() + 1, 1);
337 std::copy(Ref.begin(), Ref.end(), Mem);
338 Mem[Ref.size()] = 0;
339 return Mem;
Douglas Gregor669a25a2011-02-17 00:22:45 +0000340}
341
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000342StringRef CodeCompletionTUInfo::getParentName(const DeclContext *DC) {
343 const NamedDecl *ND = dyn_cast<NamedDecl>(DC);
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000344 if (!ND)
345 return StringRef();
346
347 // Check whether we've already cached the parent name.
348 StringRef &CachedParentName = ParentNames[DC];
349 if (!CachedParentName.empty())
350 return CachedParentName;
351
352 // If we already processed this DeclContext and assigned empty to it, the
353 // data pointer will be non-null.
Craig Topperc3ec1492014-05-26 06:22:03 +0000354 if (CachedParentName.data() != nullptr)
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000355 return StringRef();
356
357 // Find the interesting names.
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000358 SmallVector<const DeclContext *, 2> Contexts;
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000359 while (DC && !DC->isFunctionOrMethod()) {
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000360 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC)) {
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000361 if (ND->getIdentifier())
362 Contexts.push_back(DC);
363 }
364
365 DC = DC->getParent();
366 }
367
368 {
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000369 SmallString<128> S;
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000370 llvm::raw_svector_ostream OS(S);
371 bool First = true;
372 for (unsigned I = Contexts.size(); I != 0; --I) {
373 if (First)
374 First = false;
375 else {
376 OS << "::";
377 }
378
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000379 const DeclContext *CurDC = Contexts[I-1];
380 if (const ObjCCategoryImplDecl *CatImpl = dyn_cast<ObjCCategoryImplDecl>(CurDC))
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000381 CurDC = CatImpl->getCategoryDecl();
382
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000383 if (const ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CurDC)) {
384 const ObjCInterfaceDecl *Interface = Cat->getClassInterface();
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000385 if (!Interface) {
386 // Assign an empty StringRef but with non-null data to distinguish
387 // between empty because we didn't process the DeclContext yet.
Reid Klecknerd16cebe2016-02-10 19:09:15 +0000388 CachedParentName = StringRef((const char *)(uintptr_t)~0U, 0);
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000389 return StringRef();
390 }
391
392 OS << Interface->getName() << '(' << Cat->getName() << ')';
393 } else {
394 OS << cast<NamedDecl>(CurDC)->getName();
395 }
396 }
397
398 CachedParentName = AllocatorRef->CopyString(OS.str());
399 }
400
401 return CachedParentName;
402}
403
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000404CodeCompletionString *CodeCompletionBuilder::TakeString() {
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000405 void *Mem = getAllocator().Allocate(
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000406 sizeof(CodeCompletionString) + sizeof(Chunk) * Chunks.size() +
407 sizeof(const char *) * Annotations.size(),
408 alignof(CodeCompletionString));
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000409 CodeCompletionString *Result
410 = new (Mem) CodeCompletionString(Chunks.data(), Chunks.size(),
Erik Verbruggen98ea7f62011-10-14 15:31:08 +0000411 Priority, Availability,
Douglas Gregor78254c82012-03-27 23:34:16 +0000412 Annotations.data(), Annotations.size(),
Argyrios Kyrtzidis9ae39562012-09-26 16:39:56 +0000413 ParentName, BriefComment);
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000414 Chunks.clear();
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000415 return Result;
416}
Douglas Gregor9eb77012009-11-07 00:00:49 +0000417
Benjamin Kramerdb534a42012-03-26 16:57:36 +0000418void CodeCompletionBuilder::AddTypedTextChunk(const char *Text) {
419 Chunks.push_back(Chunk(CodeCompletionString::CK_TypedText, Text));
420}
421
422void CodeCompletionBuilder::AddTextChunk(const char *Text) {
423 Chunks.push_back(Chunk::CreateText(Text));
424}
425
426void CodeCompletionBuilder::AddOptionalChunk(CodeCompletionString *Optional) {
427 Chunks.push_back(Chunk::CreateOptional(Optional));
428}
429
430void CodeCompletionBuilder::AddPlaceholderChunk(const char *Placeholder) {
431 Chunks.push_back(Chunk::CreatePlaceholder(Placeholder));
432}
433
434void CodeCompletionBuilder::AddInformativeChunk(const char *Text) {
435 Chunks.push_back(Chunk::CreateInformative(Text));
436}
437
438void CodeCompletionBuilder::AddResultTypeChunk(const char *ResultType) {
439 Chunks.push_back(Chunk::CreateResultType(ResultType));
440}
441
442void
443CodeCompletionBuilder::AddCurrentParameterChunk(const char *CurrentParameter) {
444 Chunks.push_back(Chunk::CreateCurrentParameter(CurrentParameter));
445}
446
447void CodeCompletionBuilder::AddChunk(CodeCompletionString::ChunkKind CK,
448 const char *Text) {
449 Chunks.push_back(Chunk(CK, Text));
450}
451
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000452void CodeCompletionBuilder::addParentContext(const DeclContext *DC) {
Douglas Gregor78254c82012-03-27 23:34:16 +0000453 if (DC->isTranslationUnit()) {
Douglas Gregor78254c82012-03-27 23:34:16 +0000454 return;
455 }
456
457 if (DC->isFunctionOrMethod())
458 return;
459
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000460 const NamedDecl *ND = dyn_cast<NamedDecl>(DC);
Douglas Gregor78254c82012-03-27 23:34:16 +0000461 if (!ND)
462 return;
463
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000464 ParentName = getCodeCompletionTUInfo().getParentName(DC);
Douglas Gregor78254c82012-03-27 23:34:16 +0000465}
466
Dmitri Gribenko3292d062012-07-02 17:35:10 +0000467void CodeCompletionBuilder::addBriefComment(StringRef Comment) {
468 BriefComment = Allocator.CopyString(Comment);
469}
470
Douglas Gregorfedc3282009-09-18 22:15:54 +0000471//===----------------------------------------------------------------------===//
Douglas Gregor05f477c2009-09-23 00:16:58 +0000472// Code completion overload candidate implementation
473//===----------------------------------------------------------------------===//
474FunctionDecl *
475CodeCompleteConsumer::OverloadCandidate::getFunction() const {
476 if (getKind() == CK_Function)
477 return Function;
478 else if (getKind() == CK_FunctionTemplate)
479 return FunctionTemplate->getTemplatedDecl();
480 else
Craig Topperc3ec1492014-05-26 06:22:03 +0000481 return nullptr;
Douglas Gregor05f477c2009-09-23 00:16:58 +0000482}
483
484const FunctionType *
485CodeCompleteConsumer::OverloadCandidate::getFunctionType() const {
486 switch (Kind) {
487 case CK_Function:
488 return Function->getType()->getAs<FunctionType>();
489
490 case CK_FunctionTemplate:
491 return FunctionTemplate->getTemplatedDecl()->getType()
492 ->getAs<FunctionType>();
493
494 case CK_FunctionType:
495 return Type;
496 }
David Blaikie8a40f702012-01-17 06:56:22 +0000497
498 llvm_unreachable("Invalid CandidateKind!");
Douglas Gregor05f477c2009-09-23 00:16:58 +0000499}
500
501//===----------------------------------------------------------------------===//
Douglas Gregorfedc3282009-09-18 22:15:54 +0000502// Code completion consumer implementation
503//===----------------------------------------------------------------------===//
504
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000505CodeCompleteConsumer::~CodeCompleteConsumer() { }
Douglas Gregorfedc3282009-09-18 22:15:54 +0000506
Vassil Vassilev644ea612016-07-27 14:56:59 +0000507bool PrintingCodeCompleteConsumer::isResultFilteredOut(StringRef Filter,
508 CodeCompletionResult Result) {
509 switch (Result.Kind) {
510 case CodeCompletionResult::RK_Declaration: {
511 return !(Result.Declaration->getIdentifier() &&
512 Result.Declaration->getIdentifier()->getName().startswith(Filter));
513 }
514 case CodeCompletionResult::RK_Keyword: {
515 return !StringRef(Result.Keyword).startswith(Filter);
516 }
517 case CodeCompletionResult::RK_Macro: {
518 return !Result.Macro->getName().startswith(Filter);
519 }
520 case CodeCompletionResult::RK_Pattern: {
521 return !StringRef(Result.Pattern->getAsString()).startswith(Filter);
522 }
Vassil Vassilev644ea612016-07-27 14:56:59 +0000523 }
Simon Pilgrime71a1f92016-07-27 16:41:56 +0000524 llvm_unreachable("Unknown code completion result Kind.");
Vassil Vassilev644ea612016-07-27 14:56:59 +0000525}
526
Douglas Gregor2436e712009-09-17 21:32:03 +0000527void
Daniel Dunbar242ea9a2009-11-13 08:58:20 +0000528PrintingCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &SemaRef,
Douglas Gregor00c37ef2010-08-11 21:23:17 +0000529 CodeCompletionContext Context,
John McCall276321a2010-08-25 06:19:51 +0000530 CodeCompletionResult *Results,
Douglas Gregor2436e712009-09-17 21:32:03 +0000531 unsigned NumResults) {
Douglas Gregor49f67ce2010-08-26 13:48:20 +0000532 std::stable_sort(Results, Results + NumResults);
533
Vassil Vassilev644ea612016-07-27 14:56:59 +0000534 StringRef Filter = SemaRef.getPreprocessor().getCodeCompletionFilter();
535
Douglas Gregor2436e712009-09-17 21:32:03 +0000536 // Print the results.
537 for (unsigned I = 0; I != NumResults; ++I) {
Vassil Vassilev644ea612016-07-27 14:56:59 +0000538 if(!Filter.empty() && isResultFilteredOut(Filter, Results[I]))
539 continue;
Douglas Gregor58acf322009-10-09 22:16:47 +0000540 OS << "COMPLETION: ";
Douglas Gregor2436e712009-09-17 21:32:03 +0000541 switch (Results[I].Kind) {
John McCall276321a2010-08-25 06:19:51 +0000542 case CodeCompletionResult::RK_Declaration:
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000543 OS << *Results[I].Declaration;
Douglas Gregor2436e712009-09-17 21:32:03 +0000544 if (Results[I].Hidden)
545 OS << " (Hidden)";
Douglas Gregor3545ff42009-09-21 16:56:56 +0000546 if (CodeCompletionString *CCS
Douglas Gregorc3425b12015-07-07 06:20:19 +0000547 = Results[I].CreateCodeCompletionString(SemaRef, Context,
548 getAllocator(),
Dmitri Gribenko3292d062012-07-02 17:35:10 +0000549 CCTUInfo,
550 includeBriefComments())) {
Douglas Gregorfedc3282009-09-18 22:15:54 +0000551 OS << " : " << CCS->getAsString();
Dmitri Gribenko3292d062012-07-02 17:35:10 +0000552 if (const char *BriefComment = CCS->getBriefComment())
553 OS << " : " << BriefComment;
Douglas Gregorfedc3282009-09-18 22:15:54 +0000554 }
555
Douglas Gregor2436e712009-09-17 21:32:03 +0000556 OS << '\n';
557 break;
558
John McCall276321a2010-08-25 06:19:51 +0000559 case CodeCompletionResult::RK_Keyword:
Douglas Gregor52ce62f2010-01-13 23:24:38 +0000560 OS << Results[I].Keyword << '\n';
Douglas Gregor2436e712009-09-17 21:32:03 +0000561 break;
Douglas Gregorf329c7c2009-10-30 16:50:04 +0000562
John McCall276321a2010-08-25 06:19:51 +0000563 case CodeCompletionResult::RK_Macro: {
Douglas Gregor52ce62f2010-01-13 23:24:38 +0000564 OS << Results[I].Macro->getName();
Douglas Gregorf329c7c2009-10-30 16:50:04 +0000565 if (CodeCompletionString *CCS
Douglas Gregorc3425b12015-07-07 06:20:19 +0000566 = Results[I].CreateCodeCompletionString(SemaRef, Context,
567 getAllocator(),
Dmitri Gribenko3292d062012-07-02 17:35:10 +0000568 CCTUInfo,
569 includeBriefComments())) {
Douglas Gregorf329c7c2009-10-30 16:50:04 +0000570 OS << " : " << CCS->getAsString();
Douglas Gregorf329c7c2009-10-30 16:50:04 +0000571 }
572 OS << '\n';
573 break;
574 }
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000575
John McCall276321a2010-08-25 06:19:51 +0000576 case CodeCompletionResult::RK_Pattern: {
Douglas Gregor52ce62f2010-01-13 23:24:38 +0000577 OS << "Pattern : "
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000578 << Results[I].Pattern->getAsString() << '\n';
579 break;
580 }
Douglas Gregor2436e712009-09-17 21:32:03 +0000581 }
582 }
Douglas Gregor2436e712009-09-17 21:32:03 +0000583}
Douglas Gregor05f477c2009-09-23 00:16:58 +0000584
Francisco Lopes da Silva0c010cd2015-01-28 14:17:22 +0000585// This function is used solely to preserve the former presentation of overloads
586// by "clang -cc1 -code-completion-at", since CodeCompletionString::getAsString
587// needs to be improved for printing the newer and more detailed overload
588// chunks.
589static std::string getOverloadAsString(const CodeCompletionString &CCS) {
590 std::string Result;
591 llvm::raw_string_ostream OS(Result);
592
593 for (auto &C : CCS) {
594 switch (C.Kind) {
595 case CodeCompletionString::CK_Informative:
596 case CodeCompletionString::CK_ResultType:
597 OS << "[#" << C.Text << "#]";
598 break;
599
600 case CodeCompletionString::CK_CurrentParameter:
601 OS << "<#" << C.Text << "#>";
602 break;
603
604 default: OS << C.Text; break;
605 }
606 }
607 return OS.str();
608}
609
Douglas Gregor05f477c2009-09-23 00:16:58 +0000610void
Daniel Dunbar242ea9a2009-11-13 08:58:20 +0000611PrintingCodeCompleteConsumer::ProcessOverloadCandidates(Sema &SemaRef,
612 unsigned CurrentArg,
Douglas Gregor05f477c2009-09-23 00:16:58 +0000613 OverloadCandidate *Candidates,
614 unsigned NumCandidates) {
615 for (unsigned I = 0; I != NumCandidates; ++I) {
Douglas Gregorf0f51982009-09-23 00:34:09 +0000616 if (CodeCompletionString *CCS
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000617 = Candidates[I].CreateSignatureString(CurrentArg, SemaRef,
Francisco Lopes da Silva0c010cd2015-01-28 14:17:22 +0000618 getAllocator(), CCTUInfo,
619 includeBriefComments())) {
620 OS << "OVERLOAD: " << getOverloadAsString(*CCS) << "\n";
Douglas Gregor05f477c2009-09-23 00:16:58 +0000621 }
622 }
Douglas Gregor05f477c2009-09-23 00:16:58 +0000623}
Douglas Gregor9eb77012009-11-07 00:00:49 +0000624
Douglas Gregor7b316822012-03-17 06:39:06 +0000625/// \brief Retrieve the effective availability of the given declaration.
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000626static AvailabilityResult getDeclAvailability(const Decl *D) {
Douglas Gregor7b316822012-03-17 06:39:06 +0000627 AvailabilityResult AR = D->getAvailability();
628 if (isa<EnumConstantDecl>(D))
629 AR = std::max(AR, cast<Decl>(D->getDeclContext())->getAvailability());
630 return AR;
631}
632
Erik Verbruggen2e657ff2011-10-06 07:27:49 +0000633void CodeCompletionResult::computeCursorKindAndAvailability(bool Accessible) {
Douglas Gregorb14904c2010-08-13 22:48:40 +0000634 switch (Kind) {
Douglas Gregor78254c82012-03-27 23:34:16 +0000635 case RK_Pattern:
636 if (!Declaration) {
637 // Do nothing: Patterns can come with cursor kinds!
638 break;
639 }
Adrian Prantlf3b3ccd2017-12-19 22:06:11 +0000640 LLVM_FALLTHROUGH;
Douglas Gregor78254c82012-03-27 23:34:16 +0000641
Douglas Gregor7b316822012-03-17 06:39:06 +0000642 case RK_Declaration: {
Douglas Gregorf757a122010-08-23 23:00:57 +0000643 // Set the availability based on attributes.
Douglas Gregor7b316822012-03-17 06:39:06 +0000644 switch (getDeclAvailability(Declaration)) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000645 case AR_Available:
646 case AR_NotYetIntroduced:
647 Availability = CXAvailability_Available;
648 break;
Douglas Gregorf757a122010-08-23 23:00:57 +0000649
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000650 case AR_Deprecated:
651 Availability = CXAvailability_Deprecated;
652 break;
653
654 case AR_Unavailable:
655 Availability = CXAvailability_NotAvailable;
656 break;
657 }
658
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000659 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(Declaration))
Douglas Gregor09c0eb12010-09-03 23:30:36 +0000660 if (Function->isDeleted())
Douglas Gregorf757a122010-08-23 23:00:57 +0000661 Availability = CXAvailability_NotAvailable;
Douglas Gregor09c0eb12010-09-03 23:30:36 +0000662
663 CursorKind = getCursorKindForDecl(Declaration);
Douglas Gregordeafd0b2011-12-27 22:43:10 +0000664 if (CursorKind == CXCursor_UnexposedDecl) {
Douglas Gregorf6102672012-01-01 21:23:57 +0000665 // FIXME: Forward declarations of Objective-C classes and protocols
666 // are not directly exposed, but we want code completion to treat them
667 // like a definition.
Douglas Gregordeafd0b2011-12-27 22:43:10 +0000668 if (isa<ObjCInterfaceDecl>(Declaration))
669 CursorKind = CXCursor_ObjCInterfaceDecl;
Douglas Gregorf6102672012-01-01 21:23:57 +0000670 else if (isa<ObjCProtocolDecl>(Declaration))
671 CursorKind = CXCursor_ObjCProtocolDecl;
Douglas Gregordeafd0b2011-12-27 22:43:10 +0000672 else
673 CursorKind = CXCursor_NotImplemented;
674 }
Douglas Gregorb14904c2010-08-13 22:48:40 +0000675 break;
Douglas Gregor7b316822012-03-17 06:39:06 +0000676 }
Douglas Gregorb14904c2010-08-13 22:48:40 +0000677
John McCall276321a2010-08-25 06:19:51 +0000678 case RK_Macro:
John McCall276321a2010-08-25 06:19:51 +0000679 case RK_Keyword:
Benjamin Kramerc25c0b92012-05-20 14:19:46 +0000680 llvm_unreachable("Macro and keyword kinds are handled by the constructors");
Douglas Gregor8e984da2010-08-04 16:47:14 +0000681 }
Erik Verbruggen2e657ff2011-10-06 07:27:49 +0000682
683 if (!Accessible)
684 Availability = CXAvailability_NotAccessible;
Douglas Gregor8e984da2010-08-04 16:47:14 +0000685}
Douglas Gregorf09935f2009-12-01 05:55:20 +0000686
Douglas Gregor0de55ce2010-08-25 18:41:16 +0000687/// \brief Retrieve the name that should be used to order a result.
688///
689/// If the name needs to be constructed as a string, that string will be
690/// saved into Saved and the returned StringRef will refer to it.
Sam McCall1102a862017-11-15 09:15:06 +0000691StringRef CodeCompletionResult::getOrderedName(std::string &Saved) const {
692 switch (Kind) {
693 case RK_Keyword:
694 return Keyword;
695 case RK_Pattern:
696 return Pattern->getTypedText();
697 case RK_Macro:
698 return Macro->getName();
699 case RK_Declaration:
Douglas Gregor0de55ce2010-08-25 18:41:16 +0000700 // Handle declarations below.
701 break;
702 }
703
Sam McCall1102a862017-11-15 09:15:06 +0000704 DeclarationName Name = Declaration->getDeclName();
Douglas Gregor0de55ce2010-08-25 18:41:16 +0000705
706 // If the name is a simple identifier (by far the common case), or a
707 // zero-argument selector, just return a reference to that identifier.
708 if (IdentifierInfo *Id = Name.getAsIdentifierInfo())
709 return Id->getName();
710 if (Name.isObjCZeroArgSelector())
711 if (IdentifierInfo *Id
712 = Name.getObjCSelector().getIdentifierInfoForSlot(0))
713 return Id->getName();
714
715 Saved = Name.getAsString();
716 return Saved;
717}
718
719bool clang::operator<(const CodeCompletionResult &X,
720 const CodeCompletionResult &Y) {
721 std::string XSaved, YSaved;
Sam McCall1102a862017-11-15 09:15:06 +0000722 StringRef XStr = X.getOrderedName(XSaved);
723 StringRef YStr = Y.getOrderedName(YSaved);
Douglas Gregor0de55ce2010-08-25 18:41:16 +0000724 int cmp = XStr.compare_lower(YStr);
725 if (cmp)
726 return cmp < 0;
727
Douglas Gregor49f67ce2010-08-26 13:48:20 +0000728 // If case-insensitive comparison fails, try case-sensitive comparison.
729 cmp = XStr.compare(YStr);
730 if (cmp)
731 return cmp < 0;
Douglas Gregor0de55ce2010-08-25 18:41:16 +0000732
733 return false;
734}