1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
|
//===- IndexTypeSourceInfo.cpp - Indexing types ---------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "IndexingContext.h"
#include "clang/AST/ASTConcept.h"
#include "clang/AST/PrettyPrinter.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/AST/TypeLoc.h"
#include "llvm/ADT/ScopeExit.h"
using namespace clang;
using namespace index;
namespace {
class TypeIndexer : public RecursiveASTVisitor<TypeIndexer> {
IndexingContext &IndexCtx;
const NamedDecl *Parent;
const DeclContext *ParentDC;
bool IsBase;
SmallVector<SymbolRelation, 3> Relations;
typedef RecursiveASTVisitor<TypeIndexer> base;
public:
TypeIndexer(IndexingContext &indexCtx, const NamedDecl *parent,
const DeclContext *DC, bool isBase, bool isIBType)
: IndexCtx(indexCtx), Parent(parent), ParentDC(DC), IsBase(isBase) {
if (IsBase) {
assert(Parent);
Relations.emplace_back((unsigned)SymbolRole::RelationBaseOf, Parent);
}
if (isIBType) {
assert(Parent);
Relations.emplace_back((unsigned)SymbolRole::RelationIBTypeOf, Parent);
}
}
bool shouldWalkTypesOfTypeLocs() const { return false; }
#define TRY_TO(CALL_EXPR) \
do { \
if (!CALL_EXPR) \
return false; \
} while (0)
bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TTPL) {
SourceLocation Loc = TTPL.getNameLoc();
TemplateTypeParmDecl *TTPD = TTPL.getDecl();
return IndexCtx.handleReference(TTPD, Loc, Parent, ParentDC,
SymbolRoleSet());
}
bool VisitTypedefTypeLoc(TypedefTypeLoc TL) {
SourceLocation Loc = TL.getNameLoc();
TypedefNameDecl *ND = TL.getTypedefNameDecl();
if (ND->isTransparentTag()) {
TagDecl *Underlying = ND->getUnderlyingType()->getAsTagDecl();
return IndexCtx.handleReference(Underlying, Loc, Parent,
ParentDC, SymbolRoleSet(), Relations);
}
if (IsBase) {
TRY_TO(IndexCtx.handleReference(ND, Loc,
Parent, ParentDC, SymbolRoleSet()));
if (auto *CD = TL.getType()->getAsCXXRecordDecl()) {
TRY_TO(IndexCtx.handleReference(CD, Loc, Parent, ParentDC,
(unsigned)SymbolRole::Implicit,
Relations));
}
} else {
TRY_TO(IndexCtx.handleReference(ND, Loc,
Parent, ParentDC, SymbolRoleSet(),
Relations));
}
return true;
}
bool VisitAutoTypeLoc(AutoTypeLoc TL) {
if (auto *C = TL.getNamedConcept())
return IndexCtx.handleReference(C, TL.getConceptNameLoc(), Parent,
ParentDC);
return true;
}
bool traverseParamVarHelper(ParmVarDecl *D) {
TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
if (D->getTypeSourceInfo())
TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
return true;
}
bool TraverseParmVarDecl(ParmVarDecl *D) {
// Avoid visiting default arguments from the definition that were already
// visited in the declaration.
// FIXME: A free function definition can have default arguments.
// Avoiding double visitaiton of default arguments should be handled by the
// visitor probably with a bit in the AST to indicate if the attached
// default argument was 'inherited' or written in source.
if (auto FD = dyn_cast<FunctionDecl>(D->getDeclContext())) {
if (FD->isThisDeclarationADefinition()) {
return traverseParamVarHelper(D);
}
}
return base::TraverseParmVarDecl(D);
}
bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
IndexCtx.indexNestedNameSpecifierLoc(NNS, Parent, ParentDC);
return true;
}
bool VisitTagTypeLoc(TagTypeLoc TL) {
TagDecl *D = TL.getDecl();
if (!IndexCtx.shouldIndexFunctionLocalSymbols() &&
D->getParentFunctionOrMethod())
return true;
if (TL.isDefinition()) {
IndexCtx.indexTagDecl(D);
return true;
}
return IndexCtx.handleReference(D, TL.getNameLoc(),
Parent, ParentDC, SymbolRoleSet(),
Relations);
}
bool VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
return IndexCtx.handleReference(TL.getIFaceDecl(), TL.getNameLoc(),
Parent, ParentDC, SymbolRoleSet(), Relations);
}
bool VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) {
IndexCtx.handleReference(TL.getProtocol(i), TL.getProtocolLoc(i),
Parent, ParentDC, SymbolRoleSet(), Relations);
}
return true;
}
void HandleTemplateSpecializationTypeLoc(TemplateName TemplName,
SourceLocation TemplNameLoc,
CXXRecordDecl *ResolvedClass,
bool IsTypeAlias) {
// In presence of type aliases, the resolved class was never written in
// the code so don't report it.
if (!IsTypeAlias && ResolvedClass &&
(!ResolvedClass->isImplicit() ||
IndexCtx.shouldIndexImplicitInstantiation())) {
IndexCtx.handleReference(ResolvedClass, TemplNameLoc, Parent, ParentDC,
SymbolRoleSet(), Relations);
} else if (const TemplateDecl *D = TemplName.getAsTemplateDecl()) {
IndexCtx.handleReference(D, TemplNameLoc, Parent, ParentDC,
SymbolRoleSet(), Relations);
}
}
bool VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
auto *T = TL.getTypePtr();
if (!T)
return true;
HandleTemplateSpecializationTypeLoc(
T->getTemplateName(), TL.getTemplateNameLoc(), T->getAsCXXRecordDecl(),
T->isTypeAlias());
return true;
}
bool TraverseTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
if (!WalkUpFromTemplateSpecializationTypeLoc(TL))
return false;
if (!TraverseTemplateName(TL.getTypePtr()->getTemplateName()))
return false;
// The relations we have to `Parent` do not apply to our template arguments,
// so clear them while visiting the args.
SmallVector<SymbolRelation, 3> SavedRelations = Relations;
Relations.clear();
auto ResetSavedRelations =
llvm::make_scope_exit([&] { this->Relations = SavedRelations; });
for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
if (!TraverseTemplateArgumentLoc(TL.getArgLoc(I)))
return false;
}
return true;
}
bool VisitDeducedTemplateSpecializationTypeLoc(DeducedTemplateSpecializationTypeLoc TL) {
auto *T = TL.getTypePtr();
if (!T)
return true;
HandleTemplateSpecializationTypeLoc(
T->getTemplateName(), TL.getTemplateNameLoc(), T->getAsCXXRecordDecl(),
/*IsTypeAlias=*/false);
return true;
}
bool VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
return IndexCtx.handleReference(TL.getDecl(), TL.getNameLoc(), Parent,
ParentDC, SymbolRoleSet(), Relations);
}
bool VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
const DependentNameType *DNT = TL.getTypePtr();
const NestedNameSpecifier *NNS = DNT->getQualifier();
const Type *T = NNS->getAsType();
if (!T)
return true;
const TemplateSpecializationType *TST =
T->getAs<TemplateSpecializationType>();
if (!TST)
return true;
TemplateName TN = TST->getTemplateName();
const ClassTemplateDecl *TD =
dyn_cast_or_null<ClassTemplateDecl>(TN.getAsTemplateDecl());
if (!TD)
return true;
CXXRecordDecl *RD = TD->getTemplatedDecl();
if (!RD->hasDefinition())
return true;
RD = RD->getDefinition();
DeclarationName Name(DNT->getIdentifier());
std::vector<const NamedDecl *> Symbols = RD->lookupDependentName(
Name, [](const NamedDecl *ND) { return isa<TypeDecl>(ND); });
if (Symbols.size() != 1)
return true;
return IndexCtx.handleReference(Symbols[0], TL.getNameLoc(), Parent,
ParentDC, SymbolRoleSet(), Relations);
}
bool TraverseStmt(Stmt *S) {
IndexCtx.indexBody(S, Parent, ParentDC);
return true;
}
};
} // anonymous namespace
void IndexingContext::indexTypeSourceInfo(TypeSourceInfo *TInfo,
const NamedDecl *Parent,
const DeclContext *DC,
bool isBase,
bool isIBType) {
if (!TInfo || TInfo->getTypeLoc().isNull())
return;
indexTypeLoc(TInfo->getTypeLoc(), Parent, DC, isBase, isIBType);
}
void IndexingContext::indexTypeLoc(TypeLoc TL,
const NamedDecl *Parent,
const DeclContext *DC,
bool isBase,
bool isIBType) {
if (TL.isNull())
return;
if (!DC)
DC = Parent->getLexicalDeclContext();
TypeIndexer(*this, Parent, DC, isBase, isIBType).TraverseTypeLoc(TL);
}
void IndexingContext::indexNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
const NamedDecl *Parent,
const DeclContext *DC) {
if (!NNS)
return;
if (NestedNameSpecifierLoc Prefix = NNS.getPrefix())
indexNestedNameSpecifierLoc(Prefix, Parent, DC);
if (!DC)
DC = Parent->getLexicalDeclContext();
SourceLocation Loc = NNS.getLocalBeginLoc();
switch (NNS.getNestedNameSpecifier()->getKind()) {
case NestedNameSpecifier::Identifier:
case NestedNameSpecifier::Global:
case NestedNameSpecifier::Super:
break;
case NestedNameSpecifier::Namespace:
handleReference(NNS.getNestedNameSpecifier()->getAsNamespace(),
Loc, Parent, DC, SymbolRoleSet());
break;
case NestedNameSpecifier::NamespaceAlias:
handleReference(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(),
Loc, Parent, DC, SymbolRoleSet());
break;
case NestedNameSpecifier::TypeSpec:
case NestedNameSpecifier::TypeSpecWithTemplate:
indexTypeLoc(NNS.getTypeLoc(), Parent, DC);
break;
}
}
void IndexingContext::indexTagDecl(const TagDecl *D,
ArrayRef<SymbolRelation> Relations) {
if (!shouldIndex(D))
return;
if (!shouldIndexFunctionLocalSymbols() && isFunctionLocalSymbol(D))
return;
if (handleDecl(D, /*Roles=*/SymbolRoleSet(), Relations)) {
if (D->isThisDeclarationADefinition()) {
indexNestedNameSpecifierLoc(D->getQualifierLoc(), D);
if (auto CXXRD = dyn_cast<CXXRecordDecl>(D)) {
for (const auto &I : CXXRD->bases()) {
indexTypeSourceInfo(I.getTypeSourceInfo(), CXXRD, CXXRD, /*isBase=*/true);
}
}
indexDeclContext(D);
}
}
}
|