| 1 |
//===----------------------------------------------------------------------===// |
| 2 |
// |
| 3 |
// Copyright (c) 2012, 2013, 2018, 2019 The University of Utah |
| 4 |
// All rights reserved. |
| 5 |
// |
| 6 |
// This file is distributed under the University of Illinois Open Source |
| 7 |
// License. See the file COPYING for details. |
| 8 |
// |
| 9 |
//===----------------------------------------------------------------------===// |
| 10 |
|
| 11 |
#ifndef COMMON_RENAME_CLASS_REWRITE_VISITOR_H |
| 12 |
#define COMMON_RENAME_CLASS_REWRITE_VISITOR_H |
| 13 |
|
| 14 |
#include "llvm/ADT/SmallPtrSet.h" |
| 15 |
#include "clang/AST/RecursiveASTVisitor.h" |
| 16 |
|
| 17 |
class Transformation; |
| 18 |
|
| 19 |
namespace clang_delta_common_visitor { |
| 20 |
|
| 21 |
using namespace clang; |
| 22 |
|
| 23 |
template<typename T> |
| 24 |
class CommonRenameClassRewriteVisitor : public RecursiveASTVisitor<T> { |
| 25 |
public: |
| 26 |
CommonRenameClassRewriteVisitor(Transformation *Instance, |
| 27 |
Rewriter *RT, |
| 28 |
RewriteUtils *Helper, |
| 29 |
const CXXRecordDecl *CXXRD, |
| 30 |
const std::string &Name) |
| 31 |
: ConsumerInstance(Instance), |
| 32 |
TheRewriter(RT), |
| 33 |
RewriteHelper(Helper), |
| 34 |
TheCXXRecordDecl(CXXRD), |
| 35 |
NewNameStr(Name) |
| 36 |
{ } |
| 37 |
|
| 38 |
T &getDerived() { return *static_cast<T*>(this); }; |
| 39 |
|
| 40 |
bool VisitCXXRecordDecl(CXXRecordDecl *CXXRD); |
| 41 |
|
| 42 |
bool VisitCXXConstructorDecl(CXXConstructorDecl *CtorDecl); |
| 43 |
|
| 44 |
bool VisitCXXDestructorDecl(CXXDestructorDecl *DtorDecl); |
| 45 |
|
| 46 |
bool VisitCXXMemberCallExpr(CXXMemberCallExpr *CE); |
| 47 |
|
| 48 |
bool VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TyLoc); |
| 49 |
|
| 50 |
bool VisitRecordTypeLoc(RecordTypeLoc RTLoc); |
| 51 |
|
| 52 |
bool VisitTemplateSpecializationTypeLoc( |
| 53 |
TemplateSpecializationTypeLoc TSPLoc); |
| 54 |
|
| 55 |
bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc); |
| 56 |
|
| 57 |
bool VisitDependentTemplateSpecializationTypeLoc( |
| 58 |
DependentTemplateSpecializationTypeLoc DTSLoc); |
| 59 |
|
| 60 |
bool VisitClassTemplateSpecializationDecl( |
| 61 |
ClassTemplateSpecializationDecl *TSD); |
| 62 |
|
| 63 |
bool TraverseConstructorInitializer(CXXCtorInitializer *Init); |
| 64 |
|
| 65 |
bool VisitUsingDecl(UsingDecl *D); |
| 66 |
|
| 67 |
private: |
| 68 |
typedef llvm::SmallPtrSet<void *, 20> LocPtrSet; |
| 69 |
|
| 70 |
void renameTemplateName(TemplateName TmplName, SourceLocation LocStart); |
| 71 |
|
| 72 |
bool getNewName(const CXXRecordDecl *CXXRD, std::string &NewName); |
| 73 |
|
| 74 |
bool getNewNameByName(const std::string &Name, std::string &NewName); |
| 75 |
|
| 76 |
LocPtrSet VisitedLocs; |
| 77 |
|
| 78 |
Transformation *ConsumerInstance; |
| 79 |
|
| 80 |
Rewriter *TheRewriter; |
| 81 |
|
| 82 |
RewriteUtils *RewriteHelper; |
| 83 |
|
| 84 |
const CXXRecordDecl *TheCXXRecordDecl; |
| 85 |
|
| 86 |
std::string NewNameStr; |
| 87 |
}; |
| 88 |
|
| 89 |
template<typename T> |
| 90 |
bool CommonRenameClassRewriteVisitor<T>::VisitUsingDecl(UsingDecl *D) |
| 91 |
{ |
| 92 |
DeclarationNameInfo NameInfo = D->getNameInfo(); |
| 93 |
DeclarationName DeclName = NameInfo.getName(); |
| 94 |
if (DeclName.getNameKind() != DeclarationName::Identifier) |
| 95 |
return true; |
| 96 |
|
| 97 |
IdentifierInfo *IdInfo = DeclName.getAsIdentifierInfo(); |
| 98 |
std::string IdName = IdInfo->getName().str(); |
| 99 |
std::string Name; |
| 100 |
if (getNewNameByName(IdName, Name)) { |
| 101 |
SourceLocation LocStart = NameInfo.getBeginLoc(); |
| 102 |
TheRewriter->ReplaceText(LocStart, IdName.size(), Name); |
| 103 |
} |
| 104 |
return true; |
| 105 |
} |
| 106 |
|
| 107 |
template<typename T> |
| 108 |
bool CommonRenameClassRewriteVisitor<T>::TraverseConstructorInitializer( |
| 109 |
CXXCtorInitializer *Init) |
| 110 |
{ |
| 111 |
if (Init->isBaseInitializer() && !Init->isWritten()) |
| 112 |
return true; |
| 113 |
|
| 114 |
if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo()) |
| 115 |
getDerived().TraverseTypeLoc(TInfo->getTypeLoc()); |
| 116 |
|
| 117 |
if (Init->isWritten()) |
| 118 |
getDerived().TraverseStmt(Init->getInit()); |
| 119 |
return true; |
| 120 |
} |
| 121 |
|
| 122 |
// ISSUE: I am not sure why, but RecursiveASTVisitor doesn't recursively |
| 123 |
// visit base classes from explicit template specialization, e.g., |
| 124 |
// struct A { }; |
| 125 |
// template<typename T> class B : public A<T> { }; |
| 126 |
// template<> class B : public A<short> { }; |
| 127 |
// In the above case, A<short> won't be touched. |
| 128 |
// So we have to do it manually |
| 129 |
template<typename T> |
| 130 |
bool CommonRenameClassRewriteVisitor<T>::VisitClassTemplateSpecializationDecl( |
| 131 |
ClassTemplateSpecializationDecl *TSD) |
| 132 |
{ |
| 133 |
if (!TSD->isExplicitInstantiationOrSpecialization()) |
| 134 |
return true; |
| 135 |
|
| 136 |
const CXXRecordDecl *CXXRD = TSD->getSpecializedTemplate()->getTemplatedDecl(); |
| 137 |
std::string Name; |
| 138 |
if (getNewName(CXXRD, Name)) { |
| 139 |
SourceLocation LocStart = TSD->getLocation(); |
| 140 |
TransAssert(LocStart.isValid() && "Invalid Location!"); |
| 141 |
TheRewriter->ReplaceText(LocStart, CXXRD->getNameAsString().size(), Name); |
| 142 |
} |
| 143 |
|
| 144 |
if (TSD->isExplicitSpecialization() && TSD->isCompleteDefinition()) { |
| 145 |
for (CXXRecordDecl::base_class_const_iterator I = TSD->bases_begin(), |
| 146 |
E = TSD->bases_end(); I != E; ++I) { |
| 147 |
TypeSourceInfo *TSI = (*I).getTypeSourceInfo(); |
| 148 |
TransAssert(TSI && "Bad TypeSourceInfo!"); |
| 149 |
getDerived().TraverseTypeLoc(TSI->getTypeLoc()); |
| 150 |
} |
| 151 |
} |
| 152 |
return true; |
| 153 |
} |
| 154 |
|
| 155 |
template<typename T> |
| 156 |
bool CommonRenameClassRewriteVisitor<T>::VisitCXXRecordDecl( |
| 157 |
CXXRecordDecl *CXXRD) |
| 158 |
{ |
| 159 |
std::string Name; |
| 160 |
if (!getNewName(CXXRD, Name)) |
| 161 |
return true; |
| 162 |
|
| 163 |
void *LocPtr = CXXRD->getLocation().getPtrEncoding(); |
| 164 |
if (VisitedLocs.count(LocPtr)) |
| 165 |
return true; |
| 166 |
VisitedLocs.insert(LocPtr); |
| 167 |
|
| 168 |
if (ConsumerInstance->isDeclaringRecordDecl(CXXRD) && CXXRD->isThisDeclarationADefinition()) { |
| 169 |
RewriteHelper->replaceRecordDeclDef(CXXRD, Name); |
| 170 |
} |
| 171 |
else { |
| 172 |
RewriteHelper->replaceRecordDeclName(CXXRD, Name); |
| 173 |
} |
| 174 |
|
| 175 |
return true; |
| 176 |
} |
| 177 |
|
| 178 |
template<typename T> |
| 179 |
bool CommonRenameClassRewriteVisitor<T>::VisitCXXConstructorDecl |
| 180 |
(CXXConstructorDecl *CtorDecl) |
| 181 |
{ |
| 182 |
const DeclContext *Ctx = CtorDecl->getDeclContext(); |
| 183 |
const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(Ctx); |
| 184 |
TransAssert(CXXRD && "Invalid CXXRecordDecl"); |
| 185 |
|
| 186 |
std::string Name; |
| 187 |
if (getNewName(CXXRD, Name)) |
| 188 |
RewriteHelper->replaceFunctionDeclName(CtorDecl, Name); |
| 189 |
|
| 190 |
return true; |
| 191 |
} |
| 192 |
|
| 193 |
template<typename T> |
| 194 |
bool CommonRenameClassRewriteVisitor<T>::VisitCXXDestructorDecl( |
| 195 |
CXXDestructorDecl *DtorDecl) |
| 196 |
{ |
| 197 |
const DeclContext *Ctx = DtorDecl->getDeclContext(); |
| 198 |
const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(Ctx); |
| 199 |
TransAssert(CXXRD && "Invalid CXXRecordDecl"); |
| 200 |
|
| 201 |
// Avoid duplicated VisitDtor. |
| 202 |
// For example, in the code below: |
| 203 |
// template<typename T> |
| 204 |
// class SomeClass { |
| 205 |
// public: |
| 206 |
// ~SomeClass<T>() {} |
| 207 |
// }; |
| 208 |
// ~SomeClass<T>'s TypeLoc is represented as TemplateSpecializationTypeLoc |
| 209 |
// In this case, ~SomeClass will be renamed from |
| 210 |
// VisitTemplateSpecializationTypeLoc. |
| 211 |
DeclarationNameInfo NameInfo = DtorDecl->getNameInfo(); |
| 212 |
if ( TypeSourceInfo *TSInfo = NameInfo.getNamedTypeInfo()) { |
| 213 |
TypeLoc DtorLoc = TSInfo->getTypeLoc(); |
| 214 |
if (!DtorLoc.isNull() && |
| 215 |
(DtorLoc.getTypeLocClass() == TypeLoc::TemplateSpecialization)) |
| 216 |
return true; |
| 217 |
} |
| 218 |
|
| 219 |
SourceLocation StartLoc = DtorDecl->getLocation(); |
| 220 |
// skip '~' |
| 221 |
StartLoc = StartLoc.getLocWithOffset(1); |
| 222 |
StartLoc = RewriteHelper->getLocationAfterSkiping(StartLoc, ' '); |
| 223 |
void *LocPtr = StartLoc.getPtrEncoding(); |
| 224 |
if (VisitedLocs.count(LocPtr)) |
| 225 |
return true; |
| 226 |
VisitedLocs.insert(LocPtr); |
| 227 |
|
| 228 |
std::string Name; |
| 229 |
if (getNewName(CXXRD, Name)) { |
| 230 |
RewriteHelper->replaceCXXDestructorDeclName(DtorDecl, Name); |
| 231 |
} |
| 232 |
|
| 233 |
return true; |
| 234 |
} |
| 235 |
|
| 236 |
template<typename T> |
| 237 |
bool CommonRenameClassRewriteVisitor<T>::VisitInjectedClassNameTypeLoc( |
| 238 |
InjectedClassNameTypeLoc TyLoc) |
| 239 |
{ |
| 240 |
const CXXRecordDecl *CXXRD = TyLoc.getDecl(); |
| 241 |
TransAssert(CXXRD && "Invalid CXXRecordDecl!"); |
| 242 |
|
| 243 |
std::string Name; |
| 244 |
if (getNewName(CXXRD, Name)) { |
| 245 |
SourceLocation LocStart = TyLoc.getBeginLoc(); |
| 246 |
TransAssert(LocStart.isValid() && "Invalid Location!"); |
| 247 |
|
| 248 |
void *LocPtr = LocStart.getPtrEncoding(); |
| 249 |
if (VisitedLocs.count(LocPtr)) |
| 250 |
return true; |
| 251 |
VisitedLocs.insert(LocPtr); |
| 252 |
TheRewriter->ReplaceText(LocStart, CXXRD->getNameAsString().size(), Name); |
| 253 |
} |
| 254 |
return true; |
| 255 |
} |
| 256 |
|
| 257 |
template<typename T> |
| 258 |
bool CommonRenameClassRewriteVisitor<T>::VisitCXXMemberCallExpr( |
| 259 |
CXXMemberCallExpr *CE) |
| 260 |
{ |
| 261 |
const CXXRecordDecl *CXXRD = CE->getRecordDecl(); |
| 262 |
// getRecordDEcl could return NULL if getImplicitObjectArgument() |
| 263 |
// returns NULL |
| 264 |
if (!CXXRD) |
| 265 |
return true; |
| 266 |
|
| 267 |
std::string Name; |
| 268 |
if (getNewName(CXXRD, Name)) { |
| 269 |
RewriteHelper->replaceCXXDtorCallExpr(CE, Name); |
| 270 |
} |
| 271 |
return true; |
| 272 |
} |
| 273 |
|
| 274 |
template<typename T> |
| 275 |
bool CommonRenameClassRewriteVisitor<T>::VisitRecordTypeLoc(RecordTypeLoc RTLoc) |
| 276 |
{ |
| 277 |
const Type *Ty = RTLoc.getTypePtr(); |
| 278 |
if (Ty->isUnionType()) |
| 279 |
return true; |
| 280 |
|
| 281 |
const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RTLoc.getDecl()); |
| 282 |
if (!RD) |
| 283 |
return true; |
| 284 |
|
| 285 |
std::string Name; |
| 286 |
if (!getNewName(RD, Name)) |
| 287 |
return true; |
| 288 |
|
| 289 |
// Let VisitCXXRecordDecl handle this case. |
| 290 |
if (ConsumerInstance->isDeclaringRecordDecl(RD)) |
| 291 |
return true; |
| 292 |
|
| 293 |
// Avoid duplicated rewrites to Decls from the same DeclGroup, e.g., |
| 294 |
// struct S s1, s2 |
| 295 |
SourceLocation LocStart = RTLoc.getBeginLoc(); |
| 296 |
void *LocPtr = LocStart.getPtrEncoding(); |
| 297 |
if (VisitedLocs.count(LocPtr)) |
| 298 |
return true; |
| 299 |
VisitedLocs.insert(LocPtr); |
| 300 |
RewriteHelper->replaceRecordType(RTLoc, Name); |
| 301 |
return true; |
| 302 |
} |
| 303 |
|
| 304 |
template<typename T> bool CommonRenameClassRewriteVisitor<T>:: |
| 305 |
VisitDependentTemplateSpecializationTypeLoc( |
| 306 |
DependentTemplateSpecializationTypeLoc DTSLoc) |
| 307 |
{ |
| 308 |
const Type *Ty = DTSLoc.getTypePtr(); |
| 309 |
const DependentTemplateSpecializationType *DTST = |
| 310 |
dyn_cast<DependentTemplateSpecializationType>(Ty); |
| 311 |
TransAssert(DTST && "Bad DependentTemplateSpecializationType!"); |
| 312 |
|
| 313 |
const IdentifierInfo *IdInfo = DTST->getIdentifier(); |
| 314 |
std::string IdName = IdInfo->getName().str(); |
| 315 |
std::string Name; |
| 316 |
if (getNewNameByName(IdName, Name)) { |
| 317 |
SourceLocation LocStart = DTSLoc.getTemplateNameLoc(); |
| 318 |
TheRewriter->ReplaceText(LocStart, IdName.size(), Name); |
| 319 |
} |
| 320 |
|
| 321 |
return true; |
| 322 |
} |
| 323 |
|
| 324 |
template<typename T> |
| 325 |
void CommonRenameClassRewriteVisitor<T>::renameTemplateName( |
| 326 |
TemplateName TmplName, SourceLocation LocStart) |
| 327 |
{ |
| 328 |
if (TmplName.getKind() == TemplateName::DependentTemplate) |
| 329 |
return; |
| 330 |
const TemplateDecl *TmplD = TmplName.getAsTemplateDecl(); |
| 331 |
TransAssert(TmplD && "Invalid TemplateDecl!"); |
| 332 |
NamedDecl *ND = TmplD->getTemplatedDecl(); |
| 333 |
// in some cases, ND could be NULL, e.g., the |
| 334 |
// template template parameter code below: |
| 335 |
// template<template<class> class BBB> |
| 336 |
// struct AAA { |
| 337 |
// template <class T> |
| 338 |
// struct CCC { |
| 339 |
// static BBB<T> a; |
| 340 |
// }; |
| 341 |
// }; |
| 342 |
// where we don't know BBB |
| 343 |
if (!ND) |
| 344 |
return; |
| 345 |
|
| 346 |
const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(ND); |
| 347 |
if (!CXXRD) |
| 348 |
return; |
| 349 |
|
| 350 |
std::string Name; |
| 351 |
if (getNewName(CXXRD, Name)) { |
| 352 |
TheRewriter->ReplaceText(LocStart, CXXRD->getNameAsString().size(), Name); |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
// ISSUE: we don't have loc info for TemplateName, so we have to |
| 357 |
// overload RecursiveASTVisitor TraverseTemplateArgumentLoc |
| 358 |
// function, or else we will omit arguments for template template |
| 359 |
// parameters |
| 360 |
template<typename T> |
| 361 |
bool CommonRenameClassRewriteVisitor<T>::TraverseTemplateArgumentLoc( |
| 362 |
const TemplateArgumentLoc &ArgLoc) |
| 363 |
{ |
| 364 |
const TemplateArgument &Arg = ArgLoc.getArgument(); |
| 365 |
|
| 366 |
switch (Arg.getKind()) { |
| 367 |
case TemplateArgument::NullPtr: |
| 368 |
case TemplateArgument::Null: |
| 369 |
case TemplateArgument::Declaration: |
| 370 |
case TemplateArgument::Integral: |
| 371 |
#if LLVM_VERSION_MAJOR >= 18 |
| 372 |
case TemplateArgument::StructuralValue: |
| 373 |
#endif |
| 374 |
return true; |
| 375 |
|
| 376 |
case TemplateArgument::Type: { |
| 377 |
if (TypeSourceInfo *TSI = ArgLoc.getTypeSourceInfo()) |
| 378 |
return getDerived().TraverseTypeLoc(TSI->getTypeLoc()); |
| 379 |
else |
| 380 |
return getDerived().TraverseType(Arg.getAsType()); |
| 381 |
} |
| 382 |
|
| 383 |
case TemplateArgument::Template: |
| 384 |
case TemplateArgument::TemplateExpansion: { |
| 385 |
if (ArgLoc.getTemplateQualifierLoc()) { |
| 386 |
getDerived().TraverseNestedNameSpecifierLoc( |
| 387 |
ArgLoc.getTemplateQualifierLoc()); |
| 388 |
} |
| 389 |
|
| 390 |
TemplateName TmplName = Arg.getAsTemplateOrTemplatePattern(); |
| 391 |
getDerived().TraverseTemplateName(Arg.getAsTemplateOrTemplatePattern()); |
| 392 |
renameTemplateName(TmplName, ArgLoc.getLocation()); |
| 393 |
return true; |
| 394 |
} |
| 395 |
|
| 396 |
case TemplateArgument::Expression: |
| 397 |
return getDerived().TraverseStmt(ArgLoc.getSourceExpression()); |
| 398 |
|
| 399 |
case TemplateArgument::Pack: |
| 400 |
return getDerived().TraverseTemplateArguments(Arg); |
| 401 |
} |
| 402 |
|
| 403 |
return true; |
| 404 |
} |
| 405 |
|
| 406 |
template<typename T> |
| 407 |
bool CommonRenameClassRewriteVisitor<T>::VisitTemplateSpecializationTypeLoc( |
| 408 |
TemplateSpecializationTypeLoc TSPLoc) |
| 409 |
{ |
| 410 |
const Type *Ty = TSPLoc.getTypePtr(); |
| 411 |
const TemplateSpecializationType *TST = |
| 412 |
dyn_cast<TemplateSpecializationType>(Ty); |
| 413 |
TransAssert(TST && "Bad TemplateSpecializationType!"); |
| 414 |
|
| 415 |
TemplateName TmplName = TST->getTemplateName(); |
| 416 |
renameTemplateName(TmplName, TSPLoc.getTemplateNameLoc()); |
| 417 |
return true; |
| 418 |
} |
| 419 |
|
| 420 |
template<typename T> |
| 421 |
bool CommonRenameClassRewriteVisitor<T>::getNewName(const CXXRecordDecl *CXXRD, |
| 422 |
std::string &NewName) |
| 423 |
{ |
| 424 |
const CXXRecordDecl *CanonicalRD = CXXRD->getCanonicalDecl(); |
| 425 |
if (CanonicalRD == TheCXXRecordDecl) { |
| 426 |
NewName = NewNameStr; |
| 427 |
return true; |
| 428 |
} |
| 429 |
else { |
| 430 |
NewName = ""; |
| 431 |
return false; |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
template<typename T> |
| 436 |
bool CommonRenameClassRewriteVisitor<T>::getNewNameByName( |
| 437 |
const std::string &Name, std::string &NewName) |
| 438 |
{ |
| 439 |
if (TheCXXRecordDecl && (Name == TheCXXRecordDecl->getNameAsString())) { |
| 440 |
NewName = NewNameStr; |
| 441 |
return true; |
| 442 |
} |
| 443 |
else { |
| 444 |
NewName = ""; |
| 445 |
return false; |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
} // end namespace clang_delta_common_visitor |
| 450 |
|
| 451 |
#endif |
| 452 |
|