Debian Codegraph @

clang_delta/ParamToGlobal.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 "ParamToGlobal.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 "Lift the declaraion of a parameter from a function \
26 to the global scope. Also rename the lifted parameter \
27 to avoid possible name conflicts. Note that for C++ code, \
28 this pass actually lifts a parameter from a member function \
29 to the class scope rather than the global scope. \n";
30
31 static RegisterTransformation<ParamToGlobal>
32 Trans("param-to-global", DescriptionMsg);
33
34 class ParamToGlobalASTVisitor : public
35 RecursiveASTVisitor<ParamToGlobalASTVisitor> {
36
37 public:
38 explicit ParamToGlobalASTVisitor(ParamToGlobal *Instance)
39 : ConsumerInstance(Instance)
40 { }
41
42 bool VisitFunctionDecl(FunctionDecl *FD);
43
44 private:
45 ParamToGlobal *ConsumerInstance;
46
47 };
48
49 class ParamToGlobalRewriteVisitor : public
50 CommonParameterRewriteVisitor<ParamToGlobalRewriteVisitor, ParamToGlobal> {
51
52 public:
53 explicit ParamToGlobalRewriteVisitor(ParamToGlobal *Instance)
54 : CommonParameterRewriteVisitor<ParamToGlobalRewriteVisitor,
55 ParamToGlobal>(Instance)
56 { }
57
58 bool VisitDeclRefExpr(DeclRefExpr *ParmRefExpr);
59 };
60
61 bool ParamToGlobalASTVisitor::VisitFunctionDecl(FunctionDecl *FD)
62 {
63 if (ConsumerInstance->isValidFuncDecl(FD->getCanonicalDecl())) {
64 ConsumerInstance->ValidFuncDecls.push_back(FD->getCanonicalDecl());
65 }
66 return true;
67 }
68
69 std::string ParamToGlobal::getNewName(FunctionDecl *FP, const ParmVarDecl *PV)
70 {
71 std::string NewName;
72 NewName = FP->getNameInfo().getAsString();
73 NewName += "_";
74 NewName += PV->getNameAsString();
75
76 // also backup the new name
77 TheNewDeclName = NewName;
78 return NewName;
79 }
80
81 bool ParamToGlobalRewriteVisitor::VisitDeclRefExpr(DeclRefExpr *ParmRefExpr)
82 {
83 const ValueDecl *OrigDecl = ParmRefExpr->getDecl();
84
85 if (!ConsumerInstance->TheParmVarDecl)
86 return true;
87
88 if (OrigDecl != ConsumerInstance->TheParmVarDecl)
89 return true;
90
91 SourceRange ExprRange = ParmRefExpr->getSourceRange();
92 return
93 !(ConsumerInstance->TheRewriter.ReplaceText(ExprRange,
94 ConsumerInstance->TheNewDeclName));
95 }
96
97 void ParamToGlobal::Initialize(ASTContext &context)
98 {
99 Transformation::Initialize(context);
100 CollectionVisitor = new ParamToGlobalASTVisitor(this);
101 RewriteVisitor = new ParamToGlobalRewriteVisitor(this);
102 }
103
104 void ParamToGlobal::HandleTranslationUnit(ASTContext &Ctx)
105 {
106 if (TransformationManager::isOpenCLLangOpt()) {
107 ValidInstanceNum = 0;
108 }
109 else {
110 CollectionVisitor->TraverseDecl(Ctx.getTranslationUnitDecl());
111 }
112
113 if (QueryInstanceOnly)
114 return;
115
116 if (TransformationCounter > ValidInstanceNum) {
117 TransError = TransMaxInstanceError;
118 return;
119 }
120
121 TransAssert(RewriteVisitor && "NULL RewriteVisitor!");
122 TransAssert(TheFuncDecl && "NULL TheFuncDecl!");
123 TransAssert((TheParamPos >= 0) && "Invalid parameter position!");
124 Ctx.getDiagnostics().setSuppressAllDiagnostics(false);
125
126 RewriteVisitor->TraverseDecl(Ctx.getTranslationUnitDecl());
127 RewriteVisitor->rewriteAllExprs();
128
129 if (Ctx.getDiagnostics().hasErrorOccurred() ||
130 Ctx.getDiagnostics().hasFatalErrorOccurred())
131 TransError = TransInternalError;
132 }
133
134 bool ParamToGlobal::transformParamVar(FunctionDecl *FD, const ParmVarDecl *PV)
135 {
136 std::string PName = PV->getNameAsString();
137 // Safe to omit an un-named parameter
138 if (PName.empty())
139 return true;
140
141 std::string GlobalVarStr;
142
143 GlobalVarStr = PV->getType().getAsString();
144 GlobalVarStr += " ";
145 GlobalVarStr += getNewName(FD, PV);
146 GlobalVarStr += ";\n";
147
148 return RewriteHelper->insertStringBeforeFunc(FD, GlobalVarStr);
149 }
150
151 bool ParamToGlobal::rewriteFuncDecl(clang::FunctionDecl *FD)
152 {
153 const clang::ParmVarDecl *PV = FD->getParamDecl(TheParamPos);
154
155 TransAssert(PV && "Unmatched ParamPos!");
156 RewriteHelper->removeParamFromFuncDecl(PV,
157 FD->getNumParams(),
158 TheParamPos);
159
160 if (FD->isThisDeclarationADefinition()) {
161 TheParmVarDecl = PV;
162 if (!transformParamVar(FD, PV))
163 return false;
164 }
165 return true;
166 }
167
168 bool ParamToGlobal::isValidFuncDecl(FunctionDecl *FD)
169 {
170 bool IsValid = false;
171 int ParamPos = 0;
172
173 TransAssert(isa<FunctionDecl>(FD) && "Must be a FunctionDecl");
174
175 if (isInIncludedFile(FD))
176 return false;
177
178 // Skip the case like foo(int, ...), because we cannot remove
179 // the "int" there
180 if (FD->isVariadic() && (FD->getNumParams() == 1)) {
181 return false;
182 }
183
184 if (FD->isOverloadedOperator())
185 return false;
186
187 // Avoid duplications
188 if (std::find(ValidFuncDecls.begin(),
189 ValidFuncDecls.end(), FD) !=
190 ValidFuncDecls.end())
191 return false;
192
193 for (FunctionDecl::param_const_iterator PI = FD->param_begin(),
194 PE = FD->param_end(); PI != PE; ++PI) {
195 if ((*PI)->isImplicit() || (*PI)->getSourceRange().isInvalid())
196 continue;
197 if (!FD->hasBody() && (*PI)->getNameAsString().empty())
198 continue;
199
200 ValidInstanceNum++;
201
202 if (ValidInstanceNum == TransformationCounter) {
203 TheFuncDecl = FD;
204 TheParamPos = ParamPos;
205 }
206
207 IsValid = true;
208 ParamPos++;
209 }
210 return IsValid;
211 }
212
213 ParamToGlobal::~ParamToGlobal(void)
214 {
215 delete CollectionVisitor;
216 delete RewriteVisitor;
217 }
218