| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Copyright (c) 2012, 2013, 2015, 2016, 2017, 2018, 2019 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 "LocalToGlobal.h" |
| 16 | |
| 17 | #include "clang/AST/RecursiveASTVisitor.h" |
| 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/Basic/SourceManager.h" |
| 20 | |
| 21 | #include "TransformationManager.h" |
| 22 | |
| 23 | using namespace clang; |
| 24 | |
| 25 | static const char *DescriptionMsg = |
| 26 | "Move the declaraion of a non-static local variable from \ |
| 27 | a function to the global scope. Also rename the moved local variable \ |
| 28 | to avoid possible name conflicts. \n"; |
| 29 | |
| 30 | static RegisterTransformation<LocalToGlobal> |
| 31 | Trans("local-to-global", DescriptionMsg); |
| 32 | |
| 33 | class LocalToGlobalCollectionVisitor : public |
| 34 | RecursiveASTVisitor<LocalToGlobalCollectionVisitor> { |
| 35 | public: |
| 36 | |
| 37 | explicit LocalToGlobalCollectionVisitor(LocalToGlobal *Instance) |
| 38 | : ConsumerInstance(Instance), |
| 39 | CurrentFuncDecl(NULL) |
| 40 | { } |
| 41 | |
| 42 | bool VisitVarDecl(VarDecl *VD); |
| 43 | |
| 44 | bool VisitCXXCatchStmt(CXXCatchStmt *DS); |
| 45 | |
| 46 | void setCurrentFuncDecl(FunctionDecl *FD) { |
| 47 | CurrentFuncDecl = FD; |
| 48 | } |
| 49 | |
| 50 | private: |
| 51 | |
| 52 | LocalToGlobal *ConsumerInstance; |
| 53 | |
| 54 | FunctionDecl *CurrentFuncDecl; |
| 55 | }; |
| 56 | |
| 57 | class LocalToGlobalFunctionVisitor : public |
| 58 | RecursiveASTVisitor<LocalToGlobalFunctionVisitor> { |
| 59 | public: |
| 60 | |
| 61 | explicit LocalToGlobalFunctionVisitor(LocalToGlobal *Instance) |
| 62 | : ConsumerInstance(Instance) |
| 63 | { } |
| 64 | |
| 65 | bool VisitFunctionDecl(FunctionDecl *VD); |
| 66 | |
| 67 | private: |
| 68 | |
| 69 | LocalToGlobal *ConsumerInstance; |
| 70 | }; |
| 71 | |
| 72 | class LToGASTVisitor : public RecursiveASTVisitor<LToGASTVisitor> { |
| 73 | public: |
| 74 | typedef RecursiveASTVisitor<LToGASTVisitor> Inherited; |
| 75 | |
| 76 | explicit LToGASTVisitor(LocalToGlobal *Instance) |
| 77 | : ConsumerInstance(Instance) |
| 78 | { } |
| 79 | |
| 80 | bool VisitDeclStmt(DeclStmt *DS); |
| 81 | |
| 82 | bool VisitDeclRefExpr(DeclRefExpr *VarRefExpr); |
| 83 | |
| 84 | private: |
| 85 | |
| 86 | LocalToGlobal *ConsumerInstance; |
| 87 | |
| 88 | bool makeLocalAsGlobalVar(FunctionDecl *FD, VarDecl *VD, |
| 89 | Decl *PrevDecl, bool StmtRemoved); |
| 90 | |
| 91 | }; |
| 92 | |
| 93 | bool LocalToGlobalFunctionVisitor::VisitFunctionDecl(FunctionDecl *FD) |
| 94 | { |
| 95 | if (FD->isThisDeclarationADefinition()) { |
| 96 | ConsumerInstance->LocalVarCollectionVisitor->setCurrentFuncDecl(FD); |
| 97 | ConsumerInstance->LocalVarCollectionVisitor->TraverseDecl(FD); |
| 98 | ConsumerInstance->LocalVarCollectionVisitor->setCurrentFuncDecl(NULL); |
| 99 | } |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | bool LocalToGlobalCollectionVisitor::VisitVarDecl(VarDecl *VD) |
| 104 | { |
| 105 | TransAssert(CurrentFuncDecl && "NULL CurrentFuncDecl!"); |
| 106 | |
| 107 | if (ConsumerInstance->isInIncludedFile(VD) || !VD->isLocalVarDecl() || |
| 108 | VD->isStaticLocal() || VD->hasExternalStorage() || |
| 109 | ConsumerInstance->SkippedVars.count(VD->getCanonicalDecl())) |
| 110 | return true; |
| 111 | |
| 112 | ConsumerInstance->ValidInstanceNum++; |
| 113 | if (ConsumerInstance->ValidInstanceNum == |
| 114 | ConsumerInstance->TransformationCounter) { |
| 115 | ConsumerInstance->TheVarDecl = VD; |
| 116 | ConsumerInstance->TheFuncDecl = CurrentFuncDecl; |
| 117 | ConsumerInstance->setNewName(CurrentFuncDecl, VD); |
| 118 | } |
| 119 | return true; |
| 120 | } |
| 121 | |
| 122 | bool LocalToGlobalCollectionVisitor::VisitCXXCatchStmt(CXXCatchStmt *S) |
| 123 | { |
| 124 | const VarDecl *VD = S->getExceptionDecl(); |
| 125 | if (VD) { |
| 126 | ConsumerInstance->SkippedVars.insert(VD->getCanonicalDecl()); |
| 127 | } |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | void LocalToGlobal::Initialize(ASTContext &context) |
| 132 | { |
| 133 | Transformation::Initialize(context); |
| 134 | LocalVarCollectionVisitor = new LocalToGlobalCollectionVisitor(this); |
| 135 | FunctionVisitor = new LocalToGlobalFunctionVisitor(this); |
| 136 | TransformationASTVisitor = new LToGASTVisitor(this); |
| 137 | } |
| 138 | |
| 139 | void LocalToGlobal::HandleTranslationUnit(ASTContext &Ctx) |
| 140 | { |
| 141 | if (TransformationManager::isOpenCLLangOpt()) { |
| 142 | ValidInstanceNum = 0; |
| 143 | } |
| 144 | else { |
| 145 | FunctionVisitor->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 146 | } |
| 147 | |
| 148 | if (QueryInstanceOnly) |
| 149 | return; |
| 150 | |
| 151 | if (TransformationCounter > ValidInstanceNum) { |
| 152 | TransError = TransMaxInstanceError; |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | TransAssert(TransformationASTVisitor && "NULL TransformationASTVisitor!"); |
| 157 | Ctx.getDiagnostics().setSuppressAllDiagnostics(false); |
| 158 | TransAssert(TheFuncDecl && "NULL TheFuncDecl!"); |
| 159 | TransAssert(TheVarDecl && "NULL TheVarDecl!"); |
| 160 | |
| 161 | TransformationASTVisitor->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 162 | |
| 163 | if (Ctx.getDiagnostics().hasErrorOccurred() || |
| 164 | Ctx.getDiagnostics().hasFatalErrorOccurred()) |
| 165 | TransError = TransInternalError; |
| 166 | } |
| 167 | |
| 168 | void LocalToGlobal::setNewName(FunctionDecl *FD, |
| 169 | const VarDecl *VD) |
| 170 | { |
| 171 | std::string NewName; |
| 172 | NewName = FD->getNameInfo().getAsString(); |
| 173 | NewName += "_"; |
| 174 | NewName += VD->getNameAsString(); |
| 175 | |
| 176 | // also backup the new name |
| 177 | TheNewDeclName = NewName; |
| 178 | } |
| 179 | |
| 180 | LocalToGlobal::~LocalToGlobal(void) |
| 181 | { |
| 182 | delete FunctionVisitor; |
| 183 | delete LocalVarCollectionVisitor; |
| 184 | delete TransformationASTVisitor; |
| 185 | } |
| 186 | |
| 187 | bool LToGASTVisitor::makeLocalAsGlobalVar(FunctionDecl *FD, VarDecl *VD, |
| 188 | Decl *PrevDecl, bool StmtRemoved) |
| 189 | { |
| 190 | std::string GlobalVarStr; |
| 191 | std::string NewName = ConsumerInstance->getNewName(); |
| 192 | auto& TheRewriter = ConsumerInstance->TheRewriter; |
| 193 | |
| 194 | if (StmtRemoved) { |
| 195 | SourceRange Range = PrevDecl->getSourceRange(); |
| 196 | ConsumerInstance->RewriteHelper->getStringBetweenLocs( |
| 197 | GlobalVarStr, Range.getBegin(), VD->getLocation()); |
| 198 | GlobalVarStr += NewName; |
| 199 | } |
| 200 | else { |
| 201 | QualType T = VD->getType(); |
| 202 | T.getAsStringInternal(NewName, |
| 203 | ConsumerInstance->Context->getPrintingPolicy()); |
| 204 | |
| 205 | GlobalVarStr = NewName; |
| 206 | |
| 207 | if (VD->hasInit()) { |
| 208 | const Expr *InitExpr = VD->getInit(); |
| 209 | std::string InitStr(""); |
| 210 | ConsumerInstance->RewriteHelper->getExprString(InitExpr, |
| 211 | InitStr); |
| 212 | GlobalVarStr += " = "; |
| 213 | GlobalVarStr += InitStr; |
| 214 | } |
| 215 | } |
| 216 | GlobalVarStr += ";\n"; |
| 217 | |
| 218 | for (DeclContext* DC = FD; DC; DC = DC->getParent()) { |
| 219 | if (DC->getParent() && DC->getParent()->isTranslationUnit()) { |
| 220 | TheRewriter.InsertTextBefore(cast<Decl>(DC)->getBeginLoc(), GlobalVarStr); |
| 221 | return true; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | return ConsumerInstance->RewriteHelper-> |
| 226 | insertStringBeforeFunc(FD, GlobalVarStr); |
| 227 | } |
| 228 | |
| 229 | bool LToGASTVisitor::VisitDeclStmt(DeclStmt *DS) |
| 230 | { |
| 231 | Decl *PrevDecl = NULL; |
| 232 | VarDecl *VD = NULL; |
| 233 | int NumDecls = 0; |
| 234 | int VarPos = 0; |
| 235 | |
| 236 | for (DeclStmt::const_decl_iterator I = DS->decl_begin(), |
| 237 | E = DS->decl_end(); I != E; ++I) { |
| 238 | NumDecls++; |
| 239 | |
| 240 | // we have got the var |
| 241 | if (VD) |
| 242 | continue; |
| 243 | |
| 244 | Decl *CurrDecl = (*I); |
| 245 | if (CurrDecl != ConsumerInstance->TheVarDecl) { |
| 246 | PrevDecl = (*I); |
| 247 | continue; |
| 248 | } |
| 249 | |
| 250 | TransAssert(!VD && "Duplicated Definition?"); |
| 251 | VD = dyn_cast<VarDecl>(CurrDecl); |
| 252 | TransAssert(VD && "Bad VarDecl!"); |
| 253 | VarPos = NumDecls - 1; |
| 254 | } |
| 255 | |
| 256 | if (!VD) |
| 257 | return true; |
| 258 | |
| 259 | bool IsFirstDecl = (!VarPos); |
| 260 | bool StmtRemoved = false; |
| 261 | ConsumerInstance->RewriteHelper->removeVarFromDeclStmt |
| 262 | (DS, VD, PrevDecl, IsFirstDecl, &StmtRemoved); |
| 263 | |
| 264 | return makeLocalAsGlobalVar(ConsumerInstance->TheFuncDecl, VD, |
| 265 | PrevDecl, StmtRemoved); |
| 266 | } |
| 267 | |
| 268 | bool LToGASTVisitor::VisitDeclRefExpr(DeclRefExpr *VarRefExpr) |
| 269 | { |
| 270 | if (!ConsumerInstance->TheVarDecl) |
| 271 | return true; |
| 272 | |
| 273 | const ValueDecl *OrigDecl = VarRefExpr->getDecl(); |
| 274 | |
| 275 | if (OrigDecl != ConsumerInstance->TheVarDecl) |
| 276 | return true; |
| 277 | |
| 278 | SourceRange ExprRange = VarRefExpr->getSourceRange(); |
| 279 | SourceLocation StartLoc = ExprRange.getBegin(); |
| 280 | SourceLocation EndLoc = ExprRange.getEnd(); |
| 281 | if (StartLoc.isMacroID()) { |
| 282 | StartLoc = ConsumerInstance->SrcManager->getSpellingLoc(StartLoc); |
| 283 | EndLoc = ConsumerInstance->SrcManager->getSpellingLoc(EndLoc); |
| 284 | } |
| 285 | |
| 286 | return |
| 287 | !(ConsumerInstance->TheRewriter.ReplaceText( |
| 288 | SourceRange(StartLoc, EndLoc), ConsumerInstance->TheNewDeclName)); |
| 289 | } |
| 290 |