Debian Codegraph @

clang_delta/RemoveAddrTaken.cpp

raw symbols SCIP
1 //===----------------------------------------------------------------------===//
2 //
3 // Copyright (c) 2012, 2013, 2015, 2016, 2017 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 "RemoveAddrTaken.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
23 using namespace clang;
24
25 static const char *DescriptionMsg =
26 "Remove an addr-taken operator if \n\
27 * the subexpr is type of pointer, or \n\
28 * the subexpr is type of integer and the addr-taken operator is \
29 an operand of a comparison operator, or \n\
30 * the entire addr-taken expr is an argument of a function, \
31 and the argument doesn't have a correponding parameter in function's \
32 declaration.\n";
33
34 static RegisterTransformation<RemoveAddrTaken>
35 Trans("remove-addr-taken", DescriptionMsg);
36
37 class RemoveAddrTakenCollectionVisitor : public
38 RecursiveASTVisitor<RemoveAddrTakenCollectionVisitor> {
39
40 public:
41
42 explicit RemoveAddrTakenCollectionVisitor(RemoveAddrTaken *Instance)
43 : ConsumerInstance(Instance)
44 { }
45
46 bool VisitUnaryOperator(UnaryOperator *UO);
47
48 bool VisitBinaryOperator(BinaryOperator *BO);
49
50 bool VisitCallExpr(CallExpr *CE);
51
52 private:
53
54 void handleOneAddrTakenOp(const UnaryOperator *UO);
55
56 void handleOneOperand(const Expr *E);
57
58 RemoveAddrTaken *ConsumerInstance;
59
60 };
61
62 void RemoveAddrTakenCollectionVisitor::handleOneAddrTakenOp(
63 const UnaryOperator *UO)
64 {
65 if (ConsumerInstance->isInIncludedFile(UO) ||
66 ConsumerInstance->VisitedAddrTakenOps.count(UO))
67 return;
68
69 ConsumerInstance->VisitedAddrTakenOps.insert(UO);
70 ConsumerInstance->ValidInstanceNum++;
71 if (ConsumerInstance->TransformationCounter ==
72 ConsumerInstance->ValidInstanceNum)
73 ConsumerInstance->TheUO = UO;
74 }
75
76 bool RemoveAddrTakenCollectionVisitor::VisitUnaryOperator(UnaryOperator *UO)
77 {
78 if (UO->getOpcode() != UO_AddrOf)
79 return true;
80
81 const Expr *E = UO->getSubExpr();
82 const Type *Ty = E->getType().getTypePtr();
83 if (!Ty->isPointerType())
84 return true;
85
86 handleOneAddrTakenOp(UO);
87 return true;
88 }
89
90 void RemoveAddrTakenCollectionVisitor::handleOneOperand(const Expr *E)
91 {
92 const UnaryOperator *UO = dyn_cast<UnaryOperator>(E);
93 if (!UO)
94 return;
95
96 if (UO->getOpcode() != UO_AddrOf)
97 return;
98
99 const Expr *SubE = UO->getSubExpr();
100 const Type *Ty = SubE->getType().getTypePtr();
101 if (!Ty->isIntegerType())
102 return;
103
104 handleOneAddrTakenOp(UO);
105 }
106
107 bool RemoveAddrTakenCollectionVisitor::VisitBinaryOperator(BinaryOperator *BO)
108 {
109 if (!BO->isComparisonOp())
110 return true;
111
112 handleOneOperand(BO->getLHS());
113 handleOneOperand(BO->getRHS());
114 return true;
115 }
116
117 // handle special cases like
118 // void f1();
119 // void f2(void) {
120 // f1(xxx);
121 // }
122 bool RemoveAddrTakenCollectionVisitor::VisitCallExpr(CallExpr *CE)
123 {
124 const FunctionDecl *FD = CE->getDirectCallee();
125 if (!FD)
126 return true;
127
128 unsigned NumParams = FD->getNumParams();
129 if (NumParams != 0)
130 return true;
131
132 for (CallExpr::arg_iterator I = CE->arg_begin(),
133 E = CE->arg_end(); I != E; ++I) {
134 const Expr *Arg = *I;
135 const UnaryOperator *UO = dyn_cast<UnaryOperator>(Arg);
136 if (!UO || (UO->getOpcode() != UO_AddrOf))
137 continue;
138 handleOneAddrTakenOp(UO);
139 }
140 return true;
141 }
142
143 void RemoveAddrTaken::Initialize(ASTContext &context)
144 {
145 Transformation::Initialize(context);
146 CollectionVisitor = new RemoveAddrTakenCollectionVisitor(this);
147 }
148
149 bool RemoveAddrTaken::HandleTopLevelDecl(DeclGroupRef D)
150 {
151 TransAssert(CollectionVisitor && "NULL CollectionVisitor!");
152 if (TransformationManager::isCXXLangOpt()) {
153 ValidInstanceNum = 0;
154 return true;
155 }
156
157 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) {
158 FunctionDecl *FD = dyn_cast<FunctionDecl>(*I);
159 if (!FD || !FD->isThisDeclarationADefinition())
160 continue;
161 CollectionVisitor->TraverseDecl(*I);
162 }
163
164 return true;
165 }
166
167 void RemoveAddrTaken::HandleTranslationUnit(ASTContext &Ctx)
168 {
169 if (QueryInstanceOnly)
170 return;
171
172 if (TransformationCounter > ValidInstanceNum) {
173 TransError = TransMaxInstanceError;
174 return;
175 }
176
177 TransAssert(TheUO && "NULL UnaryOperator!");
178 rewriteAddrTakenOp(TheUO);
179
180 if (Ctx.getDiagnostics().hasErrorOccurred() ||
181 Ctx.getDiagnostics().hasFatalErrorOccurred())
182 TransError = TransInternalError;
183 }
184
185 void RemoveAddrTaken::rewriteAddrTakenOp(const UnaryOperator *UO)
186 {
187 SourceLocation Loc = UO->getOperatorLoc();
188 TheRewriter.RemoveText(Loc, 1);
189 }
190
191 RemoveAddrTaken::~RemoveAddrTaken(void)
192 {
193 delete CollectionVisitor;
194 }
195