Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip non-dep typedefs in replace-dependent-typedef #281

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clang_delta/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ EXTRA_DIST = \
tests/rename-fun/test1.h \
tests/rename-param/invalid.c \
tests/rename-var/rename-var.c \
tests/replace-dependent-typedef/test2.cc \
tests/replace-derived-class/replace-derived1.cpp \
tests/replace-derived-class/replace-derived2.cpp \
tests/replace-derived-class/replace-derived3.cpp \
Expand Down
1 change: 1 addition & 0 deletions clang_delta/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,7 @@ EXTRA_DIST = \
tests/rename-fun/test1.h \
tests/rename-param/invalid.c \
tests/rename-var/rename-var.c \
tests/replace-dependent-typedef/test2.cc \
tests/replace-derived-class/replace-derived1.cpp \
tests/replace-derived-class/replace-derived2.cpp \
tests/replace-derived-class/replace-derived3.cpp \
Expand Down
49 changes: 49 additions & 0 deletions clang_delta/ReplaceDependentTypedef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,53 @@ It also tries to reduce the typedef chain, e.g. \n\
static RegisterTransformation<ReplaceDependentTypedef>
Trans("replace-dependent-typedef", DescriptionMsg);

namespace {

bool DependsOnTypedef(const Type &Ty) {
switch (Ty.getTypeClass()) {
case Type::SubstTemplateTypeParm: {
const SubstTemplateTypeParmType *TP =
dyn_cast<SubstTemplateTypeParmType>(&Ty);
const Type *ReplTy = TP->getReplacementType().getTypePtr();
return DependsOnTypedef(*ReplTy);
}

case Type::Elaborated: {
const ElaboratedType *ETy = dyn_cast<ElaboratedType>(&Ty);
const Type *NamedTy = ETy->getNamedType().getTypePtr();
return DependsOnTypedef(*NamedTy);
}

case Type::Typedef: {
return true;
}

case Type::DependentName: {
const DependentNameType *DNT = dyn_cast<DependentNameType>(&Ty);
const NestedNameSpecifier *Specifier = DNT->getQualifier();
if (!Specifier)
return false;
const Type *NestedTy = Specifier->getAsType();
if (!NestedTy)
return false;
return DependsOnTypedef(*NestedTy);
}

case Type::Record:
case Type::Builtin: { // fall-through
return false;
}

default:
return false;
}

TransAssert(0 && "Unreachable code!");
return false;
}

} // namespace

class ReplaceDependentTypedefCollectionVisitor : public
RecursiveASTVisitor<ReplaceDependentTypedefCollectionVisitor> {

Expand Down Expand Up @@ -137,6 +184,8 @@ void ReplaceDependentTypedef::handleOneTypedefDecl(const TypedefDecl *D)

if (!isValidType(D->getUnderlyingType()))
return;
if (!DependsOnTypedef(*D->getUnderlyingType()))
return;

std::string Str = "";
bool Typename = false;
Expand Down
25 changes: 25 additions & 0 deletions clang_delta/tests/replace-dependent-typedef/test2.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// RUN: %clang_delta --query-instances=replace-dependent-typedef %s 2>&1 | %remove_lit_checks | FileCheck %s --check-prefix=CHECK-CNT
// RUN: %clang_delta --transformation=replace-dependent-typedef --counter=1 %s 2>&1 | %remove_lit_checks | FileCheck %s --check-prefix=CHECK-1

template <typename T>
struct A {
struct Inner;
// This shouldn't be treated as an instance:
typedef A::Inner Foo;
};

template <class T> struct S { typedef T type; };

struct B {
struct Inner;
};

template <typename T>
struct C {
typedef typename S<T>::type::Inner Bar;
};

// CHECK-1: typedef B::Inner D;
typedef C<B>::Bar D;

// CHECK-CNT: Available transformation instances: 1