Skip to content

Commit

Permalink
Merge pull request #275 from sdkrystian/fix-type-as-written
Browse files Browse the repository at this point in the history
Remove uses of ClassTemplateSpecializationDecl::getTypeAsWritten
  • Loading branch information
chenyang78 authored Jun 1, 2024
2 parents 98baa64 + f17a130 commit 31e855e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 83 deletions.
58 changes: 16 additions & 42 deletions clang_delta/CommonRenameClassRewriteVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ class CommonRenameClassRewriteVisitor : public RecursiveASTVisitor<T> {
bool VisitDependentTemplateSpecializationTypeLoc(
DependentTemplateSpecializationTypeLoc DTSLoc);

bool VisitClassTemplatePartialSpecializationDecl(
ClassTemplatePartialSpecializationDecl *D);

bool VisitClassTemplateSpecializationDecl(
ClassTemplateSpecializationDecl *TSD);

Expand Down Expand Up @@ -122,39 +119,6 @@ bool CommonRenameClassRewriteVisitor<T>::TraverseConstructorInitializer(
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.getBeginLoc();
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 { };
Expand All @@ -166,14 +130,24 @@ template<typename T>
bool CommonRenameClassRewriteVisitor<T>::VisitClassTemplateSpecializationDecl(
ClassTemplateSpecializationDecl *TSD)
{
if (!TSD->isExplicitSpecialization() || !TSD->isCompleteDefinition())
if (!TSD->isExplicitInstantiationOrSpecialization())
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());
const CXXRecordDecl *CXXRD = TSD->getSpecializedTemplate()->getTemplatedDecl();
std::string Name;
if (getNewName(CXXRD, Name)) {
SourceLocation LocStart = TSD->getLocation();
TransAssert(LocStart.isValid() && "Invalid Location!");
TheRewriter->ReplaceText(LocStart, CXXRD->getNameAsString().size(), Name);
}

if (TSD->isExplicitSpecialization() && TSD->isCompleteDefinition()) {
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;
}
Expand Down
58 changes: 17 additions & 41 deletions clang_delta/RemoveNamespace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,6 @@ class RemoveNamespaceRewriteVisitor : public
bool VisitTemplateSpecializationTypeLoc(
TemplateSpecializationTypeLoc TSPLoc);

bool VisitClassTemplatePartialSpecializationDecl(
ClassTemplatePartialSpecializationDecl *D);

bool VisitDependentTemplateSpecializationTypeLoc(
DependentTemplateSpecializationTypeLoc DTSLoc);

Expand Down Expand Up @@ -132,14 +129,25 @@ bool RemoveNamespaceASTVisitor::VisitNamespaceDecl(NamespaceDecl *ND)
bool RemoveNamespaceRewriteVisitor::VisitClassTemplateSpecializationDecl(
ClassTemplateSpecializationDecl *TSD)
{
if (!TSD->isExplicitSpecialization() || !TSD->isCompleteDefinition())
if (!TSD->isExplicitInstantiationOrSpecialization())
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!");
TraverseTypeLoc(TSI->getTypeLoc());
const CXXRecordDecl *CXXRD = TSD->getSpecializedTemplate()->getTemplatedDecl();
std::string Name;
if (ConsumerInstance->getNewName(CXXRD, Name)) {
SourceLocation LocStart = TSD->getLocation();
TransAssert(LocStart.isValid() && "Invalid Location!");
ConsumerInstance->TheRewriter.ReplaceText(
LocStart, CXXRD->getNameAsString().size(), Name);
}

if (TSD->isExplicitSpecialization() && TSD->isCompleteDefinition()) {
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!");
TraverseTypeLoc(TSI->getTypeLoc());
}
}
return true;
}
Expand Down Expand Up @@ -419,38 +427,6 @@ bool RemoveNamespaceRewriteVisitor::VisitTemplateSpecializationTypeLoc(
return true;
}

bool RemoveNamespaceRewriteVisitor::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 (ConsumerInstance->getNewName(CXXRD, Name)) {
const TypeSourceInfo *TyInfo = D->getTypeAsWritten();
if (!TyInfo)
return true;
TypeLoc TyLoc = TyInfo->getTypeLoc();
SourceLocation LocStart = TyLoc.getBeginLoc();
TransAssert(LocStart.isValid() && "Invalid Location!");
ConsumerInstance->TheRewriter.ReplaceText(
LocStart, CXXRD->getNameAsString().size(), Name);
}
return true;
}

// handle the case where a template specialization type cannot be resolved, e.g.
// template <class T> struct Base {};
// template <class T> struct Derived: public Base<T> {
Expand Down

0 comments on commit 31e855e

Please sign in to comment.