1 //===- CodeCompleteConsumer.cpp - Code Completion Interface ---------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the CodeCompleteConsumer class.
11 //===----------------------------------------------------------------------===//
13 #include "clang/Sema/CodeCompleteConsumer.h"
14 #include "clang-c/Index.h"
15 #include "clang/AST/Decl.h"
16 #include "clang/AST/DeclBase.h"
17 #include "clang/AST/DeclObjC.h"
18 #include "clang/AST/DeclTemplate.h"
19 #include "clang/AST/DeclarationName.h"
20 #include "clang/AST/Type.h"
21 #include "clang/Basic/IdentifierTable.h"
22 #include "clang/Lex/Preprocessor.h"
23 #include "clang/Sema/Sema.h"
24 #include "llvm/ADT/SmallString.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include "llvm/ADT/StringExtras.h"
27 #include "llvm/ADT/StringRef.h"
28 #include "llvm/ADT/Twine.h"
29 #include "llvm/Support/Casting.h"
30 #include "llvm/Support/Compiler.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/FormatVariadic.h"
33 #include "llvm/Support/raw_ostream.h"
39 using namespace clang
;
41 //===----------------------------------------------------------------------===//
42 // Code completion context implementation
43 //===----------------------------------------------------------------------===//
45 bool CodeCompletionContext::wantConstructorResults() const {
50 case CCC_ObjCMessageReceiver
:
51 case CCC_ParenthesizedExpression
:
53 case CCC_SymbolOrNewName
:
57 case CCC_ObjCInterface
:
58 case CCC_ObjCImplementation
:
59 case CCC_ObjCIvarList
:
60 case CCC_ClassStructUnion
:
61 case CCC_DotMemberAccess
:
62 case CCC_ArrowMemberAccess
:
63 case CCC_ObjCPropertyAccess
:
66 case CCC_ClassOrStructTag
:
67 case CCC_ObjCProtocolName
:
72 case CCC_MacroNameUse
:
73 case CCC_PreprocessorExpression
:
74 case CCC_PreprocessorDirective
:
75 case CCC_NaturalLanguage
:
76 case CCC_SelectorName
:
77 case CCC_TypeQualifiers
:
79 case CCC_OtherWithMacros
:
80 case CCC_ObjCInstanceMessage
:
81 case CCC_ObjCClassMessage
:
82 case CCC_ObjCInterfaceName
:
83 case CCC_ObjCCategoryName
:
84 case CCC_IncludedFile
:
89 llvm_unreachable("Invalid CodeCompletionContext::Kind!");
92 StringRef
clang::getCompletionKindString(CodeCompletionContext::Kind Kind
) {
93 using CCKind
= CodeCompletionContext::Kind
;
95 case CCKind::CCC_Other
:
97 case CCKind::CCC_OtherWithMacros
:
98 return "OtherWithMacros";
99 case CCKind::CCC_TopLevel
:
101 case CCKind::CCC_ObjCInterface
:
102 return "ObjCInterface";
103 case CCKind::CCC_ObjCImplementation
:
104 return "ObjCImplementation";
105 case CCKind::CCC_ObjCIvarList
:
106 return "ObjCIvarList";
107 case CCKind::CCC_ClassStructUnion
:
108 return "ClassStructUnion";
109 case CCKind::CCC_Statement
:
111 case CCKind::CCC_Expression
:
113 case CCKind::CCC_ObjCMessageReceiver
:
114 return "ObjCMessageReceiver";
115 case CCKind::CCC_DotMemberAccess
:
116 return "DotMemberAccess";
117 case CCKind::CCC_ArrowMemberAccess
:
118 return "ArrowMemberAccess";
119 case CCKind::CCC_ObjCPropertyAccess
:
120 return "ObjCPropertyAccess";
121 case CCKind::CCC_EnumTag
:
123 case CCKind::CCC_UnionTag
:
125 case CCKind::CCC_ClassOrStructTag
:
126 return "ClassOrStructTag";
127 case CCKind::CCC_ObjCProtocolName
:
128 return "ObjCProtocolName";
129 case CCKind::CCC_Namespace
:
131 case CCKind::CCC_Type
:
133 case CCKind::CCC_NewName
:
135 case CCKind::CCC_Symbol
:
137 case CCKind::CCC_SymbolOrNewName
:
138 return "SymbolOrNewName";
139 case CCKind::CCC_MacroName
:
141 case CCKind::CCC_MacroNameUse
:
142 return "MacroNameUse";
143 case CCKind::CCC_PreprocessorExpression
:
144 return "PreprocessorExpression";
145 case CCKind::CCC_PreprocessorDirective
:
146 return "PreprocessorDirective";
147 case CCKind::CCC_NaturalLanguage
:
148 return "NaturalLanguage";
149 case CCKind::CCC_SelectorName
:
150 return "SelectorName";
151 case CCKind::CCC_TypeQualifiers
:
152 return "TypeQualifiers";
153 case CCKind::CCC_ParenthesizedExpression
:
154 return "ParenthesizedExpression";
155 case CCKind::CCC_ObjCInstanceMessage
:
156 return "ObjCInstanceMessage";
157 case CCKind::CCC_ObjCClassMessage
:
158 return "ObjCClassMessage";
159 case CCKind::CCC_ObjCInterfaceName
:
160 return "ObjCInterfaceName";
161 case CCKind::CCC_ObjCCategoryName
:
162 return "ObjCCategoryName";
163 case CCKind::CCC_IncludedFile
:
164 return "IncludedFile";
165 case CCKind::CCC_Attribute
:
167 case CCKind::CCC_Recovery
:
170 llvm_unreachable("Invalid CodeCompletionContext::Kind!");
173 //===----------------------------------------------------------------------===//
174 // Code completion string implementation
175 //===----------------------------------------------------------------------===//
177 CodeCompletionString::Chunk::Chunk(ChunkKind Kind
, const char *Text
)
178 : Kind(Kind
), Text("") {
185 case CK_CurrentParameter
:
190 llvm_unreachable("Optional strings cannot be created from text");
204 case CK_RightBracket
:
240 case CK_HorizontalSpace
:
244 case CK_VerticalSpace
:
250 CodeCompletionString::Chunk
251 CodeCompletionString::Chunk::CreateText(const char *Text
) {
252 return Chunk(CK_Text
, Text
);
255 CodeCompletionString::Chunk
256 CodeCompletionString::Chunk::CreateOptional(CodeCompletionString
*Optional
) {
258 Result
.Kind
= CK_Optional
;
259 Result
.Optional
= Optional
;
263 CodeCompletionString::Chunk
264 CodeCompletionString::Chunk::CreatePlaceholder(const char *Placeholder
) {
265 return Chunk(CK_Placeholder
, Placeholder
);
268 CodeCompletionString::Chunk
269 CodeCompletionString::Chunk::CreateInformative(const char *Informative
) {
270 return Chunk(CK_Informative
, Informative
);
273 CodeCompletionString::Chunk
274 CodeCompletionString::Chunk::CreateResultType(const char *ResultType
) {
275 return Chunk(CK_ResultType
, ResultType
);
278 CodeCompletionString::Chunk
CodeCompletionString::Chunk::CreateCurrentParameter(
279 const char *CurrentParameter
) {
280 return Chunk(CK_CurrentParameter
, CurrentParameter
);
283 CodeCompletionString::CodeCompletionString(
284 const Chunk
*Chunks
, unsigned NumChunks
, unsigned Priority
,
285 CXAvailabilityKind Availability
, const char **Annotations
,
286 unsigned NumAnnotations
, StringRef ParentName
, const char *BriefComment
)
287 : NumChunks(NumChunks
), NumAnnotations(NumAnnotations
), Priority(Priority
),
288 Availability(Availability
), ParentName(ParentName
),
289 BriefComment(BriefComment
) {
290 assert(NumChunks
<= 0xffff);
291 assert(NumAnnotations
<= 0xffff);
293 Chunk
*StoredChunks
= reinterpret_cast<Chunk
*>(this + 1);
294 for (unsigned I
= 0; I
!= NumChunks
; ++I
)
295 StoredChunks
[I
] = Chunks
[I
];
297 const char **StoredAnnotations
=
298 reinterpret_cast<const char **>(StoredChunks
+ NumChunks
);
299 for (unsigned I
= 0; I
!= NumAnnotations
; ++I
)
300 StoredAnnotations
[I
] = Annotations
[I
];
303 unsigned CodeCompletionString::getAnnotationCount() const {
304 return NumAnnotations
;
307 const char *CodeCompletionString::getAnnotation(unsigned AnnotationNr
) const {
308 if (AnnotationNr
< NumAnnotations
)
309 return reinterpret_cast<const char *const *>(end())[AnnotationNr
];
314 std::string
CodeCompletionString::getAsString() const {
316 llvm::raw_string_ostream
OS(Result
);
318 for (const Chunk
&C
: *this) {
321 OS
<< "{#" << C
.Optional
->getAsString() << "#}";
324 OS
<< "<#" << C
.Text
<< "#>";
328 OS
<< "[#" << C
.Text
<< "#]";
330 case CK_CurrentParameter
:
331 OS
<< "<#" << C
.Text
<< "#>";
341 const char *CodeCompletionString::getTypedText() const {
342 for (const Chunk
&C
: *this)
343 if (C
.Kind
== CK_TypedText
)
349 std::string
CodeCompletionString::getAllTypedText() const {
351 for (const Chunk
&C
: *this)
352 if (C
.Kind
== CK_TypedText
)
358 const char *CodeCompletionAllocator::CopyString(const Twine
&String
) {
359 SmallString
<128> Data
;
360 StringRef Ref
= String
.toStringRef(Data
);
361 // FIXME: It would be more efficient to teach Twine to tell us its size and
362 // then add a routine there to fill in an allocated char* with the contents
364 char *Mem
= (char *)Allocate(Ref
.size() + 1, 1);
365 std::copy(Ref
.begin(), Ref
.end(), Mem
);
370 StringRef
CodeCompletionTUInfo::getParentName(const DeclContext
*DC
) {
371 if (!isa
<NamedDecl
>(DC
))
374 // Check whether we've already cached the parent name.
375 StringRef
&CachedParentName
= ParentNames
[DC
];
376 if (!CachedParentName
.empty())
377 return CachedParentName
;
379 // If we already processed this DeclContext and assigned empty to it, the
380 // data pointer will be non-null.
381 if (CachedParentName
.data() != nullptr)
384 // Find the interesting names.
385 SmallVector
<const DeclContext
*, 2> Contexts
;
386 while (DC
&& !DC
->isFunctionOrMethod()) {
387 if (const auto *ND
= dyn_cast
<NamedDecl
>(DC
)) {
388 if (ND
->getIdentifier())
389 Contexts
.push_back(DC
);
392 DC
= DC
->getParent();
397 llvm::raw_svector_ostream
OS(S
);
399 for (const DeclContext
*CurDC
: llvm::reverse(Contexts
)) {
406 if (const auto *CatImpl
= dyn_cast
<ObjCCategoryImplDecl
>(CurDC
))
407 CurDC
= CatImpl
->getCategoryDecl();
409 if (const auto *Cat
= dyn_cast
<ObjCCategoryDecl
>(CurDC
)) {
410 const ObjCInterfaceDecl
*Interface
= Cat
->getClassInterface();
412 // Assign an empty StringRef but with non-null data to distinguish
413 // between empty because we didn't process the DeclContext yet.
414 CachedParentName
= StringRef((const char *)(uintptr_t)~0U, 0);
418 OS
<< Interface
->getName() << '(' << Cat
->getName() << ')';
420 OS
<< cast
<NamedDecl
>(CurDC
)->getName();
424 CachedParentName
= AllocatorRef
->CopyString(OS
.str());
427 return CachedParentName
;
430 CodeCompletionString
*CodeCompletionBuilder::TakeString() {
431 void *Mem
= getAllocator().Allocate(
432 sizeof(CodeCompletionString
) + sizeof(Chunk
) * Chunks
.size() +
433 sizeof(const char *) * Annotations
.size(),
434 alignof(CodeCompletionString
));
435 CodeCompletionString
*Result
= new (Mem
) CodeCompletionString(
436 Chunks
.data(), Chunks
.size(), Priority
, Availability
, Annotations
.data(),
437 Annotations
.size(), ParentName
, BriefComment
);
442 void CodeCompletionBuilder::AddTypedTextChunk(const char *Text
) {
443 Chunks
.push_back(Chunk(CodeCompletionString::CK_TypedText
, Text
));
446 void CodeCompletionBuilder::AddTextChunk(const char *Text
) {
447 Chunks
.push_back(Chunk::CreateText(Text
));
450 void CodeCompletionBuilder::AddOptionalChunk(CodeCompletionString
*Optional
) {
451 Chunks
.push_back(Chunk::CreateOptional(Optional
));
454 void CodeCompletionBuilder::AddPlaceholderChunk(const char *Placeholder
) {
455 Chunks
.push_back(Chunk::CreatePlaceholder(Placeholder
));
458 void CodeCompletionBuilder::AddInformativeChunk(const char *Text
) {
459 Chunks
.push_back(Chunk::CreateInformative(Text
));
462 void CodeCompletionBuilder::AddResultTypeChunk(const char *ResultType
) {
463 Chunks
.push_back(Chunk::CreateResultType(ResultType
));
466 void CodeCompletionBuilder::AddCurrentParameterChunk(
467 const char *CurrentParameter
) {
468 Chunks
.push_back(Chunk::CreateCurrentParameter(CurrentParameter
));
471 void CodeCompletionBuilder::AddChunk(CodeCompletionString::ChunkKind CK
,
473 Chunks
.push_back(Chunk(CK
, Text
));
476 void CodeCompletionBuilder::addParentContext(const DeclContext
*DC
) {
477 if (DC
->isTranslationUnit())
480 if (DC
->isFunctionOrMethod())
483 if (!isa
<NamedDecl
>(DC
))
486 ParentName
= getCodeCompletionTUInfo().getParentName(DC
);
489 void CodeCompletionBuilder::addBriefComment(StringRef Comment
) {
490 BriefComment
= Allocator
.CopyString(Comment
);
493 //===----------------------------------------------------------------------===//
494 // Code completion overload candidate implementation
495 //===----------------------------------------------------------------------===//
496 FunctionDecl
*CodeCompleteConsumer::OverloadCandidate::getFunction() const {
497 if (getKind() == CK_Function
)
499 else if (getKind() == CK_FunctionTemplate
)
500 return FunctionTemplate
->getTemplatedDecl();
506 CodeCompleteConsumer::OverloadCandidate::getFunctionType() const {
509 return Function
->getType()->getAs
<FunctionType
>();
511 case CK_FunctionTemplate
:
512 return FunctionTemplate
->getTemplatedDecl()
514 ->getAs
<FunctionType
>();
516 case CK_FunctionType
:
518 case CK_FunctionProtoTypeLoc
:
519 return ProtoTypeLoc
.getTypePtr();
525 llvm_unreachable("Invalid CandidateKind!");
528 const FunctionProtoTypeLoc
529 CodeCompleteConsumer::OverloadCandidate::getFunctionProtoTypeLoc() const {
530 if (Kind
== CK_FunctionProtoTypeLoc
)
532 return FunctionProtoTypeLoc();
535 unsigned CodeCompleteConsumer::OverloadCandidate::getNumParams() const {
536 if (Kind
== CK_Template
)
537 return Template
->getTemplateParameters()->size();
539 if (Kind
== CK_Aggregate
) {
541 std::distance(AggregateType
->field_begin(), AggregateType
->field_end());
542 if (const auto *CRD
= dyn_cast
<CXXRecordDecl
>(AggregateType
))
543 Count
+= CRD
->getNumBases();
547 if (const auto *FT
= getFunctionType())
548 if (const auto *FPT
= dyn_cast
<FunctionProtoType
>(FT
))
549 return FPT
->getNumParams();
555 CodeCompleteConsumer::OverloadCandidate::getParamType(unsigned N
) const {
556 if (Kind
== CK_Aggregate
) {
557 if (const auto *CRD
= dyn_cast
<CXXRecordDecl
>(AggregateType
)) {
558 if (N
< CRD
->getNumBases())
559 return std::next(CRD
->bases_begin(), N
)->getType();
560 N
-= CRD
->getNumBases();
562 for (const auto *Field
: AggregateType
->fields())
564 return Field
->getType();
568 if (Kind
== CK_Template
) {
569 TemplateParameterList
*TPL
= getTemplate()->getTemplateParameters();
571 if (const auto *D
= dyn_cast
<NonTypeTemplateParmDecl
>(TPL
->getParam(N
)))
576 if (const auto *FT
= getFunctionType())
577 if (const auto *FPT
= dyn_cast
<FunctionProtoType
>(FT
))
578 if (N
< FPT
->getNumParams())
579 return FPT
->getParamType(N
);
584 CodeCompleteConsumer::OverloadCandidate::getParamDecl(unsigned N
) const {
585 if (Kind
== CK_Aggregate
) {
586 if (const auto *CRD
= dyn_cast
<CXXRecordDecl
>(AggregateType
)) {
587 if (N
< CRD
->getNumBases())
588 return std::next(CRD
->bases_begin(), N
)->getType()->getAsTagDecl();
589 N
-= CRD
->getNumBases();
591 for (const auto *Field
: AggregateType
->fields())
597 if (Kind
== CK_Template
) {
598 TemplateParameterList
*TPL
= getTemplate()->getTemplateParameters();
600 return TPL
->getParam(N
);
604 // Note that if we only have a FunctionProtoType, we don't have param decls.
605 if (const auto *FD
= getFunction()) {
606 if (N
< FD
->param_size())
607 return FD
->getParamDecl(N
);
608 } else if (Kind
== CK_FunctionProtoTypeLoc
) {
609 if (N
< ProtoTypeLoc
.getNumParams()) {
610 return ProtoTypeLoc
.getParam(N
);
617 //===----------------------------------------------------------------------===//
618 // Code completion consumer implementation
619 //===----------------------------------------------------------------------===//
621 CodeCompleteConsumer::~CodeCompleteConsumer() = default;
623 bool PrintingCodeCompleteConsumer::isResultFilteredOut(
624 StringRef Filter
, CodeCompletionResult Result
) {
625 switch (Result
.Kind
) {
626 case CodeCompletionResult::RK_Declaration
:
627 return !(Result
.Declaration
->getIdentifier() &&
628 Result
.Declaration
->getIdentifier()->getName().startswith(Filter
));
629 case CodeCompletionResult::RK_Keyword
:
630 return !StringRef(Result
.Keyword
).startswith(Filter
);
631 case CodeCompletionResult::RK_Macro
:
632 return !Result
.Macro
->getName().startswith(Filter
);
633 case CodeCompletionResult::RK_Pattern
:
634 return !(Result
.Pattern
->getTypedText() &&
635 StringRef(Result
.Pattern
->getTypedText()).startswith(Filter
));
637 llvm_unreachable("Unknown code completion result Kind.");
640 void PrintingCodeCompleteConsumer::ProcessCodeCompleteResults(
641 Sema
&SemaRef
, CodeCompletionContext Context
, CodeCompletionResult
*Results
,
642 unsigned NumResults
) {
643 std::stable_sort(Results
, Results
+ NumResults
);
645 if (!Context
.getPreferredType().isNull())
646 OS
<< "PREFERRED-TYPE: " << Context
.getPreferredType() << '\n';
648 StringRef Filter
= SemaRef
.getPreprocessor().getCodeCompletionFilter();
649 // Print the completions.
650 for (unsigned I
= 0; I
!= NumResults
; ++I
) {
651 if (!Filter
.empty() && isResultFilteredOut(Filter
, Results
[I
]))
653 OS
<< "COMPLETION: ";
654 switch (Results
[I
].Kind
) {
655 case CodeCompletionResult::RK_Declaration
:
656 OS
<< *Results
[I
].Declaration
;
658 std::vector
<std::string
> Tags
;
659 if (Results
[I
].Hidden
)
660 Tags
.push_back("Hidden");
661 if (Results
[I
].InBaseClass
)
662 Tags
.push_back("InBase");
663 if (Results
[I
].Availability
==
664 CXAvailabilityKind::CXAvailability_NotAccessible
)
665 Tags
.push_back("Inaccessible");
667 OS
<< " (" << llvm::join(Tags
, ",") << ")";
669 if (CodeCompletionString
*CCS
= Results
[I
].CreateCodeCompletionString(
670 SemaRef
, Context
, getAllocator(), CCTUInfo
,
671 includeBriefComments())) {
672 OS
<< " : " << CCS
->getAsString();
673 if (const char *BriefComment
= CCS
->getBriefComment())
674 OS
<< " : " << BriefComment
;
678 case CodeCompletionResult::RK_Keyword
:
679 OS
<< Results
[I
].Keyword
;
682 case CodeCompletionResult::RK_Macro
:
683 OS
<< Results
[I
].Macro
->getName();
684 if (CodeCompletionString
*CCS
= Results
[I
].CreateCodeCompletionString(
685 SemaRef
, Context
, getAllocator(), CCTUInfo
,
686 includeBriefComments())) {
687 OS
<< " : " << CCS
->getAsString();
691 case CodeCompletionResult::RK_Pattern
:
692 OS
<< "Pattern : " << Results
[I
].Pattern
->getAsString();
695 for (const FixItHint
&FixIt
: Results
[I
].FixIts
) {
696 const SourceLocation BLoc
= FixIt
.RemoveRange
.getBegin();
697 const SourceLocation ELoc
= FixIt
.RemoveRange
.getEnd();
699 SourceManager
&SM
= SemaRef
.SourceMgr
;
700 std::pair
<FileID
, unsigned> BInfo
= SM
.getDecomposedLoc(BLoc
);
701 std::pair
<FileID
, unsigned> EInfo
= SM
.getDecomposedLoc(ELoc
);
702 // Adjust for token ranges.
703 if (FixIt
.RemoveRange
.isTokenRange())
704 EInfo
.second
+= Lexer::MeasureTokenLength(ELoc
, SM
, SemaRef
.LangOpts
);
706 OS
<< " (requires fix-it:"
707 << " {" << SM
.getLineNumber(BInfo
.first
, BInfo
.second
) << ':'
708 << SM
.getColumnNumber(BInfo
.first
, BInfo
.second
) << '-'
709 << SM
.getLineNumber(EInfo
.first
, EInfo
.second
) << ':'
710 << SM
.getColumnNumber(EInfo
.first
, EInfo
.second
) << "}"
711 << " to \"" << FixIt
.CodeToInsert
<< "\")";
717 // This function is used solely to preserve the former presentation of overloads
718 // by "clang -cc1 -code-completion-at", since CodeCompletionString::getAsString
719 // needs to be improved for printing the newer and more detailed overload
721 static std::string
getOverloadAsString(const CodeCompletionString
&CCS
) {
723 llvm::raw_string_ostream
OS(Result
);
725 for (auto &C
: CCS
) {
727 case CodeCompletionString::CK_Informative
:
728 case CodeCompletionString::CK_ResultType
:
729 OS
<< "[#" << C
.Text
<< "#]";
732 case CodeCompletionString::CK_CurrentParameter
:
733 OS
<< "<#" << C
.Text
<< "#>";
736 // FIXME: We can also print optional parameters of an overload.
737 case CodeCompletionString::CK_Optional
:
748 void PrintingCodeCompleteConsumer::ProcessOverloadCandidates(
749 Sema
&SemaRef
, unsigned CurrentArg
, OverloadCandidate
*Candidates
,
750 unsigned NumCandidates
, SourceLocation OpenParLoc
, bool Braced
) {
751 OS
<< "OPENING_PAREN_LOC: ";
752 OpenParLoc
.print(OS
, SemaRef
.getSourceManager());
755 for (unsigned I
= 0; I
!= NumCandidates
; ++I
) {
756 if (CodeCompletionString
*CCS
= Candidates
[I
].CreateSignatureString(
757 CurrentArg
, SemaRef
, getAllocator(), CCTUInfo
,
758 includeBriefComments(), Braced
)) {
759 OS
<< "OVERLOAD: " << getOverloadAsString(*CCS
) << "\n";
764 /// Retrieve the effective availability of the given declaration.
765 static AvailabilityResult
getDeclAvailability(const Decl
*D
) {
766 AvailabilityResult AR
= D
->getAvailability();
767 if (isa
<EnumConstantDecl
>(D
))
768 AR
= std::max(AR
, cast
<Decl
>(D
->getDeclContext())->getAvailability());
772 void CodeCompletionResult::computeCursorKindAndAvailability(bool Accessible
) {
776 // Do nothing: Patterns can come with cursor kinds!
781 case RK_Declaration
: {
782 // Set the availability based on attributes.
783 switch (getDeclAvailability(Declaration
)) {
785 case AR_NotYetIntroduced
:
786 Availability
= CXAvailability_Available
;
790 Availability
= CXAvailability_Deprecated
;
794 Availability
= CXAvailability_NotAvailable
;
798 if (const auto *Function
= dyn_cast
<FunctionDecl
>(Declaration
))
799 if (Function
->isDeleted())
800 Availability
= CXAvailability_NotAvailable
;
802 CursorKind
= getCursorKindForDecl(Declaration
);
803 if (CursorKind
== CXCursor_UnexposedDecl
) {
804 // FIXME: Forward declarations of Objective-C classes and protocols
805 // are not directly exposed, but we want code completion to treat them
806 // like a definition.
807 if (isa
<ObjCInterfaceDecl
>(Declaration
))
808 CursorKind
= CXCursor_ObjCInterfaceDecl
;
809 else if (isa
<ObjCProtocolDecl
>(Declaration
))
810 CursorKind
= CXCursor_ObjCProtocolDecl
;
812 CursorKind
= CXCursor_NotImplemented
;
819 llvm_unreachable("Macro and keyword kinds are handled by the constructors");
823 Availability
= CXAvailability_NotAccessible
;
826 /// Retrieve the name that should be used to order a result.
828 /// If the name needs to be constructed as a string, that string will be
829 /// saved into Saved and the returned StringRef will refer to it.
830 StringRef
CodeCompletionResult::getOrderedName(std::string
&Saved
) const {
835 return Pattern
->getTypedText();
837 return Macro
->getName();
839 // Handle declarations below.
843 DeclarationName Name
= Declaration
->getDeclName();
845 // If the name is a simple identifier (by far the common case), or a
846 // zero-argument selector, just return a reference to that identifier.
847 if (IdentifierInfo
*Id
= Name
.getAsIdentifierInfo())
848 return Id
->getName();
849 if (Name
.isObjCZeroArgSelector())
850 if (IdentifierInfo
*Id
= Name
.getObjCSelector().getIdentifierInfoForSlot(0))
851 return Id
->getName();
853 Saved
= Name
.getAsString();
857 bool clang::operator<(const CodeCompletionResult
&X
,
858 const CodeCompletionResult
&Y
) {
859 std::string XSaved
, YSaved
;
860 StringRef XStr
= X
.getOrderedName(XSaved
);
861 StringRef YStr
= Y
.getOrderedName(YSaved
);
862 int cmp
= XStr
.compare_insensitive(YStr
);
866 // If case-insensitive comparison fails, try case-sensitive comparison.
867 return XStr
.compare(YStr
) < 0;