| 1 |
//===----------------------------------------------------------------------===// |
| 2 |
// |
| 3 |
// Copyright (c) 2012, 2013, 2015, 2016, 2017, 2018, 2019, 2020 The University of Utah |
| 4 |
// All rights reserved. |
| 5 |
// |
| 6 |
// This file is distributed under the University of Illinois Open Source |
| 7 |
// License. See the file COPYING for details. |
| 8 |
// |
| 9 |
//===----------------------------------------------------------------------===// |
| 10 |
|
| 11 |
#if HAVE_CONFIG_H |
| 12 |
# include <config.h> |
| 13 |
#endif |
| 14 |
|
| 15 |
#include "RenameFun.h" |
| 16 |
|
| 17 |
#include <sstream> |
| 18 |
|
| 19 |
#include "clang/AST/RecursiveASTVisitor.h" |
| 20 |
#include "clang/AST/ASTContext.h" |
| 21 |
#include "clang/Basic/SourceManager.h" |
| 22 |
|
| 23 |
#include "TransformationManager.h" |
| 24 |
|
| 25 |
using namespace clang; |
| 26 |
|
| 27 |
static const char *DescriptionMsg = |
| 28 |
"Another pass to increase readability of reduced code. \ |
| 29 |
It renames function names to fn1, fn2, ...\n"; |
| 30 |
|
| 31 |
static RegisterTransformation<RenameFun> |
| 32 |
Trans("rename-fun", DescriptionMsg); |
| 33 |
|
| 34 |
class RNFunCollectionVisitor : public |
| 35 |
RecursiveASTVisitor<RNFunCollectionVisitor> { |
| 36 |
|
| 37 |
public: |
| 38 |
|
| 39 |
explicit RNFunCollectionVisitor(RenameFun *Instance) |
| 40 |
: ConsumerInstance(Instance) |
| 41 |
{ } |
| 42 |
|
| 43 |
bool VisitFunctionDecl(FunctionDecl *FD); |
| 44 |
|
| 45 |
bool VisitCallExpr(CallExpr *CE); |
| 46 |
|
| 47 |
private: |
| 48 |
|
| 49 |
RenameFun *ConsumerInstance; |
| 50 |
|
| 51 |
}; |
| 52 |
|
| 53 |
class RenameFunVisitor : public RecursiveASTVisitor<RenameFunVisitor> { |
| 54 |
public: |
| 55 |
|
| 56 |
explicit RenameFunVisitor(RenameFun *Instance) |
| 57 |
: ConsumerInstance(Instance) |
| 58 |
{ } |
| 59 |
|
| 60 |
bool VisitFunctionDecl(FunctionDecl *FD); |
| 61 |
|
| 62 |
bool VisitDeclRefExpr(DeclRefExpr *DRE); |
| 63 |
|
| 64 |
private: |
| 65 |
|
| 66 |
RenameFun *ConsumerInstance; |
| 67 |
|
| 68 |
}; |
| 69 |
|
| 70 |
bool RNFunCollectionVisitor::VisitFunctionDecl(FunctionDecl *FD) |
| 71 |
{ |
| 72 |
// renaming CXXMethodDecl is handled in a seperate pass |
| 73 |
if (dyn_cast<CXXMethodDecl>(FD) || FD->isOverloadedOperator()) { |
| 74 |
return true; |
| 75 |
} |
| 76 |
|
| 77 |
const FunctionDecl *CanonicalFD = FD->getCanonicalDecl(); |
| 78 |
if (ConsumerInstance->isInIncludedFile(FD) || |
| 79 |
ConsumerInstance->isInIncludedFile(CanonicalFD)) |
| 80 |
return true; |
| 81 |
|
| 82 |
ConsumerInstance->addFun(CanonicalFD); |
| 83 |
if (!ConsumerInstance->hasValidPostfix(FD->getNameAsString())) |
| 84 |
ConsumerInstance->HasValidFuns = true; |
| 85 |
return true; |
| 86 |
} |
| 87 |
|
| 88 |
bool RNFunCollectionVisitor::VisitCallExpr(CallExpr *CE) |
| 89 |
{ |
| 90 |
if (ConsumerInstance->isInIncludedFile(CE)) |
| 91 |
return true; |
| 92 |
FunctionDecl *FD = CE->getDirectCallee(); |
| 93 |
// It could happen, e.g., CE could refer to a DependentScopeDeclRefExpr |
| 94 |
if (!FD || dyn_cast<CXXMethodDecl>(FD) || FD->isOverloadedOperator()) |
| 95 |
return true; |
| 96 |
if (ConsumerInstance->isInIncludedFile(FD)) |
| 97 |
return true; |
| 98 |
|
| 99 |
const FunctionDecl *CanonicalFD = FD->getCanonicalDecl(); |
| 100 |
|
| 101 |
// This case is handled by VisitFunctionDecl |
| 102 |
if (CanonicalFD->isDefined()) |
| 103 |
return true; |
| 104 |
|
| 105 |
// It's possible we don't have function definition |
| 106 |
ConsumerInstance->addFun(CanonicalFD); |
| 107 |
if (!ConsumerInstance->hasValidPostfix(FD->getNameAsString())) |
| 108 |
ConsumerInstance->HasValidFuns = true; |
| 109 |
return true; |
| 110 |
} |
| 111 |
|
| 112 |
bool RenameFunVisitor::VisitFunctionDecl(FunctionDecl *FD) |
| 113 |
{ |
| 114 |
FunctionDecl *CanonicalDecl = FD->getCanonicalDecl(); |
| 115 |
if (ConsumerInstance->isInIncludedFile(FD) || |
| 116 |
ConsumerInstance->isInIncludedFile(CanonicalDecl) || |
| 117 |
dyn_cast<CXXMethodDecl>(FD) || |
| 118 |
FD->isOverloadedOperator()) { |
| 119 |
return true; |
| 120 |
} |
| 121 |
|
| 122 |
llvm::DenseMap<const FunctionDecl *, std::string>::iterator I = |
| 123 |
ConsumerInstance->FunToNameMap.find(CanonicalDecl); |
| 124 |
|
| 125 |
TransAssert((I != ConsumerInstance->FunToNameMap.end()) && |
| 126 |
"Cannot find FunctionDecl!"); |
| 127 |
|
| 128 |
return ConsumerInstance->RewriteHelper-> |
| 129 |
replaceFunctionDeclName(FD, (*I).second); |
| 130 |
} |
| 131 |
|
| 132 |
bool RenameFunVisitor::VisitDeclRefExpr(DeclRefExpr *DRE) |
| 133 |
{ |
| 134 |
if (ConsumerInstance->isInIncludedFile(DRE)) |
| 135 |
return true; |
| 136 |
|
| 137 |
ValueDecl *OrigDecl = DRE->getDecl(); |
| 138 |
FunctionDecl *FD = dyn_cast<FunctionDecl>(OrigDecl); |
| 139 |
if (!FD || dyn_cast<CXXMethodDecl>(FD) || FD->isOverloadedOperator() || |
| 140 |
ConsumerInstance->isInIncludedFile(FD)) |
| 141 |
return true; |
| 142 |
|
| 143 |
if (FD->isTemplateInstantiation()) { |
| 144 |
FD = FD->getTemplateInstantiationPattern(); |
| 145 |
} |
| 146 |
|
| 147 |
FunctionDecl *CanonicalDecl = FD->getCanonicalDecl(); |
| 148 |
llvm::DenseMap<const FunctionDecl *, std::string>::iterator I = |
| 149 |
ConsumerInstance->FunToNameMap.find(CanonicalDecl); |
| 150 |
|
| 151 |
TransAssert((I != ConsumerInstance->FunToNameMap.end()) && |
| 152 |
"Cannot find FunctionDecl!"); |
| 153 |
ConsumerInstance->TheRewriter.ReplaceText(DRE->getBeginLoc(), |
| 154 |
FD->getNameAsString().size(), (*I).second); |
| 155 |
return true; |
| 156 |
} |
| 157 |
|
| 158 |
void RenameFun::Initialize(ASTContext &context) |
| 159 |
{ |
| 160 |
Transformation::Initialize(context); |
| 161 |
FunCollectionVisitor = new RNFunCollectionVisitor(this); |
| 162 |
RenameVisitor = new RenameFunVisitor(this); |
| 163 |
ValidInstanceNum = 1; |
| 164 |
} |
| 165 |
|
| 166 |
bool RenameFun::HandleTopLevelDecl(DeclGroupRef D) |
| 167 |
{ |
| 168 |
for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) { |
| 169 |
FunCollectionVisitor->TraverseDecl(*I); |
| 170 |
} |
| 171 |
return true; |
| 172 |
} |
| 173 |
|
| 174 |
bool RenameFun::isConsecutiveNumbersFromOne(void) |
| 175 |
{ |
| 176 |
size_t Sz = AllValidNumbers.size(); |
| 177 |
if (!Sz) |
| 178 |
return true; |
| 179 |
|
| 180 |
bool hasOne = AllValidNumbers.count(1); |
| 181 |
if (!hasOne) |
| 182 |
return false; |
| 183 |
|
| 184 |
if (Sz == 1) { |
| 185 |
return hasOne; |
| 186 |
} |
| 187 |
|
| 188 |
std::set<unsigned int>::iterator I = |
| 189 |
max_element(AllValidNumbers.begin(), AllValidNumbers.end()); |
| 190 |
return ((*I) == Sz); |
| 191 |
} |
| 192 |
|
| 193 |
bool RenameFun::hasValidFuns(void) |
| 194 |
{ |
| 195 |
return (HasValidFuns || !isConsecutiveNumbersFromOne()); |
| 196 |
} |
| 197 |
|
| 198 |
void RenameFun::HandleTranslationUnit(ASTContext &Ctx) |
| 199 |
{ |
| 200 |
if (QueryInstanceOnly) { |
| 201 |
if (hasValidFuns()) |
| 202 |
ValidInstanceNum = 1; |
| 203 |
else |
| 204 |
ValidInstanceNum = 0; |
| 205 |
return; |
| 206 |
} |
| 207 |
|
| 208 |
if (!hasValidFuns()) { |
| 209 |
TransError = TransNoValidFunsError; |
| 210 |
return; |
| 211 |
} |
| 212 |
else if (TransformationCounter > ValidInstanceNum) { |
| 213 |
TransError = TransMaxInstanceError; |
| 214 |
return; |
| 215 |
} |
| 216 |
|
| 217 |
TransAssert(RenameVisitor && "NULL RenameVisitor!"); |
| 218 |
Ctx.getDiagnostics().setSuppressAllDiagnostics(false); |
| 219 |
|
| 220 |
RenameVisitor->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 221 |
|
| 222 |
if (Ctx.getDiagnostics().hasErrorOccurred() || |
| 223 |
Ctx.getDiagnostics().hasFatalErrorOccurred()) |
| 224 |
TransError = TransInternalError; |
| 225 |
} |
| 226 |
|
| 227 |
bool RenameFun::isSpecialFun(const std::string &Name) |
| 228 |
{ |
| 229 |
if ((Name.compare("main") == 0) || |
| 230 |
(Name.compare("printf") == 0)) |
| 231 |
return true; |
| 232 |
else |
| 233 |
return false; |
| 234 |
} |
| 235 |
|
| 236 |
bool RenameFun::hasValidPostfix(const std::string &Name) |
| 237 |
{ |
| 238 |
unsigned int Value; |
| 239 |
|
| 240 |
// Don't rename special functions |
| 241 |
if (isSpecialFun(Name)) |
| 242 |
return true; |
| 243 |
|
| 244 |
if (Name.size() <= 2) |
| 245 |
return false; |
| 246 |
|
| 247 |
std::string Prefix = Name.substr(0, 2); |
| 248 |
if (Prefix != FunNamePrefix) |
| 249 |
return false; |
| 250 |
|
| 251 |
std::string RestStr = Name.substr(2); |
| 252 |
std::stringstream TmpSS(RestStr); |
| 253 |
if (!(TmpSS >> Value)) |
| 254 |
return false; |
| 255 |
|
| 256 |
AllValidNumbers.insert(Value); |
| 257 |
return true; |
| 258 |
} |
| 259 |
|
| 260 |
void RenameFun::addFun(const FunctionDecl *FD) |
| 261 |
{ |
| 262 |
std::string Name = FD->getNameAsString(); |
| 263 |
// Skip special functions |
| 264 |
if (isSpecialFun(Name) || FD->hasAttr<OpenCLKernelAttr>()) |
| 265 |
FunToNameMap[FD] = Name; |
| 266 |
|
| 267 |
if (FunToNameMap.find(FD) != FunToNameMap.end()) |
| 268 |
return; |
| 269 |
|
| 270 |
std::stringstream SS; |
| 271 |
|
| 272 |
FunNamePostfix++; |
| 273 |
SS << FunNamePrefix << FunNamePostfix; |
| 274 |
|
| 275 |
TransAssert((FunToNameMap.find(FD) == FunToNameMap.end()) && |
| 276 |
"Duplicated Fun name!"); |
| 277 |
|
| 278 |
FunToNameMap[FD] = SS.str(); |
| 279 |
} |
| 280 |
|
| 281 |
RenameFun::~RenameFun(void) |
| 282 |
{ |
| 283 |
delete FunCollectionVisitor; |
| 284 |
delete RenameVisitor; |
| 285 |
} |
| 286 |
|