diff --git a/clang-tools-extra/clangd/AST.cpp b/clang-tools-extra/clangd/AST.cpp index f3eee1c6335f9..a234155a37191 100644 --- a/clang-tools-extra/clangd/AST.cpp +++ b/clang-tools-extra/clangd/AST.cpp @@ -169,6 +169,90 @@ bool isImplementationDetail(const Decl *D) { D->getASTContext().getSourceManager()); } +// Whether T is const in a loose sense - is a variable with this type readonly? +bool isConst(QualType T) { + if (T.isNull()) + return false; + T = T.getNonReferenceType(); + if (T.isConstQualified()) + return true; + if (const auto *AT = T->getAsArrayTypeUnsafe()) + return isConst(AT->getElementType()); + if (isConst(T->getPointeeType())) + return true; + return false; +} + +bool isConst(const Decl *D) { + if (llvm::isa(D) || llvm::isa(D)) + return true; + if (llvm::isa(D) || llvm::isa(D) || + llvm::isa(D) || llvm::isa(D)) { + if (isConst(llvm::cast(D)->getType())) + return true; + } + if (const auto *OCPD = llvm::dyn_cast(D)) { + if (OCPD->isReadOnly()) + return true; + } + if (const auto *MPD = llvm::dyn_cast(D)) { + if (!MPD->hasSetter()) + return true; + } + if (const auto *CMD = llvm::dyn_cast(D)) { + if (CMD->isConst()) + return true; + } + if (const auto *FD = llvm::dyn_cast(D)) + return isConst(FD->getReturnType()); + return false; +} + +bool isStatic(const Decl *D) { + if (const auto *CMD = llvm::dyn_cast(D)) + return CMD->isStatic(); + if (const VarDecl *VD = llvm::dyn_cast(D)) + return VD->isStaticDataMember() || VD->isStaticLocal(); + if (const auto *OPD = llvm::dyn_cast(D)) + return OPD->isClassProperty(); + if (const auto *OMD = llvm::dyn_cast(D)) + return OMD->isClassMethod(); + if (const auto *FD = llvm::dyn_cast(D)) + return FD->isStatic(); + return false; +} + +bool isAbstract(const Decl *D) { + if (const auto *CMD = llvm::dyn_cast(D)) + return CMD->isPureVirtual(); + if (const auto *CRD = llvm::dyn_cast(D)) + return CRD->hasDefinition() && CRD->isAbstract(); + return false; +} + +bool isVirtual(const Decl *D) { + if (const auto *CMD = llvm::dyn_cast(D)) + return CMD->isVirtual(); + return false; +} + +bool isUniqueDefinition(const NamedDecl *Decl) { + if (auto *Func = dyn_cast(Decl)) + return Func->isThisDeclarationADefinition(); + if (auto *Klass = dyn_cast(Decl)) + return Klass->isThisDeclarationADefinition(); + if (auto *Iface = dyn_cast(Decl)) + return Iface->isThisDeclarationADefinition(); + if (auto *Proto = dyn_cast(Decl)) + return Proto->isThisDeclarationADefinition(); + if (auto *Var = dyn_cast(Decl)) + return Var->isThisDeclarationADefinition(); + return isa(Decl) || + isa(Decl) || + isa(Decl) || isa(Decl) || + isa(Decl); +} + SourceLocation nameLocation(const clang::Decl &D, const SourceManager &SM) { auto L = D.getLocation(); // For `- (void)foo` we want `foo` not the `-`. diff --git a/clang-tools-extra/clangd/AST.h b/clang-tools-extra/clangd/AST.h index fb0722d697cd0..09bd979cb8649 100644 --- a/clang-tools-extra/clangd/AST.h +++ b/clang-tools-extra/clangd/AST.h @@ -152,6 +152,37 @@ bool isImplicitTemplateInstantiation(const NamedDecl *D); /// explicit specialization. bool isExplicitTemplateSpecialization(const NamedDecl *D); +// Whether T is const in a loose sense - is a variable with this type readonly? +bool isConst(QualType T); + +// Whether D is const in a loose sense (should it be highlighted as such?) +// FIXME: This is separate from whether *a particular usage* can mutate D. +// We may want V in V.size() to be readonly even if V is mutable. +bool isConst(const Decl *D); + +// "Static" means many things in C++, only some get the "static" modifier. +// +// Meanings that do: +// - Members associated with the class rather than the instance. +// This is what 'static' most often means across languages. +// - static local variables +// These are similarly "detached from their context" by the static keyword. +// In practice, these are rarely used inside classes, reducing confusion. +// +// Meanings that don't: +// - Namespace-scoped variables, which have static storage class. +// This is implicit, so the keyword "static" isn't so strongly associated. +// If we want a modifier for these, "global scope" is probably the concept. +// - Namespace-scoped variables/functions explicitly marked "static". +// There the keyword changes *linkage* , which is a totally different concept. +// If we want to model this, "file scope" would be a nice modifier. +// +// This is confusing, and maybe we should use another name, but because "static" +// is a standard LSP modifier, having one with that name has advantages. +bool isStatic(const Decl *D); +bool isAbstract(const Decl *D); +bool isVirtual(const Decl *D); +bool isUniqueDefinition(const NamedDecl *Decl); /// Returns a nested name specifier loc of \p ND if it was present in the /// source, e.g. /// void ns::something::foo() -> returns 'ns::something' diff --git a/clang-tools-extra/clangd/FindSymbols.cpp b/clang-tools-extra/clangd/FindSymbols.cpp index 84bcbc1f2ddd3..384e0db82d228 100644 --- a/clang-tools-extra/clangd/FindSymbols.cpp +++ b/clang-tools-extra/clangd/FindSymbols.cpp @@ -187,6 +187,41 @@ std::string getSymbolName(ASTContext &Ctx, const NamedDecl &ND) { return printName(Ctx, ND); } +std::vector getSymbolTags(const NamedDecl &ND) { + std::vector Tags; + if (ND.isDeprecated()) + Tags.push_back(SymbolTag::Deprecated); + if (isConst(&ND)) + Tags.push_back(SymbolTag::ReadOnly); + if (isStatic(&ND)) + Tags.push_back(SymbolTag::Static); + if (isVirtual(&ND)) + Tags.push_back(SymbolTag::Virtual); + if (isAbstract(&ND)) + Tags.push_back(SymbolTag::Abstract); + + if (isUniqueDefinition(&ND)) + Tags.push_back(SymbolTag::Definition); + else if (!isa(ND)) + Tags.push_back(SymbolTag::Declaration); + + switch (ND.getAccess()) { + case AS_public: + Tags.push_back(SymbolTag::Public); + break; + case AS_protected: + Tags.push_back(SymbolTag::Protected); + break; + case AS_private: + Tags.push_back(SymbolTag::Private); + break; + default: + break; + } + + return Tags; +} + std::string getSymbolDetail(ASTContext &Ctx, const NamedDecl &ND) { PrintingPolicy P(Ctx.getPrintingPolicy()); P.SuppressScope = true; @@ -241,6 +276,7 @@ std::optional declToSym(ASTContext &Ctx, const NamedDecl &ND) { SI.range = Range{sourceLocToPosition(SM, SymbolRange->getBegin()), sourceLocToPosition(SM, SymbolRange->getEnd())}; SI.detail = getSymbolDetail(Ctx, ND); + SI.tags = getSymbolTags(ND); SourceLocation NameLoc = ND.getLocation(); SourceLocation FallbackNameLoc; diff --git a/clang-tools-extra/clangd/Protocol.cpp b/clang-tools-extra/clangd/Protocol.cpp index 295ccd26a4045..8805c05ae4d13 100644 --- a/clang-tools-extra/clangd/Protocol.cpp +++ b/clang-tools-extra/clangd/Protocol.cpp @@ -901,6 +901,8 @@ llvm::json::Value toJSON(const DocumentSymbol &S) { Result["children"] = S.children; if (S.deprecated) Result["deprecated"] = true; + if (!S.tags.empty()) + Result["tags"] = S.tags; // FIXME: workaround for older gcc/clang return std::move(Result); } diff --git a/clang-tools-extra/clangd/Protocol.h b/clang-tools-extra/clangd/Protocol.h index 5b28095758198..d93ccf61ac0dc 100644 --- a/clang-tools-extra/clangd/Protocol.h +++ b/clang-tools-extra/clangd/Protocol.h @@ -1090,6 +1090,30 @@ struct CodeAction { }; llvm::json::Value toJSON(const CodeAction &); +enum class SymbolTag { + Deprecated = 1, + Private = 2, + Package = 3, + Protected = 4, + Public = 5, + Internal= 6, + File = 7, + Static = 8, + Abstract = 9, + Final = 10, + Sealed = 11, + Constant = 12, + Transient = 13, + Volatile = 14, + Synchronized = 15, + Virtual = 16, + Nullable = 17, + NonNull = 18, + Declaration = 19, + Definition = 20, + ReadOnly = 21, +}; +llvm::json::Value toJSON(SymbolTag); /// Represents programming constructs like variables, classes, interfaces etc. /// that appear in a document. Document symbols can be hierarchical and they /// have two ranges: one that encloses its definition and one that points to its @@ -1107,6 +1131,9 @@ struct DocumentSymbol { /// Indicates if this symbol is deprecated. bool deprecated = false; + /// Tags for this symbol, e.g public, private, static, const etc. + std::vector tags; + /// The range enclosing this symbol not including leading/trailing whitespace /// but everything else like comments. This information is typically used to /// determine if the clients cursor is inside the symbol to reveal in the @@ -1558,8 +1585,6 @@ struct ResolveTypeHierarchyItemParams { bool fromJSON(const llvm::json::Value &, ResolveTypeHierarchyItemParams &, llvm::json::Path); -enum class SymbolTag { Deprecated = 1 }; -llvm::json::Value toJSON(SymbolTag); /// The parameter of a `textDocument/prepareCallHierarchy` request. struct CallHierarchyPrepareParams : public TextDocumentPositionParams {}; diff --git a/clang-tools-extra/clangd/SemanticHighlighting.cpp b/clang-tools-extra/clangd/SemanticHighlighting.cpp index e6d16af2495fe..6597be81040b2 100644 --- a/clang-tools-extra/clangd/SemanticHighlighting.cpp +++ b/clang-tools-extra/clangd/SemanticHighlighting.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "SemanticHighlighting.h" +#include "AST.h" #include "Config.h" #include "FindTarget.h" #include "HeuristicResolver.h" @@ -77,23 +78,6 @@ bool canHighlightName(DeclarationName Name) { llvm_unreachable("invalid name kind"); } -bool isUniqueDefinition(const NamedDecl *Decl) { - if (auto *Func = dyn_cast(Decl)) - return Func->isThisDeclarationADefinition(); - if (auto *Klass = dyn_cast(Decl)) - return Klass->isThisDeclarationADefinition(); - if (auto *Iface = dyn_cast(Decl)) - return Iface->isThisDeclarationADefinition(); - if (auto *Proto = dyn_cast(Decl)) - return Proto->isThisDeclarationADefinition(); - if (auto *Var = dyn_cast(Decl)) - return Var->isThisDeclarationADefinition(); - return isa(Decl) || - isa(Decl) || - isa(Decl) || isa(Decl) || - isa(Decl); -} - std::optional kindForType(const Type *TP, const HeuristicResolver *Resolver); std::optional kindForDecl(const NamedDecl *D, @@ -192,91 +176,6 @@ std::optional kindForType(const Type *TP, return std::nullopt; } -// Whether T is const in a loose sense - is a variable with this type readonly? -bool isConst(QualType T) { - if (T.isNull()) - return false; - T = T.getNonReferenceType(); - if (T.isConstQualified()) - return true; - if (const auto *AT = T->getAsArrayTypeUnsafe()) - return isConst(AT->getElementType()); - if (isConst(T->getPointeeType())) - return true; - return false; -} - -// Whether D is const in a loose sense (should it be highlighted as such?) -// FIXME: This is separate from whether *a particular usage* can mutate D. -// We may want V in V.size() to be readonly even if V is mutable. -bool isConst(const Decl *D) { - if (llvm::isa(D) || llvm::isa(D)) - return true; - if (llvm::isa(D) || llvm::isa(D) || - llvm::isa(D) || llvm::isa(D)) { - if (isConst(llvm::cast(D)->getType())) - return true; - } - if (const auto *OCPD = llvm::dyn_cast(D)) { - if (OCPD->isReadOnly()) - return true; - } - if (const auto *MPD = llvm::dyn_cast(D)) { - if (!MPD->hasSetter()) - return true; - } - if (const auto *CMD = llvm::dyn_cast(D)) { - if (CMD->isConst()) - return true; - } - return false; -} - -// "Static" means many things in C++, only some get the "static" modifier. -// -// Meanings that do: -// - Members associated with the class rather than the instance. -// This is what 'static' most often means across languages. -// - static local variables -// These are similarly "detached from their context" by the static keyword. -// In practice, these are rarely used inside classes, reducing confusion. -// -// Meanings that don't: -// - Namespace-scoped variables, which have static storage class. -// This is implicit, so the keyword "static" isn't so strongly associated. -// If we want a modifier for these, "global scope" is probably the concept. -// - Namespace-scoped variables/functions explicitly marked "static". -// There the keyword changes *linkage* , which is a totally different concept. -// If we want to model this, "file scope" would be a nice modifier. -// -// This is confusing, and maybe we should use another name, but because "static" -// is a standard LSP modifier, having one with that name has advantages. -bool isStatic(const Decl *D) { - if (const auto *CMD = llvm::dyn_cast(D)) - return CMD->isStatic(); - if (const VarDecl *VD = llvm::dyn_cast(D)) - return VD->isStaticDataMember() || VD->isStaticLocal(); - if (const auto *OPD = llvm::dyn_cast(D)) - return OPD->isClassProperty(); - if (const auto *OMD = llvm::dyn_cast(D)) - return OMD->isClassMethod(); - return false; -} - -bool isAbstract(const Decl *D) { - if (const auto *CMD = llvm::dyn_cast(D)) - return CMD->isPureVirtual(); - if (const auto *CRD = llvm::dyn_cast(D)) - return CRD->hasDefinition() && CRD->isAbstract(); - return false; -} - -bool isVirtual(const Decl *D) { - if (const auto *CMD = llvm::dyn_cast(D)) - return CMD->isVirtual(); - return false; -} - bool isDependent(const Decl *D) { if (isa(D)) return true;