|
23 | 23 | #include "llvm/ADT/StringRef.h" |
24 | 24 | #include <limits> |
25 | 25 | #include <optional> |
26 | | -#include <tuple> |
27 | 26 |
|
28 | 27 | #define DEBUG_TYPE "FindSymbols" |
29 | 28 |
|
30 | 29 | namespace clang { |
31 | 30 | namespace clangd { |
32 | 31 |
|
| 32 | +namespace { |
| 33 | + |
| 34 | +// "Static" means many things in C++, only some get the "static" modifier. |
| 35 | +// |
| 36 | +// Meanings that do: |
| 37 | +// - Members associated with the class rather than the instance. |
| 38 | +// This is what 'static' most often means across languages. |
| 39 | +// - static local variables |
| 40 | +// These are similarly "detached from their context" by the static keyword. |
| 41 | +// In practice, these are rarely used inside classes, reducing confusion. |
| 42 | +// |
| 43 | +// Meanings that don't: |
| 44 | +// - Namespace-scoped variables, which have static storage class. |
| 45 | +// This is implicit, so the keyword "static" isn't so strongly associated. |
| 46 | +// If we want a modifier for these, "global scope" is probably the concept. |
| 47 | +// - Namespace-scoped variables/functions explicitly marked "static". |
| 48 | +// There the keyword changes *linkage* , which is a totally different concept. |
| 49 | +// If we want to model this, "file scope" would be a nice modifier. |
| 50 | +// |
| 51 | +// This is confusing, and maybe we should use another name, but because "static" |
| 52 | +// is a standard LSP modifier, having one with that name has advantages. |
| 53 | +bool isStatic(const Decl *D) { |
| 54 | + if (const auto *CMD = llvm::dyn_cast<CXXMethodDecl>(D)) |
| 55 | + return CMD->isStatic(); |
| 56 | + if (const VarDecl *VD = llvm::dyn_cast<VarDecl>(D)) |
| 57 | + return VD->isStaticDataMember() || VD->isStaticLocal(); |
| 58 | + if (const auto *OPD = llvm::dyn_cast<ObjCPropertyDecl>(D)) |
| 59 | + return OPD->isClassProperty(); |
| 60 | + if (const auto *OMD = llvm::dyn_cast<ObjCMethodDecl>(D)) |
| 61 | + return OMD->isClassMethod(); |
| 62 | + if (const auto *FD = llvm::dyn_cast<FunctionDecl>(D)) |
| 63 | + return FD->isStatic(); |
| 64 | + return false; |
| 65 | +} |
| 66 | + |
| 67 | +// Whether T is const in a loose sense - is a variable with this type readonly? |
| 68 | +bool isConst(QualType T) { |
| 69 | + if (T.isNull()) |
| 70 | + return false; |
| 71 | + T = T.getNonReferenceType(); |
| 72 | + if (T.isConstQualified()) |
| 73 | + return true; |
| 74 | + if (const auto *AT = T->getAsArrayTypeUnsafe()) |
| 75 | + return isConst(AT->getElementType()); |
| 76 | + if (isConst(T->getPointeeType())) |
| 77 | + return true; |
| 78 | + return false; |
| 79 | +} |
| 80 | + |
| 81 | +// Whether D is const in a loose sense (should it be highlighted as such?) |
| 82 | +// FIXME: This is separate from whether *a particular usage* can mutate D. |
| 83 | +// We may want V in V.size() to be readonly even if V is mutable. |
| 84 | +bool isConst(const Decl *D) { |
| 85 | + if (llvm::isa<EnumConstantDecl>(D) || llvm::isa<NonTypeTemplateParmDecl>(D)) |
| 86 | + return true; |
| 87 | + if (llvm::isa<FieldDecl>(D) || llvm::isa<VarDecl>(D) || |
| 88 | + llvm::isa<MSPropertyDecl>(D) || llvm::isa<BindingDecl>(D)) { |
| 89 | + if (isConst(llvm::cast<ValueDecl>(D)->getType())) |
| 90 | + return true; |
| 91 | + } |
| 92 | + if (const auto *OCPD = llvm::dyn_cast<ObjCPropertyDecl>(D)) { |
| 93 | + if (OCPD->isReadOnly()) |
| 94 | + return true; |
| 95 | + } |
| 96 | + if (const auto *MPD = llvm::dyn_cast<MSPropertyDecl>(D)) { |
| 97 | + if (!MPD->hasSetter()) |
| 98 | + return true; |
| 99 | + } |
| 100 | + if (const auto *CMD = llvm::dyn_cast<CXXMethodDecl>(D)) { |
| 101 | + if (CMD->isConst()) |
| 102 | + return true; |
| 103 | + } |
| 104 | + if (const auto *FD = llvm::dyn_cast<FunctionDecl>(D)) |
| 105 | + return isConst(FD->getReturnType()); |
| 106 | + return false; |
| 107 | +} |
| 108 | + |
| 109 | +// Indicates whether declaration D is abstract in cases where D is a struct or a |
| 110 | +// class. |
| 111 | +bool isAbstract(const Decl *D) { |
| 112 | + if (const auto *CMD = llvm::dyn_cast<CXXMethodDecl>(D)) |
| 113 | + return CMD->isPureVirtual(); |
| 114 | + if (const auto *CRD = llvm::dyn_cast<CXXRecordDecl>(D)) |
| 115 | + return CRD->hasDefinition() && CRD->isAbstract(); |
| 116 | + return false; |
| 117 | +} |
| 118 | + |
| 119 | +// Indicates whether declaration D is virtual in cases where D is a method. |
| 120 | +bool isVirtual(const Decl *D) { |
| 121 | + if (const auto *CMD = llvm::dyn_cast<CXXMethodDecl>(D)) |
| 122 | + return CMD->isVirtual(); |
| 123 | + return false; |
| 124 | +} |
| 125 | + |
| 126 | +// Indicates whether declaration D is final in cases where D is a struct, class |
| 127 | +// or method. |
| 128 | +bool isFinal(const Decl *D) { |
| 129 | + if (const auto *CRD = dyn_cast<CXXMethodDecl>(D)) |
| 130 | + return CRD->hasAttr<FinalAttr>(); |
| 131 | + |
| 132 | + if (const auto *CRD = dyn_cast<CXXRecordDecl>(D)) |
| 133 | + return CRD->hasAttr<FinalAttr>(); |
| 134 | + |
| 135 | + return false; |
| 136 | +} |
| 137 | + |
| 138 | +// Indicates whether declaration D is a unique definition (as opposed to a |
| 139 | +// declaration). |
| 140 | +bool isUniqueDefinition(const NamedDecl *Decl) { |
| 141 | + if (auto *Func = dyn_cast<FunctionDecl>(Decl)) |
| 142 | + return Func->isThisDeclarationADefinition(); |
| 143 | + if (auto *Klass = dyn_cast<CXXRecordDecl>(Decl)) |
| 144 | + return Klass->isThisDeclarationADefinition(); |
| 145 | + if (auto *Iface = dyn_cast<ObjCInterfaceDecl>(Decl)) |
| 146 | + return Iface->isThisDeclarationADefinition(); |
| 147 | + if (auto *Proto = dyn_cast<ObjCProtocolDecl>(Decl)) |
| 148 | + return Proto->isThisDeclarationADefinition(); |
| 149 | + if (auto *Var = dyn_cast<VarDecl>(Decl)) |
| 150 | + return Var->isThisDeclarationADefinition(); |
| 151 | + return isa<TemplateTypeParmDecl>(Decl) || |
| 152 | + isa<NonTypeTemplateParmDecl>(Decl) || |
| 153 | + isa<TemplateTemplateParmDecl>(Decl) || isa<ObjCCategoryDecl>(Decl) || |
| 154 | + isa<ObjCImplDecl>(Decl); |
| 155 | +} |
| 156 | +} // namespace |
| 157 | + |
| 158 | +SymbolTags toSymbolTagBitmask(const SymbolTag ST) { |
| 159 | + return (1 << static_cast<unsigned>(ST)); |
| 160 | +} |
| 161 | + |
| 162 | +SymbolTags computeSymbolTags(const NamedDecl &ND) { |
| 163 | + SymbolTags Result = 0; |
| 164 | + const auto IsDef = isUniqueDefinition(&ND); |
| 165 | + |
| 166 | + if (ND.isDeprecated()) |
| 167 | + Result |= toSymbolTagBitmask(SymbolTag::Deprecated); |
| 168 | + |
| 169 | + if (isConst(&ND)) |
| 170 | + Result |= toSymbolTagBitmask(SymbolTag::ReadOnly); |
| 171 | + |
| 172 | + if (isStatic(&ND)) |
| 173 | + Result |= toSymbolTagBitmask(SymbolTag::Static); |
| 174 | + |
| 175 | + if (isVirtual(&ND)) |
| 176 | + Result |= toSymbolTagBitmask(SymbolTag::Virtual); |
| 177 | + |
| 178 | + if (isAbstract(&ND)) |
| 179 | + Result |= toSymbolTagBitmask(SymbolTag::Abstract); |
| 180 | + |
| 181 | + if (isFinal(&ND)) |
| 182 | + Result |= toSymbolTagBitmask(SymbolTag::Final); |
| 183 | + |
| 184 | + if (not isa<UnresolvedUsingValueDecl>(ND)) { |
| 185 | + // Do not treat an UnresolvedUsingValueDecl as a declaration. |
| 186 | + // It's more common to think of it as a reference to the |
| 187 | + // underlying declaration. |
| 188 | + Result |= toSymbolTagBitmask(SymbolTag::Declaration); |
| 189 | + |
| 190 | + if (IsDef) |
| 191 | + Result |= toSymbolTagBitmask(SymbolTag::Definition); |
| 192 | + } |
| 193 | + |
| 194 | + switch (ND.getAccess()) { |
| 195 | + case AS_public: |
| 196 | + Result |= toSymbolTagBitmask(SymbolTag::Public); |
| 197 | + break; |
| 198 | + case AS_protected: |
| 199 | + Result |= toSymbolTagBitmask(SymbolTag::Protected); |
| 200 | + break; |
| 201 | + case AS_private: |
| 202 | + Result |= toSymbolTagBitmask(SymbolTag::Private); |
| 203 | + break; |
| 204 | + default: |
| 205 | + break; |
| 206 | + } |
| 207 | + |
| 208 | + return Result; |
| 209 | +} |
| 210 | + |
| 211 | +std::vector<SymbolTag> getSymbolTags(const NamedDecl &ND) { |
| 212 | + const auto symbolTags = computeSymbolTags(ND); |
| 213 | + std::vector<SymbolTag> Tags; |
| 214 | + |
| 215 | + if (symbolTags == 0) |
| 216 | + return Tags; |
| 217 | + |
| 218 | + // Iterate through SymbolTag enum values and collect any that are present in |
| 219 | + // the bitmask. SymbolTag values are in the numeric range |
| 220 | + // [FirstTag .. LastTag]. |
| 221 | + constexpr unsigned MinTag = static_cast<unsigned>(SymbolTag::FirstTag); |
| 222 | + constexpr unsigned MaxTag = static_cast<unsigned>(SymbolTag::LastTag); |
| 223 | + for (unsigned I = MinTag; I <= MaxTag; ++I) { |
| 224 | + auto ST = static_cast<SymbolTag>(I); |
| 225 | + if (symbolTags & toSymbolTagBitmask(ST)) |
| 226 | + Tags.push_back(ST); |
| 227 | + } |
| 228 | + return Tags; |
| 229 | +} |
| 230 | + |
33 | 231 | namespace { |
34 | 232 | using ScoredSymbolInfo = std::pair<float, SymbolInformation>; |
35 | 233 | struct ScoredSymbolGreater { |
@@ -242,6 +440,7 @@ std::optional<DocumentSymbol> declToSym(ASTContext &Ctx, const NamedDecl &ND) { |
242 | 440 | SI.range = Range{sourceLocToPosition(SM, SymbolRange->getBegin()), |
243 | 441 | sourceLocToPosition(SM, SymbolRange->getEnd())}; |
244 | 442 | SI.detail = getSymbolDetail(Ctx, ND); |
| 443 | + SI.tags = getSymbolTags(ND); |
245 | 444 |
|
246 | 445 | SourceLocation NameLoc = ND.getLocation(); |
247 | 446 | SourceLocation FallbackNameLoc; |
|
0 commit comments