blob: 3db9e4dcd523f1059187494e27a3262b4dec0d85 [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 Gregor577cdfd2011-02-17 00:22:45 +000022#include "llvm/ADT/Twine.h"
Douglas Gregor81b747b2009-09-17 21:32:03 +000023#include "llvm/Support/raw_ostream.h"
24#include <algorithm>
Douglas Gregore6e03612009-09-18 22:15:54 +000025#include <cstring>
26#include <functional>
Douglas Gregor92eff462009-11-17 16:43:05 +000027
Douglas Gregor81b747b2009-09-17 21:32:03 +000028using namespace clang;
29
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:
Douglas Gregor3da626b2011-07-07 16:03:39 +000048 case CCC_DotMemberAccess:
49 case CCC_ArrowMemberAccess:
50 case CCC_ObjCPropertyAccess:
Douglas Gregor6f942b22010-09-21 16:06:22 +000051 case CCC_EnumTag:
52 case CCC_UnionTag:
53 case CCC_ClassOrStructTag:
54 case CCC_ObjCProtocolName:
55 case CCC_Namespace:
56 case CCC_Type:
57 case CCC_Name:
58 case CCC_PotentiallyQualifiedName:
59 case CCC_MacroName:
60 case CCC_MacroNameUse:
61 case CCC_PreprocessorExpression:
62 case CCC_PreprocessorDirective:
63 case CCC_NaturalLanguage:
64 case CCC_SelectorName:
65 case CCC_TypeQualifiers:
Douglas Gregor52779fb2010-09-23 23:01:17 +000066 case CCC_Other:
Douglas Gregor5c722c702011-02-18 23:30:37 +000067 case CCC_OtherWithMacros:
Douglas Gregor3da626b2011-07-07 16:03:39 +000068 case CCC_ObjCInstanceMessage:
69 case CCC_ObjCClassMessage:
Douglas Gregor0f91c8c2011-07-30 06:55:39 +000070 case CCC_ObjCInterfaceName:
Douglas Gregor3da626b2011-07-07 16:03:39 +000071 case CCC_ObjCCategoryName:
Douglas Gregor6f942b22010-09-21 16:06:22 +000072 return false;
73 }
74
75 return false;
76}
77
78//===----------------------------------------------------------------------===//
Douglas Gregore6e03612009-09-18 22:15:54 +000079// Code completion string implementation
80//===----------------------------------------------------------------------===//
Douglas Gregor218937c2011-02-01 19:23:04 +000081CodeCompletionString::Chunk::Chunk(ChunkKind Kind, const char *Text)
Daniel Dunbarad5757f2009-11-12 18:40:12 +000082 : Kind(Kind), Text("")
Douglas Gregor0563c262009-09-22 23:15:58 +000083{
Douglas Gregor0c8296d2009-11-07 00:00:49 +000084 switch (Kind) {
85 case CK_TypedText:
86 case CK_Text:
87 case CK_Placeholder:
88 case CK_Informative:
Douglas Gregorff5ce6e2009-12-18 18:53:37 +000089 case CK_ResultType:
Douglas Gregor218937c2011-02-01 19:23:04 +000090 case CK_CurrentParameter:
91 this->Text = Text;
Douglas Gregor0c8296d2009-11-07 00:00:49 +000092 break;
Douglas Gregor0c8296d2009-11-07 00:00:49 +000093
94 case CK_Optional:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +000095 llvm_unreachable("Optional strings cannot be created from text");
Douglas Gregor0c8296d2009-11-07 00:00:49 +000096 break;
97
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 Gregor01dfea02010-01-10 23:08:15 +0000133
134 case CK_Colon:
Douglas Gregore8f5a172010-04-07 00:21:17 +0000135 this->Text = ":";
Douglas Gregor01dfea02010-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 Gregor0c8296d2009-11-07 00:00:49 +0000153 }
Douglas Gregor0563c262009-09-22 23:15:58 +0000154}
155
156CodeCompletionString::Chunk
Douglas Gregor218937c2011-02-01 19:23:04 +0000157CodeCompletionString::Chunk::CreateText(const char *Text) {
Douglas Gregor0563c262009-09-22 23:15:58 +0000158 return Chunk(CK_Text, Text);
Douglas Gregore6e03612009-09-18 22:15:54 +0000159}
160
161CodeCompletionString::Chunk
Douglas Gregor218937c2011-02-01 19:23:04 +0000162CodeCompletionString::Chunk::CreateOptional(CodeCompletionString *Optional) {
Douglas Gregore6e03612009-09-18 22:15:54 +0000163 Chunk Result;
164 Result.Kind = CK_Optional;
Douglas Gregor218937c2011-02-01 19:23:04 +0000165 Result.Optional = Optional;
Douglas Gregore6e03612009-09-18 22:15:54 +0000166 return Result;
167}
168
169CodeCompletionString::Chunk
Douglas Gregor218937c2011-02-01 19:23:04 +0000170CodeCompletionString::Chunk::CreatePlaceholder(const char *Placeholder) {
Douglas Gregor0563c262009-09-22 23:15:58 +0000171 return Chunk(CK_Placeholder, Placeholder);
172}
173
174CodeCompletionString::Chunk
Douglas Gregor218937c2011-02-01 19:23:04 +0000175CodeCompletionString::Chunk::CreateInformative(const char *Informative) {
Douglas Gregor0563c262009-09-22 23:15:58 +0000176 return Chunk(CK_Informative, Informative);
Douglas Gregore6e03612009-09-18 22:15:54 +0000177}
178
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000179CodeCompletionString::Chunk
Douglas Gregor218937c2011-02-01 19:23:04 +0000180CodeCompletionString::Chunk::CreateResultType(const char *ResultType) {
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000181 return Chunk(CK_ResultType, ResultType);
182}
183
184CodeCompletionString::Chunk
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000185CodeCompletionString::Chunk::CreateCurrentParameter(
Douglas Gregor218937c2011-02-01 19:23:04 +0000186 const char *CurrentParameter) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000187 return Chunk(CK_CurrentParameter, CurrentParameter);
188}
189
Douglas Gregor218937c2011-02-01 19:23:04 +0000190CodeCompletionString::CodeCompletionString(const Chunk *Chunks,
191 unsigned NumChunks,
192 unsigned Priority,
193 CXAvailabilityKind Availability)
194 : NumChunks(NumChunks), Priority(Priority), Availability(Availability)
195{
196 Chunk *StoredChunks = reinterpret_cast<Chunk *>(this + 1);
197 for (unsigned I = 0; I != NumChunks; ++I)
198 StoredChunks[I] = Chunks[I];
Douglas Gregore6e03612009-09-18 22:15:54 +0000199}
200
201std::string CodeCompletionString::getAsString() const {
202 std::string Result;
203 llvm::raw_string_ostream OS(Result);
204
205 for (iterator C = begin(), CEnd = end(); C != CEnd; ++C) {
206 switch (C->Kind) {
Douglas Gregore6e03612009-09-18 22:15:54 +0000207 case CK_Optional: OS << "{#" << C->Optional->getAsString() << "#}"; break;
Douglas Gregor0563c262009-09-22 23:15:58 +0000208 case CK_Placeholder: OS << "<#" << C->Text << "#>"; break;
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000209
210 case CK_Informative:
211 case CK_ResultType:
212 OS << "[#" << C->Text << "#]";
213 break;
214
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000215 case CK_CurrentParameter: OS << "<#" << C->Text << "#>"; break;
216 default: OS << C->Text; break;
Douglas Gregore6e03612009-09-18 22:15:54 +0000217 }
218 }
Dan Gohman6bdeb402010-07-26 21:33:22 +0000219 return OS.str();
Douglas Gregore6e03612009-09-18 22:15:54 +0000220}
221
Douglas Gregor54f01612009-11-19 00:01:57 +0000222const char *CodeCompletionString::getTypedText() const {
223 for (iterator C = begin(), CEnd = end(); C != CEnd; ++C)
224 if (C->Kind == CK_TypedText)
225 return C->Text;
226
227 return 0;
228}
229
Chris Lattner5f9e2722011-07-23 10:55:15 +0000230const char *CodeCompletionAllocator::CopyString(StringRef String) {
Douglas Gregordae68752011-02-01 22:57:45 +0000231 char *Mem = (char *)Allocate(String.size() + 1, 1);
232 std::copy(String.begin(), String.end(), Mem);
233 Mem[String.size()] = 0;
234 return Mem;
235}
236
Chris Lattner5f9e2722011-07-23 10:55:15 +0000237const char *CodeCompletionAllocator::CopyString(Twine String) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +0000238 // FIXME: It would be more efficient to teach Twine to tell us its size and
239 // then add a routine there to fill in an allocated char* with the contents
240 // of the string.
241 llvm::SmallString<128> Data;
242 return CopyString(String.toStringRef(Data));
243}
244
Douglas Gregor218937c2011-02-01 19:23:04 +0000245CodeCompletionString *CodeCompletionBuilder::TakeString() {
246 void *Mem = Allocator.Allocate(
247 sizeof(CodeCompletionString) + sizeof(Chunk) * Chunks.size(),
248 llvm::alignOf<CodeCompletionString>());
249 CodeCompletionString *Result
250 = new (Mem) CodeCompletionString(Chunks.data(), Chunks.size(),
251 Priority, Availability);
252 Chunks.clear();
Douglas Gregor54f01612009-11-19 00:01:57 +0000253 return Result;
254}
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000255
John McCall0a2c5e22010-08-25 06:19:51 +0000256unsigned CodeCompletionResult::getPriorityFromDecl(NamedDecl *ND) {
Douglas Gregor12e13132010-05-26 22:00:08 +0000257 if (!ND)
258 return CCP_Unlikely;
259
260 // Context-based decisions.
Sebastian Redl7a126a42010-08-31 00:36:30 +0000261 DeclContext *DC = ND->getDeclContext()->getRedeclContext();
Douglas Gregor97fe61c2010-09-18 15:16:27 +0000262 if (DC->isFunctionOrMethod() || isa<BlockDecl>(DC)) {
263 // _cmd is relatively rare
264 if (ImplicitParamDecl *ImplicitParam = dyn_cast<ImplicitParamDecl>(ND))
265 if (ImplicitParam->getIdentifier() &&
266 ImplicitParam->getIdentifier()->isStr("_cmd"))
267 return CCP_ObjC_cmd;
268
Douglas Gregor12e13132010-05-26 22:00:08 +0000269 return CCP_LocalDeclaration;
Douglas Gregor97fe61c2010-09-18 15:16:27 +0000270 }
Douglas Gregor12e13132010-05-26 22:00:08 +0000271 if (DC->isRecord() || isa<ObjCContainerDecl>(DC))
272 return CCP_MemberDeclaration;
273
274 // Content-based decisions.
275 if (isa<EnumConstantDecl>(ND))
276 return CCP_Constant;
277 if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND))
278 return CCP_Type;
Douglas Gregor97fe61c2010-09-18 15:16:27 +0000279
Douglas Gregor12e13132010-05-26 22:00:08 +0000280 return CCP_Declaration;
281}
282
Douglas Gregore6e03612009-09-18 22:15:54 +0000283//===----------------------------------------------------------------------===//
Douglas Gregor05944382009-09-23 00:16:58 +0000284// Code completion overload candidate implementation
285//===----------------------------------------------------------------------===//
286FunctionDecl *
287CodeCompleteConsumer::OverloadCandidate::getFunction() const {
288 if (getKind() == CK_Function)
289 return Function;
290 else if (getKind() == CK_FunctionTemplate)
291 return FunctionTemplate->getTemplatedDecl();
292 else
293 return 0;
294}
295
296const FunctionType *
297CodeCompleteConsumer::OverloadCandidate::getFunctionType() const {
298 switch (Kind) {
299 case CK_Function:
300 return Function->getType()->getAs<FunctionType>();
301
302 case CK_FunctionTemplate:
303 return FunctionTemplate->getTemplatedDecl()->getType()
304 ->getAs<FunctionType>();
305
306 case CK_FunctionType:
307 return Type;
308 }
309
310 return 0;
311}
312
313//===----------------------------------------------------------------------===//
Douglas Gregore6e03612009-09-18 22:15:54 +0000314// Code completion consumer implementation
315//===----------------------------------------------------------------------===//
316
Douglas Gregor86d9a522009-09-21 16:56:56 +0000317CodeCompleteConsumer::~CodeCompleteConsumer() { }
Douglas Gregore6e03612009-09-18 22:15:54 +0000318
Douglas Gregor81b747b2009-09-17 21:32:03 +0000319void
Daniel Dunbar3a2838d2009-11-13 08:58:20 +0000320PrintingCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &SemaRef,
Douglas Gregore6b1bb62010-08-11 21:23:17 +0000321 CodeCompletionContext Context,
John McCall0a2c5e22010-08-25 06:19:51 +0000322 CodeCompletionResult *Results,
Douglas Gregor81b747b2009-09-17 21:32:03 +0000323 unsigned NumResults) {
Douglas Gregor1e5e6682010-08-26 13:48:20 +0000324 std::stable_sort(Results, Results + NumResults);
325
Douglas Gregor81b747b2009-09-17 21:32:03 +0000326 // Print the results.
327 for (unsigned I = 0; I != NumResults; ++I) {
Douglas Gregorf52cede2009-10-09 22:16:47 +0000328 OS << "COMPLETION: ";
Douglas Gregor81b747b2009-09-17 21:32:03 +0000329 switch (Results[I].Kind) {
John McCall0a2c5e22010-08-25 06:19:51 +0000330 case CodeCompletionResult::RK_Declaration:
Benjamin Kramer900fc632010-04-17 09:33:03 +0000331 OS << Results[I].Declaration;
Douglas Gregor81b747b2009-09-17 21:32:03 +0000332 if (Results[I].Hidden)
333 OS << " (Hidden)";
Douglas Gregor86d9a522009-09-21 16:56:56 +0000334 if (CodeCompletionString *CCS
Douglas Gregor218937c2011-02-01 19:23:04 +0000335 = Results[I].CreateCodeCompletionString(SemaRef, Allocator)) {
Douglas Gregore6e03612009-09-18 22:15:54 +0000336 OS << " : " << CCS->getAsString();
Douglas Gregore6e03612009-09-18 22:15:54 +0000337 }
338
Douglas Gregor81b747b2009-09-17 21:32:03 +0000339 OS << '\n';
340 break;
341
John McCall0a2c5e22010-08-25 06:19:51 +0000342 case CodeCompletionResult::RK_Keyword:
Douglas Gregorab0b4f12010-01-13 23:24:38 +0000343 OS << Results[I].Keyword << '\n';
Douglas Gregor81b747b2009-09-17 21:32:03 +0000344 break;
Douglas Gregor3f7c7f42009-10-30 16:50:04 +0000345
John McCall0a2c5e22010-08-25 06:19:51 +0000346 case CodeCompletionResult::RK_Macro: {
Douglas Gregorab0b4f12010-01-13 23:24:38 +0000347 OS << Results[I].Macro->getName();
Douglas Gregor3f7c7f42009-10-30 16:50:04 +0000348 if (CodeCompletionString *CCS
Douglas Gregor218937c2011-02-01 19:23:04 +0000349 = Results[I].CreateCodeCompletionString(SemaRef, Allocator)) {
Douglas Gregor3f7c7f42009-10-30 16:50:04 +0000350 OS << " : " << CCS->getAsString();
Douglas Gregor3f7c7f42009-10-30 16:50:04 +0000351 }
352 OS << '\n';
353 break;
354 }
Douglas Gregor54f01612009-11-19 00:01:57 +0000355
John McCall0a2c5e22010-08-25 06:19:51 +0000356 case CodeCompletionResult::RK_Pattern: {
Douglas Gregorab0b4f12010-01-13 23:24:38 +0000357 OS << "Pattern : "
Douglas Gregor54f01612009-11-19 00:01:57 +0000358 << Results[I].Pattern->getAsString() << '\n';
359 break;
360 }
Douglas Gregor81b747b2009-09-17 21:32:03 +0000361 }
362 }
Douglas Gregor81b747b2009-09-17 21:32:03 +0000363}
Douglas Gregor05944382009-09-23 00:16:58 +0000364
365void
Daniel Dunbar3a2838d2009-11-13 08:58:20 +0000366PrintingCodeCompleteConsumer::ProcessOverloadCandidates(Sema &SemaRef,
367 unsigned CurrentArg,
Douglas Gregor05944382009-09-23 00:16:58 +0000368 OverloadCandidate *Candidates,
369 unsigned NumCandidates) {
370 for (unsigned I = 0; I != NumCandidates; ++I) {
Douglas Gregor86d802e2009-09-23 00:34:09 +0000371 if (CodeCompletionString *CCS
Douglas Gregor218937c2011-02-01 19:23:04 +0000372 = Candidates[I].CreateSignatureString(CurrentArg, SemaRef,
373 Allocator)) {
Douglas Gregorf52cede2009-10-09 22:16:47 +0000374 OS << "OVERLOAD: " << CCS->getAsString() << "\n";
Douglas Gregor05944382009-09-23 00:16:58 +0000375 }
376 }
Douglas Gregor05944382009-09-23 00:16:58 +0000377}
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000378
John McCall0a2c5e22010-08-25 06:19:51 +0000379void CodeCompletionResult::computeCursorKindAndAvailability() {
Douglas Gregor87c08a52010-08-13 22:48:40 +0000380 switch (Kind) {
381 case RK_Declaration:
Douglas Gregor58ddb602010-08-23 23:00:57 +0000382 // Set the availability based on attributes.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000383 switch (Declaration->getAvailability()) {
384 case AR_Available:
385 case AR_NotYetIntroduced:
386 Availability = CXAvailability_Available;
387 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +0000388
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000389 case AR_Deprecated:
390 Availability = CXAvailability_Deprecated;
391 break;
392
393 case AR_Unavailable:
394 Availability = CXAvailability_NotAvailable;
395 break;
396 }
397
Douglas Gregore8d7beb2010-09-03 23:30:36 +0000398 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Declaration))
399 if (Function->isDeleted())
Douglas Gregor58ddb602010-08-23 23:00:57 +0000400 Availability = CXAvailability_NotAvailable;
Douglas Gregore8d7beb2010-09-03 23:30:36 +0000401
402 CursorKind = getCursorKindForDecl(Declaration);
403 if (CursorKind == CXCursor_UnexposedDecl)
Douglas Gregor87c08a52010-08-13 22:48:40 +0000404 CursorKind = CXCursor_NotImplemented;
Douglas Gregor87c08a52010-08-13 22:48:40 +0000405 break;
406
John McCall0a2c5e22010-08-25 06:19:51 +0000407 case RK_Macro:
Douglas Gregor58ddb602010-08-23 23:00:57 +0000408 Availability = CXAvailability_Available;
Douglas Gregor87c08a52010-08-13 22:48:40 +0000409 CursorKind = CXCursor_MacroDefinition;
410 break;
411
John McCall0a2c5e22010-08-25 06:19:51 +0000412 case RK_Keyword:
Douglas Gregor58ddb602010-08-23 23:00:57 +0000413 Availability = CXAvailability_Available;
Douglas Gregor87c08a52010-08-13 22:48:40 +0000414 CursorKind = CXCursor_NotImplemented;
415 break;
416
John McCall0a2c5e22010-08-25 06:19:51 +0000417 case RK_Pattern:
Douglas Gregor87c08a52010-08-13 22:48:40 +0000418 // Do nothing: Patterns can come with cursor kinds!
419 break;
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000420 }
421}
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000422
Douglas Gregor721f3592010-08-25 18:41:16 +0000423/// \brief Retrieve the name that should be used to order a result.
424///
425/// If the name needs to be constructed as a string, that string will be
426/// saved into Saved and the returned StringRef will refer to it.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000427static StringRef getOrderedName(const CodeCompletionResult &R,
Douglas Gregor721f3592010-08-25 18:41:16 +0000428 std::string &Saved) {
429 switch (R.Kind) {
430 case CodeCompletionResult::RK_Keyword:
431 return R.Keyword;
432
433 case CodeCompletionResult::RK_Pattern:
434 return R.Pattern->getTypedText();
435
436 case CodeCompletionResult::RK_Macro:
437 return R.Macro->getName();
438
439 case CodeCompletionResult::RK_Declaration:
440 // Handle declarations below.
441 break;
442 }
443
444 DeclarationName Name = R.Declaration->getDeclName();
445
446 // If the name is a simple identifier (by far the common case), or a
447 // zero-argument selector, just return a reference to that identifier.
448 if (IdentifierInfo *Id = Name.getAsIdentifierInfo())
449 return Id->getName();
450 if (Name.isObjCZeroArgSelector())
451 if (IdentifierInfo *Id
452 = Name.getObjCSelector().getIdentifierInfoForSlot(0))
453 return Id->getName();
454
455 Saved = Name.getAsString();
456 return Saved;
457}
458
459bool clang::operator<(const CodeCompletionResult &X,
460 const CodeCompletionResult &Y) {
461 std::string XSaved, YSaved;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000462 StringRef XStr = getOrderedName(X, XSaved);
463 StringRef YStr = getOrderedName(Y, YSaved);
Douglas Gregor721f3592010-08-25 18:41:16 +0000464 int cmp = XStr.compare_lower(YStr);
465 if (cmp)
466 return cmp < 0;
467
Douglas Gregor1e5e6682010-08-26 13:48:20 +0000468 // If case-insensitive comparison fails, try case-sensitive comparison.
469 cmp = XStr.compare(YStr);
470 if (cmp)
471 return cmp < 0;
Douglas Gregor721f3592010-08-25 18:41:16 +0000472
473 return false;
474}