blob: 754169fe5f140dbc65db022b822593e5a2a88187 [file] [log] [blame]
Eugene Zelenko711964d2018-02-20 02:16:28 +00001//===- CodeCompleteConsumer.cpp - Code Completion Interface ---------------===//
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//===----------------------------------------------------------------------===//
Eugene Zelenko711964d2018-02-20 02:16:28 +000013
Douglas Gregor2436e712009-09-17 21:32:03 +000014#include "clang/Sema/CodeCompleteConsumer.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000015#include "clang-c/Index.h"
Eugene Zelenko711964d2018-02-20 02:16:28 +000016#include "clang/AST/Decl.h"
17#include "clang/AST/DeclBase.h"
John McCallde6836a2010-08-24 07:21:54 +000018#include "clang/AST/DeclObjC.h"
John McCall19c1bfd2010-08-25 05:32:35 +000019#include "clang/AST/DeclTemplate.h"
Eugene Zelenko711964d2018-02-20 02:16:28 +000020#include "clang/AST/DeclarationName.h"
21#include "clang/AST/Type.h"
22#include "clang/Basic/IdentifierTable.h"
Vassil Vassilev644ea612016-07-27 14:56:59 +000023#include "clang/Lex/Preprocessor.h"
Ilya Biryukov2fab2352018-08-30 13:08:03 +000024#include "clang/Sema/Sema.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000025#include "llvm/ADT/SmallString.h"
Eugene Zelenko711964d2018-02-20 02:16:28 +000026#include "llvm/ADT/SmallVector.h"
27#include "llvm/ADT/StringRef.h"
Douglas Gregor669a25a2011-02-17 00:22:45 +000028#include "llvm/ADT/Twine.h"
Eugene Zelenko711964d2018-02-20 02:16:28 +000029#include "llvm/Support/Casting.h"
30#include "llvm/Support/Compiler.h"
31#include "llvm/Support/ErrorHandling.h"
Ilya Biryukov2fab2352018-08-30 13:08:03 +000032#include "llvm/Support/FormatVariadic.h"
Douglas Gregor2436e712009-09-17 21:32:03 +000033#include "llvm/Support/raw_ostream.h"
34#include <algorithm>
Eugene Zelenko711964d2018-02-20 02:16:28 +000035#include <cassert>
36#include <cstdint>
37#include <string>
Douglas Gregorab6ccb52009-11-17 16:43:05 +000038
Douglas Gregor2436e712009-09-17 21:32:03 +000039using namespace clang;
40
Douglas Gregorfedc3282009-09-18 22:15:54 +000041//===----------------------------------------------------------------------===//
Douglas Gregor0212fd72010-09-21 16:06:22 +000042// Code completion context implementation
43//===----------------------------------------------------------------------===//
44
45bool CodeCompletionContext::wantConstructorResults() const {
Ilya Biryukov3289ab22018-02-19 13:53:49 +000046 switch (CCKind) {
Douglas Gregor0ac41382010-09-23 23:01:17 +000047 case CCC_Recovery:
Douglas Gregor0212fd72010-09-21 16:06:22 +000048 case CCC_Statement:
49 case CCC_Expression:
50 case CCC_ObjCMessageReceiver:
51 case CCC_ParenthesizedExpression:
52 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +000053
Douglas Gregor0212fd72010-09-21 16:06:22 +000054 case CCC_TopLevel:
55 case CCC_ObjCInterface:
56 case CCC_ObjCImplementation:
57 case CCC_ObjCIvarList:
58 case CCC_ClassStructUnion:
Douglas Gregor21325842011-07-07 16:03:39 +000059 case CCC_DotMemberAccess:
60 case CCC_ArrowMemberAccess:
61 case CCC_ObjCPropertyAccess:
Douglas Gregor0212fd72010-09-21 16:06:22 +000062 case CCC_EnumTag:
63 case CCC_UnionTag:
64 case CCC_ClassOrStructTag:
65 case CCC_ObjCProtocolName:
66 case CCC_Namespace:
67 case CCC_Type:
68 case CCC_Name:
69 case CCC_PotentiallyQualifiedName:
70 case CCC_MacroName:
71 case CCC_MacroNameUse:
72 case CCC_PreprocessorExpression:
73 case CCC_PreprocessorDirective:
74 case CCC_NaturalLanguage:
75 case CCC_SelectorName:
76 case CCC_TypeQualifiers:
Douglas Gregor0ac41382010-09-23 23:01:17 +000077 case CCC_Other:
Douglas Gregor3a69eaf2011-02-18 23:30:37 +000078 case CCC_OtherWithMacros:
Douglas Gregor21325842011-07-07 16:03:39 +000079 case CCC_ObjCInstanceMessage:
80 case CCC_ObjCClassMessage:
Douglas Gregor2c595ad2011-07-30 06:55:39 +000081 case CCC_ObjCInterfaceName:
Douglas Gregor21325842011-07-07 16:03:39 +000082 case CCC_ObjCCategoryName:
Sam McCall3d8051a2018-09-18 08:40:41 +000083 case CCC_IncludedFile:
Douglas Gregor0212fd72010-09-21 16:06:22 +000084 return false;
85 }
David Blaikie8a40f702012-01-17 06:56:22 +000086
87 llvm_unreachable("Invalid CodeCompletionContext::Kind!");
Douglas Gregor0212fd72010-09-21 16:06:22 +000088}
89
Ilya Biryukov3289ab22018-02-19 13:53:49 +000090StringRef clang::getCompletionKindString(CodeCompletionContext::Kind Kind) {
91 using CCKind = CodeCompletionContext::Kind;
Ilya Biryukov27d82582018-02-19 12:35:33 +000092 switch (Kind) {
93 case CCKind::CCC_Other:
94 return "Other";
95 case CCKind::CCC_OtherWithMacros:
96 return "OtherWithMacros";
97 case CCKind::CCC_TopLevel:
98 return "TopLevel";
99 case CCKind::CCC_ObjCInterface:
100 return "ObjCInterface";
101 case CCKind::CCC_ObjCImplementation:
102 return "ObjCImplementation";
103 case CCKind::CCC_ObjCIvarList:
104 return "ObjCIvarList";
105 case CCKind::CCC_ClassStructUnion:
106 return "ClassStructUnion";
107 case CCKind::CCC_Statement:
108 return "Statement";
109 case CCKind::CCC_Expression:
110 return "Expression";
111 case CCKind::CCC_ObjCMessageReceiver:
112 return "ObjCMessageReceiver";
113 case CCKind::CCC_DotMemberAccess:
114 return "DotMemberAccess";
115 case CCKind::CCC_ArrowMemberAccess:
116 return "ArrowMemberAccess";
117 case CCKind::CCC_ObjCPropertyAccess:
118 return "ObjCPropertyAccess";
119 case CCKind::CCC_EnumTag:
120 return "EnumTag";
121 case CCKind::CCC_UnionTag:
122 return "UnionTag";
123 case CCKind::CCC_ClassOrStructTag:
124 return "ClassOrStructTag";
125 case CCKind::CCC_ObjCProtocolName:
126 return "ObjCProtocolName";
127 case CCKind::CCC_Namespace:
128 return "Namespace";
129 case CCKind::CCC_Type:
130 return "Type";
131 case CCKind::CCC_Name:
132 return "Name";
133 case CCKind::CCC_PotentiallyQualifiedName:
134 return "PotentiallyQualifiedName";
135 case CCKind::CCC_MacroName:
136 return "MacroName";
137 case CCKind::CCC_MacroNameUse:
138 return "MacroNameUse";
139 case CCKind::CCC_PreprocessorExpression:
140 return "PreprocessorExpression";
141 case CCKind::CCC_PreprocessorDirective:
142 return "PreprocessorDirective";
143 case CCKind::CCC_NaturalLanguage:
144 return "NaturalLanguage";
145 case CCKind::CCC_SelectorName:
146 return "SelectorName";
147 case CCKind::CCC_TypeQualifiers:
148 return "TypeQualifiers";
149 case CCKind::CCC_ParenthesizedExpression:
150 return "ParenthesizedExpression";
151 case CCKind::CCC_ObjCInstanceMessage:
152 return "ObjCInstanceMessage";
153 case CCKind::CCC_ObjCClassMessage:
154 return "ObjCClassMessage";
155 case CCKind::CCC_ObjCInterfaceName:
156 return "ObjCInterfaceName";
157 case CCKind::CCC_ObjCCategoryName:
158 return "ObjCCategoryName";
Sam McCall3d8051a2018-09-18 08:40:41 +0000159 case CCKind::CCC_IncludedFile:
160 return "IncludedFile";
Ilya Biryukov27d82582018-02-19 12:35:33 +0000161 case CCKind::CCC_Recovery:
162 return "Recovery";
163 }
164 llvm_unreachable("Invalid CodeCompletionContext::Kind!");
165}
166
Douglas Gregor0212fd72010-09-21 16:06:22 +0000167//===----------------------------------------------------------------------===//
Douglas Gregorfedc3282009-09-18 22:15:54 +0000168// Code completion string implementation
169//===----------------------------------------------------------------------===//
Eugene Zelenko711964d2018-02-20 02:16:28 +0000170
Fangrui Song6907ce22018-07-30 19:24:48 +0000171CodeCompletionString::Chunk::Chunk(ChunkKind Kind, const char *Text)
Eugene Zelenko711964d2018-02-20 02:16:28 +0000172 : Kind(Kind), Text("") {
Douglas Gregor9eb77012009-11-07 00:00:49 +0000173 switch (Kind) {
174 case CK_TypedText:
175 case CK_Text:
176 case CK_Placeholder:
177 case CK_Informative:
Douglas Gregorb3fa9192009-12-18 18:53:37 +0000178 case CK_ResultType:
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000179 case CK_CurrentParameter:
180 this->Text = Text;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000181 break;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000182
183 case CK_Optional:
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000184 llvm_unreachable("Optional strings cannot be created from text");
Fangrui Song6907ce22018-07-30 19:24:48 +0000185
Douglas Gregor9eb77012009-11-07 00:00:49 +0000186 case CK_LeftParen:
187 this->Text = "(";
188 break;
189
190 case CK_RightParen:
191 this->Text = ")";
192 break;
193
194 case CK_LeftBracket:
195 this->Text = "[";
196 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000197
Douglas Gregor9eb77012009-11-07 00:00:49 +0000198 case CK_RightBracket:
199 this->Text = "]";
200 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000201
Douglas Gregor9eb77012009-11-07 00:00:49 +0000202 case CK_LeftBrace:
203 this->Text = "{";
204 break;
205
206 case CK_RightBrace:
207 this->Text = "}";
208 break;
209
210 case CK_LeftAngle:
211 this->Text = "<";
212 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000213
Douglas Gregor9eb77012009-11-07 00:00:49 +0000214 case CK_RightAngle:
215 this->Text = ">";
216 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000217
Douglas Gregor9eb77012009-11-07 00:00:49 +0000218 case CK_Comma:
219 this->Text = ", ";
220 break;
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000221
222 case CK_Colon:
Douglas Gregor636a61e2010-04-07 00:21:17 +0000223 this->Text = ":";
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000224 break;
225
226 case CK_SemiColon:
227 this->Text = ";";
228 break;
229
230 case CK_Equal:
231 this->Text = " = ";
232 break;
233
234 case CK_HorizontalSpace:
235 this->Text = " ";
236 break;
237
238 case CK_VerticalSpace:
239 this->Text = "\n";
240 break;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000241 }
Douglas Gregor5bf52692009-09-22 23:15:58 +0000242}
243
244CodeCompletionString::Chunk
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000245CodeCompletionString::Chunk::CreateText(const char *Text) {
Douglas Gregor5bf52692009-09-22 23:15:58 +0000246 return Chunk(CK_Text, Text);
Douglas Gregorfedc3282009-09-18 22:15:54 +0000247}
248
Fangrui Song6907ce22018-07-30 19:24:48 +0000249CodeCompletionString::Chunk
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000250CodeCompletionString::Chunk::CreateOptional(CodeCompletionString *Optional) {
Douglas Gregorfedc3282009-09-18 22:15:54 +0000251 Chunk Result;
252 Result.Kind = CK_Optional;
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000253 Result.Optional = Optional;
Douglas Gregorfedc3282009-09-18 22:15:54 +0000254 return Result;
255}
256
Fangrui Song6907ce22018-07-30 19:24:48 +0000257CodeCompletionString::Chunk
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000258CodeCompletionString::Chunk::CreatePlaceholder(const char *Placeholder) {
Douglas Gregor5bf52692009-09-22 23:15:58 +0000259 return Chunk(CK_Placeholder, Placeholder);
260}
261
Fangrui Song6907ce22018-07-30 19:24:48 +0000262CodeCompletionString::Chunk
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000263CodeCompletionString::Chunk::CreateInformative(const char *Informative) {
Douglas Gregor5bf52692009-09-22 23:15:58 +0000264 return Chunk(CK_Informative, Informative);
Douglas Gregorfedc3282009-09-18 22:15:54 +0000265}
266
Fangrui Song6907ce22018-07-30 19:24:48 +0000267CodeCompletionString::Chunk
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000268CodeCompletionString::Chunk::CreateResultType(const char *ResultType) {
Douglas Gregorb3fa9192009-12-18 18:53:37 +0000269 return Chunk(CK_ResultType, ResultType);
270}
271
Fangrui Song6907ce22018-07-30 19:24:48 +0000272CodeCompletionString::Chunk
Douglas Gregor9eb77012009-11-07 00:00:49 +0000273CodeCompletionString::Chunk::CreateCurrentParameter(
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000274 const char *CurrentParameter) {
Douglas Gregor9eb77012009-11-07 00:00:49 +0000275 return Chunk(CK_CurrentParameter, CurrentParameter);
276}
277
Fangrui Song6907ce22018-07-30 19:24:48 +0000278CodeCompletionString::CodeCompletionString(const Chunk *Chunks,
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000279 unsigned NumChunks,
Fangrui Song6907ce22018-07-30 19:24:48 +0000280 unsigned Priority,
Erik Verbruggen98ea7f62011-10-14 15:31:08 +0000281 CXAvailabilityKind Availability,
282 const char **Annotations,
Douglas Gregor78254c82012-03-27 23:34:16 +0000283 unsigned NumAnnotations,
Dmitri Gribenko3292d062012-07-02 17:35:10 +0000284 StringRef ParentName,
285 const char *BriefComment)
Eugene Zelenko711964d2018-02-20 02:16:28 +0000286 : NumChunks(NumChunks), NumAnnotations(NumAnnotations),
287 Priority(Priority), Availability(Availability),
Fangrui Song6907ce22018-07-30 19:24:48 +0000288 ParentName(ParentName), BriefComment(BriefComment) {
Erik Verbruggen98ea7f62011-10-14 15:31:08 +0000289 assert(NumChunks <= 0xffff);
290 assert(NumAnnotations <= 0xffff);
291
Eugene Zelenko1e95bc02018-04-05 22:15:42 +0000292 Chunk *StoredChunks = reinterpret_cast<Chunk *>(this + 1);
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000293 for (unsigned I = 0; I != NumChunks; ++I)
294 StoredChunks[I] = Chunks[I];
Erik Verbruggen98ea7f62011-10-14 15:31:08 +0000295
Eugene Zelenko1e95bc02018-04-05 22:15:42 +0000296 const char **StoredAnnotations = reinterpret_cast<const char **>(StoredChunks + NumChunks);
Erik Verbruggen98ea7f62011-10-14 15:31:08 +0000297 for (unsigned I = 0; I != NumAnnotations; ++I)
298 StoredAnnotations[I] = Annotations[I];
Douglas Gregorfedc3282009-09-18 22:15:54 +0000299}
300
Erik Verbruggen98ea7f62011-10-14 15:31:08 +0000301unsigned CodeCompletionString::getAnnotationCount() const {
302 return NumAnnotations;
303}
304
305const char *CodeCompletionString::getAnnotation(unsigned AnnotationNr) const {
306 if (AnnotationNr < NumAnnotations)
307 return reinterpret_cast<const char * const*>(end())[AnnotationNr];
308 else
Craig Topperc3ec1492014-05-26 06:22:03 +0000309 return nullptr;
Erik Verbruggen98ea7f62011-10-14 15:31:08 +0000310}
311
Douglas Gregorfedc3282009-09-18 22:15:54 +0000312std::string CodeCompletionString::getAsString() const {
313 std::string Result;
314 llvm::raw_string_ostream OS(Result);
Fangrui Song6907ce22018-07-30 19:24:48 +0000315
Douglas Gregorfedc3282009-09-18 22:15:54 +0000316 for (iterator C = begin(), CEnd = end(); C != CEnd; ++C) {
317 switch (C->Kind) {
Douglas Gregorfedc3282009-09-18 22:15:54 +0000318 case CK_Optional: OS << "{#" << C->Optional->getAsString() << "#}"; break;
Douglas Gregor5bf52692009-09-22 23:15:58 +0000319 case CK_Placeholder: OS << "<#" << C->Text << "#>"; break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000320
321 case CK_Informative:
Douglas Gregorb3fa9192009-12-18 18:53:37 +0000322 case CK_ResultType:
Fangrui Song6907ce22018-07-30 19:24:48 +0000323 OS << "[#" << C->Text << "#]";
Douglas Gregorb3fa9192009-12-18 18:53:37 +0000324 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000325
Douglas Gregor9eb77012009-11-07 00:00:49 +0000326 case CK_CurrentParameter: OS << "<#" << C->Text << "#>"; break;
327 default: OS << C->Text; break;
Douglas Gregorfedc3282009-09-18 22:15:54 +0000328 }
329 }
Dan Gohman4888f1a2010-07-26 21:33:22 +0000330 return OS.str();
Douglas Gregorfedc3282009-09-18 22:15:54 +0000331}
332
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000333const char *CodeCompletionString::getTypedText() const {
334 for (iterator C = begin(), CEnd = end(); C != CEnd; ++C)
335 if (C->Kind == CK_TypedText)
336 return C->Text;
Craig Topperc3ec1492014-05-26 06:22:03 +0000337
338 return nullptr;
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000339}
340
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000341const char *CodeCompletionAllocator::CopyString(const Twine &String) {
342 SmallString<128> Data;
343 StringRef Ref = String.toStringRef(Data);
Douglas Gregor669a25a2011-02-17 00:22:45 +0000344 // FIXME: It would be more efficient to teach Twine to tell us its size and
345 // then add a routine there to fill in an allocated char* with the contents
346 // of the string.
Eugene Zelenko1e95bc02018-04-05 22:15:42 +0000347 char *Mem = (char *)Allocate(Ref.size() + 1, 1);
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000348 std::copy(Ref.begin(), Ref.end(), Mem);
349 Mem[Ref.size()] = 0;
350 return Mem;
Douglas Gregor669a25a2011-02-17 00:22:45 +0000351}
352
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000353StringRef CodeCompletionTUInfo::getParentName(const DeclContext *DC) {
Eugene Zelenko1e95bc02018-04-05 22:15:42 +0000354 const NamedDecl *ND = dyn_cast<NamedDecl>(DC);
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000355 if (!ND)
Eugene Zelenko711964d2018-02-20 02:16:28 +0000356 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +0000357
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000358 // Check whether we've already cached the parent name.
359 StringRef &CachedParentName = ParentNames[DC];
360 if (!CachedParentName.empty())
361 return CachedParentName;
362
363 // If we already processed this DeclContext and assigned empty to it, the
364 // data pointer will be non-null.
Craig Topperc3ec1492014-05-26 06:22:03 +0000365 if (CachedParentName.data() != nullptr)
Eugene Zelenko711964d2018-02-20 02:16:28 +0000366 return {};
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000367
368 // Find the interesting names.
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000369 SmallVector<const DeclContext *, 2> Contexts;
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000370 while (DC && !DC->isFunctionOrMethod()) {
Eugene Zelenko1e95bc02018-04-05 22:15:42 +0000371 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC)) {
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000372 if (ND->getIdentifier())
373 Contexts.push_back(DC);
374 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000375
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000376 DC = DC->getParent();
377 }
378
379 {
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000380 SmallString<128> S;
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000381 llvm::raw_svector_ostream OS(S);
382 bool First = true;
383 for (unsigned I = Contexts.size(); I != 0; --I) {
384 if (First)
385 First = false;
386 else {
387 OS << "::";
388 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000389
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000390 const DeclContext *CurDC = Contexts[I-1];
Eugene Zelenko1e95bc02018-04-05 22:15:42 +0000391 if (const ObjCCategoryImplDecl *CatImpl = dyn_cast<ObjCCategoryImplDecl>(CurDC))
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000392 CurDC = CatImpl->getCategoryDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +0000393
Eugene Zelenko1e95bc02018-04-05 22:15:42 +0000394 if (const ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CurDC)) {
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000395 const ObjCInterfaceDecl *Interface = Cat->getClassInterface();
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000396 if (!Interface) {
397 // Assign an empty StringRef but with non-null data to distinguish
398 // between empty because we didn't process the DeclContext yet.
Reid Klecknerd16cebe2016-02-10 19:09:15 +0000399 CachedParentName = StringRef((const char *)(uintptr_t)~0U, 0);
Eugene Zelenko711964d2018-02-20 02:16:28 +0000400 return {};
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000401 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000402
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000403 OS << Interface->getName() << '(' << Cat->getName() << ')';
404 } else {
405 OS << cast<NamedDecl>(CurDC)->getName();
406 }
407 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000408
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000409 CachedParentName = AllocatorRef->CopyString(OS.str());
410 }
411
412 return CachedParentName;
413}
414
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000415CodeCompletionString *CodeCompletionBuilder::TakeString() {
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000416 void *Mem = getAllocator().Allocate(
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000417 sizeof(CodeCompletionString) + sizeof(Chunk) * Chunks.size() +
418 sizeof(const char *) * Annotations.size(),
419 alignof(CodeCompletionString));
Fangrui Song6907ce22018-07-30 19:24:48 +0000420 CodeCompletionString *Result
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000421 = new (Mem) CodeCompletionString(Chunks.data(), Chunks.size(),
Erik Verbruggen98ea7f62011-10-14 15:31:08 +0000422 Priority, Availability,
Douglas Gregor78254c82012-03-27 23:34:16 +0000423 Annotations.data(), Annotations.size(),
Argyrios Kyrtzidis9ae39562012-09-26 16:39:56 +0000424 ParentName, BriefComment);
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000425 Chunks.clear();
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000426 return Result;
427}
Douglas Gregor9eb77012009-11-07 00:00:49 +0000428
Benjamin Kramerdb534a42012-03-26 16:57:36 +0000429void CodeCompletionBuilder::AddTypedTextChunk(const char *Text) {
430 Chunks.push_back(Chunk(CodeCompletionString::CK_TypedText, Text));
431}
432
433void CodeCompletionBuilder::AddTextChunk(const char *Text) {
434 Chunks.push_back(Chunk::CreateText(Text));
435}
436
437void CodeCompletionBuilder::AddOptionalChunk(CodeCompletionString *Optional) {
438 Chunks.push_back(Chunk::CreateOptional(Optional));
439}
440
441void CodeCompletionBuilder::AddPlaceholderChunk(const char *Placeholder) {
442 Chunks.push_back(Chunk::CreatePlaceholder(Placeholder));
443}
444
445void CodeCompletionBuilder::AddInformativeChunk(const char *Text) {
446 Chunks.push_back(Chunk::CreateInformative(Text));
447}
448
449void CodeCompletionBuilder::AddResultTypeChunk(const char *ResultType) {
450 Chunks.push_back(Chunk::CreateResultType(ResultType));
451}
452
453void
454CodeCompletionBuilder::AddCurrentParameterChunk(const char *CurrentParameter) {
455 Chunks.push_back(Chunk::CreateCurrentParameter(CurrentParameter));
456}
457
458void CodeCompletionBuilder::AddChunk(CodeCompletionString::ChunkKind CK,
459 const char *Text) {
460 Chunks.push_back(Chunk(CK, Text));
461}
462
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000463void CodeCompletionBuilder::addParentContext(const DeclContext *DC) {
Eugene Zelenko711964d2018-02-20 02:16:28 +0000464 if (DC->isTranslationUnit())
Douglas Gregor78254c82012-03-27 23:34:16 +0000465 return;
Fangrui Song6907ce22018-07-30 19:24:48 +0000466
Douglas Gregor78254c82012-03-27 23:34:16 +0000467 if (DC->isFunctionOrMethod())
468 return;
Fangrui Song6907ce22018-07-30 19:24:48 +0000469
Eugene Zelenko1e95bc02018-04-05 22:15:42 +0000470 const NamedDecl *ND = dyn_cast<NamedDecl>(DC);
Douglas Gregor78254c82012-03-27 23:34:16 +0000471 if (!ND)
472 return;
Fangrui Song6907ce22018-07-30 19:24:48 +0000473
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000474 ParentName = getCodeCompletionTUInfo().getParentName(DC);
Douglas Gregor78254c82012-03-27 23:34:16 +0000475}
476
Dmitri Gribenko3292d062012-07-02 17:35:10 +0000477void CodeCompletionBuilder::addBriefComment(StringRef Comment) {
478 BriefComment = Allocator.CopyString(Comment);
479}
480
Douglas Gregorfedc3282009-09-18 22:15:54 +0000481//===----------------------------------------------------------------------===//
Douglas Gregor05f477c2009-09-23 00:16:58 +0000482// Code completion overload candidate implementation
483//===----------------------------------------------------------------------===//
484FunctionDecl *
485CodeCompleteConsumer::OverloadCandidate::getFunction() const {
486 if (getKind() == CK_Function)
487 return Function;
488 else if (getKind() == CK_FunctionTemplate)
489 return FunctionTemplate->getTemplatedDecl();
490 else
Craig Topperc3ec1492014-05-26 06:22:03 +0000491 return nullptr;
Douglas Gregor05f477c2009-09-23 00:16:58 +0000492}
493
494const FunctionType *
495CodeCompleteConsumer::OverloadCandidate::getFunctionType() const {
496 switch (Kind) {
497 case CK_Function:
498 return Function->getType()->getAs<FunctionType>();
Fangrui Song6907ce22018-07-30 19:24:48 +0000499
Douglas Gregor05f477c2009-09-23 00:16:58 +0000500 case CK_FunctionTemplate:
501 return FunctionTemplate->getTemplatedDecl()->getType()
502 ->getAs<FunctionType>();
Fangrui Song6907ce22018-07-30 19:24:48 +0000503
Douglas Gregor05f477c2009-09-23 00:16:58 +0000504 case CK_FunctionType:
505 return Type;
506 }
David Blaikie8a40f702012-01-17 06:56:22 +0000507
508 llvm_unreachable("Invalid CandidateKind!");
Douglas Gregor05f477c2009-09-23 00:16:58 +0000509}
510
511//===----------------------------------------------------------------------===//
Douglas Gregorfedc3282009-09-18 22:15:54 +0000512// Code completion consumer implementation
513//===----------------------------------------------------------------------===//
514
Eugene Zelenko711964d2018-02-20 02:16:28 +0000515CodeCompleteConsumer::~CodeCompleteConsumer() = default;
Douglas Gregorfedc3282009-09-18 22:15:54 +0000516
Vassil Vassilev644ea612016-07-27 14:56:59 +0000517bool PrintingCodeCompleteConsumer::isResultFilteredOut(StringRef Filter,
518 CodeCompletionResult Result) {
519 switch (Result.Kind) {
Eugene Zelenko711964d2018-02-20 02:16:28 +0000520 case CodeCompletionResult::RK_Declaration:
Vassil Vassilev644ea612016-07-27 14:56:59 +0000521 return !(Result.Declaration->getIdentifier() &&
522 Result.Declaration->getIdentifier()->getName().startswith(Filter));
Eugene Zelenko711964d2018-02-20 02:16:28 +0000523 case CodeCompletionResult::RK_Keyword:
Vassil Vassilev644ea612016-07-27 14:56:59 +0000524 return !StringRef(Result.Keyword).startswith(Filter);
Eugene Zelenko711964d2018-02-20 02:16:28 +0000525 case CodeCompletionResult::RK_Macro:
Vassil Vassilev644ea612016-07-27 14:56:59 +0000526 return !Result.Macro->getName().startswith(Filter);
Eugene Zelenko711964d2018-02-20 02:16:28 +0000527 case CodeCompletionResult::RK_Pattern:
Sam McCall3d8051a2018-09-18 08:40:41 +0000528 return !(Result.Pattern->getTypedText() &&
529 StringRef(Result.Pattern->getTypedText()).startswith(Filter));
Vassil Vassilev644ea612016-07-27 14:56:59 +0000530 }
Simon Pilgrime71a1f92016-07-27 16:41:56 +0000531 llvm_unreachable("Unknown code completion result Kind.");
Vassil Vassilev644ea612016-07-27 14:56:59 +0000532}
533
Fangrui Song6907ce22018-07-30 19:24:48 +0000534void
Daniel Dunbar242ea9a2009-11-13 08:58:20 +0000535PrintingCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &SemaRef,
Douglas Gregor00c37ef2010-08-11 21:23:17 +0000536 CodeCompletionContext Context,
John McCall276321a2010-08-25 06:19:51 +0000537 CodeCompletionResult *Results,
Douglas Gregor2436e712009-09-17 21:32:03 +0000538 unsigned NumResults) {
Douglas Gregor49f67ce2010-08-26 13:48:20 +0000539 std::stable_sort(Results, Results + NumResults);
Fangrui Song6907ce22018-07-30 19:24:48 +0000540
Vassil Vassilev644ea612016-07-27 14:56:59 +0000541 StringRef Filter = SemaRef.getPreprocessor().getCodeCompletionFilter();
542
Douglas Gregor2436e712009-09-17 21:32:03 +0000543 // Print the results.
544 for (unsigned I = 0; I != NumResults; ++I) {
Vassil Vassilev644ea612016-07-27 14:56:59 +0000545 if(!Filter.empty() && isResultFilteredOut(Filter, Results[I]))
546 continue;
Douglas Gregor58acf322009-10-09 22:16:47 +0000547 OS << "COMPLETION: ";
Douglas Gregor2436e712009-09-17 21:32:03 +0000548 switch (Results[I].Kind) {
John McCall276321a2010-08-25 06:19:51 +0000549 case CodeCompletionResult::RK_Declaration:
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000550 OS << *Results[I].Declaration;
Douglas Gregor2436e712009-09-17 21:32:03 +0000551 if (Results[I].Hidden)
552 OS << " (Hidden)";
Fangrui Song6907ce22018-07-30 19:24:48 +0000553 if (CodeCompletionString *CCS
Douglas Gregorc3425b12015-07-07 06:20:19 +0000554 = Results[I].CreateCodeCompletionString(SemaRef, Context,
555 getAllocator(),
Dmitri Gribenko3292d062012-07-02 17:35:10 +0000556 CCTUInfo,
557 includeBriefComments())) {
Douglas Gregorfedc3282009-09-18 22:15:54 +0000558 OS << " : " << CCS->getAsString();
Dmitri Gribenko3292d062012-07-02 17:35:10 +0000559 if (const char *BriefComment = CCS->getBriefComment())
560 OS << " : " << BriefComment;
Douglas Gregorfedc3282009-09-18 22:15:54 +0000561 }
Ivan Donchevskiib4670fc2018-05-25 12:56:26 +0000562 for (const FixItHint &FixIt : Results[I].FixIts) {
563 const SourceLocation BLoc = FixIt.RemoveRange.getBegin();
564 const SourceLocation ELoc = FixIt.RemoveRange.getEnd();
565
566 SourceManager &SM = SemaRef.SourceMgr;
567 std::pair<FileID, unsigned> BInfo = SM.getDecomposedLoc(BLoc);
568 std::pair<FileID, unsigned> EInfo = SM.getDecomposedLoc(ELoc);
569 // Adjust for token ranges.
570 if (FixIt.RemoveRange.isTokenRange())
571 EInfo.second += Lexer::MeasureTokenLength(ELoc, SM, SemaRef.LangOpts);
572
573 OS << " (requires fix-it:"
574 << " {" << SM.getLineNumber(BInfo.first, BInfo.second) << ':'
575 << SM.getColumnNumber(BInfo.first, BInfo.second) << '-'
576 << SM.getLineNumber(EInfo.first, EInfo.second) << ':'
577 << SM.getColumnNumber(EInfo.first, EInfo.second) << "}"
578 << " to \"" << FixIt.CodeToInsert << "\")";
579 }
Douglas Gregor2436e712009-09-17 21:32:03 +0000580 OS << '\n';
581 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000582
John McCall276321a2010-08-25 06:19:51 +0000583 case CodeCompletionResult::RK_Keyword:
Douglas Gregor52ce62f2010-01-13 23:24:38 +0000584 OS << Results[I].Keyword << '\n';
Douglas Gregor2436e712009-09-17 21:32:03 +0000585 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000586
Eugene Zelenko711964d2018-02-20 02:16:28 +0000587 case CodeCompletionResult::RK_Macro:
Douglas Gregor52ce62f2010-01-13 23:24:38 +0000588 OS << Results[I].Macro->getName();
Fangrui Song6907ce22018-07-30 19:24:48 +0000589 if (CodeCompletionString *CCS
Douglas Gregorc3425b12015-07-07 06:20:19 +0000590 = Results[I].CreateCodeCompletionString(SemaRef, Context,
591 getAllocator(),
Dmitri Gribenko3292d062012-07-02 17:35:10 +0000592 CCTUInfo,
593 includeBriefComments())) {
Douglas Gregorf329c7c2009-10-30 16:50:04 +0000594 OS << " : " << CCS->getAsString();
Douglas Gregorf329c7c2009-10-30 16:50:04 +0000595 }
596 OS << '\n';
597 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000598
Eugene Zelenko711964d2018-02-20 02:16:28 +0000599 case CodeCompletionResult::RK_Pattern:
Fangrui Song6907ce22018-07-30 19:24:48 +0000600 OS << "Pattern : "
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000601 << Results[I].Pattern->getAsString() << '\n';
602 break;
603 }
Douglas Gregor2436e712009-09-17 21:32:03 +0000604 }
Douglas Gregor2436e712009-09-17 21:32:03 +0000605}
Douglas Gregor05f477c2009-09-23 00:16:58 +0000606
Francisco Lopes da Silva0c010cd2015-01-28 14:17:22 +0000607// This function is used solely to preserve the former presentation of overloads
608// by "clang -cc1 -code-completion-at", since CodeCompletionString::getAsString
609// needs to be improved for printing the newer and more detailed overload
610// chunks.
611static std::string getOverloadAsString(const CodeCompletionString &CCS) {
612 std::string Result;
613 llvm::raw_string_ostream OS(Result);
614
615 for (auto &C : CCS) {
616 switch (C.Kind) {
617 case CodeCompletionString::CK_Informative:
618 case CodeCompletionString::CK_ResultType:
619 OS << "[#" << C.Text << "#]";
620 break;
621
622 case CodeCompletionString::CK_CurrentParameter:
623 OS << "<#" << C.Text << "#>";
624 break;
625
Kadir Cetinkayaa32d2532018-09-10 13:46:28 +0000626 // FIXME: We can also print optional parameters of an overload.
627 case CodeCompletionString::CK_Optional:
628 break;
629
Francisco Lopes da Silva0c010cd2015-01-28 14:17:22 +0000630 default: OS << C.Text; break;
631 }
632 }
633 return OS.str();
634}
635
Ilya Biryukov2fab2352018-08-30 13:08:03 +0000636void PrintingCodeCompleteConsumer::ProcessOverloadCandidates(
637 Sema &SemaRef, unsigned CurrentArg, OverloadCandidate *Candidates,
638 unsigned NumCandidates, SourceLocation OpenParLoc) {
639 OS << "OPENING_PAREN_LOC: ";
640 OpenParLoc.print(OS, SemaRef.getSourceManager());
641 OS << "\n";
642
Douglas Gregor05f477c2009-09-23 00:16:58 +0000643 for (unsigned I = 0; I != NumCandidates; ++I) {
Ilya Biryukov2fab2352018-08-30 13:08:03 +0000644 if (CodeCompletionString *CCS = Candidates[I].CreateSignatureString(
645 CurrentArg, SemaRef, getAllocator(), CCTUInfo,
646 includeBriefComments())) {
Francisco Lopes da Silva0c010cd2015-01-28 14:17:22 +0000647 OS << "OVERLOAD: " << getOverloadAsString(*CCS) << "\n";
Douglas Gregor05f477c2009-09-23 00:16:58 +0000648 }
649 }
Douglas Gregor05f477c2009-09-23 00:16:58 +0000650}
Douglas Gregor9eb77012009-11-07 00:00:49 +0000651
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000652/// Retrieve the effective availability of the given declaration.
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000653static AvailabilityResult getDeclAvailability(const Decl *D) {
Douglas Gregor7b316822012-03-17 06:39:06 +0000654 AvailabilityResult AR = D->getAvailability();
655 if (isa<EnumConstantDecl>(D))
656 AR = std::max(AR, cast<Decl>(D->getDeclContext())->getAvailability());
657 return AR;
658}
659
Erik Verbruggen2e657ff2011-10-06 07:27:49 +0000660void CodeCompletionResult::computeCursorKindAndAvailability(bool Accessible) {
Douglas Gregorb14904c2010-08-13 22:48:40 +0000661 switch (Kind) {
Douglas Gregor78254c82012-03-27 23:34:16 +0000662 case RK_Pattern:
663 if (!Declaration) {
664 // Do nothing: Patterns can come with cursor kinds!
665 break;
666 }
Adrian Prantlf3b3ccd2017-12-19 22:06:11 +0000667 LLVM_FALLTHROUGH;
Fangrui Song6907ce22018-07-30 19:24:48 +0000668
Douglas Gregor7b316822012-03-17 06:39:06 +0000669 case RK_Declaration: {
Douglas Gregorf757a122010-08-23 23:00:57 +0000670 // Set the availability based on attributes.
Douglas Gregor7b316822012-03-17 06:39:06 +0000671 switch (getDeclAvailability(Declaration)) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000672 case AR_Available:
673 case AR_NotYetIntroduced:
Fangrui Song6907ce22018-07-30 19:24:48 +0000674 Availability = CXAvailability_Available;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000675 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000676
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000677 case AR_Deprecated:
678 Availability = CXAvailability_Deprecated;
679 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000680
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000681 case AR_Unavailable:
682 Availability = CXAvailability_NotAvailable;
683 break;
684 }
685
Eugene Zelenko1e95bc02018-04-05 22:15:42 +0000686 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(Declaration))
Douglas Gregor09c0eb12010-09-03 23:30:36 +0000687 if (Function->isDeleted())
Douglas Gregorf757a122010-08-23 23:00:57 +0000688 Availability = CXAvailability_NotAvailable;
Fangrui Song6907ce22018-07-30 19:24:48 +0000689
Douglas Gregor09c0eb12010-09-03 23:30:36 +0000690 CursorKind = getCursorKindForDecl(Declaration);
Douglas Gregordeafd0b2011-12-27 22:43:10 +0000691 if (CursorKind == CXCursor_UnexposedDecl) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000692 // FIXME: Forward declarations of Objective-C classes and protocols
693 // are not directly exposed, but we want code completion to treat them
Douglas Gregorf6102672012-01-01 21:23:57 +0000694 // like a definition.
Douglas Gregordeafd0b2011-12-27 22:43:10 +0000695 if (isa<ObjCInterfaceDecl>(Declaration))
696 CursorKind = CXCursor_ObjCInterfaceDecl;
Douglas Gregorf6102672012-01-01 21:23:57 +0000697 else if (isa<ObjCProtocolDecl>(Declaration))
698 CursorKind = CXCursor_ObjCProtocolDecl;
Douglas Gregordeafd0b2011-12-27 22:43:10 +0000699 else
700 CursorKind = CXCursor_NotImplemented;
701 }
Douglas Gregorb14904c2010-08-13 22:48:40 +0000702 break;
Douglas Gregor7b316822012-03-17 06:39:06 +0000703 }
Douglas Gregorb14904c2010-08-13 22:48:40 +0000704
John McCall276321a2010-08-25 06:19:51 +0000705 case RK_Macro:
John McCall276321a2010-08-25 06:19:51 +0000706 case RK_Keyword:
Benjamin Kramerc25c0b92012-05-20 14:19:46 +0000707 llvm_unreachable("Macro and keyword kinds are handled by the constructors");
Douglas Gregor8e984da2010-08-04 16:47:14 +0000708 }
Erik Verbruggen2e657ff2011-10-06 07:27:49 +0000709
710 if (!Accessible)
711 Availability = CXAvailability_NotAccessible;
Douglas Gregor8e984da2010-08-04 16:47:14 +0000712}
Douglas Gregorf09935f2009-12-01 05:55:20 +0000713
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000714/// Retrieve the name that should be used to order a result.
Douglas Gregor0de55ce2010-08-25 18:41:16 +0000715///
716/// If the name needs to be constructed as a string, that string will be
717/// saved into Saved and the returned StringRef will refer to it.
Sam McCall1102a862017-11-15 09:15:06 +0000718StringRef CodeCompletionResult::getOrderedName(std::string &Saved) const {
719 switch (Kind) {
720 case RK_Keyword:
721 return Keyword;
722 case RK_Pattern:
723 return Pattern->getTypedText();
724 case RK_Macro:
725 return Macro->getName();
726 case RK_Declaration:
Douglas Gregor0de55ce2010-08-25 18:41:16 +0000727 // Handle declarations below.
728 break;
729 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000730
Sam McCall1102a862017-11-15 09:15:06 +0000731 DeclarationName Name = Declaration->getDeclName();
Fangrui Song6907ce22018-07-30 19:24:48 +0000732
Douglas Gregor0de55ce2010-08-25 18:41:16 +0000733 // If the name is a simple identifier (by far the common case), or a
734 // zero-argument selector, just return a reference to that identifier.
735 if (IdentifierInfo *Id = Name.getAsIdentifierInfo())
736 return Id->getName();
737 if (Name.isObjCZeroArgSelector())
738 if (IdentifierInfo *Id
739 = Name.getObjCSelector().getIdentifierInfoForSlot(0))
740 return Id->getName();
Fangrui Song6907ce22018-07-30 19:24:48 +0000741
Douglas Gregor0de55ce2010-08-25 18:41:16 +0000742 Saved = Name.getAsString();
743 return Saved;
744}
Fangrui Song6907ce22018-07-30 19:24:48 +0000745
746bool clang::operator<(const CodeCompletionResult &X,
Douglas Gregor0de55ce2010-08-25 18:41:16 +0000747 const CodeCompletionResult &Y) {
748 std::string XSaved, YSaved;
Sam McCall1102a862017-11-15 09:15:06 +0000749 StringRef XStr = X.getOrderedName(XSaved);
750 StringRef YStr = Y.getOrderedName(YSaved);
Douglas Gregor0de55ce2010-08-25 18:41:16 +0000751 int cmp = XStr.compare_lower(YStr);
752 if (cmp)
753 return cmp < 0;
Fangrui Song6907ce22018-07-30 19:24:48 +0000754
Douglas Gregor49f67ce2010-08-26 13:48:20 +0000755 // If case-insensitive comparison fails, try case-sensitive comparison.
756 cmp = XStr.compare(YStr);
757 if (cmp)
758 return cmp < 0;
Fangrui Song6907ce22018-07-30 19:24:48 +0000759
Douglas Gregor0de55ce2010-08-25 18:41:16 +0000760 return false;
761}