blob: 3431ddcf70a231411a9bb1b96e5783fbe899aa66 [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
79//===----------------------------------------------------------------------===//
Douglas Gregorfedc3282009-09-18 22:15:54 +000080// Code completion string implementation
81//===----------------------------------------------------------------------===//
Douglas Gregorb278aaf2011-02-01 19:23:04 +000082CodeCompletionString::Chunk::Chunk(ChunkKind Kind, const char *Text)
Daniel Dunbarb0a19422009-11-12 18:40:12 +000083 : Kind(Kind), Text("")
Douglas Gregor5bf52692009-09-22 23:15:58 +000084{
Douglas Gregor9eb77012009-11-07 00:00:49 +000085 switch (Kind) {
86 case CK_TypedText:
87 case CK_Text:
88 case CK_Placeholder:
89 case CK_Informative:
Douglas Gregorb3fa9192009-12-18 18:53:37 +000090 case CK_ResultType:
Douglas Gregorb278aaf2011-02-01 19:23:04 +000091 case CK_CurrentParameter:
92 this->Text = Text;
Douglas Gregor9eb77012009-11-07 00:00:49 +000093 break;
Douglas Gregor9eb77012009-11-07 00:00:49 +000094
95 case CK_Optional:
Jeffrey Yasskin1615d452009-12-12 05:05:38 +000096 llvm_unreachable("Optional strings cannot be created from text");
Douglas Gregor9eb77012009-11-07 00:00:49 +000097
98 case CK_LeftParen:
99 this->Text = "(";
100 break;
101
102 case CK_RightParen:
103 this->Text = ")";
104 break;
105
106 case CK_LeftBracket:
107 this->Text = "[";
108 break;
109
110 case CK_RightBracket:
111 this->Text = "]";
112 break;
113
114 case CK_LeftBrace:
115 this->Text = "{";
116 break;
117
118 case CK_RightBrace:
119 this->Text = "}";
120 break;
121
122 case CK_LeftAngle:
123 this->Text = "<";
124 break;
125
126 case CK_RightAngle:
127 this->Text = ">";
128 break;
129
130 case CK_Comma:
131 this->Text = ", ";
132 break;
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000133
134 case CK_Colon:
Douglas Gregor636a61e2010-04-07 00:21:17 +0000135 this->Text = ":";
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000136 break;
137
138 case CK_SemiColon:
139 this->Text = ";";
140 break;
141
142 case CK_Equal:
143 this->Text = " = ";
144 break;
145
146 case CK_HorizontalSpace:
147 this->Text = " ";
148 break;
149
150 case CK_VerticalSpace:
151 this->Text = "\n";
152 break;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000153 }
Douglas Gregor5bf52692009-09-22 23:15:58 +0000154}
155
156CodeCompletionString::Chunk
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000157CodeCompletionString::Chunk::CreateText(const char *Text) {
Douglas Gregor5bf52692009-09-22 23:15:58 +0000158 return Chunk(CK_Text, Text);
Douglas Gregorfedc3282009-09-18 22:15:54 +0000159}
160
161CodeCompletionString::Chunk
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000162CodeCompletionString::Chunk::CreateOptional(CodeCompletionString *Optional) {
Douglas Gregorfedc3282009-09-18 22:15:54 +0000163 Chunk Result;
164 Result.Kind = CK_Optional;
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000165 Result.Optional = Optional;
Douglas Gregorfedc3282009-09-18 22:15:54 +0000166 return Result;
167}
168
169CodeCompletionString::Chunk
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000170CodeCompletionString::Chunk::CreatePlaceholder(const char *Placeholder) {
Douglas Gregor5bf52692009-09-22 23:15:58 +0000171 return Chunk(CK_Placeholder, Placeholder);
172}
173
174CodeCompletionString::Chunk
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000175CodeCompletionString::Chunk::CreateInformative(const char *Informative) {
Douglas Gregor5bf52692009-09-22 23:15:58 +0000176 return Chunk(CK_Informative, Informative);
Douglas Gregorfedc3282009-09-18 22:15:54 +0000177}
178
Douglas Gregor9eb77012009-11-07 00:00:49 +0000179CodeCompletionString::Chunk
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000180CodeCompletionString::Chunk::CreateResultType(const char *ResultType) {
Douglas Gregorb3fa9192009-12-18 18:53:37 +0000181 return Chunk(CK_ResultType, ResultType);
182}
183
184CodeCompletionString::Chunk
Douglas Gregor9eb77012009-11-07 00:00:49 +0000185CodeCompletionString::Chunk::CreateCurrentParameter(
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000186 const char *CurrentParameter) {
Douglas Gregor9eb77012009-11-07 00:00:49 +0000187 return Chunk(CK_CurrentParameter, CurrentParameter);
188}
189
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000190CodeCompletionString::CodeCompletionString(const Chunk *Chunks,
191 unsigned NumChunks,
192 unsigned Priority,
Erik Verbruggen98ea7f62011-10-14 15:31:08 +0000193 CXAvailabilityKind Availability,
194 const char **Annotations,
Douglas Gregor78254c82012-03-27 23:34:16 +0000195 unsigned NumAnnotations,
Dmitri Gribenko3292d062012-07-02 17:35:10 +0000196 StringRef ParentName,
197 const char *BriefComment)
Douglas Gregor78254c82012-03-27 23:34:16 +0000198 : NumChunks(NumChunks), NumAnnotations(NumAnnotations),
Argyrios Kyrtzidis9ae39562012-09-26 16:39:56 +0000199 Priority(Priority), Availability(Availability),
Dmitri Gribenko3292d062012-07-02 17:35:10 +0000200 ParentName(ParentName), BriefComment(BriefComment)
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000201{
Erik Verbruggen98ea7f62011-10-14 15:31:08 +0000202 assert(NumChunks <= 0xffff);
203 assert(NumAnnotations <= 0xffff);
204
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000205 Chunk *StoredChunks = reinterpret_cast<Chunk *>(this + 1);
206 for (unsigned I = 0; I != NumChunks; ++I)
207 StoredChunks[I] = Chunks[I];
Erik Verbruggen98ea7f62011-10-14 15:31:08 +0000208
209 const char **StoredAnnotations = reinterpret_cast<const char **>(StoredChunks + NumChunks);
210 for (unsigned I = 0; I != NumAnnotations; ++I)
211 StoredAnnotations[I] = Annotations[I];
Douglas Gregorfedc3282009-09-18 22:15:54 +0000212}
213
Erik Verbruggen98ea7f62011-10-14 15:31:08 +0000214unsigned CodeCompletionString::getAnnotationCount() const {
215 return NumAnnotations;
216}
217
218const char *CodeCompletionString::getAnnotation(unsigned AnnotationNr) const {
219 if (AnnotationNr < NumAnnotations)
220 return reinterpret_cast<const char * const*>(end())[AnnotationNr];
221 else
Craig Topperc3ec1492014-05-26 06:22:03 +0000222 return nullptr;
Erik Verbruggen98ea7f62011-10-14 15:31:08 +0000223}
224
225
Douglas Gregorfedc3282009-09-18 22:15:54 +0000226std::string CodeCompletionString::getAsString() const {
227 std::string Result;
228 llvm::raw_string_ostream OS(Result);
229
230 for (iterator C = begin(), CEnd = end(); C != CEnd; ++C) {
231 switch (C->Kind) {
Douglas Gregorfedc3282009-09-18 22:15:54 +0000232 case CK_Optional: OS << "{#" << C->Optional->getAsString() << "#}"; break;
Douglas Gregor5bf52692009-09-22 23:15:58 +0000233 case CK_Placeholder: OS << "<#" << C->Text << "#>"; break;
Douglas Gregorb3fa9192009-12-18 18:53:37 +0000234
235 case CK_Informative:
236 case CK_ResultType:
237 OS << "[#" << C->Text << "#]";
238 break;
239
Douglas Gregor9eb77012009-11-07 00:00:49 +0000240 case CK_CurrentParameter: OS << "<#" << C->Text << "#>"; break;
241 default: OS << C->Text; break;
Douglas Gregorfedc3282009-09-18 22:15:54 +0000242 }
243 }
Dan Gohman4888f1a2010-07-26 21:33:22 +0000244 return OS.str();
Douglas Gregorfedc3282009-09-18 22:15:54 +0000245}
246
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000247const char *CodeCompletionString::getTypedText() const {
248 for (iterator C = begin(), CEnd = end(); C != CEnd; ++C)
249 if (C->Kind == CK_TypedText)
250 return C->Text;
Craig Topperc3ec1492014-05-26 06:22:03 +0000251
252 return nullptr;
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000253}
254
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000255const char *CodeCompletionAllocator::CopyString(const Twine &String) {
256 SmallString<128> Data;
257 StringRef Ref = String.toStringRef(Data);
Douglas Gregor669a25a2011-02-17 00:22:45 +0000258 // FIXME: It would be more efficient to teach Twine to tell us its size and
259 // then add a routine there to fill in an allocated char* with the contents
260 // of the string.
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000261 char *Mem = (char *)Allocate(Ref.size() + 1, 1);
262 std::copy(Ref.begin(), Ref.end(), Mem);
263 Mem[Ref.size()] = 0;
264 return Mem;
Douglas Gregor669a25a2011-02-17 00:22:45 +0000265}
266
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000267StringRef CodeCompletionTUInfo::getParentName(const DeclContext *DC) {
268 const NamedDecl *ND = dyn_cast<NamedDecl>(DC);
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000269 if (!ND)
270 return StringRef();
271
272 // Check whether we've already cached the parent name.
273 StringRef &CachedParentName = ParentNames[DC];
274 if (!CachedParentName.empty())
275 return CachedParentName;
276
277 // If we already processed this DeclContext and assigned empty to it, the
278 // data pointer will be non-null.
Craig Topperc3ec1492014-05-26 06:22:03 +0000279 if (CachedParentName.data() != nullptr)
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000280 return StringRef();
281
282 // Find the interesting names.
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000283 SmallVector<const DeclContext *, 2> Contexts;
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000284 while (DC && !DC->isFunctionOrMethod()) {
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000285 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC)) {
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000286 if (ND->getIdentifier())
287 Contexts.push_back(DC);
288 }
289
290 DC = DC->getParent();
291 }
292
293 {
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000294 SmallString<128> S;
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000295 llvm::raw_svector_ostream OS(S);
296 bool First = true;
297 for (unsigned I = Contexts.size(); I != 0; --I) {
298 if (First)
299 First = false;
300 else {
301 OS << "::";
302 }
303
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000304 const DeclContext *CurDC = Contexts[I-1];
305 if (const ObjCCategoryImplDecl *CatImpl = dyn_cast<ObjCCategoryImplDecl>(CurDC))
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000306 CurDC = CatImpl->getCategoryDecl();
307
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000308 if (const ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CurDC)) {
309 const ObjCInterfaceDecl *Interface = Cat->getClassInterface();
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000310 if (!Interface) {
311 // Assign an empty StringRef but with non-null data to distinguish
312 // between empty because we didn't process the DeclContext yet.
Reid Klecknerd16cebe2016-02-10 19:09:15 +0000313 CachedParentName = StringRef((const char *)(uintptr_t)~0U, 0);
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000314 return StringRef();
315 }
316
317 OS << Interface->getName() << '(' << Cat->getName() << ')';
318 } else {
319 OS << cast<NamedDecl>(CurDC)->getName();
320 }
321 }
322
323 CachedParentName = AllocatorRef->CopyString(OS.str());
324 }
325
326 return CachedParentName;
327}
328
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000329CodeCompletionString *CodeCompletionBuilder::TakeString() {
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000330 void *Mem = getAllocator().Allocate(
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000331 sizeof(CodeCompletionString) + sizeof(Chunk) * Chunks.size() +
332 sizeof(const char *) * Annotations.size(),
333 alignof(CodeCompletionString));
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000334 CodeCompletionString *Result
335 = new (Mem) CodeCompletionString(Chunks.data(), Chunks.size(),
Erik Verbruggen98ea7f62011-10-14 15:31:08 +0000336 Priority, Availability,
Douglas Gregor78254c82012-03-27 23:34:16 +0000337 Annotations.data(), Annotations.size(),
Argyrios Kyrtzidis9ae39562012-09-26 16:39:56 +0000338 ParentName, BriefComment);
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000339 Chunks.clear();
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000340 return Result;
341}
Douglas Gregor9eb77012009-11-07 00:00:49 +0000342
Benjamin Kramerdb534a42012-03-26 16:57:36 +0000343void CodeCompletionBuilder::AddTypedTextChunk(const char *Text) {
344 Chunks.push_back(Chunk(CodeCompletionString::CK_TypedText, Text));
345}
346
347void CodeCompletionBuilder::AddTextChunk(const char *Text) {
348 Chunks.push_back(Chunk::CreateText(Text));
349}
350
351void CodeCompletionBuilder::AddOptionalChunk(CodeCompletionString *Optional) {
352 Chunks.push_back(Chunk::CreateOptional(Optional));
353}
354
355void CodeCompletionBuilder::AddPlaceholderChunk(const char *Placeholder) {
356 Chunks.push_back(Chunk::CreatePlaceholder(Placeholder));
357}
358
359void CodeCompletionBuilder::AddInformativeChunk(const char *Text) {
360 Chunks.push_back(Chunk::CreateInformative(Text));
361}
362
363void CodeCompletionBuilder::AddResultTypeChunk(const char *ResultType) {
364 Chunks.push_back(Chunk::CreateResultType(ResultType));
365}
366
367void
368CodeCompletionBuilder::AddCurrentParameterChunk(const char *CurrentParameter) {
369 Chunks.push_back(Chunk::CreateCurrentParameter(CurrentParameter));
370}
371
372void CodeCompletionBuilder::AddChunk(CodeCompletionString::ChunkKind CK,
373 const char *Text) {
374 Chunks.push_back(Chunk(CK, Text));
375}
376
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000377void CodeCompletionBuilder::addParentContext(const DeclContext *DC) {
Douglas Gregor78254c82012-03-27 23:34:16 +0000378 if (DC->isTranslationUnit()) {
Douglas Gregor78254c82012-03-27 23:34:16 +0000379 return;
380 }
381
382 if (DC->isFunctionOrMethod())
383 return;
384
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000385 const NamedDecl *ND = dyn_cast<NamedDecl>(DC);
Douglas Gregor78254c82012-03-27 23:34:16 +0000386 if (!ND)
387 return;
388
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000389 ParentName = getCodeCompletionTUInfo().getParentName(DC);
Douglas Gregor78254c82012-03-27 23:34:16 +0000390}
391
Dmitri Gribenko3292d062012-07-02 17:35:10 +0000392void CodeCompletionBuilder::addBriefComment(StringRef Comment) {
393 BriefComment = Allocator.CopyString(Comment);
394}
395
Douglas Gregorfedc3282009-09-18 22:15:54 +0000396//===----------------------------------------------------------------------===//
Douglas Gregor05f477c2009-09-23 00:16:58 +0000397// Code completion overload candidate implementation
398//===----------------------------------------------------------------------===//
399FunctionDecl *
400CodeCompleteConsumer::OverloadCandidate::getFunction() const {
401 if (getKind() == CK_Function)
402 return Function;
403 else if (getKind() == CK_FunctionTemplate)
404 return FunctionTemplate->getTemplatedDecl();
405 else
Craig Topperc3ec1492014-05-26 06:22:03 +0000406 return nullptr;
Douglas Gregor05f477c2009-09-23 00:16:58 +0000407}
408
409const FunctionType *
410CodeCompleteConsumer::OverloadCandidate::getFunctionType() const {
411 switch (Kind) {
412 case CK_Function:
413 return Function->getType()->getAs<FunctionType>();
414
415 case CK_FunctionTemplate:
416 return FunctionTemplate->getTemplatedDecl()->getType()
417 ->getAs<FunctionType>();
418
419 case CK_FunctionType:
420 return Type;
421 }
David Blaikie8a40f702012-01-17 06:56:22 +0000422
423 llvm_unreachable("Invalid CandidateKind!");
Douglas Gregor05f477c2009-09-23 00:16:58 +0000424}
425
426//===----------------------------------------------------------------------===//
Douglas Gregorfedc3282009-09-18 22:15:54 +0000427// Code completion consumer implementation
428//===----------------------------------------------------------------------===//
429
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000430CodeCompleteConsumer::~CodeCompleteConsumer() { }
Douglas Gregorfedc3282009-09-18 22:15:54 +0000431
Vassil Vassilev644ea612016-07-27 14:56:59 +0000432bool PrintingCodeCompleteConsumer::isResultFilteredOut(StringRef Filter,
433 CodeCompletionResult Result) {
434 switch (Result.Kind) {
435 case CodeCompletionResult::RK_Declaration: {
436 return !(Result.Declaration->getIdentifier() &&
437 Result.Declaration->getIdentifier()->getName().startswith(Filter));
438 }
439 case CodeCompletionResult::RK_Keyword: {
440 return !StringRef(Result.Keyword).startswith(Filter);
441 }
442 case CodeCompletionResult::RK_Macro: {
443 return !Result.Macro->getName().startswith(Filter);
444 }
445 case CodeCompletionResult::RK_Pattern: {
446 return !StringRef(Result.Pattern->getAsString()).startswith(Filter);
447 }
Vassil Vassilev644ea612016-07-27 14:56:59 +0000448 }
Simon Pilgrime71a1f92016-07-27 16:41:56 +0000449 llvm_unreachable("Unknown code completion result Kind.");
Vassil Vassilev644ea612016-07-27 14:56:59 +0000450}
451
Douglas Gregor2436e712009-09-17 21:32:03 +0000452void
Daniel Dunbar242ea9a2009-11-13 08:58:20 +0000453PrintingCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &SemaRef,
Douglas Gregor00c37ef2010-08-11 21:23:17 +0000454 CodeCompletionContext Context,
John McCall276321a2010-08-25 06:19:51 +0000455 CodeCompletionResult *Results,
Douglas Gregor2436e712009-09-17 21:32:03 +0000456 unsigned NumResults) {
Douglas Gregor49f67ce2010-08-26 13:48:20 +0000457 std::stable_sort(Results, Results + NumResults);
458
Vassil Vassilev644ea612016-07-27 14:56:59 +0000459 StringRef Filter = SemaRef.getPreprocessor().getCodeCompletionFilter();
460
Douglas Gregor2436e712009-09-17 21:32:03 +0000461 // Print the results.
462 for (unsigned I = 0; I != NumResults; ++I) {
Vassil Vassilev644ea612016-07-27 14:56:59 +0000463 if(!Filter.empty() && isResultFilteredOut(Filter, Results[I]))
464 continue;
Douglas Gregor58acf322009-10-09 22:16:47 +0000465 OS << "COMPLETION: ";
Douglas Gregor2436e712009-09-17 21:32:03 +0000466 switch (Results[I].Kind) {
John McCall276321a2010-08-25 06:19:51 +0000467 case CodeCompletionResult::RK_Declaration:
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000468 OS << *Results[I].Declaration;
Douglas Gregor2436e712009-09-17 21:32:03 +0000469 if (Results[I].Hidden)
470 OS << " (Hidden)";
Douglas Gregor3545ff42009-09-21 16:56:56 +0000471 if (CodeCompletionString *CCS
Douglas Gregorc3425b12015-07-07 06:20:19 +0000472 = Results[I].CreateCodeCompletionString(SemaRef, Context,
473 getAllocator(),
Dmitri Gribenko3292d062012-07-02 17:35:10 +0000474 CCTUInfo,
475 includeBriefComments())) {
Douglas Gregorfedc3282009-09-18 22:15:54 +0000476 OS << " : " << CCS->getAsString();
Dmitri Gribenko3292d062012-07-02 17:35:10 +0000477 if (const char *BriefComment = CCS->getBriefComment())
478 OS << " : " << BriefComment;
Douglas Gregorfedc3282009-09-18 22:15:54 +0000479 }
480
Douglas Gregor2436e712009-09-17 21:32:03 +0000481 OS << '\n';
482 break;
483
John McCall276321a2010-08-25 06:19:51 +0000484 case CodeCompletionResult::RK_Keyword:
Douglas Gregor52ce62f2010-01-13 23:24:38 +0000485 OS << Results[I].Keyword << '\n';
Douglas Gregor2436e712009-09-17 21:32:03 +0000486 break;
Douglas Gregorf329c7c2009-10-30 16:50:04 +0000487
John McCall276321a2010-08-25 06:19:51 +0000488 case CodeCompletionResult::RK_Macro: {
Douglas Gregor52ce62f2010-01-13 23:24:38 +0000489 OS << Results[I].Macro->getName();
Douglas Gregorf329c7c2009-10-30 16:50:04 +0000490 if (CodeCompletionString *CCS
Douglas Gregorc3425b12015-07-07 06:20:19 +0000491 = Results[I].CreateCodeCompletionString(SemaRef, Context,
492 getAllocator(),
Dmitri Gribenko3292d062012-07-02 17:35:10 +0000493 CCTUInfo,
494 includeBriefComments())) {
Douglas Gregorf329c7c2009-10-30 16:50:04 +0000495 OS << " : " << CCS->getAsString();
Douglas Gregorf329c7c2009-10-30 16:50:04 +0000496 }
497 OS << '\n';
498 break;
499 }
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000500
John McCall276321a2010-08-25 06:19:51 +0000501 case CodeCompletionResult::RK_Pattern: {
Douglas Gregor52ce62f2010-01-13 23:24:38 +0000502 OS << "Pattern : "
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000503 << Results[I].Pattern->getAsString() << '\n';
504 break;
505 }
Douglas Gregor2436e712009-09-17 21:32:03 +0000506 }
507 }
Douglas Gregor2436e712009-09-17 21:32:03 +0000508}
Douglas Gregor05f477c2009-09-23 00:16:58 +0000509
Francisco Lopes da Silva0c010cd2015-01-28 14:17:22 +0000510// This function is used solely to preserve the former presentation of overloads
511// by "clang -cc1 -code-completion-at", since CodeCompletionString::getAsString
512// needs to be improved for printing the newer and more detailed overload
513// chunks.
514static std::string getOverloadAsString(const CodeCompletionString &CCS) {
515 std::string Result;
516 llvm::raw_string_ostream OS(Result);
517
518 for (auto &C : CCS) {
519 switch (C.Kind) {
520 case CodeCompletionString::CK_Informative:
521 case CodeCompletionString::CK_ResultType:
522 OS << "[#" << C.Text << "#]";
523 break;
524
525 case CodeCompletionString::CK_CurrentParameter:
526 OS << "<#" << C.Text << "#>";
527 break;
528
529 default: OS << C.Text; break;
530 }
531 }
532 return OS.str();
533}
534
Douglas Gregor05f477c2009-09-23 00:16:58 +0000535void
Daniel Dunbar242ea9a2009-11-13 08:58:20 +0000536PrintingCodeCompleteConsumer::ProcessOverloadCandidates(Sema &SemaRef,
537 unsigned CurrentArg,
Douglas Gregor05f477c2009-09-23 00:16:58 +0000538 OverloadCandidate *Candidates,
539 unsigned NumCandidates) {
540 for (unsigned I = 0; I != NumCandidates; ++I) {
Douglas Gregorf0f51982009-09-23 00:34:09 +0000541 if (CodeCompletionString *CCS
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000542 = Candidates[I].CreateSignatureString(CurrentArg, SemaRef,
Francisco Lopes da Silva0c010cd2015-01-28 14:17:22 +0000543 getAllocator(), CCTUInfo,
544 includeBriefComments())) {
545 OS << "OVERLOAD: " << getOverloadAsString(*CCS) << "\n";
Douglas Gregor05f477c2009-09-23 00:16:58 +0000546 }
547 }
Douglas Gregor05f477c2009-09-23 00:16:58 +0000548}
Douglas Gregor9eb77012009-11-07 00:00:49 +0000549
Douglas Gregor7b316822012-03-17 06:39:06 +0000550/// \brief Retrieve the effective availability of the given declaration.
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000551static AvailabilityResult getDeclAvailability(const Decl *D) {
Douglas Gregor7b316822012-03-17 06:39:06 +0000552 AvailabilityResult AR = D->getAvailability();
553 if (isa<EnumConstantDecl>(D))
554 AR = std::max(AR, cast<Decl>(D->getDeclContext())->getAvailability());
555 return AR;
556}
557
Erik Verbruggen2e657ff2011-10-06 07:27:49 +0000558void CodeCompletionResult::computeCursorKindAndAvailability(bool Accessible) {
Douglas Gregorb14904c2010-08-13 22:48:40 +0000559 switch (Kind) {
Douglas Gregor78254c82012-03-27 23:34:16 +0000560 case RK_Pattern:
561 if (!Declaration) {
562 // Do nothing: Patterns can come with cursor kinds!
563 break;
564 }
Adrian Prantlf3b3ccd2017-12-19 22:06:11 +0000565 LLVM_FALLTHROUGH;
Douglas Gregor78254c82012-03-27 23:34:16 +0000566
Douglas Gregor7b316822012-03-17 06:39:06 +0000567 case RK_Declaration: {
Douglas Gregorf757a122010-08-23 23:00:57 +0000568 // Set the availability based on attributes.
Douglas Gregor7b316822012-03-17 06:39:06 +0000569 switch (getDeclAvailability(Declaration)) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000570 case AR_Available:
571 case AR_NotYetIntroduced:
572 Availability = CXAvailability_Available;
573 break;
Douglas Gregorf757a122010-08-23 23:00:57 +0000574
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000575 case AR_Deprecated:
576 Availability = CXAvailability_Deprecated;
577 break;
578
579 case AR_Unavailable:
580 Availability = CXAvailability_NotAvailable;
581 break;
582 }
583
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000584 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(Declaration))
Douglas Gregor09c0eb12010-09-03 23:30:36 +0000585 if (Function->isDeleted())
Douglas Gregorf757a122010-08-23 23:00:57 +0000586 Availability = CXAvailability_NotAvailable;
Douglas Gregor09c0eb12010-09-03 23:30:36 +0000587
588 CursorKind = getCursorKindForDecl(Declaration);
Douglas Gregordeafd0b2011-12-27 22:43:10 +0000589 if (CursorKind == CXCursor_UnexposedDecl) {
Douglas Gregorf6102672012-01-01 21:23:57 +0000590 // FIXME: Forward declarations of Objective-C classes and protocols
591 // are not directly exposed, but we want code completion to treat them
592 // like a definition.
Douglas Gregordeafd0b2011-12-27 22:43:10 +0000593 if (isa<ObjCInterfaceDecl>(Declaration))
594 CursorKind = CXCursor_ObjCInterfaceDecl;
Douglas Gregorf6102672012-01-01 21:23:57 +0000595 else if (isa<ObjCProtocolDecl>(Declaration))
596 CursorKind = CXCursor_ObjCProtocolDecl;
Douglas Gregordeafd0b2011-12-27 22:43:10 +0000597 else
598 CursorKind = CXCursor_NotImplemented;
599 }
Douglas Gregorb14904c2010-08-13 22:48:40 +0000600 break;
Douglas Gregor7b316822012-03-17 06:39:06 +0000601 }
Douglas Gregorb14904c2010-08-13 22:48:40 +0000602
John McCall276321a2010-08-25 06:19:51 +0000603 case RK_Macro:
John McCall276321a2010-08-25 06:19:51 +0000604 case RK_Keyword:
Benjamin Kramerc25c0b92012-05-20 14:19:46 +0000605 llvm_unreachable("Macro and keyword kinds are handled by the constructors");
Douglas Gregor8e984da2010-08-04 16:47:14 +0000606 }
Erik Verbruggen2e657ff2011-10-06 07:27:49 +0000607
608 if (!Accessible)
609 Availability = CXAvailability_NotAccessible;
Douglas Gregor8e984da2010-08-04 16:47:14 +0000610}
Douglas Gregorf09935f2009-12-01 05:55:20 +0000611
Douglas Gregor0de55ce2010-08-25 18:41:16 +0000612/// \brief Retrieve the name that should be used to order a result.
613///
614/// If the name needs to be constructed as a string, that string will be
615/// saved into Saved and the returned StringRef will refer to it.
Sam McCall1102a862017-11-15 09:15:06 +0000616StringRef CodeCompletionResult::getOrderedName(std::string &Saved) const {
617 switch (Kind) {
618 case RK_Keyword:
619 return Keyword;
620 case RK_Pattern:
621 return Pattern->getTypedText();
622 case RK_Macro:
623 return Macro->getName();
624 case RK_Declaration:
Douglas Gregor0de55ce2010-08-25 18:41:16 +0000625 // Handle declarations below.
626 break;
627 }
628
Sam McCall1102a862017-11-15 09:15:06 +0000629 DeclarationName Name = Declaration->getDeclName();
Douglas Gregor0de55ce2010-08-25 18:41:16 +0000630
631 // If the name is a simple identifier (by far the common case), or a
632 // zero-argument selector, just return a reference to that identifier.
633 if (IdentifierInfo *Id = Name.getAsIdentifierInfo())
634 return Id->getName();
635 if (Name.isObjCZeroArgSelector())
636 if (IdentifierInfo *Id
637 = Name.getObjCSelector().getIdentifierInfoForSlot(0))
638 return Id->getName();
639
640 Saved = Name.getAsString();
641 return Saved;
642}
643
644bool clang::operator<(const CodeCompletionResult &X,
645 const CodeCompletionResult &Y) {
646 std::string XSaved, YSaved;
Sam McCall1102a862017-11-15 09:15:06 +0000647 StringRef XStr = X.getOrderedName(XSaved);
648 StringRef YStr = Y.getOrderedName(YSaved);
Douglas Gregor0de55ce2010-08-25 18:41:16 +0000649 int cmp = XStr.compare_lower(YStr);
650 if (cmp)
651 return cmp < 0;
652
Douglas Gregor49f67ce2010-08-26 13:48:20 +0000653 // If case-insensitive comparison fails, try case-sensitive comparison.
654 cmp = XStr.compare(YStr);
655 if (cmp)
656 return cmp < 0;
Douglas Gregor0de55ce2010-08-25 18:41:16 +0000657
658 return false;
659}