| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Copyright (c) 2012, 2013, 2015, 2016, 2017 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 "RenameVar.h" |
| 16 | |
| 17 | #include <algorithm> |
| 18 | #include <sstream> |
| 19 | |
| 20 | #include "clang/AST/RecursiveASTVisitor.h" |
| 21 | #include "clang/AST/ASTContext.h" |
| 22 | #include "clang/Basic/SourceManager.h" |
| 23 | |
| 24 | #include "TransformationManager.h" |
| 25 | |
| 26 | using namespace clang; |
| 27 | |
| 28 | static const char *DescriptionMsg = |
| 29 | "To increase readability, rename global and local variables \ |
| 30 | to a, b, ..., z. The transformation returns with error message \ |
| 31 | if there are more than 26 different variables. \n"; |
| 32 | |
| 33 | static RegisterTransformation<RenameVar> |
| 34 | Trans("rename-var", DescriptionMsg); |
| 35 | |
| 36 | class RNVCollectionVisitor : public RecursiveASTVisitor<RNVCollectionVisitor> { |
| 37 | public: |
| 38 | |
| 39 | explicit RNVCollectionVisitor(RenameVar *Instance) |
| 40 | : ConsumerInstance(Instance) |
| 41 | { } |
| 42 | |
| 43 | bool VisitVarDecl(VarDecl *VD); |
| 44 | |
| 45 | private: |
| 46 | |
| 47 | RenameVar *ConsumerInstance; |
| 48 | |
| 49 | }; |
| 50 | |
| 51 | class RenameVarVisitor : public RecursiveASTVisitor<RenameVarVisitor> { |
| 52 | public: |
| 53 | |
| 54 | explicit RenameVarVisitor(RenameVar *Instance) |
| 55 | : ConsumerInstance(Instance) |
| 56 | { } |
| 57 | |
| 58 | bool VisitVarDecl(VarDecl *VD); |
| 59 | |
| 60 | bool VisitDeclRefExpr(DeclRefExpr *DRE); |
| 61 | |
| 62 | private: |
| 63 | |
| 64 | RenameVar *ConsumerInstance; |
| 65 | |
| 66 | }; |
| 67 | |
| 68 | bool RNVCollectionVisitor::VisitVarDecl(VarDecl *VD) |
| 69 | { |
| 70 | if (ConsumerInstance->isInIncludedFile(VD)) |
| 71 | return true; |
| 72 | |
| 73 | ParmVarDecl *PV = dyn_cast<ParmVarDecl>(VD); |
| 74 | // Skip parameters |
| 75 | if (PV) |
| 76 | return true; |
| 77 | |
| 78 | VarDecl *CanonicalVD = VD->getCanonicalDecl(); |
| 79 | ConsumerInstance->addVar(CanonicalVD); |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | bool RenameVarVisitor::VisitVarDecl(VarDecl *VD) |
| 84 | { |
| 85 | VarDecl *CanonicalDecl = VD->getCanonicalDecl(); |
| 86 | llvm::DenseMap<VarDecl *, std::string>::iterator I = |
| 87 | ConsumerInstance->VarToNameMap.find(CanonicalDecl); |
| 88 | |
| 89 | if (I == ConsumerInstance->VarToNameMap.end()) |
| 90 | return true; |
| 91 | |
| 92 | return ConsumerInstance->RewriteHelper->replaceVarDeclName(VD, (*I).second); |
| 93 | } |
| 94 | |
| 95 | bool RenameVarVisitor::VisitDeclRefExpr(DeclRefExpr *DRE) |
| 96 | { |
| 97 | ValueDecl *OrigDecl = DRE->getDecl(); |
| 98 | VarDecl *VD = dyn_cast<VarDecl>(OrigDecl); |
| 99 | if (!VD) |
| 100 | return true; |
| 101 | |
| 102 | VarDecl *CanonicalDecl = VD->getCanonicalDecl(); |
| 103 | llvm::DenseMap<VarDecl *, std::string>::iterator I = |
| 104 | ConsumerInstance->VarToNameMap.find(CanonicalDecl); |
| 105 | |
| 106 | if (I == ConsumerInstance->VarToNameMap.end()) |
| 107 | return true; |
| 108 | |
| 109 | // We can visit the same DRE twice from an InitListExpr, i.e., |
| 110 | // through InitListExpr's semantic form and syntactic form. |
| 111 | if (ConsumerInstance->VisitedDREs.count(DRE)) |
| 112 | return true; |
| 113 | ConsumerInstance->VisitedDREs.insert(DRE); |
| 114 | |
| 115 | return ConsumerInstance->RewriteHelper->replaceExpr(DRE, (*I).second); |
| 116 | } |
| 117 | |
| 118 | void RenameVar::Initialize(ASTContext &context) |
| 119 | { |
| 120 | Transformation::Initialize(context); |
| 121 | |
| 122 | VarCollectionVisitor = new RNVCollectionVisitor(this); |
| 123 | RenameVisitor = new RenameVarVisitor(this); |
| 124 | |
| 125 | for (char C = 'z'; C >= 'a'; C--) { |
| 126 | AvailableNames.push_back(C); |
| 127 | } |
| 128 | ValidInstanceNum = 1; |
| 129 | } |
| 130 | |
| 131 | bool RenameVar::HandleTopLevelDecl(DeclGroupRef D) |
| 132 | { |
| 133 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) { |
| 134 | VarCollectionVisitor->TraverseDecl(*I); |
| 135 | } |
| 136 | return true; |
| 137 | } |
| 138 | |
| 139 | void RenameVar::HandleTranslationUnit(ASTContext &Ctx) |
| 140 | { |
| 141 | unsigned int NumNames = AvailableNames.size(); |
| 142 | unsigned int NumVars = ValidVars.size(); |
| 143 | |
| 144 | if (NumVars == 0) { |
| 145 | ValidInstanceNum = 0; |
| 146 | } |
| 147 | else if (NumVars > NumNames) { |
| 148 | // TEMP: currently not to rename vars in C++ files if there are |
| 149 | // more than 26 global or local vars |
| 150 | if (TransformationManager::isCXXLangOpt() || allValidNames()) { |
| 151 | ValidInstanceNum = 0; |
| 152 | } |
| 153 | else { |
| 154 | NumNames = NumVars; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | if (QueryInstanceOnly) { |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | if (NumVars > NumNames) { |
| 163 | TransError = TransMaxVarsError; |
| 164 | return; |
| 165 | } |
| 166 | else if (NumVars == 0) { |
| 167 | TransError = TransNoValidVarsError; |
| 168 | return; |
| 169 | } |
| 170 | else if (TransformationCounter > ValidInstanceNum) { |
| 171 | TransError = TransMaxInstanceError; |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | TransAssert(RenameVisitor && "NULL RenameVisitor!"); |
| 176 | Ctx.getDiagnostics().setSuppressAllDiagnostics(false); |
| 177 | |
| 178 | collectVars(); |
| 179 | RenameVisitor->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 180 | |
| 181 | if (Ctx.getDiagnostics().hasErrorOccurred() || |
| 182 | Ctx.getDiagnostics().hasFatalErrorOccurred()) |
| 183 | TransError = TransInternalError; |
| 184 | } |
| 185 | |
| 186 | void RenameVar::addVar(VarDecl *VD) |
| 187 | { |
| 188 | std::string Name = VD->getNameAsString(); |
| 189 | if (Name.size() > 1) { |
| 190 | ValidVars.push_back(VD); |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | char C = Name[0]; |
| 195 | if ((C < 'a') || (C > 'z')) |
| 196 | return; |
| 197 | |
| 198 | SmallVector<char, 26>::iterator I = |
| 199 | std::find(AvailableNames.begin(), AvailableNames.end(), C); |
| 200 | |
| 201 | // It could be the case where C has been erased, e.g., |
| 202 | // two variables from different scopes have the same name |
| 203 | if (I == AvailableNames.end()) |
| 204 | return; |
| 205 | |
| 206 | AvailableNames.erase(I); |
| 207 | } |
| 208 | |
| 209 | bool RenameVar::allValidNames(void) |
| 210 | { |
| 211 | for (std::vector<VarDecl*>::iterator I = ValidVars.begin(), |
| 212 | E = ValidVars.end(); I != E; ++I) { |
| 213 | VarDecl *VD = (*I); |
| 214 | std::string Name = VD->getNameAsString(); |
| 215 | if (Name.size() == 1) { |
| 216 | char C = Name[0]; |
| 217 | if ((C < 'a') || (C > 'z')) |
| 218 | return false; |
| 219 | } |
| 220 | else { |
| 221 | std::stringstream TmpSS(Name.substr(1)); |
| 222 | unsigned int Value; |
| 223 | if (!(TmpSS >> Value)) |
| 224 | return false; |
| 225 | } |
| 226 | } |
| 227 | return true; |
| 228 | } |
| 229 | |
| 230 | void RenameVar::collectVars(void) |
| 231 | { |
| 232 | unsigned Count = 1; |
| 233 | for (std::vector<VarDecl*>::iterator I = ValidVars.begin(), |
| 234 | E = ValidVars.end(); I != E; ++I) { |
| 235 | VarDecl *VD = (*I); |
| 236 | if (AvailableNames.size()) { |
| 237 | char Name = AvailableNames.back(); |
| 238 | AvailableNames.pop_back(); |
| 239 | VarToNameMap[VD] = std::string(1, Name); |
| 240 | } |
| 241 | else { |
| 242 | std::stringstream TmpSS; |
| 243 | TmpSS << RenamePrefix << Count; |
| 244 | VarToNameMap[VD] = TmpSS.str(); |
| 245 | Count++; |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | RenameVar::~RenameVar(void) |
| 251 | { |
| 252 | delete VarCollectionVisitor; |
| 253 | delete RenameVisitor; |
| 254 | } |
| 255 |