| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Copyright (c) 2012, 2013, 2014, 2015 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 "SimplifyCommaExpr.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 a comma expression. It transforms the following code: \n\ |
| 28 | (x, y); \n\ |
| 29 | to \n\ |
| 30 | x; \n\ |
| 31 | (y); \n"; |
| 32 | |
| 33 | static RegisterTransformation<SimplifyCommaExpr> |
| 34 | Trans("simplify-comma-expr", DescriptionMsg); |
| 35 | |
| 36 | class SimplifyCommaExprCollectionVisitor : public |
| 37 | RecursiveASTVisitor<SimplifyCommaExprCollectionVisitor> { |
| 38 | public: |
| 39 | |
| 40 | explicit SimplifyCommaExprCollectionVisitor(SimplifyCommaExpr *Instance) |
| 41 | : ConsumerInstance(Instance) |
| 42 | { } |
| 43 | |
| 44 | bool VisitFunctionDecl(FunctionDecl *FD); |
| 45 | |
| 46 | private: |
| 47 | SimplifyCommaExpr *ConsumerInstance; |
| 48 | }; |
| 49 | |
| 50 | class SimplifyCommaExprStmtVisitor : public |
| 51 | CommonStatementVisitor<SimplifyCommaExprStmtVisitor> { |
| 52 | public: |
| 53 | |
| 54 | explicit SimplifyCommaExprStmtVisitor(SimplifyCommaExpr *Instance) |
| 55 | : ConsumerInstance(Instance) |
| 56 | { } |
| 57 | |
| 58 | bool VisitForStmt(ForStmt *FS); |
| 59 | |
| 60 | bool VisitWhileStmt(WhileStmt *WS); |
| 61 | |
| 62 | bool VisitDoStmt(DoStmt *DS); |
| 63 | |
| 64 | bool VisitBinaryOperator(BinaryOperator *BO); |
| 65 | |
| 66 | private: |
| 67 | |
| 68 | SimplifyCommaExpr *ConsumerInstance; |
| 69 | }; |
| 70 | |
| 71 | bool SimplifyCommaExprCollectionVisitor::VisitFunctionDecl(FunctionDecl *FD) |
| 72 | { |
| 73 | if (ConsumerInstance->isInIncludedFile(FD) || |
| 74 | !FD->isThisDeclarationADefinition()) |
| 75 | return true; |
| 76 | |
| 77 | ConsumerInstance->StmtVisitor->TraverseDecl(FD); |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | // It causes unsound transformation because |
| 82 | // the semantics of loop execution has been changed. |
| 83 | // For example, |
| 84 | // int foo(int x) |
| 85 | // { |
| 86 | // int i; |
| 87 | // for(i = 0; i < bar(bar(x)); i++) |
| 88 | // ... |
| 89 | // } |
| 90 | // will be transformed to: |
| 91 | // int foo(int x) |
| 92 | // { |
| 93 | // int i; |
| 94 | // int tmp_var = bar(x); |
| 95 | // for(i = 0; i < bar(tmp_var); i++) |
| 96 | // ... |
| 97 | // } |
| 98 | bool SimplifyCommaExprStmtVisitor::VisitForStmt(ForStmt *FS) |
| 99 | { |
| 100 | Stmt *Body = FS->getBody(); |
| 101 | visitNonCompoundStmt(Body); |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | bool SimplifyCommaExprStmtVisitor::VisitWhileStmt(WhileStmt *WS) |
| 106 | { |
| 107 | Stmt *Body = WS->getBody(); |
| 108 | visitNonCompoundStmt(Body); |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | bool SimplifyCommaExprStmtVisitor::VisitDoStmt(DoStmt *DS) |
| 113 | { |
| 114 | Stmt *Body = DS->getBody(); |
| 115 | visitNonCompoundStmt(Body); |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | bool SimplifyCommaExprStmtVisitor::VisitBinaryOperator( |
| 120 | BinaryOperator *BO) |
| 121 | { |
| 122 | BinaryOperator::Opcode Op = BO->getOpcode(); |
| 123 | if (Op == clang::BO_Comma) { |
| 124 | ConsumerInstance->ValidInstanceNum++; |
| 125 | if (ConsumerInstance->ValidInstanceNum == |
| 126 | ConsumerInstance->TransformationCounter) { |
| 127 | ConsumerInstance->TheBinaryOperator = BO; |
| 128 | ConsumerInstance->TheStmt = CurrentStmt; |
| 129 | ConsumerInstance->NeedParen = NeedParen; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | Expr *LHS = BO->getLHS(); |
| 134 | TraverseStmt(LHS); |
| 135 | |
| 136 | Expr *RHS = BO->getRHS(); |
| 137 | TraverseStmt(RHS); |
| 138 | |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | void SimplifyCommaExpr::Initialize(ASTContext &context) |
| 143 | { |
| 144 | Transformation::Initialize(context); |
| 145 | CollectionVisitor = new SimplifyCommaExprCollectionVisitor(this); |
| 146 | StmtVisitor = new SimplifyCommaExprStmtVisitor(this); |
| 147 | } |
| 148 | |
| 149 | bool SimplifyCommaExpr::HandleTopLevelDecl(DeclGroupRef D) |
| 150 | { |
| 151 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) { |
| 152 | CollectionVisitor->TraverseDecl(*I); |
| 153 | } |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | void SimplifyCommaExpr::HandleTranslationUnit(ASTContext &Ctx) |
| 158 | { |
| 159 | if (QueryInstanceOnly) |
| 160 | return; |
| 161 | |
| 162 | if (TransformationCounter > ValidInstanceNum) { |
| 163 | TransError = TransMaxInstanceError; |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | Ctx.getDiagnostics().setSuppressAllDiagnostics(false); |
| 168 | |
| 169 | TransAssert(TheStmt && "NULL TheStmt!"); |
| 170 | TransAssert(TheBinaryOperator && "NULL TheBinaryOperator"); |
| 171 | |
| 172 | simplifyCommaExpr(); |
| 173 | |
| 174 | if (Ctx.getDiagnostics().hasErrorOccurred() || |
| 175 | Ctx.getDiagnostics().hasFatalErrorOccurred()) |
| 176 | TransError = TransInternalError; |
| 177 | } |
| 178 | |
| 179 | void SimplifyCommaExpr::simplifyCommaExpr(void) |
| 180 | { |
| 181 | TransAssert((TheBinaryOperator->getOpcode() == clang::BO_Comma) && |
| 182 | "Non Comma Operator!"); |
| 183 | const Expr *LHS = TheBinaryOperator->getLHS(); |
| 184 | std::string LHSStr; |
| 185 | RewriteHelper->getExprString(LHS, LHSStr); |
| 186 | |
| 187 | SourceRange LHSRange = LHS->getSourceRange(); |
| 188 | SourceLocation StartLoc = LHSRange.getBegin(); |
| 189 | SourceLocation EndLoc; |
| 190 | if (StartLoc.isMacroID()) { |
| 191 | StartLoc = SrcManager->getFileLoc(StartLoc); |
| 192 | EndLoc = LHSRange.getEnd(); |
| 193 | TransAssert(EndLoc.isMacroID() && "EndLoc is not from a macro!"); |
| 194 | LHSRange = SourceRange(StartLoc, SrcManager->getFileLoc(EndLoc)); |
| 195 | } |
| 196 | EndLoc = RewriteHelper->getEndLocationUntil(LHSRange, ','); |
| 197 | TheRewriter.RemoveText(SourceRange(StartLoc, EndLoc)); |
| 198 | |
| 199 | LHSStr += ";"; |
| 200 | RewriteHelper->addStringBeforeStmt(TheStmt, LHSStr, NeedParen); |
| 201 | } |
| 202 | |
| 203 | SimplifyCommaExpr::~SimplifyCommaExpr(void) |
| 204 | { |
| 205 | delete CollectionVisitor; |
| 206 | delete StmtVisitor; |
| 207 | } |
| 208 |