Skip to content

Commit

Permalink
Teach TemplateNonTypeArgToInt to replace template params, too.
Browse files Browse the repository at this point in the history
  • Loading branch information
vgvassilev committed Feb 13, 2016
1 parent c278cf7 commit 80f3525
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
7 changes: 7 additions & 0 deletions clang_delta/RewriteUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,13 @@ bool RewriteUtils::replaceNamedDeclName(const NamedDecl *ND,
ND->getNameAsString().size(), NameStr));
}

bool RewriteUtils::replaceValueDecl(const ValueDecl *VD, const std::string &Str)
{
SourceRange Range = VD->getSourceRange();
unsigned RangeSize = TheRewriter->getRangeSize(Range);
return !(TheRewriter->ReplaceText(Range.getBegin(), RangeSize, Str));
}

bool RewriteUtils::replaceVarDeclName(VarDecl *VD,
const std::string &NameStr)
{
Expand Down
14 changes: 14 additions & 0 deletions clang_delta/RewriteUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ namespace clang {
class ClassTemplateDecl;
class CXXMethodDecl;
class NestedNameSpecifierLoc;
class ValueDecl;
}

class RewriteUtils {
Expand Down Expand Up @@ -203,6 +204,19 @@ class RewriteUtils {
bool replaceNamedDeclName(const clang::NamedDecl *ND,
const std::string &NameStr);

///\brief Replaces a value decl with a given string.
///
///For example: \code
/// enum E {...};
/// template <E argName> struct S { } => template <int> struct S { }
///\endcode
///
///\param[in] VD - The decl to be replaced.
///\param[in] Str - The replacement
///\returns true on success.
///
bool replaceValueDecl(const clang::ValueDecl *ValD, const std::string &Str);

bool replaceCXXDtorCallExpr(const clang::CXXMemberCallExpr *CE,
std::string &Name);

Expand Down
12 changes: 9 additions & 3 deletions clang_delta/TemplateNonTypeArgToInt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ void TemplateNonTypeArgToInt::HandleTranslationUnit(ASTContext &Ctx)
}

Ctx.getDiagnostics().setSuppressAllDiagnostics(false);
TransAssert(TheExpr && "NULL TheExpr");
RewriteHelper->replaceExpr(TheExpr, IntString);
if (TheExpr)
RewriteHelper->replaceExpr(TheExpr, IntString);

if (Ctx.getDiagnostics().hasErrorOccurred() ||
Ctx.getDiagnostics().hasFatalErrorOccurred())
Expand Down Expand Up @@ -219,8 +219,14 @@ void TemplateNonTypeArgToInt::handleOneTemplateDecl(const TemplateDecl *D)
for (TemplateParameterList::const_iterator I = TPList->begin(),
E = TPList->end(); I != E; ++I) {
const NamedDecl *ParamND = (*I);
if (isValidParameter(ParamND))
if (isValidParameter(ParamND)) {
ValidParamIdx->insert(Idx);
if (const ValueDecl* ValD = dyn_cast<ValueDecl>(ParamND)) {
++ValidInstanceNum;
RewriteHelper->replaceValueDecl(ValD,
"int " + ParamND->getNameAsString());
}
}
Idx++;
}

Expand Down
12 changes: 12 additions & 0 deletions clang_delta/test/TemplateNonTypeArgToInt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//RUN: %clang_delta --transformation=template-non-type-arg-to-int --counter=1 %s | FileCheck %s

enum _Lock_policy { _S_atomic } const __default_lock_policy = _S_atomic;
template <_Lock_policy _Lp> class __shared_count {
public:
template <typename _Ptr> __shared_count(_Ptr __p) { }
};

//CHECK: template <int _Lp> class __shared_count {
//CHECK-NEXT: public:
//CHECK-NEXT: template <typename _Ptr> __shared_count(_Ptr __p) { }
//CHECK-NEXT: };

0 comments on commit 80f3525

Please sign in to comment.