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 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
|
//===--- SymbolOccurrenceFinder.cpp - Clang refactoring library -----------===//
//
// 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
//
//===----------------------------------------------------------------------===//
///
/// \file
/// \brief Methods for finding all instances of a USR. Our strategy is very
/// simple; we just compare the USR at every relevant AST node with the one
/// provided.
///
//===----------------------------------------------------------------------===//
#include "clang/Tooling/Refactor/SymbolOccurrenceFinder.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/DependentASTVisitor.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Index/USRGeneration.h"
#include "clang/Lex/Lexer.h"
#include "clang/Tooling/Refactor/USRFinder.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
using namespace llvm;
namespace clang {
namespace tooling {
namespace rename {
namespace {
// \brief This visitor recursively searches for all instances of a USR in a
// translation unit and stores them for later usage.
class SymbolOccurrenceFinderASTVisitor
: public DependentASTVisitor<SymbolOccurrenceFinderASTVisitor> {
public:
explicit SymbolOccurrenceFinderASTVisitor(
const SymbolOperation &Operation, const ASTContext &Context,
std::vector<OldSymbolOccurrence> &Occurrences)
: Operation(Operation), Context(Context), Occurrences(Occurrences) {}
/// Returns a \c Symbol if the given declaration corresponds to the symbol
/// that we're looking for.
const Symbol *symbolForDecl(const Decl *D) const {
if (!D)
return nullptr;
std::string USR = getUSRForDecl(D);
return Operation.getSymbolForUSR(USR);
}
void checkDecl(const Decl *D, SourceLocation Loc,
OldSymbolOccurrence::OccurrenceKind Kind =
OldSymbolOccurrence::MatchingSymbol) {
if (!D)
return;
std::string USR = getUSRForDecl(D);
if (const Symbol *S = Operation.getSymbolForUSR(USR))
checkAndAddLocations(S->SymbolIndex, Loc, Kind);
}
// Declaration visitors:
bool VisitCXXConstructorDecl(clang::CXXConstructorDecl *ConstructorDecl) {
for (const auto *Initializer : ConstructorDecl->inits()) {
// Ignore implicit initializers.
if (!Initializer->isWritten())
continue;
if (const clang::FieldDecl *FieldDecl = Initializer->getMember())
checkDecl(FieldDecl, Initializer->getSourceLocation());
}
return true;
}
bool VisitNamedDecl(const NamedDecl *Decl) {
checkDecl(Decl, Decl->getLocation());
return true;
}
bool WalkUpFromTypedefNameDecl(const TypedefNameDecl *D) {
// Don't visit the NamedDecl for TypedefNameDecl.
return VisitTypedefNamedDecl(D);
}
bool VisitTypedefNamedDecl(const TypedefNameDecl *D) {
if (D->isTransparentTag()) {
if (const auto *Underlying = D->getUnderlyingType()->getAsTagDecl()) {
checkDecl(Underlying, D->getLocation());
return true;
}
}
return VisitNamedDecl(D);
}
bool WalkUpFromUsingDecl(const UsingDecl *D) {
// Don't visit the NamedDecl for UsingDecl.
return VisitUsingDecl(D);
}
bool VisitUsingDecl(const UsingDecl *D) {
for (const auto *Shadow : D->shadows()) {
const NamedDecl *UD = Shadow->getUnderlyingDecl();
if (UD->isImplicit() || UD == D)
continue;
if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(UD)) {
UD = FTD->getTemplatedDecl();
if (!UD)
continue;
}
checkDecl(UD, D->getLocation());
}
return true;
}
bool WalkUpFromUsingDirectiveDecl(const UsingDirectiveDecl *D) {
// Don't visit the NamedDecl for UsingDirectiveDecl.
return VisitUsingDirectiveDecl(D);
}
bool VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
checkDecl(D->getNominatedNamespaceAsWritten(), D->getLocation());
return true;
}
bool WalkUpFromUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D) {
// Don't visit the NamedDecl for UnresolvedUsingValueDecl.
// FIXME: Can we try to lookup the name?
return true;
}
bool
WalkUpFromUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D) {
// Don't visit the NamedDecl for UnresolvedUsingTypenameDecl.
// FIXME: Can we try to lookup the name?
return true;
}
bool WalkUpFromObjCMethodDecl(const ObjCMethodDecl *Decl) {
// Don't visit the NamedDecl for Objective-C methods.
return VisitObjCMethodDecl(Decl);
}
bool VisitObjCMethodDecl(const ObjCMethodDecl *Decl) {
const Symbol *S = symbolForDecl(Decl);
if (!S)
return true;
SmallVector<SourceLocation, 8> SelectorLocs;
Decl->getSelectorLocs(SelectorLocs);
checkAndAddLocations(S->SymbolIndex, SelectorLocs);
return true;
}
bool handleObjCProtocolList(const ObjCProtocolList &Protocols) {
for (auto It : enumerate(Protocols))
checkDecl(It.value(), Protocols.loc_begin()[It.index()]);
return true;
}
bool VisitObjCInterfaceDecl(const ObjCInterfaceDecl *Decl) {
if (!Decl->hasDefinition())
return true;
return handleObjCProtocolList(Decl->getReferencedProtocols());
}
bool VisitObjCProtocolDecl(const ObjCProtocolDecl *Decl) {
if (!Decl->hasDefinition())
return true;
return handleObjCProtocolList(Decl->getReferencedProtocols());
}
bool VisitObjCCategoryDecl(const ObjCCategoryDecl *Decl) {
checkDecl(Decl, Decl->getCategoryNameLoc());
// The location of the class name is the location of the declaration.
checkDecl(Decl->getClassInterface(), Decl->getLocation());
return handleObjCProtocolList(Decl->getReferencedProtocols());
}
bool VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *Decl) {
checkDecl(Decl, Decl->getCategoryNameLoc());
// The location of the class name is the location of the declaration.
checkDecl(Decl->getClassInterface(), Decl->getLocation());
return true;
}
bool VisitObjCCompatibleAliasDecl(const ObjCCompatibleAliasDecl *Decl) {
checkDecl(Decl->getClassInterface(), Decl->getClassInterfaceLoc());
return true;
}
bool VisitObjCPropertyDecl(const ObjCPropertyDecl *Decl) {
if (Decl->hasExplicitGetterName())
checkDecl(Decl->getGetterMethodDecl(), Decl->getGetterNameLoc());
if (Decl->hasExplicitSetterName())
checkDecl(Decl->getSetterMethodDecl(), Decl->getSetterNameLoc());
return true;
}
bool VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *Decl) {
checkDecl(Decl->getPropertyDecl(), Decl->getLocation());
if (Decl->isIvarNameSpecified())
checkDecl(Decl->getPropertyIvarDecl(), Decl->getPropertyIvarDeclLoc());
return true;
}
// Expression visitors:
bool VisitDeclRefExpr(const DeclRefExpr *Expr) {
checkDecl(Expr->getFoundDecl(), Expr->getLocation());
return true;
}
bool VisitMemberExpr(const MemberExpr *Expr) {
checkDecl(Expr->getFoundDecl().getDecl(), Expr->getMemberLoc());
return true;
}
bool VisitObjCMessageExpr(const ObjCMessageExpr *Expr) {
const Symbol *S = symbolForDecl(Expr->getMethodDecl());
if (!S)
return true;
SmallVector<SourceLocation, 8> SelectorLocs;
Expr->getSelectorLocs(SelectorLocs);
checkAndAddLocations(S->SymbolIndex, SelectorLocs);
return true;
}
bool VisitObjCProtocolExpr(const ObjCProtocolExpr *Expr) {
checkDecl(Expr->getProtocol(), Expr->getProtocolIdLoc());
return true;
}
bool VisitObjCIvarRefExpr(const ObjCIvarRefExpr *Expr) {
checkDecl(Expr->getDecl(), Expr->getLocation());
return true;
}
bool VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Expr) {
if (Expr->isClassReceiver())
checkDecl(Expr->getClassReceiver(), Expr->getReceiverLocation());
if (Expr->isImplicitProperty()) {
// Class properties that are explicitly defined using @property
// declarations are represented implicitly as there is no ivar for class
// properties.
if (const ObjCMethodDecl *Getter = Expr->getImplicitPropertyGetter()) {
if (Getter->isClassMethod())
if (const auto *PD = Getter->getCanonicalDecl()->findPropertyDecl()) {
checkDecl(PD, Expr->getLocation());
return true;
}
}
checkDecl(Expr->getImplicitPropertyGetter(), Expr->getLocation(),
OldSymbolOccurrence::MatchingImplicitProperty);
// Add a manual location for a setter since a token like 'property' won't
// match the the name of the renamed symbol like 'setProperty'.
if (const auto *S = symbolForDecl(Expr->getImplicitPropertySetter()))
addLocation(S->SymbolIndex, Expr->getLocation(),
OldSymbolOccurrence::MatchingImplicitProperty);
return true;
}
checkDecl(Expr->getExplicitProperty(), Expr->getLocation());
return true;
}
// Other visitors:
bool VisitTypeLoc(const TypeLoc Loc) {
TypedefTypeLoc TTL = Loc.getAs<TypedefTypeLoc>();
if (TTL) {
const auto *TND = TTL.getTypedefNameDecl();
if (TND->isTransparentTag()) {
if (const auto *Underlying = TND->getUnderlyingType()->getAsTagDecl()) {
checkDecl(Underlying, TTL.getNameLoc());
return true;
}
}
checkDecl(TND, TTL.getNameLoc());
return true;
}
TypeSpecTypeLoc TSTL = Loc.getAs<TypeSpecTypeLoc>();
if (TSTL) {
checkDecl(Loc.getType()->getAsTagDecl(), TSTL.getNameLoc());
}
if (const auto *TemplateTypeParm =
dyn_cast<TemplateTypeParmType>(Loc.getType())) {
checkDecl(TemplateTypeParm->getDecl(), Loc.getBeginLoc());
}
if (const auto *TemplateSpecType =
dyn_cast<TemplateSpecializationType>(Loc.getType())) {
checkDecl(TemplateSpecType->getTemplateName().getAsTemplateDecl(),
Loc.getBeginLoc());
}
return true;
}
bool VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc Loc) {
checkDecl(Loc.getIFaceDecl(), Loc.getNameLoc());
return true;
}
bool VisitObjCObjectTypeLoc(ObjCObjectTypeLoc Loc) {
for (unsigned I = 0, E = Loc.getNumProtocols(); I < E; ++I)
checkDecl(Loc.getProtocol(I), Loc.getProtocolLoc(I));
return true;
}
bool VisitDependentSymbolReference(const NamedDecl *Symbol,
SourceLocation SymbolNameLoc) {
checkDecl(Symbol, SymbolNameLoc);
return true;
}
// Non-visitors:
// Namespace traversal:
void handleNestedNameSpecifierLoc(NestedNameSpecifierLoc NameLoc) {
while (NameLoc) {
checkDecl(NameLoc.getNestedNameSpecifier()->getAsNamespace(),
NameLoc.getLocalBeginLoc());
NameLoc = NameLoc.getPrefix();
}
}
private:
size_t getOffsetForString(SourceLocation Loc, StringRef PrevNameString) {
const SourceLocation BeginLoc = Loc;
const SourceLocation EndLoc = Lexer::getLocForEndOfToken(
BeginLoc, 0, Context.getSourceManager(), Context.getLangOpts());
StringRef TokenName =
Lexer::getSourceText(CharSourceRange::getTokenRange(BeginLoc, EndLoc),
Context.getSourceManager(), Context.getLangOpts());
return TokenName.find(PrevNameString);
}
void checkAndAddLocations(unsigned SymbolIndex,
ArrayRef<SourceLocation> Locations,
OldSymbolOccurrence::OccurrenceKind Kind =
OldSymbolOccurrence::MatchingSymbol) {
if (Locations.size() !=
Operation.symbols()[SymbolIndex].Name.getNamePieces().size())
return;
SmallVector<SourceLocation, 4> StringLocations;
for (size_t I = 0, E = Locations.size(); I != E; ++I) {
SourceLocation Loc = Locations[I];
bool IsMacroExpansion = Loc.isMacroID();
if (IsMacroExpansion) {
const SourceManager &SM = Context.getSourceManager();
if (SM.isMacroArgExpansion(Loc)) {
Loc = SM.getSpellingLoc(Loc);
IsMacroExpansion = false;
} else
Loc = SM.getExpansionLoc(Loc);
}
if (IsMacroExpansion) {
Occurrences.push_back(OldSymbolOccurrence(
Kind, /*IsMacroExpansion=*/true, SymbolIndex, Loc));
return;
}
size_t Offset = getOffsetForString(
Loc, Operation.symbols()[SymbolIndex].Name.getNamePieces()[I]);
if (Offset == StringRef::npos)
return;
StringLocations.push_back(Loc.getLocWithOffset(Offset));
}
Occurrences.push_back(OldSymbolOccurrence(Kind, /*IsMacroExpansion=*/false,
SymbolIndex, StringLocations));
}
/// Adds a location without checking if the name is actually there.
void addLocation(unsigned SymbolIndex, SourceLocation Location,
OldSymbolOccurrence::OccurrenceKind Kind) {
if (1 != Operation.symbols()[SymbolIndex].Name.getNamePieces().size())
return;
bool IsMacroExpansion = Location.isMacroID();
if (IsMacroExpansion) {
const SourceManager &SM = Context.getSourceManager();
if (SM.isMacroArgExpansion(Location)) {
Location = SM.getSpellingLoc(Location);
IsMacroExpansion = false;
} else
Location = SM.getExpansionLoc(Location);
}
Occurrences.push_back(
OldSymbolOccurrence(Kind, IsMacroExpansion, SymbolIndex, Location));
}
const SymbolOperation &Operation;
const ASTContext &Context;
std::vector<OldSymbolOccurrence> &Occurrences;
};
} // namespace
std::vector<OldSymbolOccurrence>
findSymbolOccurrences(const SymbolOperation &Operation, Decl *Decl) {
std::vector<OldSymbolOccurrence> Occurrences;
SymbolOccurrenceFinderASTVisitor Visitor(Operation, Decl->getASTContext(),
Occurrences);
Visitor.TraverseDecl(Decl);
NestedNameSpecifierLocFinder Finder(Decl->getASTContext());
for (const auto &Location : Finder.getNestedNameSpecifierLocations())
Visitor.handleNestedNameSpecifierLoc(Location);
return Occurrences;
}
} // end namespace rename
} // end namespace tooling
} // end namespace clang
|