| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Copyright (c) 2012, 2013, 2014, 2015, 2016, 2017, 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 "RenameParam.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 parameters to p1, p2, ...\n"; |
| 30 | |
| 31 | static RegisterTransformation<RenameParam> |
| 32 | Trans("rename-param", DescriptionMsg); |
| 33 | |
| 34 | class ExistingVarCollectionVisitor : public |
| 35 | RecursiveASTVisitor<ExistingVarCollectionVisitor> { |
| 36 | public: |
| 37 | |
| 38 | explicit ExistingVarCollectionVisitor(RenameParam *Instance) |
| 39 | : ConsumerInstance(Instance) |
| 40 | { } |
| 41 | |
| 42 | bool VisitVarDecl(VarDecl *VD); |
| 43 | |
| 44 | private: |
| 45 | |
| 46 | RenameParam *ConsumerInstance; |
| 47 | }; |
| 48 | |
| 49 | class RenameParamVisitor : public RecursiveASTVisitor<RenameParamVisitor> { |
| 50 | public: |
| 51 | |
| 52 | explicit RenameParamVisitor(RenameParam *Instance) |
| 53 | : ConsumerInstance(Instance) |
| 54 | { } |
| 55 | |
| 56 | bool VisitFunctionDecl(FunctionDecl *FD); |
| 57 | |
| 58 | bool VisitDeclRefExpr(DeclRefExpr *DRE); |
| 59 | |
| 60 | private: |
| 61 | |
| 62 | RenameParam *ConsumerInstance; |
| 63 | |
| 64 | llvm::DenseMap<ParmVarDecl *, std::string> ParamNameMap; |
| 65 | |
| 66 | }; |
| 67 | |
| 68 | bool ExistingVarCollectionVisitor::VisitVarDecl(VarDecl *VD) |
| 69 | { |
| 70 | if (ConsumerInstance->isInIncludedFile(VD)) |
| 71 | return true; |
| 72 | |
| 73 | ParmVarDecl *PD = dyn_cast<ParmVarDecl>(VD); |
| 74 | if (PD) { |
| 75 | ConsumerInstance->validateParam(PD); |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | VarDecl *CanonicalVD = VD->getCanonicalDecl(); |
| 80 | |
| 81 | if (CanonicalVD->isLocalVarDecl()) { |
| 82 | ConsumerInstance->addLocalVar(VD); |
| 83 | } |
| 84 | else { |
| 85 | ConsumerInstance->addGlobalVar(VD); |
| 86 | } |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | bool RenameParamVisitor::VisitFunctionDecl(FunctionDecl *FD) |
| 91 | { |
| 92 | if (ConsumerInstance->isInIncludedFile(FD) || (FD->param_size() == 0)) |
| 93 | return true; |
| 94 | |
| 95 | FunctionDecl *CanonicalFD = FD->getCanonicalDecl(); |
| 96 | unsigned int CurrPostfix = 0; |
| 97 | ParamNameMap.clear(); |
| 98 | |
| 99 | for(FunctionDecl::param_iterator I = FD->param_begin(), |
| 100 | E = FD->param_end(); I != E; ++I) { |
| 101 | |
| 102 | CurrPostfix++; |
| 103 | ParmVarDecl *PD = (*I); |
| 104 | if (PD->getNameAsString().empty()) |
| 105 | continue; |
| 106 | |
| 107 | CurrPostfix = ConsumerInstance->validatePostfix(CanonicalFD, CurrPostfix); |
| 108 | std::stringstream TmpSS; |
| 109 | TmpSS << ConsumerInstance->ParamNamePrefix << CurrPostfix; |
| 110 | |
| 111 | ConsumerInstance->RewriteHelper->replaceVarDeclName(PD, TmpSS.str()); |
| 112 | |
| 113 | ParamNameMap[*I] = TmpSS.str(); |
| 114 | } |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | bool RenameParamVisitor::VisitDeclRefExpr(DeclRefExpr *DRE) |
| 119 | { |
| 120 | if (ConsumerInstance->isInIncludedFile(DRE)) |
| 121 | return true; |
| 122 | ValueDecl *OrigDecl = DRE->getDecl(); |
| 123 | ParmVarDecl *PD = dyn_cast<ParmVarDecl>(OrigDecl); |
| 124 | |
| 125 | if (!PD || ConsumerInstance->isInIncludedFile(PD)) |
| 126 | return true; |
| 127 | |
| 128 | llvm::DenseMap<ParmVarDecl *, std::string>::iterator I = |
| 129 | ParamNameMap.find(PD); |
| 130 | if (I == ParamNameMap.end()) |
| 131 | return true; |
| 132 | |
| 133 | return ConsumerInstance->RewriteHelper->replaceExpr(DRE, (*I).second); |
| 134 | } |
| 135 | |
| 136 | void RenameParam::Initialize(ASTContext &context) |
| 137 | { |
| 138 | Transformation::Initialize(context); |
| 139 | VarCollectionVisitor = new ExistingVarCollectionVisitor(this); |
| 140 | RenameVisitor = new RenameParamVisitor(this); |
| 141 | ValidInstanceNum = 1; |
| 142 | } |
| 143 | |
| 144 | void RenameParam::HandleTranslationUnit(ASTContext &Ctx) |
| 145 | { |
| 146 | VarCollectionVisitor->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 147 | if (QueryInstanceOnly) { |
| 148 | if (!HasValidParams) |
| 149 | ValidInstanceNum = 0; |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | if (!HasValidParams) { |
| 154 | TransError = TransNoValidParamsError; |
| 155 | return; |
| 156 | } |
| 157 | else if (TransformationCounter > ValidInstanceNum) { |
| 158 | TransError = TransMaxInstanceError; |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | TransAssert(RenameVisitor && "NULL RenameVisitor!"); |
| 163 | Ctx.getDiagnostics().setSuppressAllDiagnostics(false); |
| 164 | |
| 165 | RenameVisitor->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 166 | |
| 167 | if (Ctx.getDiagnostics().hasErrorOccurred() || |
| 168 | Ctx.getDiagnostics().hasFatalErrorOccurred()) |
| 169 | TransError = TransInternalError; |
| 170 | } |
| 171 | |
| 172 | bool RenameParam::getPostfixValue(const std::string &Name, |
| 173 | unsigned int &Value) |
| 174 | { |
| 175 | // It's an unamed parameter, we skip it |
| 176 | if (Name.size() == 0) |
| 177 | return true; |
| 178 | |
| 179 | if (Name.size() == 1) |
| 180 | return false; |
| 181 | |
| 182 | std::string Prefix = Name.substr(0, 1); |
| 183 | if (Prefix != ParamNamePrefix) |
| 184 | return false; |
| 185 | |
| 186 | std::string RestStr = Name.substr(1); |
| 187 | std::stringstream TmpSS(RestStr); |
| 188 | if (!(TmpSS >> Value)) |
| 189 | return false; |
| 190 | |
| 191 | return true; |
| 192 | } |
| 193 | |
| 194 | void RenameParam::validateParam(ParmVarDecl *PD) |
| 195 | { |
| 196 | unsigned int Value; |
| 197 | if (PD->isReferenced() && !getPostfixValue(PD->getNameAsString(), Value)) |
| 198 | HasValidParams = true; |
| 199 | } |
| 200 | |
| 201 | void RenameParam::addGlobalVar(VarDecl *VD) |
| 202 | { |
| 203 | unsigned int PostValue; |
| 204 | if (!getPostfixValue(VD->getNameAsString(), PostValue)) |
| 205 | return; |
| 206 | |
| 207 | ExistingGlobalVars.insert(PostValue); |
| 208 | } |
| 209 | |
| 210 | void RenameParam::addLocalVar(VarDecl *VD) |
| 211 | { |
| 212 | unsigned int PostValue; |
| 213 | if (!getPostfixValue(VD->getNameAsString(), PostValue)) |
| 214 | return; |
| 215 | |
| 216 | DeclContext *Ctx = VD->getDeclContext(); |
| 217 | FunctionDecl *FD = dyn_cast<FunctionDecl>(Ctx); |
| 218 | TransAssert(FD && "Bad function declaration!"); |
| 219 | FunctionDecl *CanonicalFD = FD->getCanonicalDecl(); |
| 220 | |
| 221 | ExistingNumberSet *CurrSet; |
| 222 | llvm::DenseMap<FunctionDecl *, ExistingNumberSet *>::iterator I = |
| 223 | FunExistingVarsMap.find(CanonicalFD); |
| 224 | |
| 225 | if (I == FunExistingVarsMap.end()) { |
| 226 | CurrSet = new ExistingNumberSet(); |
| 227 | FunExistingVarsMap[CanonicalFD] = CurrSet; |
| 228 | } |
| 229 | else { |
| 230 | CurrSet = (*I).second; |
| 231 | } |
| 232 | |
| 233 | CurrSet->insert(PostValue); |
| 234 | } |
| 235 | |
| 236 | bool RenameParam::isValidPostfix(ExistingNumberSet *LocalSet, |
| 237 | unsigned int Postfix) |
| 238 | { |
| 239 | if (ExistingGlobalVars.count(Postfix)) |
| 240 | return false; |
| 241 | |
| 242 | if (!LocalSet) |
| 243 | return true; |
| 244 | |
| 245 | return !LocalSet->count(Postfix); |
| 246 | } |
| 247 | |
| 248 | unsigned int RenameParam::validatePostfix(FunctionDecl *FD, |
| 249 | unsigned int CurrPostfix) |
| 250 | { |
| 251 | int MaxIteration = 0; |
| 252 | ExistingNumberSet *LocalNumberSet = NULL; |
| 253 | |
| 254 | llvm::DenseMap<FunctionDecl *, ExistingNumberSet *>::iterator I = |
| 255 | FunExistingVarsMap.find(FD); |
| 256 | |
| 257 | if (I != FunExistingVarsMap.end()) { |
| 258 | LocalNumberSet = (*I).second; |
| 259 | MaxIteration += static_cast<int>(LocalNumberSet->size()); |
| 260 | } |
| 261 | MaxIteration += static_cast<int>(ExistingGlobalVars.size()); |
| 262 | |
| 263 | while (!isValidPostfix(LocalNumberSet, CurrPostfix)) { |
| 264 | CurrPostfix++; |
| 265 | MaxIteration--; |
| 266 | TransAssert((MaxIteration >= 0) && "Bad Postfix!"); |
| 267 | } |
| 268 | |
| 269 | return CurrPostfix; |
| 270 | } |
| 271 | |
| 272 | RenameParam::~RenameParam(void) |
| 273 | { |
| 274 | delete VarCollectionVisitor; |
| 275 | delete RenameVisitor; |
| 276 | |
| 277 | for (llvm::DenseMap<FunctionDecl *, ExistingNumberSet *>::iterator |
| 278 | I = FunExistingVarsMap.begin(), E = FunExistingVarsMap.end(); |
| 279 | I != E; ++I) { |
| 280 | delete (*I).second; |
| 281 | } |
| 282 | } |