| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Copyright (c) 2012, 2013, 2015, 2016, 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 "LiftAssignmentExpr.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 | #include "CommonStatementVisitor.h" |
| 23 | |
| 24 | using namespace clang; |
| 25 | |
| 26 | static const char *DescriptionMsg = |
| 27 | "Lift an assignment expression to an assignment statement. \ |
| 28 | An assignment expression will be lifted if it apprears at : \n\ |
| 29 | * the guard position of an if, while or do statement; \n\ |
| 30 | * function argument; \n\ |
| 31 | * init, condition or inc position of a for statement \n"; |
| 32 | |
| 33 | static RegisterTransformation<LiftAssignmentExpr> |
| 34 | Trans("lift-assignment-expr", DescriptionMsg); |
| 35 | |
| 36 | class AssignExprCollectionVisitor : public |
| 37 | RecursiveASTVisitor<AssignExprCollectionVisitor> { |
| 38 | public: |
| 39 | |
| 40 | explicit AssignExprCollectionVisitor(LiftAssignmentExpr *Instance) |
| 41 | : ConsumerInstance(Instance) |
| 42 | { } |
| 43 | |
| 44 | bool VisitFunctionDecl(FunctionDecl *FD); |
| 45 | |
| 46 | private: |
| 47 | LiftAssignmentExpr *ConsumerInstance; |
| 48 | |
| 49 | }; |
| 50 | |
| 51 | class AssignExprStatementVisitor : public |
| 52 | CommonStatementVisitor<AssignExprStatementVisitor> { |
| 53 | public: |
| 54 | |
| 55 | explicit AssignExprStatementVisitor(LiftAssignmentExpr *Instance) |
| 56 | : ConsumerInstance(Instance) |
| 57 | { } |
| 58 | |
| 59 | bool VisitIfStmt(IfStmt *IS); |
| 60 | |
| 61 | bool VisitForStmt(ForStmt *FS); |
| 62 | |
| 63 | bool VisitWhileStmt(WhileStmt *WS); |
| 64 | |
| 65 | bool VisitDoStmt(DoStmt *DS); |
| 66 | |
| 67 | bool VisitCallExpr(CallExpr *CallE); |
| 68 | |
| 69 | private: |
| 70 | |
| 71 | LiftAssignmentExpr *ConsumerInstance; |
| 72 | |
| 73 | void handleSubExpr(Expr *E); |
| 74 | |
| 75 | }; |
| 76 | |
| 77 | bool AssignExprCollectionVisitor::VisitFunctionDecl(FunctionDecl *FD) |
| 78 | { |
| 79 | if (!FD->isThisDeclarationADefinition() || |
| 80 | ConsumerInstance->isInIncludedFile(FD)) |
| 81 | return true; |
| 82 | |
| 83 | ConsumerInstance->StmtVisitor->setCurrentFunctionDecl(FD); |
| 84 | ConsumerInstance->StmtVisitor->TraverseDecl(FD); |
| 85 | ConsumerInstance->StmtVisitor->setCurrentFunctionDecl(NULL); |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | // It is used to handle the case where if-then or else branch |
| 90 | // is not treated as a CompoundStmt. So it cannot be traversed |
| 91 | // from VisitCompoundStmt, e.g., |
| 92 | // if (x) |
| 93 | // foo(bar()) |
| 94 | bool AssignExprStatementVisitor::VisitIfStmt(IfStmt *IS) |
| 95 | { |
| 96 | Expr *E = IS->getCond(); |
| 97 | handleSubExpr(E); |
| 98 | |
| 99 | Stmt *ThenB = IS->getThen(); |
| 100 | visitNonCompoundStmt(ThenB); |
| 101 | |
| 102 | Stmt *ElseB = IS->getElse(); |
| 103 | visitNonCompoundStmt(ElseB); |
| 104 | |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | // It causes unsound transformation because |
| 109 | // the semantics of loop execution has been changed. |
| 110 | // For example, |
| 111 | // int foo(int x) |
| 112 | // { |
| 113 | // int i; |
| 114 | // for(i = 0; i < bar(bar(x)); i++) |
| 115 | // ... |
| 116 | // } |
| 117 | // will be transformed to: |
| 118 | // int foo(int x) |
| 119 | // { |
| 120 | // int i; |
| 121 | // int tmp_var = bar(x); |
| 122 | // for(i = 0; i < bar(tmp_var); i++) |
| 123 | // ... |
| 124 | // } |
| 125 | bool AssignExprStatementVisitor::VisitForStmt(ForStmt *FS) |
| 126 | { |
| 127 | Stmt *Init = FS->getInit(); |
| 128 | Expr *InitE = NULL; |
| 129 | if (Init) |
| 130 | InitE = dyn_cast<Expr>(Init); |
| 131 | |
| 132 | if (InitE) |
| 133 | handleSubExpr(InitE); |
| 134 | else |
| 135 | TraverseStmt(Init); |
| 136 | |
| 137 | Expr *Cond = FS->getCond(); |
| 138 | handleSubExpr(Cond); |
| 139 | |
| 140 | Expr *Inc = FS->getInc(); |
| 141 | handleSubExpr(Inc); |
| 142 | |
| 143 | Stmt *Body = FS->getBody(); |
| 144 | visitNonCompoundStmt(Body); |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | bool AssignExprStatementVisitor::VisitWhileStmt(WhileStmt *WS) |
| 149 | { |
| 150 | Expr *E = WS->getCond(); |
| 151 | handleSubExpr(E); |
| 152 | |
| 153 | Stmt *Body = WS->getBody(); |
| 154 | visitNonCompoundStmt(Body); |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | bool AssignExprStatementVisitor::VisitDoStmt(DoStmt *DS) |
| 159 | { |
| 160 | Expr *E = DS->getCond(); |
| 161 | handleSubExpr(E); |
| 162 | |
| 163 | Stmt *Body = DS->getBody(); |
| 164 | visitNonCompoundStmt(Body); |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | bool AssignExprStatementVisitor::VisitCallExpr(CallExpr *CallE) |
| 169 | { |
| 170 | if (!CurrentStmt) |
| 171 | return false; |
| 172 | |
| 173 | for (CallExpr::arg_iterator I = CallE->arg_begin(), |
| 174 | E = CallE->arg_end(); I != E; ++I) { |
| 175 | Expr *Exp = *I; |
| 176 | handleSubExpr(Exp); |
| 177 | } |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | void AssignExprStatementVisitor::handleSubExpr(Expr *E) |
| 182 | { |
| 183 | if (!E) |
| 184 | return; |
| 185 | |
| 186 | if (dyn_cast<AttributedStmt>(CurrentStmt)) |
| 187 | return; |
| 188 | |
| 189 | BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E->IgnoreParenCasts()); |
| 190 | if (!BinOp) { |
| 191 | TraverseStmt(E); |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | if (!BinOp->isAssignmentOp() && !BinOp->isCompoundAssignmentOp()) { |
| 196 | TraverseStmt(E); |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | TransAssert(std::find(ConsumerInstance->ValidAssignExprs.begin(), |
| 201 | ConsumerInstance->ValidAssignExprs.end(), BinOp) |
| 202 | == ConsumerInstance->ValidAssignExprs.end()); |
| 203 | |
| 204 | ConsumerInstance->ValidAssignExprs.push_back(BinOp); |
| 205 | ConsumerInstance->ValidInstanceNum++; |
| 206 | |
| 207 | if (ConsumerInstance->ValidInstanceNum != |
| 208 | ConsumerInstance->TransformationCounter) |
| 209 | return; |
| 210 | |
| 211 | ConsumerInstance->TheFuncDecl = CurrentFuncDecl; |
| 212 | ConsumerInstance->TheStmt = CurrentStmt; |
| 213 | ConsumerInstance->TheAssignExpr = BinOp; |
| 214 | ConsumerInstance->NeedParen = NeedParen; |
| 215 | } |
| 216 | |
| 217 | void LiftAssignmentExpr::Initialize(ASTContext &context) |
| 218 | { |
| 219 | Transformation::Initialize(context); |
| 220 | CollectionVisitor = new AssignExprCollectionVisitor(this); |
| 221 | StmtVisitor = new AssignExprStatementVisitor(this); |
| 222 | } |
| 223 | |
| 224 | bool LiftAssignmentExpr::HandleTopLevelDecl(DeclGroupRef D) |
| 225 | { |
| 226 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) { |
| 227 | CollectionVisitor->TraverseDecl(*I); |
| 228 | } |
| 229 | return true; |
| 230 | } |
| 231 | |
| 232 | void LiftAssignmentExpr::HandleTranslationUnit(ASTContext &Ctx) |
| 233 | { |
| 234 | if (QueryInstanceOnly) |
| 235 | return; |
| 236 | |
| 237 | if (TransformationCounter > ValidInstanceNum) { |
| 238 | TransError = TransMaxInstanceError; |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | Ctx.getDiagnostics().setSuppressAllDiagnostics(false); |
| 243 | |
| 244 | TransAssert(TheFuncDecl && "NULL TheFuncDecl!"); |
| 245 | TransAssert(TheStmt && "NULL TheStmt!"); |
| 246 | TransAssert(TheAssignExpr && "NULL TheAssignExpr"); |
| 247 | |
| 248 | TransAssert(TheAssignExpr->isAssignmentOp() || |
| 249 | TheAssignExpr->isCompoundAssignmentOp()); |
| 250 | addNewAssignStmt(); |
| 251 | replaceAssignExpr(); |
| 252 | |
| 253 | if (Ctx.getDiagnostics().hasErrorOccurred() || |
| 254 | Ctx.getDiagnostics().hasFatalErrorOccurred()) |
| 255 | TransError = TransInternalError; |
| 256 | } |
| 257 | |
| 258 | bool LiftAssignmentExpr::addNewAssignStmt(void) |
| 259 | { |
| 260 | std::string AssignStr(""); |
| 261 | RewriteHelper->getExprString(TheAssignExpr, AssignStr); |
| 262 | AssignStr += ";"; |
| 263 | return RewriteHelper->addStringBeforeStmt(TheStmt, AssignStr, NeedParen); |
| 264 | } |
| 265 | |
| 266 | bool LiftAssignmentExpr::replaceAssignExpr(void) |
| 267 | { |
| 268 | const Expr *Lhs = TheAssignExpr->getLHS(); |
| 269 | std::string LhsStr(""); |
| 270 | RewriteHelper->getExprString(Lhs, LhsStr); |
| 271 | return RewriteHelper->replaceExpr(TheAssignExpr, LhsStr); |
| 272 | } |
| 273 | |
| 274 | LiftAssignmentExpr::~LiftAssignmentExpr(void) |
| 275 | { |
| 276 | delete CollectionVisitor; |
| 277 | delete StmtVisitor; |
| 278 | } |