| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Copyright (c) 2012, 2013, 2015, 2017 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 "CombineLocalVarDecl.h" |
| 16 | |
| 17 | #include <algorithm> |
| 18 | |
| 19 | #include "clang/AST/RecursiveASTVisitor.h" |
| 20 | #include "clang/AST/ASTContext.h" |
| 21 | #include "clang/Basic/SourceManager.h" |
| 22 | |
| 23 | #include "TransformationManager.h" |
| 24 | |
| 25 | using namespace clang; |
| 26 | |
| 27 | static const char *DescriptionMsg = |
| 28 | "Combine local variable declarations with the same type. \ |
| 29 | This transformation records the location of the first \ |
| 30 | local variable declaration, and tries to combine other \ |
| 31 | declarations in the same DeclContext as the first one. \ |
| 32 | For example, for the following cod3: \n\ |
| 33 | int foo(void) { \n\ |
| 34 | int x; \n\ |
| 35 | int y; \n\ |
| 36 | if (...) { \n\ |
| 37 | int z; \n\ |
| 38 | } \n\ |
| 39 | } \n\ |
| 40 | We will only combine x and y, and won't touch z, \ |
| 41 | because z is not in the same DeclContext as x and y. \ |
| 42 | Note that this transformation is unsound for the same \ |
| 43 | reason as combine-global-var. \n"; |
| 44 | |
| 45 | static RegisterTransformation<CombineLocalVarDecl> |
| 46 | Trans("combine-local-var", DescriptionMsg); |
| 47 | |
| 48 | class CombLocalVarCollectionVisitor : |
| 49 | public RecursiveASTVisitor<CombLocalVarCollectionVisitor> { |
| 50 | |
| 51 | public: |
| 52 | |
| 53 | explicit CombLocalVarCollectionVisitor(CombineLocalVarDecl *Instance) |
| 54 | : ConsumerInstance(Instance) |
| 55 | { } |
| 56 | |
| 57 | bool VisitCompoundStmt(CompoundStmt *DS); |
| 58 | |
| 59 | private: |
| 60 | |
| 61 | const Type *getTypeFromDeclStmt(DeclStmt *DS); |
| 62 | |
| 63 | CombineLocalVarDecl *ConsumerInstance; |
| 64 | |
| 65 | }; |
| 66 | |
| 67 | const Type *CombLocalVarCollectionVisitor::getTypeFromDeclStmt(DeclStmt *DS) |
| 68 | { |
| 69 | Decl *D; |
| 70 | if (DS->isSingleDecl()) { |
| 71 | D = DS->getSingleDecl(); |
| 72 | } |
| 73 | else { |
| 74 | DeclGroupRef DGR = DS->getDeclGroup(); |
| 75 | DeclGroupRef::iterator I = DGR.begin(); |
| 76 | D = (*I); |
| 77 | } |
| 78 | |
| 79 | VarDecl *VD = dyn_cast<VarDecl>(D); |
| 80 | if (!VD) |
| 81 | return NULL; |
| 82 | |
| 83 | return VD->getType().getTypePtr(); |
| 84 | } |
| 85 | |
| 86 | bool CombLocalVarCollectionVisitor::VisitCompoundStmt(CompoundStmt *CS) |
| 87 | { |
| 88 | if (ConsumerInstance->isInIncludedFile(CS)) |
| 89 | return true; |
| 90 | |
| 91 | ConsumerInstance->DeclStmts.clear(); |
| 92 | |
| 93 | for (CompoundStmt::body_iterator I = CS->body_begin(), |
| 94 | E = CS->body_end(); I != E; ++I) { |
| 95 | |
| 96 | DeclStmt *DS = dyn_cast<DeclStmt>(*I); |
| 97 | if (!DS) |
| 98 | continue; |
| 99 | const Type *T = getTypeFromDeclStmt(DS); |
| 100 | if (!T) |
| 101 | continue; |
| 102 | |
| 103 | const Type *CanonicalT = |
| 104 | ConsumerInstance->Context->getCanonicalType(T); |
| 105 | llvm::DenseMap<const Type *, DeclStmt *>::iterator TI = |
| 106 | ConsumerInstance->DeclStmts.find(CanonicalT); |
| 107 | if (TI == ConsumerInstance->DeclStmts.end()) { |
| 108 | ConsumerInstance->DeclStmts[CanonicalT] = DS; |
| 109 | } |
| 110 | else { |
| 111 | ConsumerInstance->ValidInstanceNum++; |
| 112 | if (ConsumerInstance->ValidInstanceNum != |
| 113 | ConsumerInstance->TransformationCounter) |
| 114 | continue; |
| 115 | ConsumerInstance->TheDeclStmts.push_back((*TI).second); |
| 116 | ConsumerInstance->TheDeclStmts.push_back(DS); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | void CombineLocalVarDecl::Initialize(ASTContext &context) |
| 124 | { |
| 125 | Transformation::Initialize(context); |
| 126 | CollectionVisitor = new CombLocalVarCollectionVisitor(this); |
| 127 | } |
| 128 | |
| 129 | bool CombineLocalVarDecl::HandleTopLevelDecl(DeclGroupRef D) |
| 130 | { |
| 131 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) { |
| 132 | CollectionVisitor->TraverseDecl(*I); |
| 133 | } |
| 134 | return true; |
| 135 | } |
| 136 | |
| 137 | void CombineLocalVarDecl::HandleTranslationUnit(ASTContext &Ctx) |
| 138 | { |
| 139 | if (QueryInstanceOnly) { |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | if (TransformationCounter > ValidInstanceNum) { |
| 144 | TransError = TransMaxInstanceError; |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | Ctx.getDiagnostics().setSuppressAllDiagnostics(false); |
| 149 | |
| 150 | doCombination(); |
| 151 | |
| 152 | if (Ctx.getDiagnostics().hasErrorOccurred() || |
| 153 | Ctx.getDiagnostics().hasFatalErrorOccurred()) |
| 154 | TransError = TransInternalError; |
| 155 | } |
| 156 | |
| 157 | void CombineLocalVarDecl::doCombination(void) |
| 158 | { |
| 159 | TransAssert(TheDeclStmts.size() == 2); |
| 160 | DeclStmt *DS2 = TheDeclStmts.pop_back_val(); |
| 161 | DeclStmt *DS1 = TheDeclStmts.pop_back_val(); |
| 162 | |
| 163 | SourceLocation EndLoc = RewriteHelper->getDeclStmtEndLoc(DS1); |
| 164 | |
| 165 | std::string DStr; |
| 166 | RewriteHelper->getDeclStmtStrAndRemove(DS2, DStr); |
| 167 | TheRewriter.InsertText(EndLoc, ", " + DStr, /*InsertAfter=*/false); |
| 168 | } |
| 169 | |
| 170 | CombineLocalVarDecl::~CombineLocalVarDecl(void) |
| 171 | { |
| 172 | delete CollectionVisitor; |
| 173 | } |
| 174 |