Skip to content

Commit 615ff8e

Browse files
committed
Teach TemplateNonTypeArgToInt to replace template params, too.
1 parent c278cf7 commit 615ff8e

4 files changed

Lines changed: 43 additions & 3 deletions

File tree

clang_delta/RewriteUtils.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,24 @@ bool RewriteUtils::replaceNamedDeclName(const NamedDecl *ND,
882882
ND->getNameAsString().size(), NameStr));
883883
}
884884

885+
///\brief Replaces a value decl with a given string.
886+
///
887+
///For example: \code
888+
/// enum E {...};
889+
/// template <E argName> struct S { } => template <int> struct S { }
890+
///\endcode
891+
///
892+
///\param[in] VD - The decl to be replaced.
893+
///\param[in] Str - The replacement
894+
///\returns true on success.
895+
///
896+
bool RewriteUtils::replaceValueDecl(const ValueDecl *VD, const std::string &Str)
897+
{
898+
SourceRange Range = VD->getSourceRange();
899+
unsigned RangeSize = TheRewriter->getRangeSize(Range);
900+
return !(TheRewriter->ReplaceText(Range.getBegin(), RangeSize, Str));
901+
}
902+
885903
bool RewriteUtils::replaceVarDeclName(VarDecl *VD,
886904
const std::string &NameStr)
887905
{

clang_delta/RewriteUtils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ namespace clang {
4949
class ClassTemplateDecl;
5050
class CXXMethodDecl;
5151
class NestedNameSpecifierLoc;
52+
class ValueDecl;
5253
}
5354

5455
class RewriteUtils {
@@ -203,6 +204,8 @@ class RewriteUtils {
203204
bool replaceNamedDeclName(const clang::NamedDecl *ND,
204205
const std::string &NameStr);
205206

207+
bool replaceValueDecl(const clang::ValueDecl *ValD, const std::string &Str);
208+
206209
bool replaceCXXDtorCallExpr(const clang::CXXMemberCallExpr *CE,
207210
std::string &Name);
208211

clang_delta/TemplateNonTypeArgToInt.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ void TemplateNonTypeArgToInt::HandleTranslationUnit(ASTContext &Ctx)
105105
}
106106

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

111111
if (Ctx.getDiagnostics().hasErrorOccurred() ||
112112
Ctx.getDiagnostics().hasFatalErrorOccurred())
@@ -219,8 +219,15 @@ void TemplateNonTypeArgToInt::handleOneTemplateDecl(const TemplateDecl *D)
219219
for (TemplateParameterList::const_iterator I = TPList->begin(),
220220
E = TPList->end(); I != E; ++I) {
221221
const NamedDecl *ParamND = (*I);
222-
if (isValidParameter(ParamND))
222+
if (isValidParameter(ParamND)) {
223223
ValidParamIdx->insert(Idx);
224+
if (const ValueDecl* ValD = dyn_cast<ValueDecl>(ParamND)) {
225+
++ValidInstanceNum;
226+
RewriteHelper->replaceValueDecl(ValD,
227+
"int " + ParamND->getNameAsString());
228+
229+
}
230+
}
224231
Idx++;
225232
}
226233

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//RUN: %clang_delta --transformation=template-non-type-arg-to-int --counter=1 %s | FileCheck %s
2+
3+
enum _Lock_policy { _S_atomic } const __default_lock_policy = _S_atomic;
4+
template <_Lock_policy _Lp> class __shared_count {
5+
public:
6+
template <typename _Ptr> __shared_count(_Ptr __p) { }
7+
};
8+
9+
//CHECK: template <int _Lp> class __shared_count {
10+
//CHECK-NEXT: public:
11+
//CHECK-NEXT: template <typename _Ptr> __shared_count(_Ptr __p) { }
12+
//CHECK-NEXT: };

0 commit comments

Comments
 (0)