| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Copyright (c) 2012, 2013, 2015, 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 "SimplifyIf.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 | "Simplify an if-else statement. It transforms the following code: \n\ |
| 28 | if (guard1) \n\ |
| 29 | {... } \n\ |
| 30 | else if (guard2) \n\ |
| 31 | else \n\ |
| 32 | {...} \n\ |
| 33 | to \n\ |
| 34 | (guard1) \n\ |
| 35 | {... } \n\ |
| 36 | if (guard2) \n\ |
| 37 | else \n\ |
| 38 | {...} \n\ |
| 39 | if there is no else-if left, the last else keyword will be removed. \n"; |
| 40 | |
| 41 | class SimplifyIfCollectionVisitor : public |
| 42 | RecursiveASTVisitor<SimplifyIfCollectionVisitor> { |
| 43 | public: |
| 44 | |
| 45 | explicit SimplifyIfCollectionVisitor(SimplifyIf *Instance) |
| 46 | : ConsumerInstance(Instance) |
| 47 | { } |
| 48 | |
| 49 | bool VisitFunctionDecl(FunctionDecl *FD); |
| 50 | |
| 51 | private: |
| 52 | SimplifyIf *ConsumerInstance; |
| 53 | |
| 54 | }; |
| 55 | |
| 56 | static RegisterTransformation<SimplifyIf> |
| 57 | Trans("simplify-if", DescriptionMsg); |
| 58 | |
| 59 | class SimplifyIfStatementVisitor : public |
| 60 | CommonStatementVisitor<SimplifyIfStatementVisitor> { |
| 61 | public: |
| 62 | |
| 63 | explicit SimplifyIfStatementVisitor(SimplifyIf *Instance) |
| 64 | : ConsumerInstance(Instance) |
| 65 | { } |
| 66 | |
| 67 | bool VisitIfStmt(IfStmt *IS); |
| 68 | |
| 69 | bool VisitForStmt(ForStmt *FS); |
| 70 | |
| 71 | bool VisitWhileStmt(WhileStmt *WS); |
| 72 | |
| 73 | bool VisitDoStmt(DoStmt *DS); |
| 74 | |
| 75 | private: |
| 76 | |
| 77 | SimplifyIf *ConsumerInstance; |
| 78 | |
| 79 | }; |
| 80 | |
| 81 | bool SimplifyIfCollectionVisitor::VisitFunctionDecl(FunctionDecl *FD) |
| 82 | { |
| 83 | if (ConsumerInstance->isInIncludedFile(FD) || |
| 84 | !FD->isThisDeclarationADefinition()) |
| 85 | return true; |
| 86 | |
| 87 | ConsumerInstance->StmtVisitor->TraverseDecl(FD); |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | // It is used to handle the case where if-then or else branch |
| 92 | // is not treated as a CompoundStmt. So it cannot be traversed |
| 93 | // from VisitCompoundStmt, e.g., |
| 94 | // if (x) |
| 95 | // foo(bar()) |
| 96 | bool SimplifyIfStatementVisitor::VisitIfStmt(IfStmt *IS) |
| 97 | { |
| 98 | if (IS->getBeginLoc().isMacroID()) { |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | ConsumerInstance->ValidInstanceNum++; |
| 103 | if (ConsumerInstance->ValidInstanceNum == |
| 104 | ConsumerInstance->TransformationCounter) { |
| 105 | ConsumerInstance->TheIfStmt = IS; |
| 106 | ConsumerInstance->NeedParen = NeedParen; |
| 107 | } |
| 108 | |
| 109 | Stmt *ThenB = IS->getThen(); |
| 110 | visitNonCompoundStmt(ThenB); |
| 111 | |
| 112 | Stmt *ElseB = IS->getElse(); |
| 113 | visitNonCompoundStmt(ElseB); |
| 114 | |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | // It causes unsound transformation because |
| 119 | // the semantics of loop execution has been changed. |
| 120 | // For example, |
| 121 | // int foo(int x) |
| 122 | // { |
| 123 | // int i; |
| 124 | // for(i = 0; i < bar(bar(x)); i++) |
| 125 | // ... |
| 126 | // } |
| 127 | // will be transformed to: |
| 128 | // int foo(int x) |
| 129 | // { |
| 130 | // int i; |
| 131 | // int tmp_var = bar(x); |
| 132 | // for(i = 0; i < bar(tmp_var); i++) |
| 133 | // ... |
| 134 | // } |
| 135 | bool SimplifyIfStatementVisitor::VisitForStmt(ForStmt *FS) |
| 136 | { |
| 137 | Stmt *Body = FS->getBody(); |
| 138 | visitNonCompoundStmt(Body); |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | bool SimplifyIfStatementVisitor::VisitWhileStmt(WhileStmt *WS) |
| 143 | { |
| 144 | Stmt *Body = WS->getBody(); |
| 145 | visitNonCompoundStmt(Body); |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | bool SimplifyIfStatementVisitor::VisitDoStmt(DoStmt *DS) |
| 150 | { |
| 151 | Stmt *Body = DS->getBody(); |
| 152 | visitNonCompoundStmt(Body); |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | void SimplifyIf::Initialize(ASTContext &context) |
| 157 | { |
| 158 | Transformation::Initialize(context); |
| 159 | CollectionVisitor = new SimplifyIfCollectionVisitor(this); |
| 160 | StmtVisitor = new SimplifyIfStatementVisitor(this); |
| 161 | } |
| 162 | |
| 163 | bool SimplifyIf::HandleTopLevelDecl(DeclGroupRef D) |
| 164 | { |
| 165 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) { |
| 166 | CollectionVisitor->TraverseDecl(*I); |
| 167 | } |
| 168 | return true; |
| 169 | } |
| 170 | |
| 171 | void SimplifyIf::HandleTranslationUnit(ASTContext &Ctx) |
| 172 | { |
| 173 | if (QueryInstanceOnly) |
| 174 | return; |
| 175 | |
| 176 | if (TransformationCounter > ValidInstanceNum) { |
| 177 | TransError = TransMaxInstanceError; |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | Ctx.getDiagnostics().setSuppressAllDiagnostics(false); |
| 182 | |
| 183 | TransAssert(TheIfStmt && "NULL TheIfStmt"); |
| 184 | |
| 185 | simplifyIfStmt(); |
| 186 | |
| 187 | if (Ctx.getDiagnostics().hasErrorOccurred() || |
| 188 | Ctx.getDiagnostics().hasFatalErrorOccurred()) |
| 189 | TransError = TransInternalError; |
| 190 | } |
| 191 | |
| 192 | void SimplifyIf::simplifyIfStmt(void) |
| 193 | { |
| 194 | const Expr *Cond = TheIfStmt->getCond(); |
| 195 | TransAssert(Cond && "Bad Cond Expr!"); |
| 196 | std::string CondStr; |
| 197 | RewriteHelper->getExprString(Cond, CondStr); |
| 198 | CondStr += ";"; |
| 199 | RewriteHelper->addStringBeforeStmt(TheIfStmt, CondStr, NeedParen); |
| 200 | |
| 201 | RewriteHelper->removeIfAndCond(TheIfStmt); |
| 202 | |
| 203 | const Stmt *ElseS = TheIfStmt->getElse(); |
| 204 | if (ElseS) { |
| 205 | SourceLocation ElseLoc = |
| 206 | RewriteHelper->getRealLocation(TheIfStmt->getElseLoc()); |
| 207 | std::string ElseStr = "else"; |
| 208 | TheRewriter.RemoveText(ElseLoc, ElseStr.size()); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | SimplifyIf::~SimplifyIf(void) |
| 213 | { |
| 214 | delete CollectionVisitor; |
| 215 | delete StmtVisitor; |
| 216 | } |
| 217 |