| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Copyright (c) 2012, 2013, 2014, 2015, 2017, 2020 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 "RemoveArray.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 | |
| 23 | using namespace clang; |
| 24 | |
| 25 | static const char *DescriptionMsg = |
| 26 | "Change an array var to a corresponding non-array one. \ |
| 27 | For example, replace \"int a[10];\" with \"int a;\". Conditions \n\ |
| 28 | * Array dimension is 1, and \n\ |
| 29 | * array var is not referenced except via ArraySubscriptExpr. \n\ |
| 30 | All relevant ArraySubscriptExpr[s] will be rewritten accordingly. \n"; |
| 31 | |
| 32 | static RegisterTransformation<RemoveArray> |
| 33 | Trans("remove-array", DescriptionMsg); |
| 34 | |
| 35 | typedef llvm::SmallPtrSet<const clang::ArraySubscriptExpr *, 5> |
| 36 | ArraySubscriptExprSet; |
| 37 | |
| 38 | class InvalidArraySubscriptExprVisitor : public |
| 39 | RecursiveASTVisitor<InvalidArraySubscriptExprVisitor> { |
| 40 | |
| 41 | public: |
| 42 | |
| 43 | InvalidArraySubscriptExprVisitor(RemoveArray *Instance, |
| 44 | const VarDecl *VD, |
| 45 | ArraySubscriptExprSet &ES) |
| 46 | : ConsumerInstance(Instance), |
| 47 | TheVarDecl(VD), |
| 48 | InvalidExprs(ES) |
| 49 | { } |
| 50 | |
| 51 | bool VisitArraySubscriptExpr(ArraySubscriptExpr *ASE); |
| 52 | |
| 53 | private: |
| 54 | |
| 55 | RemoveArray *ConsumerInstance; |
| 56 | |
| 57 | const VarDecl *TheVarDecl; |
| 58 | |
| 59 | ArraySubscriptExprSet &InvalidExprs; |
| 60 | }; |
| 61 | |
| 62 | bool InvalidArraySubscriptExprVisitor::VisitArraySubscriptExpr( |
| 63 | ArraySubscriptExpr *ASE) |
| 64 | { |
| 65 | const VarDecl *VD = ConsumerInstance->getVarDeclFromArraySubscriptExpr(ASE); |
| 66 | if (VD == TheVarDecl) |
| 67 | InvalidExprs.insert(ASE); |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | class RemoveArrayCollectionVisitor : public |
| 72 | RecursiveASTVisitor<RemoveArrayCollectionVisitor> { |
| 73 | |
| 74 | public: |
| 75 | |
| 76 | explicit RemoveArrayCollectionVisitor(RemoveArray *Instance) |
| 77 | : ConsumerInstance(Instance), |
| 78 | CurrDeclRefExpr(NULL) |
| 79 | { } |
| 80 | |
| 81 | bool VisitVarDecl(VarDecl *VD); |
| 82 | |
| 83 | bool VisitArraySubscriptExpr(ArraySubscriptExpr *ASE); |
| 84 | |
| 85 | bool VisitDeclRefExpr(DeclRefExpr *DRE); |
| 86 | |
| 87 | private: |
| 88 | |
| 89 | RemoveArray *ConsumerInstance; |
| 90 | |
| 91 | const DeclRefExpr *CurrDeclRefExpr; |
| 92 | }; |
| 93 | |
| 94 | bool RemoveArrayCollectionVisitor::VisitVarDecl(VarDecl *VD) |
| 95 | { |
| 96 | ConsumerInstance->handleOneVarDecl(VD); |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | bool RemoveArrayCollectionVisitor::VisitArraySubscriptExpr( |
| 101 | ArraySubscriptExpr *ASE) |
| 102 | { |
| 103 | // we only have one-dimension array, so we are safe here. |
| 104 | const Expr *BaseE = ASE->getBase()->IgnoreParenCasts(); |
| 105 | |
| 106 | TransAssert(BaseE && "Empty Base expression!"); |
| 107 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseE); |
| 108 | if (!DRE) |
| 109 | return true; |
| 110 | |
| 111 | CurrDeclRefExpr = DRE; |
| 112 | ConsumerInstance->addOneArraySubscriptExpr(ASE, DRE); |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | bool RemoveArrayCollectionVisitor::VisitDeclRefExpr(DeclRefExpr *DRE) |
| 117 | { |
| 118 | if (CurrDeclRefExpr != DRE) |
| 119 | ConsumerInstance->deleteOneVarDecl(DRE); |
| 120 | |
| 121 | CurrDeclRefExpr = NULL; |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | void RemoveArray::Initialize(ASTContext &context) |
| 126 | { |
| 127 | Transformation::Initialize(context); |
| 128 | CollectionVisitor = new RemoveArrayCollectionVisitor(this); |
| 129 | } |
| 130 | |
| 131 | void RemoveArray::HandleTranslationUnit(ASTContext &Ctx) |
| 132 | { |
| 133 | TransAssert(CollectionVisitor && "NULL CollectionVisitor!"); |
| 134 | CollectionVisitor->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 135 | doAnalysis(); |
| 136 | |
| 137 | if (QueryInstanceOnly) |
| 138 | return; |
| 139 | |
| 140 | if (TransformationCounter > ValidInstanceNum) { |
| 141 | TransError = TransMaxInstanceError; |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | Ctx.getDiagnostics().setSuppressAllDiagnostics(false); |
| 146 | TransAssert(TheArrayVarDecl && "NULL TheArrayVarDecl!"); |
| 147 | |
| 148 | doRewriting(); |
| 149 | |
| 150 | if (Ctx.getDiagnostics().hasErrorOccurred() || |
| 151 | Ctx.getDiagnostics().hasFatalErrorOccurred()) |
| 152 | TransError = TransInternalError; |
| 153 | } |
| 154 | |
| 155 | void RemoveArray::doAnalysis(void) |
| 156 | { |
| 157 | for (VarDeclToArraySubscriptExprMap::iterator I = ValidVarToASEMap.begin(), |
| 158 | E = ValidVarToASEMap.end(); I != E; ++I) { |
| 159 | if (!((*I).second)) |
| 160 | continue; |
| 161 | ValidInstanceNum++; |
| 162 | if (TransformationCounter == ValidInstanceNum) { |
| 163 | TheArrayVarDecl = (*I).first; |
| 164 | TheASEVec = (*I).second; |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | const VarDecl * |
| 170 | RemoveArray::getVarDeclFromArraySubscriptExpr(const ArraySubscriptExpr *ASE) |
| 171 | { |
| 172 | const Expr *BaseE = ASE->getBase()->IgnoreParenCasts(); |
| 173 | TransAssert(BaseE && "Empty Base expression!"); |
| 174 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseE); |
| 175 | if (!DRE) |
| 176 | return NULL; |
| 177 | const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()); |
| 178 | if (!VD) |
| 179 | return NULL; |
| 180 | return VD->getCanonicalDecl(); |
| 181 | } |
| 182 | |
| 183 | void RemoveArray::getBracketLocPair(const VarDecl *VD, |
| 184 | BracketLocPair &LocPair) |
| 185 | { |
| 186 | SourceLocation StartLoc = VD->getLocation(); |
| 187 | SourceLocation BPStartLoc, BPEndLoc; |
| 188 | BPStartLoc = RewriteHelper->getLocationUntil(StartLoc, '['); |
| 189 | BPEndLoc = RewriteHelper->getLocationUntil(BPStartLoc, ']'); |
| 190 | |
| 191 | LocPair.first = BPStartLoc; |
| 192 | LocPair.second = BPEndLoc; |
| 193 | } |
| 194 | |
| 195 | void RemoveArray::doRewriting(void) |
| 196 | { |
| 197 | // rewrite decls first |
| 198 | const VarDecl *FirstVD = TheArrayVarDecl->getCanonicalDecl(); |
| 199 | for(VarDecl::redecl_iterator RI = FirstVD->redecls_begin(), |
| 200 | RE = FirstVD->redecls_end(); RI != RE; ++RI) { |
| 201 | |
| 202 | BracketLocPair LocPair; |
| 203 | getBracketLocPair((*RI), LocPair); |
| 204 | TheRewriter.RemoveText(SourceRange(LocPair.first, LocPair.second)); |
| 205 | } |
| 206 | |
| 207 | // Then rewrite ArraySubscriptExprs |
| 208 | if (!TheASEVec) |
| 209 | return; |
| 210 | |
| 211 | // filter out inner subscript that refers to the same var as the outer |
| 212 | // expr, e.g.: |
| 213 | // x[x[0]] |
| 214 | // we don't need to rewrite x[0] in this case |
| 215 | ArraySubscriptExprSet InvalidExprs; |
| 216 | for (ArraySubscriptExprVector::iterator I = TheASEVec->begin(), |
| 217 | E = TheASEVec->end(); I != E; ++I) { |
| 218 | ArraySubscriptExpr *ASE = (*I); |
| 219 | if (InvalidExprs.count(ASE)) |
| 220 | continue; |
| 221 | Expr *IdxE = ASE->getIdx(); |
| 222 | if (!IdxE) |
| 223 | continue; |
| 224 | const VarDecl *VD = getVarDeclFromArraySubscriptExpr(ASE); |
| 225 | TransAssert(VD && "NULL VarDecl from ArraySubscriptExpr!"); |
| 226 | InvalidArraySubscriptExprVisitor V(this, VD, InvalidExprs); |
| 227 | V.TraverseStmt(IdxE); |
| 228 | } |
| 229 | |
| 230 | for (ArraySubscriptExprVector::iterator I = TheASEVec->begin(), |
| 231 | E = TheASEVec->end(); I != E; ++I) { |
| 232 | if (InvalidExprs.count(*I)) |
| 233 | continue; |
| 234 | const Expr *IdxE = (*I)->getIdx(); |
| 235 | RewriteHelper->removeArraySubscriptExpr(IdxE); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | void RemoveArray::deleteOneVarDecl(const DeclRefExpr *DRE) |
| 240 | { |
| 241 | const ValueDecl *OrigDecl = DRE->getDecl(); |
| 242 | const VarDecl *VD = dyn_cast<VarDecl>(OrigDecl); |
| 243 | if (!VD) |
| 244 | return; |
| 245 | const VarDecl *CanonicalVD = VD->getCanonicalDecl(); |
| 246 | ArraySubscriptExprVector *ASEVec = ValidVarToASEMap[CanonicalVD]; |
| 247 | if (ASEVec) { |
| 248 | delete ASEVec; |
| 249 | ValidVarToASEMap[CanonicalVD] = NULL; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | void RemoveArray::addOneArraySubscriptExpr(ArraySubscriptExpr *ASE, |
| 254 | const DeclRefExpr *DRE) |
| 255 | { |
| 256 | const ValueDecl *OrigDecl = DRE->getDecl(); |
| 257 | const VarDecl *VD = dyn_cast<VarDecl>(OrigDecl); |
| 258 | if (!VD) |
| 259 | return; |
| 260 | |
| 261 | const VarDecl *CanonicalVD = VD->getCanonicalDecl(); |
| 262 | ArraySubscriptExprVector *ASEVec = ValidVarToASEMap[CanonicalVD]; |
| 263 | if (ASEVec) |
| 264 | ASEVec->push_back(ASE); |
| 265 | } |
| 266 | |
| 267 | void RemoveArray::handleOneVarDecl(const VarDecl *VD) |
| 268 | { |
| 269 | if (isInIncludedFile(VD) || VD->getAnyInitializer()) |
| 270 | return; |
| 271 | |
| 272 | const Type *Ty = VD->getType().getTypePtr(); |
| 273 | const ArrayType *ArrayTy = dyn_cast<ArrayType>(Ty); |
| 274 | if (!ArrayTy) |
| 275 | return; |
| 276 | |
| 277 | unsigned Dim = getArrayDimension(ArrayTy); |
| 278 | if (Dim != 1) |
| 279 | return; |
| 280 | |
| 281 | const VarDecl *CanonicalVD = VD->getCanonicalDecl(); |
| 282 | ArraySubscriptExprVector *ASEVec = ValidVarToASEMap[CanonicalVD]; |
| 283 | if (ASEVec) |
| 284 | return; |
| 285 | |
| 286 | ASEVec = new ArraySubscriptExprVector(); |
| 287 | ValidVarToASEMap[CanonicalVD] = ASEVec; |
| 288 | } |
| 289 | |
| 290 | RemoveArray::~RemoveArray(void) |
| 291 | { |
| 292 | delete CollectionVisitor; |
| 293 | |
| 294 | for (VarDeclToArraySubscriptExprMap::iterator I = ValidVarToASEMap.begin(), |
| 295 | E = ValidVarToASEMap.end(); I != E; ++I) { |
| 296 | ArraySubscriptExprVector *ASEVec = (*I).second; |
| 297 | if (ASEVec) |
| 298 | delete ASEVec; |
| 299 | } |
| 300 | } |