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 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
|
//===----------------------------------------------------------------------===//
//
// Copyright (c) 2012, 2013 The University of Utah
// All rights reserved.
//
// This file is distributed under the University of Illinois Open Source
// License. See the file COPYING for details.
//
//===----------------------------------------------------------------------===//
#ifndef COMMON_RENAME_CLASS_REWRITE_VISITOR_H
#define COMMON_RENAME_CLASS_REWRITE_VISITOR_H
#include "llvm/ADT/SmallPtrSet.h"
#include "clang/AST/RecursiveASTVisitor.h"
namespace clang_delta_common_visitor {
using namespace clang;
template<typename T>
class CommonRenameClassRewriteVisitor : public RecursiveASTVisitor<T> {
public:
CommonRenameClassRewriteVisitor(Rewriter *RT,
RewriteUtils *Helper,
const CXXRecordDecl *CXXRD,
const std::string &Name)
: TheRewriter(RT),
RewriteHelper(Helper),
TheCXXRecordDecl(CXXRD),
NewNameStr(Name)
{ }
T &getDerived() { return *static_cast<T*>(this); };
bool VisitCXXRecordDecl(CXXRecordDecl *CXXRD);
bool VisitCXXConstructorDecl(CXXConstructorDecl *CtorDecl);
bool VisitCXXDestructorDecl(CXXDestructorDecl *DtorDecl);
bool VisitCXXMemberCallExpr(CXXMemberCallExpr *CE);
bool VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TyLoc);
bool VisitRecordTypeLoc(RecordTypeLoc RTLoc);
bool VisitTemplateSpecializationTypeLoc(
TemplateSpecializationTypeLoc TSPLoc);
bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc);
bool VisitDependentTemplateSpecializationTypeLoc(
DependentTemplateSpecializationTypeLoc DTSLoc);
bool VisitClassTemplatePartialSpecializationDecl(
ClassTemplatePartialSpecializationDecl *D);
bool VisitClassTemplateSpecializationDecl(
ClassTemplateSpecializationDecl *TSD);
bool TraverseConstructorInitializer(CXXCtorInitializer *Init);
bool VisitUsingDecl(UsingDecl *D);
private:
typedef llvm::SmallPtrSet<void *, 20> LocPtrSet;
void renameTemplateName(TemplateName TmplName, SourceLocation LocStart);
bool getNewName(const CXXRecordDecl *CXXRD, std::string &NewName);
bool getNewNameByName(const std::string &Name, std::string &NewName);
LocPtrSet VisitedLocs;
Rewriter *TheRewriter;
RewriteUtils *RewriteHelper;
const CXXRecordDecl *TheCXXRecordDecl;
std::string NewNameStr;
};
template<typename T>
bool CommonRenameClassRewriteVisitor<T>::VisitUsingDecl(UsingDecl *D)
{
DeclarationNameInfo NameInfo = D->getNameInfo();
DeclarationName DeclName = NameInfo.getName();
if (DeclName.getNameKind() != DeclarationName::Identifier)
return true;
IdentifierInfo *IdInfo = DeclName.getAsIdentifierInfo();
std::string IdName = IdInfo->getName();
std::string Name;
if (getNewNameByName(IdName, Name)) {
SourceLocation LocStart = NameInfo.getBeginLoc();
TheRewriter->ReplaceText(LocStart, IdName.size(), Name);
}
return true;
}
template<typename T>
bool CommonRenameClassRewriteVisitor<T>::TraverseConstructorInitializer(
CXXCtorInitializer *Init)
{
if (Init->isBaseInitializer() && !Init->isWritten())
return true;
if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo())
getDerived().TraverseTypeLoc(TInfo->getTypeLoc());
if (Init->isWritten())
getDerived().TraverseStmt(Init->getInit());
return true;
}
template<typename T>
bool CommonRenameClassRewriteVisitor<T>::
VisitClassTemplatePartialSpecializationDecl(
ClassTemplatePartialSpecializationDecl *D)
{
const Type *Ty = D->getInjectedSpecializationType().getTypePtr();
TransAssert(Ty && "Bad TypePtr!");
const TemplateSpecializationType *TST =
dyn_cast<TemplateSpecializationType>(Ty);
TransAssert(TST && "Bad TemplateSpecializationType!");
TemplateName TplName = TST->getTemplateName();
const TemplateDecl *TplD = TplName.getAsTemplateDecl();
TransAssert(TplD && "Invalid TemplateDecl!");
NamedDecl *ND = TplD->getTemplatedDecl();
TransAssert(ND && "Invalid NamedDecl!");
const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(ND);
TransAssert(CXXRD && "Invalid CXXRecordDecl!");
std::string Name;
if (getNewName(CXXRD, Name)) {
const TypeSourceInfo *TyInfo = D->getTypeAsWritten();
if (!TyInfo)
return true;
TypeLoc TyLoc = TyInfo->getTypeLoc();
SourceLocation LocStart = TyLoc.getLocStart();
TransAssert(LocStart.isValid() && "Invalid Location!");
TheRewriter->ReplaceText(LocStart, CXXRD->getNameAsString().size(), Name);
}
return true;
}
// ISSUE: I am not sure why, but RecursiveASTVisitor doesn't recursively
// visit base classes from explicit template specialization, e.g.,
// struct A { };
// template<typename T> class B : public A<T> { };
// template<> class B : public A<short> { };
// In the above case, A<short> won't be touched.
// So we have to do it manually
template<typename T>
bool CommonRenameClassRewriteVisitor<T>::VisitClassTemplateSpecializationDecl(
ClassTemplateSpecializationDecl *TSD)
{
if (!TSD->isExplicitSpecialization() || !TSD->isCompleteDefinition())
return true;
for (CXXRecordDecl::base_class_const_iterator I = TSD->bases_begin(),
E = TSD->bases_end(); I != E; ++I) {
TypeSourceInfo *TSI = (*I).getTypeSourceInfo();
TransAssert(TSI && "Bad TypeSourceInfo!");
getDerived().TraverseTypeLoc(TSI->getTypeLoc());
}
return true;
}
template<typename T>
bool CommonRenameClassRewriteVisitor<T>::VisitCXXRecordDecl(
CXXRecordDecl *CXXRD)
{
std::string Name;
if (getNewName(CXXRD, Name)) {
RewriteHelper->replaceRecordDeclName(CXXRD, Name);
}
return true;
}
template<typename T>
bool CommonRenameClassRewriteVisitor<T>::VisitCXXConstructorDecl
(CXXConstructorDecl *CtorDecl)
{
const DeclContext *Ctx = CtorDecl->getDeclContext();
const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(Ctx);
TransAssert(CXXRD && "Invalid CXXRecordDecl");
std::string Name;
if (getNewName(CXXRD, Name))
RewriteHelper->replaceFunctionDeclName(CtorDecl, Name);
return true;
}
template<typename T>
bool CommonRenameClassRewriteVisitor<T>::VisitCXXDestructorDecl(
CXXDestructorDecl *DtorDecl)
{
const DeclContext *Ctx = DtorDecl->getDeclContext();
const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(Ctx);
TransAssert(CXXRD && "Invalid CXXRecordDecl");
// Avoid duplicated VisitDtor.
// For example, in the code below:
// template<typename T>
// class SomeClass {
// public:
// ~SomeClass<T>() {}
// };
// ~SomeClass<T>'s TypeLoc is represented as TemplateSpecializationTypeLoc
// In this case, ~SomeClass will be renamed from
// VisitTemplateSpecializationTypeLoc.
DeclarationNameInfo NameInfo = DtorDecl->getNameInfo();
if ( TypeSourceInfo *TSInfo = NameInfo.getNamedTypeInfo()) {
TypeLoc DtorLoc = TSInfo->getTypeLoc();
if (!DtorLoc.isNull() &&
(DtorLoc.getTypeLocClass() == TypeLoc::TemplateSpecialization))
return true;
}
std::string Name;
if (getNewName(CXXRD, Name)) {
RewriteHelper->replaceCXXDestructorDeclName(DtorDecl, Name);
}
return true;
}
template<typename T>
bool CommonRenameClassRewriteVisitor<T>::VisitInjectedClassNameTypeLoc(
InjectedClassNameTypeLoc TyLoc)
{
const CXXRecordDecl *CXXRD = TyLoc.getDecl();
TransAssert(CXXRD && "Invalid CXXRecordDecl!");
std::string Name;
if (getNewName(CXXRD, Name)) {
SourceLocation LocStart = TyLoc.getLocStart();
TransAssert(LocStart.isValid() && "Invalid Location!");
TheRewriter->ReplaceText(LocStart, CXXRD->getNameAsString().size(), Name);
}
return true;
}
template<typename T>
bool CommonRenameClassRewriteVisitor<T>::VisitCXXMemberCallExpr(
CXXMemberCallExpr *CE)
{
const CXXRecordDecl *CXXRD = CE->getRecordDecl();
// getRecordDEcl could return NULL if getImplicitObjectArgument()
// returns NULL
if (!CXXRD)
return true;
std::string Name;
if (getNewName(CXXRD, Name)) {
RewriteHelper->replaceCXXDtorCallExpr(CE, Name);
}
return true;
}
template<typename T>
bool CommonRenameClassRewriteVisitor<T>::VisitRecordTypeLoc(RecordTypeLoc RTLoc)
{
const Type *Ty = RTLoc.getTypePtr();
if (Ty->isUnionType())
return true;
const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RTLoc.getDecl());
if (!RD)
return true;
std::string Name;
if (getNewName(RD, Name)) {
// Avoid duplicated rewrites to Decls from the same DeclGroup, e.g.,
// struct S s1, s2
SourceLocation LocStart = RTLoc.getLocStart();
void *LocPtr = LocStart.getPtrEncoding();
if (VisitedLocs.count(LocPtr))
return true;
VisitedLocs.insert(LocPtr);
RewriteHelper->replaceRecordType(RTLoc, Name);
}
return true;
}
template<typename T> bool CommonRenameClassRewriteVisitor<T>::
VisitDependentTemplateSpecializationTypeLoc(
DependentTemplateSpecializationTypeLoc DTSLoc)
{
const Type *Ty = DTSLoc.getTypePtr();
const DependentTemplateSpecializationType *DTST =
dyn_cast<DependentTemplateSpecializationType>(Ty);
TransAssert(DTST && "Bad DependentTemplateSpecializationType!");
const IdentifierInfo *IdInfo = DTST->getIdentifier();
std::string IdName = IdInfo->getName();
std::string Name;
if (getNewNameByName(IdName, Name)) {
SourceLocation LocStart = DTSLoc.getTemplateNameLoc();
TheRewriter->ReplaceText(LocStart, IdName.size(), Name);
}
return true;
}
template<typename T>
void CommonRenameClassRewriteVisitor<T>::renameTemplateName(
TemplateName TmplName, SourceLocation LocStart)
{
const TemplateDecl *TmplD = TmplName.getAsTemplateDecl();
TransAssert(TmplD && "Invalid TemplateDecl!");
NamedDecl *ND = TmplD->getTemplatedDecl();
// in some cases, ND could be NULL, e.g., the
// template template parameter code below:
// template<template<class> class BBB>
// struct AAA {
// template <class T>
// struct CCC {
// static BBB<T> a;
// };
// };
// where we don't know BBB
if (!ND)
return;
const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(ND);
if (!CXXRD)
return;
std::string Name;
if (getNewName(CXXRD, Name)) {
TheRewriter->ReplaceText(LocStart, CXXRD->getNameAsString().size(), Name);
}
}
// ISSUE: we don't have loc info for TemplateName, so we have to
// overload RecursiveASTVisitor TraverseTemplateArgumentLoc
// function, or else we will omit arguments for template template
// parameters
template<typename T>
bool CommonRenameClassRewriteVisitor<T>::TraverseTemplateArgumentLoc(
const TemplateArgumentLoc &ArgLoc)
{
const TemplateArgument &Arg = ArgLoc.getArgument();
switch (Arg.getKind()) {
case TemplateArgument::NullPtr:
case TemplateArgument::Null:
case TemplateArgument::Declaration:
case TemplateArgument::Integral:
return true;
case TemplateArgument::Type: {
if (TypeSourceInfo *TSI = ArgLoc.getTypeSourceInfo())
return getDerived().TraverseTypeLoc(TSI->getTypeLoc());
else
return getDerived().TraverseType(Arg.getAsType());
}
case TemplateArgument::Template:
case TemplateArgument::TemplateExpansion: {
if (ArgLoc.getTemplateQualifierLoc()) {
getDerived().TraverseNestedNameSpecifierLoc(
ArgLoc.getTemplateQualifierLoc());
}
TemplateName TmplName = Arg.getAsTemplateOrTemplatePattern();
getDerived().TraverseTemplateName(Arg.getAsTemplateOrTemplatePattern());
renameTemplateName(TmplName, ArgLoc.getLocation());
return true;
}
case TemplateArgument::Expression:
return getDerived().TraverseStmt(ArgLoc.getSourceExpression());
case TemplateArgument::Pack:
return getDerived().TraverseTemplateArguments(Arg.pack_begin(),
Arg.pack_size());
}
return true;
}
template<typename T>
bool CommonRenameClassRewriteVisitor<T>::VisitTemplateSpecializationTypeLoc(
TemplateSpecializationTypeLoc TSPLoc)
{
const Type *Ty = TSPLoc.getTypePtr();
const TemplateSpecializationType *TST =
dyn_cast<TemplateSpecializationType>(Ty);
TransAssert(TST && "Bad TemplateSpecializationType!");
TemplateName TmplName = TST->getTemplateName();
renameTemplateName(TmplName, TSPLoc.getTemplateNameLoc());
return true;
}
template<typename T>
bool CommonRenameClassRewriteVisitor<T>::getNewName(const CXXRecordDecl *CXXRD,
std::string &NewName)
{
const CXXRecordDecl *CanonicalRD = CXXRD->getCanonicalDecl();
if (CanonicalRD == TheCXXRecordDecl) {
NewName = NewNameStr;
return true;
}
else {
NewName = "";
return false;
}
}
template<typename T>
bool CommonRenameClassRewriteVisitor<T>::getNewNameByName(
const std::string &Name, std::string &NewName)
{
if (TheCXXRecordDecl && (Name == TheCXXRecordDecl->getNameAsString())) {
NewName = NewNameStr;
return true;
}
else {
NewName = "";
return false;
}
}
} // end namespace clang_delta_common_visitor
#endif
|