Debian Codegraph @

clang_delta/ParamToLocal.cpp

raw symbols SCIP
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 "ParamToLocal.h"
16
17 #include "clang/AST/ASTContext.h"
18 #include "clang/Basic/SourceManager.h"
19
20 #include "TransformationManager.h"
21
22 using namespace clang;
23
24 static const char *DescriptionMsg =
25 "Remove an integaral/enumeration parameter from the declaration \
26 of a function. Define the removed parameter as a local variable \
27 of the same function. Initialze the newly local variable to be \
28 0. Also, make corresponding changes on all of the call sites of \
29 the modified function.\n";
30
31 static RegisterTransformation<ParamToLocal>
32 Trans("param-to-local", DescriptionMsg);
33
34 class ParamToLocalASTVisitor : public
35 RecursiveASTVisitor<ParamToLocalASTVisitor> {
36
37 public:
38 explicit ParamToLocalASTVisitor(ParamToLocal *Instance)
39 : ConsumerInstance(Instance)
40 { }
41
42 bool VisitFunctionDecl(FunctionDecl *FD);
43
44 private:
45 ParamToLocal *ConsumerInstance;
46 };
47
48 class ParamToLocalRewriteVisitor : public
49 CommonParameterRewriteVisitor<ParamToLocalRewriteVisitor, ParamToLocal> {
50
51 public:
52 explicit ParamToLocalRewriteVisitor(ParamToLocal *Instance)
53 : CommonParameterRewriteVisitor<ParamToLocalRewriteVisitor,
54 ParamToLocal>(Instance)
55 { }
56 };
57
58 bool ParamToLocalASTVisitor::VisitFunctionDecl(FunctionDecl *FD)
59 {
60 if (ConsumerInstance->isValidFuncDecl(FD->getCanonicalDecl())) {
61 ConsumerInstance->ValidFuncDecls.push_back(FD->getCanonicalDecl());
62 }
63 return true;
64 }
65
66 void ParamToLocal::Initialize(ASTContext &context)
67 {
68 Transformation::Initialize(context);
69 CollectionVisitor = new ParamToLocalASTVisitor(this);
70 RewriteVisitor = new ParamToLocalRewriteVisitor(this);
71 }
72
73 void ParamToLocal::HandleTranslationUnit(ASTContext &Ctx)
74 {
75 CollectionVisitor->TraverseDecl(Ctx.getTranslationUnitDecl());
76 if (QueryInstanceOnly)
77 return;
78
79 if (TransformationCounter > ValidInstanceNum) {
80 TransError = TransMaxInstanceError;
81 return;
82 }
83
84 TransAssert(RewriteVisitor && "NULL RewriteVisitor!");
85 Ctx.getDiagnostics().setSuppressAllDiagnostics(false);
86 TransAssert(TheFuncDecl && "NULL TheFuncDecl!");
87 TransAssert((TheParamPos >= 0) && "Invalid parameter position!");
88
89 RewriteVisitor->TraverseDecl(Ctx.getTranslationUnitDecl());
90 RewriteVisitor->rewriteAllExprs();
91
92 if (Ctx.getDiagnostics().hasErrorOccurred() ||
93 Ctx.getDiagnostics().hasFatalErrorOccurred())
94 TransError = TransInternalError;
95 }
96
97 // For CXX, this function could generate bad code, e.g.,
98 // class A {
99 // void foo(void) {...}
100 // void foo(int x) {...}
101 // };
102 //
103 // will be transformed to
104 // class A {
105 // void foo(void) {...}
106 // void foo(void) {int x ...}
107 // };
108 bool ParamToLocal::rewriteFuncDecl(FunctionDecl *FD)
109 {
110 const ParmVarDecl *PV = FD->getParamDecl(TheParamPos);
111
112 TransAssert(PV && "Unmatched ParamPos!");
113 RewriteHelper->removeParamFromFuncDecl(PV,
114 FD->getNumParams(),
115 TheParamPos);
116
117 if (FD->isThisDeclarationADefinition()) {
118 if (!transformParamVar(FD, PV))
119 return false;
120 }
121 return true;
122 }
123
124 // ISSUE: we could have another type of bad transformation, e.g.,
125 // class A { ... };
126 // class B : public A {
127 // B(int x) : A(x) {...}
128 // };
129 // ==>
130 // class A { ... };
131 // class B : public A {
132 // B(void) : A(x) {int x ...}
133 // };
134 // hence x is undeclared for A(x)
135 bool ParamToLocal::transformParamVar(FunctionDecl *FD,
136 const ParmVarDecl *PV)
137 {
138 std::string PName = PV->getNameAsString();
139 // Safe to omit an un-named parameter
140 if (PName.empty())
141 return true;
142
143 std::string LocalVarStr;
144
145 LocalVarStr += PV->getType().getAsString();
146 LocalVarStr += " ";
147 LocalVarStr += PV->getNameAsString();
148
149 QualType PVType = PV->getOriginalType();
150 const Type *T = PVType.getTypePtr();
151 if ( const Expr *DefaultArgE = PV->getDefaultArg() ) {
152 std::string ArgStr;
153 RewriteHelper->getExprString(DefaultArgE, ArgStr);
154 LocalVarStr += " = ";
155 LocalVarStr += ArgStr;
156 }
157 else if (T->isPointerType() || T->isIntegralType(*Context)) {
158 LocalVarStr += " = 0";
159 }
160 LocalVarStr += ";";
161
162 return RewriteHelper->addLocalVarToFunc(LocalVarStr, FD);
163 }
164
165 bool ParamToLocal::isValidFuncDecl(FunctionDecl *FD)
166 {
167 bool IsValid = false;
168 int ParamPos = 0;
169
170 TransAssert(isa<FunctionDecl>(FD) && "Must be a FunctionDecl");
171
172 if (isInIncludedFile(FD))
173 return false;
174
175 // Skip the case like foo(int, ...), because we cannot remove
176 // the "int" there
177 if (FD->isVariadic() && (FD->getNumParams() == 1)) {
178 return false;
179 }
180
181 if (FD->isOverloadedOperator())
182 return false;
183
184 // Avoid duplications
185 if (std::find(ValidFuncDecls.begin(),
186 ValidFuncDecls.end(), FD) !=
187 ValidFuncDecls.end())
188 return false;
189
190 for (FunctionDecl::param_const_iterator PI = FD->param_begin(),
191 PE = FD->param_end(); PI != PE; ++PI) {
192 if ((*PI)->isImplicit() || (*PI)->getSourceRange().isInvalid())
193 continue;
194 if (FD->isDeleted() || FD->isDefaulted())
195 continue;
196 if (!FD->hasBody() && (*PI)->getNameAsString().empty())
197 continue;
198
199 ValidInstanceNum++;
200
201 if (ValidInstanceNum == TransformationCounter) {
202 TheFuncDecl = FD;
203 TheParamPos = ParamPos;
204 }
205
206 IsValid = true;
207 ParamPos++;
208 }
209 return IsValid;
210 }
211
212 ParamToLocal::~ParamToLocal(void)
213 {
214 delete CollectionVisitor;
215 delete RewriteVisitor;
216 }
217