| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Copyright (c) 2012, 2013, 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 "BinOpSimplification.h" |
| 16 | |
| 17 | #include <sstream> |
| 18 | |
| 19 | #include "clang/AST/RecursiveASTVisitor.h" |
| 20 | #include "clang/AST/ASTContext.h" |
| 21 | #include "clang/Basic/SourceManager.h" |
| 22 | |
| 23 | #include "CommonStatementVisitor.h" |
| 24 | #include "TransformationManager.h" |
| 25 | |
| 26 | using namespace clang; |
| 27 | |
| 28 | static const char *DescriptionMsg = |
| 29 | "Simplify a complex binary expression to simple ones. \ |
| 30 | For example, x = a + (b + c) will be transformed to \ |
| 31 | tmp = b + c; x = a + tmp \n"; |
| 32 | |
| 33 | static RegisterTransformation<BinOpSimplification> |
| 34 | Trans("binop-simplification", DescriptionMsg); |
| 35 | |
| 36 | class BSCollectionVisitor : public RecursiveASTVisitor<BSCollectionVisitor> { |
| 37 | public: |
| 38 | |
| 39 | explicit BSCollectionVisitor(BinOpSimplification *Instance) |
| 40 | : ConsumerInstance(Instance) |
| 41 | { } |
| 42 | |
| 43 | bool VisitFunctionDecl(FunctionDecl *FD); |
| 44 | |
| 45 | private: |
| 46 | |
| 47 | BinOpSimplification *ConsumerInstance; |
| 48 | }; |
| 49 | |
| 50 | class BSStatementVisitor : public CommonStatementVisitor<BSStatementVisitor> { |
| 51 | public: |
| 52 | |
| 53 | explicit BSStatementVisitor(BinOpSimplification *Instance) |
| 54 | : ConsumerInstance(Instance) |
| 55 | { } |
| 56 | |
| 57 | bool VisitBinaryOperator(BinaryOperator *BinOp); |
| 58 | |
| 59 | private: |
| 60 | void handleSubExpr(Expr *E); |
| 61 | |
| 62 | BinOpSimplification *ConsumerInstance; |
| 63 | }; |
| 64 | |
| 65 | bool BSCollectionVisitor::VisitFunctionDecl(FunctionDecl *FD) |
| 66 | { |
| 67 | if (ConsumerInstance->isInIncludedFile(FD) || |
| 68 | !FD->isThisDeclarationADefinition()) |
| 69 | return true; |
| 70 | |
| 71 | ConsumerInstance->StmtVisitor->setCurrentFunctionDecl(FD); |
| 72 | ConsumerInstance->StmtVisitor->TraverseDecl(FD); |
| 73 | ConsumerInstance->StmtVisitor->setCurrentFunctionDecl(NULL); |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | void BSStatementVisitor::handleSubExpr(Expr *E) |
| 78 | { |
| 79 | BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E->IgnoreParenCasts()); |
| 80 | if (!BinOp) |
| 81 | return; |
| 82 | |
| 83 | TransAssert(std::find(ConsumerInstance->ValidBinOps.begin(), |
| 84 | ConsumerInstance->ValidBinOps.end(), BinOp) |
| 85 | == ConsumerInstance->ValidBinOps.end()); |
| 86 | |
| 87 | ConsumerInstance->ValidBinOps.push_back(BinOp); |
| 88 | ConsumerInstance->ValidInstanceNum++; |
| 89 | |
| 90 | if (ConsumerInstance->ValidInstanceNum == |
| 91 | ConsumerInstance->TransformationCounter) { |
| 92 | ConsumerInstance->TheFuncDecl = CurrentFuncDecl; |
| 93 | ConsumerInstance->TheStmt = CurrentStmt; |
| 94 | ConsumerInstance->TheBinOp = BinOp; |
| 95 | ConsumerInstance->NeedParen = NeedParen; |
| 96 | } |
| 97 | |
| 98 | TraverseStmt(BinOp); |
| 99 | } |
| 100 | |
| 101 | bool BSStatementVisitor::VisitBinaryOperator(BinaryOperator *BinOp) |
| 102 | { |
| 103 | if (BinOp->isAssignmentOp() && !BinOp->isCompoundAssignmentOp()) { |
| 104 | Expr *RHS = BinOp->getRHS(); |
| 105 | return TraverseStmt(RHS); |
| 106 | } |
| 107 | |
| 108 | Expr *LHS = BinOp->getLHS(); |
| 109 | handleSubExpr(LHS); |
| 110 | |
| 111 | Expr *RHS = BinOp->getRHS(); |
| 112 | handleSubExpr(RHS); |
| 113 | |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | void BinOpSimplification::Initialize(ASTContext &context) |
| 118 | { |
| 119 | Transformation::Initialize(context); |
| 120 | BinOpCollectionVisitor = new BSCollectionVisitor(this); |
| 121 | StmtVisitor = new BSStatementVisitor(this); |
| 122 | NameQueryWrap = |
| 123 | new TransNameQueryWrap(RewriteHelper->getTmpVarNamePrefix()); |
| 124 | } |
| 125 | |
| 126 | bool BinOpSimplification::HandleTopLevelDecl(DeclGroupRef D) |
| 127 | { |
| 128 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) { |
| 129 | BinOpCollectionVisitor->TraverseDecl(*I); |
| 130 | } |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | void BinOpSimplification::HandleTranslationUnit(ASTContext &Ctx) |
| 135 | { |
| 136 | if (QueryInstanceOnly) |
| 137 | return; |
| 138 | |
| 139 | if (TransformationCounter > ValidInstanceNum) { |
| 140 | TransError = TransMaxInstanceError; |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | Ctx.getDiagnostics().setSuppressAllDiagnostics(false); |
| 145 | |
| 146 | TransAssert(TheFuncDecl && "NULL TheFuncDecl!"); |
| 147 | TransAssert(TheStmt && "NULL TheStmt!"); |
| 148 | TransAssert(TheBinOp && "NULL TheBinOp"); |
| 149 | |
| 150 | NameQueryWrap->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 151 | addNewTmpVariable(); |
| 152 | addNewAssignStmt(); |
| 153 | replaceBinOp(); |
| 154 | |
| 155 | if (Ctx.getDiagnostics().hasErrorOccurred() || |
| 156 | Ctx.getDiagnostics().hasFatalErrorOccurred()) |
| 157 | TransError = TransInternalError; |
| 158 | } |
| 159 | |
| 160 | bool BinOpSimplification::addNewTmpVariable(void) |
| 161 | { |
| 162 | QualType QT = TheBinOp->getType(); |
| 163 | std::string VarStr; |
| 164 | std::stringstream SS; |
| 165 | unsigned int NamePostfix = NameQueryWrap->getMaxNamePostfix(); |
| 166 | |
| 167 | SS << RewriteHelper->getTmpVarNamePrefix() << (NamePostfix + 1); |
| 168 | VarStr = SS.str(); |
| 169 | setTmpVarName(VarStr); |
| 170 | |
| 171 | QT.getAsStringInternal(VarStr, |
| 172 | Context->getPrintingPolicy()); |
| 173 | |
| 174 | VarStr += ";"; |
| 175 | return RewriteHelper->addLocalVarToFunc(VarStr, TheFuncDecl); |
| 176 | } |
| 177 | |
| 178 | bool BinOpSimplification::addNewAssignStmt(void) |
| 179 | { |
| 180 | return RewriteHelper->addNewAssignStmtBefore(TheStmt, |
| 181 | getTmpVarName(), |
| 182 | TheBinOp, |
| 183 | NeedParen); |
| 184 | } |
| 185 | |
| 186 | bool BinOpSimplification::replaceBinOp(void) |
| 187 | { |
| 188 | return RewriteHelper->replaceExpr(TheBinOp, TmpVarName); |
| 189 | } |
| 190 | |
| 191 | BinOpSimplification::~BinOpSimplification(void) |
| 192 | { |
| 193 | delete BinOpCollectionVisitor; |
| 194 | delete StmtVisitor; |
| 195 | delete NameQueryWrap; |
| 196 | } |