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

Teach TemplateNonTypeArgToInt to replace template params, too. #83

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
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: };