Debian Codegraph @

clang_delta/ReduceArraySize.cpp

raw symbols SCIP
1 //===----------------------------------------------------------------------===//
2 //
3 // Copyright (c) 2012, 2014, 2015, 2017, 2018, 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 "ReduceArraySize.h"
16
17 #include <sstream>
18 #include <cctype>
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 "Reduce the size of an array to the maximum index of \
29 accessing this array. Each transformation iteration \
30 works on one dimension for multidimensional arrays. \
31 This transformation is legitimate for an array if: \n\
32 * this array is ConstantArrayType; \n\
33 * and all indeices to this array are constants. \n";
34
35 static RegisterTransformation<ReduceArraySize>
36 Trans("reduce-array-size", DescriptionMsg);
37
38 class ReduceArraySizeCollectionVisitor : public
39 RecursiveASTVisitor<ReduceArraySizeCollectionVisitor> {
40
41 public:
42
43 explicit ReduceArraySizeCollectionVisitor(ReduceArraySize *Instance)
44 : ConsumerInstance(Instance)
45 { }
46
47 bool VisitVarDecl(VarDecl *VD);
48
49 bool VisitArraySubscriptExpr(ArraySubscriptExpr *ASE);
50
51 private:
52
53 ReduceArraySize *ConsumerInstance;
54 };
55
56 bool ReduceArraySizeCollectionVisitor::VisitVarDecl(VarDecl *VD)
57 {
58 ConsumerInstance->handleOneVar(VD);
59 return true;
60 }
61
62 bool ReduceArraySizeCollectionVisitor::VisitArraySubscriptExpr(
63 ArraySubscriptExpr *ASE)
64 {
65 ConsumerInstance->handleOneASE(ASE);
66 return true;
67 }
68
69 void ReduceArraySize::Initialize(ASTContext &context)
70 {
71 Transformation::Initialize(context);
72 CollectionVisitor = new ReduceArraySizeCollectionVisitor(this);
73 }
74
75 bool ReduceArraySize::HandleTopLevelDecl(DeclGroupRef D)
76 {
77 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) {
78 CollectionVisitor->TraverseDecl(*I);
79 }
80 return true;
81 }
82
83 void ReduceArraySize::HandleTranslationUnit(ASTContext &Ctx)
84 {
85 doAnalysis();
86 if (QueryInstanceOnly)
87 return;
88
89 if (TransformationCounter > ValidInstanceNum) {
90 TransError = TransMaxInstanceError;
91 return;
92 }
93
94 TransAssert(CollectionVisitor && "NULL CollectionVisitor!");
95 Ctx.getDiagnostics().setSuppressAllDiagnostics(false);
96 TransAssert(TheVarDecl && "NULL TheVarDecl!");
97 TransAssert((TheDimValue >= 0) && "Bad TheDimValue!");
98
99 rewriteArrayVarDecl();
100
101 if (Ctx.getDiagnostics().hasErrorOccurred() ||
102 Ctx.getDiagnostics().hasFatalErrorOccurred())
103 TransError = TransInternalError;
104 }
105
106 void ReduceArraySize::doAnalysis(void)
107 {
108 for (VarDeclToDimMap::iterator I = VarDeclToDim.begin(),
109 E = VarDeclToDim.end(); I != E; ++I) {
110
111 const VarDecl *VD = (*I).first;
112 DimValueVector *DimVec = (*I).second;
113
114 if (!DimVec)
115 continue;
116
117 DimValueVector *OrigDimVec = OrigVarDeclToDim[VD];
118 TransAssert(OrigDimVec && "Null OrigDimVec!");
119
120 unsigned int DimSz = DimVec->size();
121 TransAssert((DimSz == OrigDimVec->size()) &&
122 "Two DimValueVectors should have the same size!");
123 for (unsigned int II = 0; II < DimSz; ++II) {
124 int DimV = (*DimVec)[II];
125 int OrigDimV = (*OrigDimVec)[II];
126 if ((DimV == -1) || (OrigDimV == 0) || ((DimV+1) == OrigDimV))
127 continue;
128
129 ValidInstanceNum++;
130 if (TransformationCounter != ValidInstanceNum)
131 continue;
132
133 TheVarDecl = VD;
134 TheDimValue = DimV;
135 TheDimIdx = II;
136 }
137 }
138 }
139
140 void ReduceArraySize::getBracketLocPair(const VarDecl *VD,
141 unsigned int Dim,
142 unsigned int DimIdx,
143 BracketLocPair &LocPair)
144 {
145 SourceLocation StartLoc = VD->getLocation();
146 SourceLocation BPStartLoc, BPEndLoc;
147 for (unsigned int I = 0; I < Dim; ++I) {
148 BPStartLoc = RewriteHelper->getLocationUntil(StartLoc, '[');
149 BPEndLoc = RewriteHelper->getLocationUntil(BPStartLoc, ']');
150
151 if (I == DimIdx) {
152 LocPair.first = BPStartLoc;
153 LocPair.second = BPEndLoc;
154 break;
155 }
156
157 StartLoc = BPEndLoc;
158 }
159 }
160
161 void ReduceArraySize::rewriteArrayVarDecl(void)
162 {
163 const Type *Ty = TheVarDecl->getType().getTypePtr();
164 const ArrayType *ArrayTy = dyn_cast<ArrayType>(Ty);
165 TransAssert(ArrayTy && "Invalid ArrayType!");
166 unsigned int Dim = getArrayDimension(ArrayTy);
167 TransAssert((Dim > TheDimIdx) && "Bad Dimension Index!");
168
169 const VarDecl *FirstVD = TheVarDecl->getCanonicalDecl();
170 for(VarDecl::redecl_iterator RI = FirstVD->redecls_begin(),
171 RE = FirstVD->redecls_end(); RI != RE; ++RI) {
172 BracketLocPair LocPair;
173 getBracketLocPair((*RI), Dim, TheDimIdx, LocPair);
174
175 std::stringstream TmpSS;
176 SourceLocation StartLoc = (LocPair.first).getLocWithOffset(1);
177 SourceLocation EndLoc = (LocPair.second).getLocWithOffset(-1);
178 TmpSS << (TheDimValue + 1);
179 TheRewriter.ReplaceText(SourceRange(StartLoc, EndLoc), TmpSS.str());
180 }
181 }
182
183 void ReduceArraySize::handleOneVar(const VarDecl *VD)
184 {
185 if (isInIncludedFile(VD))
186 return;
187
188 const Type *Ty = VD->getType().getTypePtr();
189 const ArrayType *ArrayTy = dyn_cast<ArrayType>(Ty);
190 if (!ArrayTy)
191 return;
192
193 const VarDecl *CanonicalVD = VD->getCanonicalDecl();
194 DimValueVector *DimVec = VarDeclToDim[CanonicalVD];
195 if (DimVec)
196 return;
197
198 ArraySubTypeVector TyVec;
199 unsigned int Dim = getArrayDimensionAndTypes(ArrayTy, TyVec);
200 DimVec = new DimValueVector(Dim, -1);
201 DimValueVector *OrigDimVec = new DimValueVector(Dim, -1);
202
203 TransAssert((Dim == TyVec.size()) &&
204 "Unmatched Dimension and Array Sub Types!");
205 for (unsigned int I = 0; I < Dim; ++I) {
206 const ConstantArrayType *CstArrayTy =
207 dyn_cast<ConstantArrayType>(TyVec[I]);
208 if (!CstArrayTy) {
209 continue;
210 }
211
212 unsigned int InitSz = getConstArraySize(CstArrayTy);
213 (*DimVec)[I] = 0;
214 (*OrigDimVec)[I] = InitSz;
215 }
216
217 VarDeclToDim[CanonicalVD] = DimVec;
218 OrigVarDeclToDim[CanonicalVD] = OrigDimVec;
219 }
220
221 bool ReduceArraySize::isIntegerExpr(const Expr *Exp)
222 {
223 const Expr *E = Exp->IgnoreParenCasts();
224
225 switch(E->getStmtClass()) {
226 case Expr::IntegerLiteralClass:
227 case Expr::CharacterLiteralClass: // Fall-through
228 return true;
229
230 default:
231 return false;
232 }
233 TransAssert(0 && "Unreachable code!");
234 return false;
235 }
236
237 void ReduceArraySize::handleOneASE(const ArraySubscriptExpr *ASE)
238 {
239 const Type *ASETy = ASE->getType().getTypePtr();
240 if (!ASETy->isScalarType() && !ASETy->isStructureType() &&
241 !ASETy->isUnionType())
242 return;
243
244 ExprVector IdxExprs;
245 const Expr *BaseE = getBaseExprAndIdxExprs(ASE, IdxExprs);
246 TransAssert(BaseE && "Empty Base expression!");
247
248 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseE);
249 if (!DRE)
250 return;
251
252 const ValueDecl *OrigDecl = DRE->getDecl();
253 const VarDecl *VD = dyn_cast<VarDecl>(OrigDecl);
254 if (!VD)
255 return;
256
257 const VarDecl *CanonicalVD = VD->getCanonicalDecl();
258 DimValueVector *DimVec = VarDeclToDim[CanonicalVD];
259 // It's possible DimVec is NULL, e.g.,
260 // int main(..., char *argv[]) {
261 // ... argv[1] ...
262 // }
263 if (!DimVec)
264 return;
265
266 TransAssert((DimVec->size() >= IdxExprs.size()) &&
267 "More indices than it should be!");
268
269 unsigned int DimIdx = 0;
270 for (ExprVector::reverse_iterator I = IdxExprs.rbegin(),
271 E = IdxExprs.rend(); I != E; ++I) {
272
273 int OldIdx = (*DimVec)[DimIdx];
274 if (OldIdx == -1) {
275 DimIdx++;
276 continue;
277 }
278
279 const Expr *IdxE = (*I);
280 if (isIntegerExpr(IdxE)) {
281 int Idx = getIndexAsInteger(IdxE);
282 if (Idx > OldIdx)
283 (*DimVec)[DimIdx] = Idx;
284 }
285 else {
286 (*DimVec)[DimIdx] = -1;
287 }
288 DimIdx++;
289 }
290 }
291
292 ReduceArraySize::~ReduceArraySize(void)
293 {
294 delete CollectionVisitor;
295
296 for (VarDeclToDimMap::iterator I = VarDeclToDim.begin(),
297 E = VarDeclToDim.end(); I != E; ++I) {
298 delete (*I).second;
299 }
300
301 for (VarDeclToDimMap::iterator I = OrigVarDeclToDim.begin(),
302 E = OrigVarDeclToDim.end(); I != E; ++I) {
303 delete (*I).second;
304 }
305 }
306