Debian Codegraph @

clang_delta/TemplateNonTypeArgToInt.cpp

raw symbols SCIP
1 //===----------------------------------------------------------------------===//
2 //
3 // Copyright (c) 2012, 2013, 2015, 2016, 2017, 2019 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 "TemplateNonTypeArgToInt.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 #include "CommonTemplateArgumentVisitor.h"
23
24 using namespace clang;
25 using namespace clang_delta_common_visitor;
26
27 static const char *DescriptionMsg =
28 "This pass tries to replace a template non-type argument \
29 wth an integer (if its type is compatible) \n";
30
31 static RegisterTransformation<TemplateNonTypeArgToInt>
32 Trans("template-non-type-arg-to-int", DescriptionMsg);
33
34 class TemplateNonTypeArgToIntArgCollector : public
35 RecursiveASTVisitor<TemplateNonTypeArgToIntArgCollector> {
36
37 public:
38 explicit TemplateNonTypeArgToIntArgCollector(
39 TemplateNonTypeArgToInt *Instance)
40 : ConsumerInstance(Instance)
41 { }
42
43 bool VisitClassTemplateDecl(ClassTemplateDecl *D);
44
45 bool VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
46
47 private:
48 TemplateNonTypeArgToInt *ConsumerInstance;
49
50 };
51
52 bool TemplateNonTypeArgToIntArgCollector::VisitClassTemplateDecl(
53 ClassTemplateDecl *D)
54 {
55 if (D->isThisDeclarationADefinition())
56 ConsumerInstance->handleOneTemplateDecl(D);
57 return true;
58 }
59
60 bool TemplateNonTypeArgToIntArgCollector::VisitFunctionTemplateDecl(
61 FunctionTemplateDecl *D)
62 {
63 if (D->isThisDeclarationADefinition())
64 ConsumerInstance->handleOneTemplateDecl(D);
65 return true;
66 }
67
68 class TemplateNonTypeArgToIntASTVisitor : public
69 CommonTemplateArgumentVisitor<TemplateNonTypeArgToIntASTVisitor,
70 TemplateNonTypeArgToInt> {
71
72 public:
73 explicit TemplateNonTypeArgToIntASTVisitor(
74 TemplateNonTypeArgToInt *Instance)
75 : CommonTemplateArgumentVisitor<TemplateNonTypeArgToIntASTVisitor,
76 TemplateNonTypeArgToInt>(Instance)
77 { }
78 };
79
80 void TemplateNonTypeArgToInt::Initialize(ASTContext &context)
81 {
82 Transformation::Initialize(context);
83 CollectionVisitor = new TemplateNonTypeArgToIntASTVisitor(this);
84 ArgCollector = new TemplateNonTypeArgToIntArgCollector(this);
85 }
86
87 void TemplateNonTypeArgToInt::HandleTranslationUnit(ASTContext &Ctx)
88 {
89 if (TransformationManager::isCLangOpt() ||
90 TransformationManager::isOpenCLLangOpt()) {
91 ValidInstanceNum = 0;
92 }
93 else {
94 ArgCollector->TraverseDecl(Ctx.getTranslationUnitDecl());
95 CollectionVisitor->TraverseDecl(Ctx.getTranslationUnitDecl());
96 }
97
98 if (QueryInstanceOnly)
99 return;
100
101 if (TransformationCounter > ValidInstanceNum) {
102 TransError = TransMaxInstanceError;
103 return;
104 }
105
106 Ctx.getDiagnostics().setSuppressAllDiagnostics(false);
107 if (TheExpr) {
108 RewriteHelper->replaceExpr(TheExpr, IntString);
109 }
110 else if (TheValueDecl) {
111 RewriteHelper->replaceValueDecl(TheValueDecl,
112 "int " + TheValueDecl->getNameAsString());
113 }
114 else {
115 TransAssert(0 && "No valid targets!");
116 }
117
118 if (Ctx.getDiagnostics().hasErrorOccurred() ||
119 Ctx.getDiagnostics().hasFatalErrorOccurred())
120 TransError = TransInternalError;
121 }
122
123 bool TemplateNonTypeArgToInt::isValidTemplateArgument(
124 const TemplateArgument &Arg)
125 {
126 TemplateArgument::ArgKind K = Arg.getKind();
127 switch (K) {
128 case TemplateArgument::Declaration: {
129 return true;
130 }
131
132 case TemplateArgument::Expression: {
133 const Expr *E = Arg.getAsExpr()->IgnoreParenCasts();
134 if (dyn_cast<IntegerLiteral>(E) || dyn_cast<CXXBoolLiteralExpr>(E))
135 return false;
136 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
137 UnaryOperator::Opcode Op = UO->getOpcode();
138 if((Op == UO_Minus) || (Op == UO_Plus))
139 return false;
140 }
141
142 return true;
143 }
144
145 default:
146 TransAssert(0 && "Unreachable code!");
147 return false;
148 }
149 TransAssert(0 && "Unreachable code!");
150 return false;
151 }
152
153 void TemplateNonTypeArgToInt::handleOneTemplateArgumentLoc(
154 const TemplateArgumentLoc &ArgLoc)
155 {
156 if (ArgLoc.getLocation().isInvalid())
157 return;
158 const TemplateArgument &Arg = ArgLoc.getArgument();
159
160 if (!isValidTemplateArgument(Arg))
161 return;
162
163 ValidInstanceNum++;
164 if (ValidInstanceNum == TransformationCounter) {
165 TheExpr = ArgLoc.getLocInfo().getAsExpr();
166 Expr::EvalResult Result;
167 if (!TheExpr->isValueDependent() &&
168 TheExpr->EvaluateAsInt(Result, *Context)) {
169 llvm::APSInt IVal = Result.Val.getInt();
170 llvm::SmallString<8> TmpStr;
171 IVal.toString(TmpStr, 10);
172 IntString = TmpStr.str().str();
173 }
174 }
175 }
176
177 void TemplateNonTypeArgToInt::handleTemplateArgumentLocs(
178 const TemplateDecl *D, const TemplateArgumentLoc *TAL, unsigned NumArgs)
179 {
180 TransAssert(D && "NULL TemplateDecl!");
181 if (!TAL)
182 return;
183 TemplateParameterIdxSet *ValidIdx =
184 DeclToParamIdx[dyn_cast<TemplateDecl>(D->getCanonicalDecl())];
185 if (!ValidIdx)
186 return;
187 for (unsigned I = 0; I < NumArgs; ++I) {
188 if (ValidIdx->count(I))
189 handleOneTemplateArgumentLoc(TAL[I]);
190 }
191 }
192
193 void TemplateNonTypeArgToInt::handleTemplateSpecializationTypeLoc(
194 const TemplateSpecializationTypeLoc &TLoc)
195 {
196 const Type *Ty = TLoc.getTypePtr();
197 const TemplateSpecializationType *TST =
198 Ty->getAs<TemplateSpecializationType>();
199 TemplateName TplName = TST->getTemplateName();
200 const TemplateDecl *TplD = TplName.getAsTemplateDecl();
201
202 TemplateParameterIdxSet *ValidIdx =
203 DeclToParamIdx[dyn_cast<TemplateDecl>(TplD->getCanonicalDecl())];
204 if (!ValidIdx)
205 return;
206 for (unsigned I = 0; I < TLoc.getNumArgs(); ++I) {
207 if (ValidIdx->count(I))
208 handleOneTemplateArgumentLoc(TLoc.getArgLoc(I));
209 }
210 }
211
212 bool TemplateNonTypeArgToInt::isValidParameter(const NamedDecl *ND)
213 {
214 const NonTypeTemplateParmDecl *NonTypeD =
215 dyn_cast<NonTypeTemplateParmDecl>(ND);
216 if (!NonTypeD)
217 return false;
218 // To avoid something like replacing int with int.
219 if (NonTypeD->getType().getAsString() == "int")
220 return false;
221 const Type *Ty = NonTypeD->getType().getTypePtr();
222 return Ty->isIntegerType();
223 }
224
225 void TemplateNonTypeArgToInt::handleOneTemplateDecl(const TemplateDecl *D)
226 {
227 if (isInIncludedFile(D))
228 return;
229 TemplateParameterIdxSet *ValidParamIdx = new TemplateParameterIdxSet();
230 TemplateParameterList *TPList = D->getTemplateParameters();
231 unsigned Idx = 0;
232 for (TemplateParameterList::const_iterator I = TPList->begin(),
233 E = TPList->end(); I != E; ++I) {
234 const NamedDecl *ParamND = (*I);
235 if (isValidParameter(ParamND)) {
236 ValidParamIdx->insert(Idx);
237 if (const ValueDecl* ValD = dyn_cast<ValueDecl>(ParamND)) {
238 ++ValidInstanceNum;
239 if (ValidInstanceNum == TransformationCounter)
240 TheValueDecl = ValD;
241 }
242 }
243 Idx++;
244 }
245
246 TransAssert(!DeclToParamIdx[D] && "Duplicate TemplateDecl!");
247 DeclToParamIdx[dyn_cast<TemplateDecl>(D->getCanonicalDecl())] = ValidParamIdx;
248 }
249
250 TemplateNonTypeArgToInt::~TemplateNonTypeArgToInt()
251 {
252 delete CollectionVisitor;
253 delete ArgCollector;
254 }
255