20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/ADT/Twine.h"
23 #include "llvm/Support/raw_ostream.h"
28 using namespace clang;
75 llvm_unreachable(
"Invalid CodeCompletionContext::Kind!");
82 : Kind(Kind), Text(
"")
95 llvm_unreachable(
"Optional strings cannot be created from text");
185 const char *CurrentParameter) {
189 CodeCompletionString::CodeCompletionString(
const Chunk *Chunks,
193 const char **Annotations,
194 unsigned NumAnnotations,
195 StringRef ParentName,
196 const char *BriefComment)
197 : NumChunks(NumChunks), NumAnnotations(NumAnnotations),
198 Priority(Priority), Availability(Availability),
199 ParentName(ParentName), BriefComment(BriefComment)
201 assert(NumChunks <= 0xffff);
202 assert(NumAnnotations <= 0xffff);
204 Chunk *StoredChunks =
reinterpret_cast<Chunk *
>(
this + 1);
205 for (
unsigned I = 0;
I != NumChunks; ++
I)
206 StoredChunks[
I] = Chunks[
I];
208 const char **StoredAnnotations =
reinterpret_cast<const char **
>(StoredChunks + NumChunks);
209 for (
unsigned I = 0;
I != NumAnnotations; ++
I)
210 StoredAnnotations[
I] = Annotations[
I];
214 return NumAnnotations;
218 if (AnnotationNr < NumAnnotations)
219 return reinterpret_cast<const char * const*
>(
end())[AnnotationNr];
227 llvm::raw_string_ostream OS(Result);
231 case CK_Optional: OS <<
"{#" << C->Optional->getAsString() <<
"#}";
break;
236 OS <<
"[#" << C->Text <<
"#]";
240 default: OS << C->Text;
break;
256 StringRef Ref = String.toStringRef(Data);
260 char *Mem = (
char *)Allocate(Ref.size() + 1, 1);
261 std::copy(Ref.begin(), Ref.end(), Mem);
272 StringRef &CachedParentName = ParentNames[DC];
273 if (!CachedParentName.empty())
274 return CachedParentName;
278 if (CachedParentName.data() !=
nullptr)
284 if (
const NamedDecl *ND = dyn_cast<NamedDecl>(DC)) {
286 Contexts.push_back(DC);
294 llvm::raw_svector_ostream OS(S);
296 for (
unsigned I = Contexts.size();
I != 0; --
I) {
305 CurDC = CatImpl->getCategoryDecl();
312 CachedParentName = StringRef((
const char *)(
uintptr_t)~0U, 0);
316 OS << Interface->
getName() <<
'(' << Cat->getName() <<
')';
318 OS << cast<NamedDecl>(CurDC)->getName();
322 CachedParentName = AllocatorRef->CopyString(OS.str());
325 return CachedParentName;
329 void *Mem = getAllocator().Allocate(
331 +
sizeof(
const char *) * Annotations.size(),
332 llvm::alignOf<CodeCompletionString>());
335 Priority, Availability,
336 Annotations.data(), Annotations.size(),
337 ParentName, BriefComment);
373 Chunks.push_back(
Chunk(CK, Text));
388 ParentName = getCodeCompletionTUInfo().getParentName(DC);
392 BriefComment =
Allocator.CopyString(Comment);
402 else if (
getKind() == CK_FunctionTemplate)
403 return FunctionTemplate->getTemplatedDecl();
414 case CK_FunctionTemplate:
415 return FunctionTemplate->getTemplatedDecl()->getType()
418 case CK_FunctionType:
422 llvm_unreachable(
"Invalid CandidateKind!");
435 unsigned NumResults) {
436 std::stable_sort(Results, Results + NumResults);
439 for (
unsigned I = 0;
I != NumResults; ++
I) {
440 OS <<
"COMPLETION: ";
441 switch (Results[
I].
Kind) {
444 if (Results[
I].Hidden)
447 = Results[
I].CreateCodeCompletionString(SemaRef, Context,
450 includeBriefComments())) {
451 OS <<
" : " << CCS->getAsString();
452 if (
const char *BriefComment = CCS->getBriefComment())
453 OS <<
" : " << BriefComment;
466 = Results[
I].CreateCodeCompletionString(SemaRef, Context,
469 includeBriefComments())) {
470 OS <<
" : " << CCS->getAsString();
491 llvm::raw_string_ostream OS(Result);
493 for (
auto &C : CCS) {
497 OS <<
"[#" << C.Text <<
"#]";
501 OS <<
"<#" << C.Text <<
"#>";
504 default: OS << C.Text;
break;
514 unsigned NumCandidates) {
515 for (
unsigned I = 0;
I != NumCandidates; ++
I) {
517 = Candidates[
I].CreateSignatureString(CurrentArg, SemaRef,
518 getAllocator(), CCTUInfo,
519 includeBriefComments())) {
528 if (isa<EnumConstantDecl>(D))
533 void CodeCompletionResult::computeCursorKindAndAvailability(
bool Accessible) {
542 case RK_Declaration: {
559 if (
const FunctionDecl *Function = dyn_cast<FunctionDecl>(Declaration))
568 if (isa<ObjCInterfaceDecl>(Declaration))
570 else if (isa<ObjCProtocolDecl>(Declaration))
580 llvm_unreachable(
"Macro and keyword kinds are handled by the constructors");
592 std::string &Saved) {
613 return Id->getName();
617 return Id->getName();
625 std::string XSaved, YSaved;
628 int cmp = XStr.compare_lower(YStr);
633 cmp = XStr.compare(YStr);
One piece of the code completion string.
An unknown context, in which we are recovering from a parsing error and don't know which completions ...
A code completion string that is entirely optional.
CXAvailabilityKind
Describes the availability of a particular entity, which indicates whether the use of this entity wil...
FunctionDecl - An instance of this class is created to represent a function declaration or definition...
ResultKind Kind
The kind of result stored here.
FunctionDecl * getFunction() const
Retrieve the function overload candidate or the templated function declaration for a function templat...
StringRef getName() const
getName - Get the name of identifier for this declaration as a StringRef.
StringRef getParentName(const DeclContext *DC)
Code completion for a selector, as in an @selector expression.
IdentifierInfo * getIdentifier() const
getIdentifier - Get the identifier that names this declaration, if there is one.
void AddTextChunk(const char *Text)
Add a new text chunk.
Code completion where an Objective-C class message is expected.
FunctionType - C99 6.7.5.3 - Function Declarators.
Code completion within a type-qualifier list.
void AddChunk(CodeCompletionString::ChunkKind CK, const char *Text="")
Add a new chunk.
Defines the C++ template declaration subclasses.
static Chunk CreateText(const char *Text)
Create a new text chunk.
IdentifierInfo * getAsIdentifierInfo() const
getAsIdentifierInfo - Retrieve the IdentifierInfo * stored in this declaration name, or NULL if this declaration name isn't a simple identifier.
The base class of the type hierarchy.
An unspecified code-completion context.
static Chunk CreateOptional(CodeCompletionString *Optional)
Create a new optional chunk.
Code completion occurred where an Objective-C message receiver is expected.
Code completion occurred on the right-hand side of a member access expression using the arrow operato...
ChunkKind
The different kinds of "chunks" that can occur within a code completion string.
Code completion occurred after the "enum" keyword, to indicate an enumeration name.
unsigned getAvailability() const
Retrieve the availability of this code completion result.
A piece of text that describes the type of an entity or, for functions and methods, the return type.
const char * Keyword
When Kind == RK_Keyword, the string representing the keyword or symbol's spelling.
static Chunk CreateResultType(const char *ResultType)
Create a new result type chunk.
void AddOptionalChunk(CodeCompletionString *Optional)
Add a new optional chunk.
Code completion occurred within the instance variable list of an Objective-C interface, implementation, or category implementation.
void AddTypedTextChunk(const char *Text)
Add a new typed-text chunk.
The entity is not available; any use of it will be an error.
const FunctionType * getFunctionType() const
Retrieve the function type of the entity, regardless of how the function is stored.
One of these records is kept for each identifier that is lexed.
CodeCompletionString * TakeString()
Take the resulting completion string.
class LLVM_ALIGNAS(8) DependentTemplateSpecializationType const IdentifierInfo * Name
Represents a template specialization type whose template cannot be resolved, e.g. ...
IdentifierInfo * getIdentifierInfoForSlot(unsigned argIndex) const
Retrieve the identifier at a given position in the selector.
static Chunk CreateInformative(const char *Informative)
Create a new informative chunk.
unsigned getAnnotationCount() const
Retrieve the number of annotations for this code completion result.
A "string" used to describe how code completion can be performed for an entity.
An Objective-C @protocol declaration.
bool isTranslationUnit() const
void AddResultTypeChunk(const char *ResultType)
Add a new result-type chunk.
static Chunk CreatePlaceholder(const char *Placeholder)
Create a new placeholder chunk.
CodeCompletionString * Pattern
When Kind == RK_Pattern, the code-completion string that describes the completion text to insert...
Code completion occurred where a preprocessor directive is expected.
Code completion occurred within an Objective-C implementation or category implementation.
bool wantConstructorResults() const
Determines whether we want C++ constructors as results within this context.
Code completion occurred where a namespace or namespace alias is expected.
static Chunk CreateCurrentParameter(const char *CurrentParameter)
Create a new current-parameter chunk.
std::string getAsString() const
getNameAsString - Retrieve the human-readable string for this name.
Represents an ObjC class declaration.
The piece of text that the user is expected to type to match the code-completion string, typically a keyword or the name of a declarator or macro.
detail::InMemoryDirectory::const_iterator I
Refers to a keyword or symbol.
AvailabilityResult
Captures the result of checking the availability of a declaration.
A right parenthesis (')').
Code completion where an Objective-C category name is expected.
Sema - This implements semantic analysis and AST building for C.
Code completion occurred within a "top-level" completion context, e.g., at namespace or global scope...
static std::string getOverloadAsString(const CodeCompletionString &CCS)
const char * getTypedText() const
Returns the text in the TypedText chunk.
Code completion occurred where a protocol name is expected.
CXCursorKind getCursorKindForDecl(const Decl *D)
Determine the libclang cursor kind associated with the given declaration.
StringRef getName() const
Return the actual identifier string.
Code completion occurred where a new name is expected.
const char * Text
The text string associated with a CK_Text, CK_Placeholder, CK_Informative, or CK_Comma chunk...
const NamedDecl * Declaration
When Kind == RK_Declaration or RK_Pattern, the declaration we are referring to.
Captures a result of code completion.
Code completion occurred where a new name is expected and a qualified name is permissible.
Code completion in a parenthesized expression, which means that we may also have types here in C and ...
bool isFunctionOrMethod() const
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Code completion occurred in a context where natural language is expected, e.g., a comment or string l...
DeclarationName getDeclName() const
getDeclName - Get the actual, stored name of the declaration, which may be a special name...
A piece of text that should be placed in the buffer, e.g., parentheses or a comma in a function call...
The result type of a method or function.
const char * CopyString(const Twine &String)
Copy the given string into this allocator.
__UINTPTR_TYPE__ uintptr_t
An unsigned integer type with the property that any valid pointer to void can be converted to this ty...
CodeCompletionString * Optional
The code completion string associated with a CK_Optional chunk.
The context in which code completion occurred, so that the code-completion consumer can process the r...
const IdentifierInfo * Macro
When Kind == RK_Macro, the identifier that refers to a macro.
Code completion occurred within a class, struct, or union.
static StringRef getOrderedName(const CodeCompletionResult &R, std::string &Saved)
Retrieve the name that should be used to order a result.
Code completion where the name of an Objective-C class is expected.
Code completion occurred within an Objective-C interface, protocol, or category interface.
ObjCCategoryDecl - Represents a category declaration.
void addParentContext(const DeclContext *DC)
Add the parent context information to this code completion.
bool operator<(DeclarationName LHS, DeclarationName RHS)
Ordering on two declaration names.
The entity is available, but not accessible; any use of it will be an error.
ChunkKind Kind
The kind of data stored in this piece of the code completion string.
void addBriefComment(StringRef Comment)
Selector getObjCSelector() const
getObjCSelector - Get the Objective-C selector stored in this declaration name.
Code completion occurred where an macro is being defined.
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
A piece of text that describes something about the result but should not be inserted into the buffer...
Code completion occurred after the "struct" or "class" keyword, to indicate a struct or class name...
static AvailabilityResult getDeclAvailability(const Decl *D)
Retrieve the effective availability of the given declaration.
void ProcessCodeCompleteResults(Sema &S, CodeCompletionContext Context, CodeCompletionResult *Results, unsigned NumResults) override
Prints the finalized code-completion results.
Code completion occurred after the "union" keyword, to indicate a union name.
Code completion occurred where a macro name is expected (without any arguments, in the case of a func...
std::string getAsString() const
Retrieve a string representation of the code completion string, which is mainly useful for debugging...
Code completion where an Objective-C instance message is expected.
DeclarationName - The name of a declaration.
Refers to a precomputed pattern.
Code completion occurred where a statement (or declaration) is expected in a function, method, or block.
Code completion occurred on the right-hand side of an Objective-C property access expression...
SmallVector< Context, 8 > Contexts
void AddInformativeChunk(const char *Text)
Add a new informative chunk.
const T * getAs() const
Member-template getAs<specific type>'.
void AddCurrentParameterChunk(const char *CurrentParameter)
Add a new current-parameter chunk.
A declaration whose specific kind is not exposed via this interface.
A piece of text that describes the parameter that corresponds to the code-completion location within ...
A string that acts as a placeholder for, e.g., a function call argument.
bool isObjCZeroArgSelector() const
char __ovld __cnfn max(char x, char y)
Returns y if x < y, otherwise it returns x.
A left parenthesis ('(').
X
Add a minimal nested name specifier fixit hint to allow lookup of a tag name from an outer enclosing ...
Code completion occurred within a preprocessor expression.
Code completion occurred where an expression is expected.
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
An unspecified code-completion context where we should also add macro completions.
const char * getAnnotation(unsigned AnnotationNr) const
Retrieve the annotation string specified by AnnotationNr.
A left angle bracket ('<').
A right angle bracket ('>').
Vertical whitespace ('\n' or '\r\n', depending on the platform).
void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, OverloadCandidate *Candidates, unsigned NumCandidates) override
static Decl::Kind getKind(const Decl *D)
Code completion occurred on the right-hand side of a member access expression using the dot operator...
void AddPlaceholderChunk(const char *Placeholder)
Add a new placeholder chunk.
Code completion occurred where a type name is expected.
Horizontal whitespace (' ').
The entity is available, but has been deprecated (and its use is not recommended).
NamedDecl - This represents a decl with a name.
An Objective-C @interface.
virtual ~CodeCompleteConsumer()
Deregisters and destroys this code-completion consumer.
ObjCCategoryImplDecl - An object of this class encapsulates a category @implementation declaration...