Debian Codegraph @

clang_delta/SimplifyCallExpr.cpp

raw symbols SCIP
1 //===----------------------------------------------------------------------===//
2 //
3 // Copyright (c) 2012, 2013, 2015, 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 "SimplifyCallExpr.h"
16
17 #include <sstream>
18
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 "Simplify a call expression to a comma expression. \
29 Replace function arguments with: \n\
30 * 0 for integer and pointer arguments \n\
31 * a global temp variable for structs/unions \
32 and also append a representative return value which is the \
33 the last inner expression of the comma expression. \n\
34 For example, assume we have a function foo: \n\
35 int foo(int x, int *y, struct z) \
36 Then this transformation will transform \n\
37 foo(i, p, s); \n\
38 to \n\
39 (0, 0, tmp_var, 0);\n";
40
41 static RegisterTransformation<SimplifyCallExpr>
42 Trans("simplify-callexpr", DescriptionMsg);
43
44 class SimplifyCallExprVisitor : public
45 RecursiveASTVisitor<SimplifyCallExprVisitor> {
46
47 public:
48
49 explicit SimplifyCallExprVisitor(SimplifyCallExpr *Instance)
50 : ConsumerInstance(Instance),
51 CurrentFD(NULL)
52 { }
53
54 bool VisitCallExpr(CallExpr *CE);
55
56 bool VisitFunctionDecl(FunctionDecl *FD);
57
58 private:
59
60 SimplifyCallExpr *ConsumerInstance;
61
62 const FunctionDecl *CurrentFD;
63 };
64
65 bool SimplifyCallExprVisitor::VisitCallExpr(CallExpr *CE)
66 {
67 if (ConsumerInstance->isInIncludedFile(CE))
68 return true;
69
70 ConsumerInstance->ValidInstanceNum++;
71 if (ConsumerInstance->TransformationCounter !=
72 ConsumerInstance->ValidInstanceNum)
73 return true;
74
75 ConsumerInstance->TheCallExpr = CE;
76 ConsumerInstance->CurrentFD = CurrentFD;
77 return true;
78 }
79
80 bool SimplifyCallExprVisitor::VisitFunctionDecl(FunctionDecl *FD)
81 {
82 CurrentFD = FD;
83 return true;
84 }
85
86 void SimplifyCallExpr::Initialize(ASTContext &context)
87 {
88 Transformation::Initialize(context);
89 CollectionVisitor = new SimplifyCallExprVisitor(this);
90 NameQueryWrap =
91 new TransNameQueryWrap(RewriteHelper->getTmpVarNamePrefix());
92 }
93
94 void SimplifyCallExpr::HandleTranslationUnit(ASTContext &Ctx)
95 {
96 CollectionVisitor->TraverseDecl(Ctx.getTranslationUnitDecl());
97 if (QueryInstanceOnly)
98 return;
99
100 if (TransformationCounter > ValidInstanceNum) {
101 TransError = TransMaxInstanceError;
102 return;
103 }
104
105 TransAssert(TheCallExpr && "NULL TheCallExpr!");
106
107 Ctx.getDiagnostics().setSuppressAllDiagnostics(false);
108
109 NameQueryWrap->TraverseDecl(Ctx.getTranslationUnitDecl());
110 NamePostfix = NameQueryWrap->getMaxNamePostfix() + 1;
111
112 replaceCallExpr();
113
114 if (Ctx.getDiagnostics().hasErrorOccurred() ||
115 Ctx.getDiagnostics().hasFatalErrorOccurred())
116 TransError = TransInternalError;
117 }
118
119 void SimplifyCallExpr::handleOneArgStr(const Expr *Arg, std::string &Str)
120 {
121 Str = "0";
122 const Type *ArgT = Arg->getType().getTypePtr();
123 if (!ArgT->isUnionType() && !ArgT->isStructureType())
124 return;
125
126 RewriteHelper->getTmpTransName(NamePostfix, Str);
127 NamePostfix++;
128
129 std::string TmpVarStr = Str;
130 Arg->getType().getAsStringInternal(TmpVarStr, Context->getPrintingPolicy());
131 TmpVarStr += ";\n";
132 if (CurrentFD) {
133 RewriteHelper->insertStringBeforeFunc(CurrentFD, TmpVarStr);
134 }
135 else {
136 TheRewriter.InsertTextBefore(getRealLocation(TheCallExpr->getBeginLoc()), TmpVarStr);
137 }
138 }
139
140 void SimplifyCallExpr::replaceCallExpr(void)
141 {
142 std::string CommaStr("");
143 unsigned int NumArg = TheCallExpr->getNumArgs();
144 if (NumArg == 0) {
145 RewriteHelper->replaceExpr(TheCallExpr, CommaStr);
146 return;
147 }
148
149 const Expr *Arg = TheCallExpr->getArg(0);
150 std::string ArgStr;
151 handleOneArgStr(Arg, ArgStr);
152 CommaStr += ("(" + ArgStr);
153
154 for (unsigned int I = 1; I < NumArg; ++I) {
155 Arg = TheCallExpr->getArg(I);
156 handleOneArgStr(Arg, ArgStr);
157 CommaStr += ("," + ArgStr);
158 }
159
160 QualType RVQualType = TheCallExpr->getType();
161 const Type *RVType = RVQualType.getTypePtr();
162 if (RVType->isVoidType()) {
163 // Nothing to do
164 }
165 else if (RVType->isUnionType() || RVType->isStructureType()) {
166 std::string RVStr("");
167 RewriteHelper->getTmpTransName(NamePostfix, RVStr);
168 NamePostfix++;
169
170 CommaStr += ("," + RVStr);
171 RVQualType.getAsStringInternal(RVStr, Context->getPrintingPolicy());
172 RVStr += ";\n";
173 if (CurrentFD) {
174 RewriteHelper->insertStringBeforeFunc(CurrentFD, RVStr);
175 }
176 else {
177 TheRewriter.InsertTextBefore(getRealLocation(TheCallExpr->getBeginLoc()), RVStr);
178 }
179 }
180 else {
181 CommaStr += ",0";
182 }
183
184 CommaStr += ")";
185 RewriteHelper->replaceExpr(TheCallExpr, CommaStr);
186 }
187
188 SimplifyCallExpr::~SimplifyCallExpr(void)
189 {
190 delete CollectionVisitor;
191 }
192