blob: cb2dd234b4867b7f610d2236618cb4bde383c71b [file] [log] [blame]
Douglas Gregor86d9a522009-09-21 16:56:56 +00001//===--- CodeCompleteConsumer.cpp - Code Completion Interface ---*- C++ -*-===//
Douglas Gregor81b747b2009-09-17 21:32:03 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the CodeCompleteConsumer class.
11//
12//===----------------------------------------------------------------------===//
13#include "clang/Sema/CodeCompleteConsumer.h"
John McCall19510852010-08-20 18:27:03 +000014#include "clang/Sema/Scope.h"
Douglas Gregore737f502010-08-12 20:07:10 +000015#include "clang/Sema/Sema.h"
Douglas Gregor75b71282009-09-18 17:54:00 +000016#include "clang/AST/DeclCXX.h"
John McCall7cd088e2010-08-24 07:21:54 +000017#include "clang/AST/DeclObjC.h"
John McCall2a7fb272010-08-25 05:32:35 +000018#include "clang/AST/DeclTemplate.h"
Douglas Gregor81b747b2009-09-17 21:32:03 +000019#include "clang/Lex/Preprocessor.h"
Douglas Gregor2b4074f2009-12-01 05:55:20 +000020#include "clang-c/Index.h"
Douglas Gregor81b747b2009-09-17 21:32:03 +000021#include "llvm/ADT/STLExtras.h"
Douglas Gregor81b747b2009-09-17 21:32:03 +000022#include "llvm/Support/raw_ostream.h"
23#include <algorithm>
Douglas Gregore6e03612009-09-18 22:15:54 +000024#include <cstring>
25#include <functional>
Douglas Gregor92eff462009-11-17 16:43:05 +000026
Douglas Gregor81b747b2009-09-17 21:32:03 +000027using namespace clang;
Douglas Gregor92eff462009-11-17 16:43:05 +000028using llvm::StringRef;
Douglas Gregor81b747b2009-09-17 21:32:03 +000029
Douglas Gregore6e03612009-09-18 22:15:54 +000030//===----------------------------------------------------------------------===//
Douglas Gregor6f942b22010-09-21 16:06:22 +000031// Code completion context implementation
32//===----------------------------------------------------------------------===//
33
34bool CodeCompletionContext::wantConstructorResults() const {
35 switch (Kind) {
Douglas Gregor52779fb2010-09-23 23:01:17 +000036 case CCC_Recovery:
Douglas Gregor6f942b22010-09-21 16:06:22 +000037 case CCC_Statement:
38 case CCC_Expression:
39 case CCC_ObjCMessageReceiver:
40 case CCC_ParenthesizedExpression:
41 return true;
42
43 case CCC_TopLevel:
44 case CCC_ObjCInterface:
45 case CCC_ObjCImplementation:
46 case CCC_ObjCIvarList:
47 case CCC_ClassStructUnion:
48 case CCC_MemberAccess:
49 case CCC_EnumTag:
50 case CCC_UnionTag:
51 case CCC_ClassOrStructTag:
52 case CCC_ObjCProtocolName:
53 case CCC_Namespace:
54 case CCC_Type:
55 case CCC_Name:
56 case CCC_PotentiallyQualifiedName:
57 case CCC_MacroName:
58 case CCC_MacroNameUse:
59 case CCC_PreprocessorExpression:
60 case CCC_PreprocessorDirective:
61 case CCC_NaturalLanguage:
62 case CCC_SelectorName:
63 case CCC_TypeQualifiers:
Douglas Gregor52779fb2010-09-23 23:01:17 +000064 case CCC_Other:
Douglas Gregor6f942b22010-09-21 16:06:22 +000065 return false;
66 }
67
68 return false;
69}
70
71//===----------------------------------------------------------------------===//
Douglas Gregore6e03612009-09-18 22:15:54 +000072// Code completion string implementation
73//===----------------------------------------------------------------------===//
Douglas Gregor218937c2011-02-01 19:23:04 +000074CodeCompletionString::Chunk::Chunk(ChunkKind Kind, const char *Text)
Daniel Dunbarad5757f2009-11-12 18:40:12 +000075 : Kind(Kind), Text("")
Douglas Gregor0563c262009-09-22 23:15:58 +000076{
Douglas Gregor0c8296d2009-11-07 00:00:49 +000077 switch (Kind) {
78 case CK_TypedText:
79 case CK_Text:
80 case CK_Placeholder:
81 case CK_Informative:
Douglas Gregorff5ce6e2009-12-18 18:53:37 +000082 case CK_ResultType:
Douglas Gregor218937c2011-02-01 19:23:04 +000083 case CK_CurrentParameter:
84 this->Text = Text;
Douglas Gregor0c8296d2009-11-07 00:00:49 +000085 break;
Douglas Gregor0c8296d2009-11-07 00:00:49 +000086
87 case CK_Optional:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +000088 llvm_unreachable("Optional strings cannot be created from text");
Douglas Gregor0c8296d2009-11-07 00:00:49 +000089 break;
90
91 case CK_LeftParen:
92 this->Text = "(";
93 break;
94
95 case CK_RightParen:
96 this->Text = ")";
97 break;
98
99 case CK_LeftBracket:
100 this->Text = "[";
101 break;
102
103 case CK_RightBracket:
104 this->Text = "]";
105 break;
106
107 case CK_LeftBrace:
108 this->Text = "{";
109 break;
110
111 case CK_RightBrace:
112 this->Text = "}";
113 break;
114
115 case CK_LeftAngle:
116 this->Text = "<";
117 break;
118
119 case CK_RightAngle:
120 this->Text = ">";
121 break;
122
123 case CK_Comma:
124 this->Text = ", ";
125 break;
Douglas Gregor01dfea02010-01-10 23:08:15 +0000126
127 case CK_Colon:
Douglas Gregore8f5a172010-04-07 00:21:17 +0000128 this->Text = ":";
Douglas Gregor01dfea02010-01-10 23:08:15 +0000129 break;
130
131 case CK_SemiColon:
132 this->Text = ";";
133 break;
134
135 case CK_Equal:
136 this->Text = " = ";
137 break;
138
139 case CK_HorizontalSpace:
140 this->Text = " ";
141 break;
142
143 case CK_VerticalSpace:
144 this->Text = "\n";
145 break;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000146 }
Douglas Gregor0563c262009-09-22 23:15:58 +0000147}
148
149CodeCompletionString::Chunk
Douglas Gregor218937c2011-02-01 19:23:04 +0000150CodeCompletionString::Chunk::CreateText(const char *Text) {
Douglas Gregor0563c262009-09-22 23:15:58 +0000151 return Chunk(CK_Text, Text);
Douglas Gregore6e03612009-09-18 22:15:54 +0000152}
153
154CodeCompletionString::Chunk
Douglas Gregor218937c2011-02-01 19:23:04 +0000155CodeCompletionString::Chunk::CreateOptional(CodeCompletionString *Optional) {
Douglas Gregore6e03612009-09-18 22:15:54 +0000156 Chunk Result;
157 Result.Kind = CK_Optional;
Douglas Gregor218937c2011-02-01 19:23:04 +0000158 Result.Optional = Optional;
Douglas Gregore6e03612009-09-18 22:15:54 +0000159 return Result;
160}
161
162CodeCompletionString::Chunk
Douglas Gregor218937c2011-02-01 19:23:04 +0000163CodeCompletionString::Chunk::CreatePlaceholder(const char *Placeholder) {
Douglas Gregor0563c262009-09-22 23:15:58 +0000164 return Chunk(CK_Placeholder, Placeholder);
165}
166
167CodeCompletionString::Chunk
Douglas Gregor218937c2011-02-01 19:23:04 +0000168CodeCompletionString::Chunk::CreateInformative(const char *Informative) {
Douglas Gregor0563c262009-09-22 23:15:58 +0000169 return Chunk(CK_Informative, Informative);
Douglas Gregore6e03612009-09-18 22:15:54 +0000170}
171
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000172CodeCompletionString::Chunk
Douglas Gregor218937c2011-02-01 19:23:04 +0000173CodeCompletionString::Chunk::CreateResultType(const char *ResultType) {
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000174 return Chunk(CK_ResultType, ResultType);
175}
176
177CodeCompletionString::Chunk
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000178CodeCompletionString::Chunk::CreateCurrentParameter(
Douglas Gregor218937c2011-02-01 19:23:04 +0000179 const char *CurrentParameter) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000180 return Chunk(CK_CurrentParameter, CurrentParameter);
181}
182
Douglas Gregor218937c2011-02-01 19:23:04 +0000183CodeCompletionString::CodeCompletionString(const Chunk *Chunks,
184 unsigned NumChunks,
185 unsigned Priority,
186 CXAvailabilityKind Availability)
187 : NumChunks(NumChunks), Priority(Priority), Availability(Availability)
188{
189 Chunk *StoredChunks = reinterpret_cast<Chunk *>(this + 1);
190 for (unsigned I = 0; I != NumChunks; ++I)
191 StoredChunks[I] = Chunks[I];
Douglas Gregore6e03612009-09-18 22:15:54 +0000192}
193
194std::string CodeCompletionString::getAsString() const {
195 std::string Result;
196 llvm::raw_string_ostream OS(Result);
197
198 for (iterator C = begin(), CEnd = end(); C != CEnd; ++C) {
199 switch (C->Kind) {
Douglas Gregore6e03612009-09-18 22:15:54 +0000200 case CK_Optional: OS << "{#" << C->Optional->getAsString() << "#}"; break;
Douglas Gregor0563c262009-09-22 23:15:58 +0000201 case CK_Placeholder: OS << "<#" << C->Text << "#>"; break;
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000202
203 case CK_Informative:
204 case CK_ResultType:
205 OS << "[#" << C->Text << "#]";
206 break;
207
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000208 case CK_CurrentParameter: OS << "<#" << C->Text << "#>"; break;
209 default: OS << C->Text; break;
Douglas Gregore6e03612009-09-18 22:15:54 +0000210 }
211 }
Dan Gohman6bdeb402010-07-26 21:33:22 +0000212 return OS.str();
Douglas Gregore6e03612009-09-18 22:15:54 +0000213}
214
Douglas Gregor54f01612009-11-19 00:01:57 +0000215const char *CodeCompletionString::getTypedText() const {
216 for (iterator C = begin(), CEnd = end(); C != CEnd; ++C)
217 if (C->Kind == CK_TypedText)
218 return C->Text;
219
220 return 0;
221}
222
Douglas Gregordae68752011-02-01 22:57:45 +0000223const char *CodeCompletionAllocator::CopyString(llvm::StringRef String) {
224 char *Mem = (char *)Allocate(String.size() + 1, 1);
225 std::copy(String.begin(), String.end(), Mem);
226 Mem[String.size()] = 0;
227 return Mem;
228}
229
Douglas Gregor218937c2011-02-01 19:23:04 +0000230CodeCompletionString *CodeCompletionBuilder::TakeString() {
231 void *Mem = Allocator.Allocate(
232 sizeof(CodeCompletionString) + sizeof(Chunk) * Chunks.size(),
233 llvm::alignOf<CodeCompletionString>());
234 CodeCompletionString *Result
235 = new (Mem) CodeCompletionString(Chunks.data(), Chunks.size(),
236 Priority, Availability);
237 Chunks.clear();
Douglas Gregor54f01612009-11-19 00:01:57 +0000238 return Result;
239}
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000240
John McCall0a2c5e22010-08-25 06:19:51 +0000241unsigned CodeCompletionResult::getPriorityFromDecl(NamedDecl *ND) {
Douglas Gregor12e13132010-05-26 22:00:08 +0000242 if (!ND)
243 return CCP_Unlikely;
244
245 // Context-based decisions.
Sebastian Redl7a126a42010-08-31 00:36:30 +0000246 DeclContext *DC = ND->getDeclContext()->getRedeclContext();
Douglas Gregor97fe61c2010-09-18 15:16:27 +0000247 if (DC->isFunctionOrMethod() || isa<BlockDecl>(DC)) {
248 // _cmd is relatively rare
249 if (ImplicitParamDecl *ImplicitParam = dyn_cast<ImplicitParamDecl>(ND))
250 if (ImplicitParam->getIdentifier() &&
251 ImplicitParam->getIdentifier()->isStr("_cmd"))
252 return CCP_ObjC_cmd;
253
Douglas Gregor12e13132010-05-26 22:00:08 +0000254 return CCP_LocalDeclaration;
Douglas Gregor97fe61c2010-09-18 15:16:27 +0000255 }
Douglas Gregor12e13132010-05-26 22:00:08 +0000256 if (DC->isRecord() || isa<ObjCContainerDecl>(DC))
257 return CCP_MemberDeclaration;
258
259 // Content-based decisions.
260 if (isa<EnumConstantDecl>(ND))
261 return CCP_Constant;
262 if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND))
263 return CCP_Type;
Douglas Gregor97fe61c2010-09-18 15:16:27 +0000264
Douglas Gregor12e13132010-05-26 22:00:08 +0000265 return CCP_Declaration;
266}
267
Douglas Gregore6e03612009-09-18 22:15:54 +0000268//===----------------------------------------------------------------------===//
Douglas Gregor05944382009-09-23 00:16:58 +0000269// Code completion overload candidate implementation
270//===----------------------------------------------------------------------===//
271FunctionDecl *
272CodeCompleteConsumer::OverloadCandidate::getFunction() const {
273 if (getKind() == CK_Function)
274 return Function;
275 else if (getKind() == CK_FunctionTemplate)
276 return FunctionTemplate->getTemplatedDecl();
277 else
278 return 0;
279}
280
281const FunctionType *
282CodeCompleteConsumer::OverloadCandidate::getFunctionType() const {
283 switch (Kind) {
284 case CK_Function:
285 return Function->getType()->getAs<FunctionType>();
286
287 case CK_FunctionTemplate:
288 return FunctionTemplate->getTemplatedDecl()->getType()
289 ->getAs<FunctionType>();
290
291 case CK_FunctionType:
292 return Type;
293 }
294
295 return 0;
296}
297
298//===----------------------------------------------------------------------===//
Douglas Gregore6e03612009-09-18 22:15:54 +0000299// Code completion consumer implementation
300//===----------------------------------------------------------------------===//
301
Douglas Gregor86d9a522009-09-21 16:56:56 +0000302CodeCompleteConsumer::~CodeCompleteConsumer() { }
Douglas Gregore6e03612009-09-18 22:15:54 +0000303
Douglas Gregor81b747b2009-09-17 21:32:03 +0000304void
Daniel Dunbar3a2838d2009-11-13 08:58:20 +0000305PrintingCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &SemaRef,
Douglas Gregore6b1bb62010-08-11 21:23:17 +0000306 CodeCompletionContext Context,
John McCall0a2c5e22010-08-25 06:19:51 +0000307 CodeCompletionResult *Results,
Douglas Gregor81b747b2009-09-17 21:32:03 +0000308 unsigned NumResults) {
Douglas Gregor1e5e6682010-08-26 13:48:20 +0000309 std::stable_sort(Results, Results + NumResults);
310
Douglas Gregor81b747b2009-09-17 21:32:03 +0000311 // Print the results.
312 for (unsigned I = 0; I != NumResults; ++I) {
Douglas Gregorf52cede2009-10-09 22:16:47 +0000313 OS << "COMPLETION: ";
Douglas Gregor81b747b2009-09-17 21:32:03 +0000314 switch (Results[I].Kind) {
John McCall0a2c5e22010-08-25 06:19:51 +0000315 case CodeCompletionResult::RK_Declaration:
Benjamin Kramer900fc632010-04-17 09:33:03 +0000316 OS << Results[I].Declaration;
Douglas Gregor81b747b2009-09-17 21:32:03 +0000317 if (Results[I].Hidden)
318 OS << " (Hidden)";
Douglas Gregor86d9a522009-09-21 16:56:56 +0000319 if (CodeCompletionString *CCS
Douglas Gregor218937c2011-02-01 19:23:04 +0000320 = Results[I].CreateCodeCompletionString(SemaRef, Allocator)) {
Douglas Gregore6e03612009-09-18 22:15:54 +0000321 OS << " : " << CCS->getAsString();
Douglas Gregore6e03612009-09-18 22:15:54 +0000322 }
323
Douglas Gregor81b747b2009-09-17 21:32:03 +0000324 OS << '\n';
325 break;
326
John McCall0a2c5e22010-08-25 06:19:51 +0000327 case CodeCompletionResult::RK_Keyword:
Douglas Gregorab0b4f12010-01-13 23:24:38 +0000328 OS << Results[I].Keyword << '\n';
Douglas Gregor81b747b2009-09-17 21:32:03 +0000329 break;
Douglas Gregor3f7c7f42009-10-30 16:50:04 +0000330
John McCall0a2c5e22010-08-25 06:19:51 +0000331 case CodeCompletionResult::RK_Macro: {
Douglas Gregorab0b4f12010-01-13 23:24:38 +0000332 OS << Results[I].Macro->getName();
Douglas Gregor3f7c7f42009-10-30 16:50:04 +0000333 if (CodeCompletionString *CCS
Douglas Gregor218937c2011-02-01 19:23:04 +0000334 = Results[I].CreateCodeCompletionString(SemaRef, Allocator)) {
Douglas Gregor3f7c7f42009-10-30 16:50:04 +0000335 OS << " : " << CCS->getAsString();
Douglas Gregor3f7c7f42009-10-30 16:50:04 +0000336 }
337 OS << '\n';
338 break;
339 }
Douglas Gregor54f01612009-11-19 00:01:57 +0000340
John McCall0a2c5e22010-08-25 06:19:51 +0000341 case CodeCompletionResult::RK_Pattern: {
Douglas Gregorab0b4f12010-01-13 23:24:38 +0000342 OS << "Pattern : "
Douglas Gregor54f01612009-11-19 00:01:57 +0000343 << Results[I].Pattern->getAsString() << '\n';
344 break;
345 }
Douglas Gregor81b747b2009-09-17 21:32:03 +0000346 }
347 }
Douglas Gregor81b747b2009-09-17 21:32:03 +0000348}
Douglas Gregor05944382009-09-23 00:16:58 +0000349
350void
Daniel Dunbar3a2838d2009-11-13 08:58:20 +0000351PrintingCodeCompleteConsumer::ProcessOverloadCandidates(Sema &SemaRef,
352 unsigned CurrentArg,
Douglas Gregor05944382009-09-23 00:16:58 +0000353 OverloadCandidate *Candidates,
354 unsigned NumCandidates) {
355 for (unsigned I = 0; I != NumCandidates; ++I) {
Douglas Gregor86d802e2009-09-23 00:34:09 +0000356 if (CodeCompletionString *CCS
Douglas Gregor218937c2011-02-01 19:23:04 +0000357 = Candidates[I].CreateSignatureString(CurrentArg, SemaRef,
358 Allocator)) {
Douglas Gregorf52cede2009-10-09 22:16:47 +0000359 OS << "OVERLOAD: " << CCS->getAsString() << "\n";
Douglas Gregor05944382009-09-23 00:16:58 +0000360 }
361 }
Douglas Gregor05944382009-09-23 00:16:58 +0000362}
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000363
John McCall0a2c5e22010-08-25 06:19:51 +0000364void CodeCompletionResult::computeCursorKindAndAvailability() {
Douglas Gregor87c08a52010-08-13 22:48:40 +0000365 switch (Kind) {
366 case RK_Declaration:
Douglas Gregor58ddb602010-08-23 23:00:57 +0000367 // Set the availability based on attributes.
368 Availability = CXAvailability_Available;
369 if (Declaration->getAttr<UnavailableAttr>())
370 Availability = CXAvailability_NotAvailable;
371 else if (Declaration->getAttr<DeprecatedAttr>())
372 Availability = CXAvailability_Deprecated;
373
Douglas Gregore8d7beb2010-09-03 23:30:36 +0000374 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Declaration))
375 if (Function->isDeleted())
Douglas Gregor58ddb602010-08-23 23:00:57 +0000376 Availability = CXAvailability_NotAvailable;
Douglas Gregore8d7beb2010-09-03 23:30:36 +0000377
378 CursorKind = getCursorKindForDecl(Declaration);
379 if (CursorKind == CXCursor_UnexposedDecl)
Douglas Gregor87c08a52010-08-13 22:48:40 +0000380 CursorKind = CXCursor_NotImplemented;
Douglas Gregor87c08a52010-08-13 22:48:40 +0000381 break;
382
John McCall0a2c5e22010-08-25 06:19:51 +0000383 case RK_Macro:
Douglas Gregor58ddb602010-08-23 23:00:57 +0000384 Availability = CXAvailability_Available;
Douglas Gregor87c08a52010-08-13 22:48:40 +0000385 CursorKind = CXCursor_MacroDefinition;
386 break;
387
John McCall0a2c5e22010-08-25 06:19:51 +0000388 case RK_Keyword:
Douglas Gregor58ddb602010-08-23 23:00:57 +0000389 Availability = CXAvailability_Available;
Douglas Gregor87c08a52010-08-13 22:48:40 +0000390 CursorKind = CXCursor_NotImplemented;
391 break;
392
John McCall0a2c5e22010-08-25 06:19:51 +0000393 case RK_Pattern:
Douglas Gregor87c08a52010-08-13 22:48:40 +0000394 // Do nothing: Patterns can come with cursor kinds!
395 break;
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000396 }
397}
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000398
Douglas Gregor721f3592010-08-25 18:41:16 +0000399/// \brief Retrieve the name that should be used to order a result.
400///
401/// If the name needs to be constructed as a string, that string will be
402/// saved into Saved and the returned StringRef will refer to it.
403static llvm::StringRef getOrderedName(const CodeCompletionResult &R,
404 std::string &Saved) {
405 switch (R.Kind) {
406 case CodeCompletionResult::RK_Keyword:
407 return R.Keyword;
408
409 case CodeCompletionResult::RK_Pattern:
410 return R.Pattern->getTypedText();
411
412 case CodeCompletionResult::RK_Macro:
413 return R.Macro->getName();
414
415 case CodeCompletionResult::RK_Declaration:
416 // Handle declarations below.
417 break;
418 }
419
420 DeclarationName Name = R.Declaration->getDeclName();
421
422 // If the name is a simple identifier (by far the common case), or a
423 // zero-argument selector, just return a reference to that identifier.
424 if (IdentifierInfo *Id = Name.getAsIdentifierInfo())
425 return Id->getName();
426 if (Name.isObjCZeroArgSelector())
427 if (IdentifierInfo *Id
428 = Name.getObjCSelector().getIdentifierInfoForSlot(0))
429 return Id->getName();
430
431 Saved = Name.getAsString();
432 return Saved;
433}
434
435bool clang::operator<(const CodeCompletionResult &X,
436 const CodeCompletionResult &Y) {
437 std::string XSaved, YSaved;
438 llvm::StringRef XStr = getOrderedName(X, XSaved);
439 llvm::StringRef YStr = getOrderedName(Y, YSaved);
440 int cmp = XStr.compare_lower(YStr);
441 if (cmp)
442 return cmp < 0;
443
Douglas Gregor1e5e6682010-08-26 13:48:20 +0000444 // If case-insensitive comparison fails, try case-sensitive comparison.
445 cmp = XStr.compare(YStr);
446 if (cmp)
447 return cmp < 0;
Douglas Gregor721f3592010-08-25 18:41:16 +0000448
449 return false;
450}