blob: 98010d3cb0ca2f955cdea6dc864a4d50f4f28af5 [file] [log] [blame]
Ted Kremeneka43df952012-09-21 00:09:11 +00001//== BodyFarm.cpp - Factory for conjuring up fake bodies ----------*- C++ -*-//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// BodyFarm is a factory for creating faux implementations for functions/methods
11// for analysis purposes.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremeneka43df952012-09-21 00:09:11 +000015#include "BodyFarm.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
18#include "clang/AST/Expr.h"
19#include "clang/AST/ExprObjC.h"
20#include "llvm/ADT/StringSwitch.h"
Ted Kremeneka43df952012-09-21 00:09:11 +000021
22using namespace clang;
23
Ted Kremenek0a4c0982012-09-21 17:54:32 +000024//===----------------------------------------------------------------------===//
25// Helper creation functions for constructing faux ASTs.
26//===----------------------------------------------------------------------===//
Ted Kremeneka43df952012-09-21 00:09:11 +000027
Ted Kremenekcc85d212012-09-21 00:52:24 +000028static bool isDispatchBlock(QualType Ty) {
29 // Is it a block pointer?
30 const BlockPointerType *BPT = Ty->getAs<BlockPointerType>();
31 if (!BPT)
32 return false;
33
34 // Check if the block pointer type takes no arguments and
35 // returns void.
36 const FunctionProtoType *FT =
37 BPT->getPointeeType()->getAs<FunctionProtoType>();
38 if (!FT || !FT->getResultType()->isVoidType() ||
39 FT->getNumArgs() != 0)
40 return false;
41
42 return true;
43}
44
Ted Kremenek016c33d2012-09-21 17:54:35 +000045namespace {
46class ASTMaker {
47public:
48 ASTMaker(ASTContext &C) : C(C) {}
49
Ted Kremenekb80e5bb2012-09-21 18:33:54 +000050 /// Create a new BinaryOperator representing a simple assignment.
51 BinaryOperator *makeAssignment(const Expr *LHS, const Expr *RHS, QualType Ty);
52
Ted Kremenek48fa1362012-10-11 20:58:18 +000053 /// Create a new BinaryOperator representing a comparison.
54 BinaryOperator *makeComparison(const Expr *LHS, const Expr *RHS,
55 BinaryOperator::Opcode Op);
56
57 /// Create a new compound stmt using the provided statements.
58 CompoundStmt *makeCompound(ArrayRef<Stmt*>);
59
Ted Kremeneka6d62a12012-09-21 18:13:23 +000060 /// Create a new DeclRefExpr for the referenced variable.
Ted Kremenek016c33d2012-09-21 17:54:35 +000061 DeclRefExpr *makeDeclRefExpr(const VarDecl *D);
62
Ted Kremenek0b5c5e42012-09-21 18:33:52 +000063 /// Create a new UnaryOperator representing a dereference.
64 UnaryOperator *makeDereference(const Expr *Arg, QualType Ty);
65
Ted Kremeneka6d62a12012-09-21 18:13:23 +000066 /// Create an implicit cast for an integer conversion.
Ted Kremenek5dbd9902012-10-12 00:18:19 +000067 Expr *makeIntegralCast(const Expr *Arg, QualType Ty);
Ted Kremeneka6d62a12012-09-21 18:13:23 +000068
Ted Kremenek48fa1362012-10-11 20:58:18 +000069 /// Create an implicit cast to a builtin boolean type.
70 ImplicitCastExpr *makeIntegralCastToBoolean(const Expr *Arg);
71
Ted Kremenek9ba05cd2012-09-21 18:13:27 +000072 // Create an implicit cast for lvalue-to-rvaluate conversions.
73 ImplicitCastExpr *makeLvalueToRvalue(const Expr *Arg, QualType Ty);
74
Ted Kremenek48fa1362012-10-11 20:58:18 +000075 /// Create an Objective-C bool literal.
76 ObjCBoolLiteralExpr *makeObjCBool(bool Val);
77
78 /// Create a Return statement.
79 ReturnStmt *makeReturn(const Expr *RetVal);
80
Ted Kremenek016c33d2012-09-21 17:54:35 +000081private:
82 ASTContext &C;
83};
84}
85
Ted Kremenekb80e5bb2012-09-21 18:33:54 +000086BinaryOperator *ASTMaker::makeAssignment(const Expr *LHS, const Expr *RHS,
87 QualType Ty) {
88 return new (C) BinaryOperator(const_cast<Expr*>(LHS), const_cast<Expr*>(RHS),
89 BO_Assign, Ty, VK_RValue,
Lang Hamesbe9af122012-10-02 04:45:10 +000090 OK_Ordinary, SourceLocation(), false);
Ted Kremenekb80e5bb2012-09-21 18:33:54 +000091}
92
Ted Kremenek48fa1362012-10-11 20:58:18 +000093BinaryOperator *ASTMaker::makeComparison(const Expr *LHS, const Expr *RHS,
94 BinaryOperator::Opcode Op) {
95 assert(BinaryOperator::isLogicalOp(Op) ||
96 BinaryOperator::isComparisonOp(Op));
97 return new (C) BinaryOperator(const_cast<Expr*>(LHS),
98 const_cast<Expr*>(RHS),
99 Op,
100 C.getLogicalOperationType(),
101 VK_RValue,
102 OK_Ordinary, SourceLocation(), false);
103}
104
105CompoundStmt *ASTMaker::makeCompound(ArrayRef<Stmt *> Stmts) {
Nico Weberd36aa352012-12-29 20:03:39 +0000106 return new (C) CompoundStmt(C, Stmts, SourceLocation(), SourceLocation());
Ted Kremenek48fa1362012-10-11 20:58:18 +0000107}
108
Ted Kremenek016c33d2012-09-21 17:54:35 +0000109DeclRefExpr *ASTMaker::makeDeclRefExpr(const VarDecl *D) {
110 DeclRefExpr *DR =
111 DeclRefExpr::Create(/* Ctx = */ C,
112 /* QualifierLoc = */ NestedNameSpecifierLoc(),
113 /* TemplateKWLoc = */ SourceLocation(),
114 /* D = */ const_cast<VarDecl*>(D),
115 /* isEnclosingLocal = */ false,
116 /* NameLoc = */ SourceLocation(),
117 /* T = */ D->getType(),
118 /* VK = */ VK_LValue);
119 return DR;
120}
121
Ted Kremenek0b5c5e42012-09-21 18:33:52 +0000122UnaryOperator *ASTMaker::makeDereference(const Expr *Arg, QualType Ty) {
123 return new (C) UnaryOperator(const_cast<Expr*>(Arg), UO_Deref, Ty,
124 VK_LValue, OK_Ordinary, SourceLocation());
125}
126
Ted Kremenek9ba05cd2012-09-21 18:13:27 +0000127ImplicitCastExpr *ASTMaker::makeLvalueToRvalue(const Expr *Arg, QualType Ty) {
128 return ImplicitCastExpr::Create(C, Ty, CK_LValueToRValue,
129 const_cast<Expr*>(Arg), 0, VK_RValue);
130}
131
Ted Kremenek5dbd9902012-10-12 00:18:19 +0000132Expr *ASTMaker::makeIntegralCast(const Expr *Arg, QualType Ty) {
133 if (Arg->getType() == Ty)
134 return const_cast<Expr*>(Arg);
135
Ted Kremeneka6d62a12012-09-21 18:13:23 +0000136 return ImplicitCastExpr::Create(C, Ty, CK_IntegralCast,
137 const_cast<Expr*>(Arg), 0, VK_RValue);
138}
139
Ted Kremenek48fa1362012-10-11 20:58:18 +0000140ImplicitCastExpr *ASTMaker::makeIntegralCastToBoolean(const Expr *Arg) {
141 return ImplicitCastExpr::Create(C, C.BoolTy, CK_IntegralToBoolean,
142 const_cast<Expr*>(Arg), 0, VK_RValue);
143}
144
145ObjCBoolLiteralExpr *ASTMaker::makeObjCBool(bool Val) {
146 QualType Ty = C.getBOOLDecl() ? C.getBOOLType() : C.ObjCBuiltinBoolTy;
147 return new (C) ObjCBoolLiteralExpr(Val, Ty, SourceLocation());
148}
149
150ReturnStmt *ASTMaker::makeReturn(const Expr *RetVal) {
151 return new (C) ReturnStmt(SourceLocation(), const_cast<Expr*>(RetVal), 0);
152}
153
Ted Kremenek0a4c0982012-09-21 17:54:32 +0000154//===----------------------------------------------------------------------===//
155// Creation functions for faux ASTs.
156//===----------------------------------------------------------------------===//
157
158typedef Stmt *(*FunctionFarmer)(ASTContext &C, const FunctionDecl *D);
159
Ted Kremenekcc85d212012-09-21 00:52:24 +0000160/// Create a fake body for dispatch_once.
161static Stmt *create_dispatch_once(ASTContext &C, const FunctionDecl *D) {
162 // Check if we have at least two parameters.
163 if (D->param_size() != 2)
164 return 0;
165
166 // Check if the first parameter is a pointer to integer type.
167 const ParmVarDecl *Predicate = D->getParamDecl(0);
168 QualType PredicateQPtrTy = Predicate->getType();
169 const PointerType *PredicatePtrTy = PredicateQPtrTy->getAs<PointerType>();
170 if (!PredicatePtrTy)
171 return 0;
172 QualType PredicateTy = PredicatePtrTy->getPointeeType();
173 if (!PredicateTy->isIntegerType())
174 return 0;
175
176 // Check if the second parameter is the proper block type.
177 const ParmVarDecl *Block = D->getParamDecl(1);
178 QualType Ty = Block->getType();
179 if (!isDispatchBlock(Ty))
180 return 0;
181
182 // Everything checks out. Create a fakse body that checks the predicate,
183 // sets it, and calls the block. Basically, an AST dump of:
184 //
185 // void dispatch_once(dispatch_once_t *predicate, dispatch_block_t block) {
186 // if (!*predicate) {
187 // *predicate = 1;
188 // block();
189 // }
190 // }
191
Ted Kremenek016c33d2012-09-21 17:54:35 +0000192 ASTMaker M(C);
193
Ted Kremenekcc85d212012-09-21 00:52:24 +0000194 // (1) Create the call.
Ted Kremenek016c33d2012-09-21 17:54:35 +0000195 DeclRefExpr *DR = M.makeDeclRefExpr(Block);
Ted Kremenek9ba05cd2012-09-21 18:13:27 +0000196 ImplicitCastExpr *ICE = M.makeLvalueToRvalue(DR, Ty);
Ted Kremenekcc85d212012-09-21 00:52:24 +0000197 CallExpr *CE = new (C) CallExpr(C, ICE, ArrayRef<Expr*>(), C.VoidTy,
198 VK_RValue, SourceLocation());
199
200 // (2) Create the assignment to the predicate.
201 IntegerLiteral *IL =
202 IntegerLiteral::Create(C, llvm::APInt(C.getTypeSize(C.IntTy), (uint64_t) 1),
203 C.IntTy, SourceLocation());
Ted Kremenekfcf8eba2012-09-21 18:33:56 +0000204 BinaryOperator *B =
205 M.makeAssignment(
206 M.makeDereference(
207 M.makeLvalueToRvalue(
208 M.makeDeclRefExpr(Predicate), PredicateQPtrTy),
209 PredicateTy),
210 M.makeIntegralCast(IL, PredicateTy),
211 PredicateTy);
212
Ted Kremenekcc85d212012-09-21 00:52:24 +0000213 // (3) Create the compound statement.
214 Stmt *Stmts[2];
215 Stmts[0] = B;
Ted Kremenek48fa1362012-10-11 20:58:18 +0000216 Stmts[1] = CE;
217 CompoundStmt *CS = M.makeCompound(ArrayRef<Stmt*>(Stmts, 2));
Ted Kremenekcc85d212012-09-21 00:52:24 +0000218
219 // (4) Create the 'if' condition.
Ted Kremenekfcf8eba2012-09-21 18:33:56 +0000220 ImplicitCastExpr *LValToRval =
221 M.makeLvalueToRvalue(
222 M.makeDereference(
223 M.makeLvalueToRvalue(
224 M.makeDeclRefExpr(Predicate),
225 PredicateQPtrTy),
226 PredicateTy),
227 PredicateTy);
228
229 UnaryOperator *UO = new (C) UnaryOperator(LValToRval, UO_LNot, C.IntTy,
230 VK_RValue, OK_Ordinary,
231 SourceLocation());
Ted Kremenekcc85d212012-09-21 00:52:24 +0000232
233 // (5) Create the 'if' statement.
234 IfStmt *If = new (C) IfStmt(C, SourceLocation(), 0, UO, CS);
235 return If;
236}
237
Ted Kremeneka43df952012-09-21 00:09:11 +0000238/// Create a fake body for dispatch_sync.
239static Stmt *create_dispatch_sync(ASTContext &C, const FunctionDecl *D) {
240 // Check if we have at least two parameters.
241 if (D->param_size() != 2)
242 return 0;
243
244 // Check if the second parameter is a block.
245 const ParmVarDecl *PV = D->getParamDecl(1);
246 QualType Ty = PV->getType();
Ted Kremenekcc85d212012-09-21 00:52:24 +0000247 if (!isDispatchBlock(Ty))
Ted Kremeneka43df952012-09-21 00:09:11 +0000248 return 0;
Ted Kremenek016c33d2012-09-21 17:54:35 +0000249
Ted Kremeneka43df952012-09-21 00:09:11 +0000250 // Everything checks out. Create a fake body that just calls the block.
251 // This is basically just an AST dump of:
252 //
253 // void dispatch_sync(dispatch_queue_t queue, void (^block)(void)) {
254 // block();
255 // }
Ted Kremenek016c33d2012-09-21 17:54:35 +0000256 //
257 ASTMaker M(C);
258 DeclRefExpr *DR = M.makeDeclRefExpr(PV);
Ted Kremenek0b5c5e42012-09-21 18:33:52 +0000259 ImplicitCastExpr *ICE = M.makeLvalueToRvalue(DR, Ty);
Ted Kremeneka43df952012-09-21 00:09:11 +0000260 CallExpr *CE = new (C) CallExpr(C, ICE, ArrayRef<Expr*>(), C.VoidTy,
261 VK_RValue, SourceLocation());
262 return CE;
263}
264
Ted Kremenek48fa1362012-10-11 20:58:18 +0000265static Stmt *create_OSAtomicCompareAndSwap(ASTContext &C, const FunctionDecl *D)
266{
267 // There are exactly 3 arguments.
268 if (D->param_size() != 3)
269 return 0;
270
271 // Body for:
272 // if (oldValue == *theValue) {
273 // *theValue = newValue;
274 // return YES;
275 // }
276 // else return NO;
277
Ted Kremenek5dbd9902012-10-12 00:18:19 +0000278 QualType ResultTy = D->getResultType();
279 bool isBoolean = ResultTy->isBooleanType();
280 if (!isBoolean && !ResultTy->isIntegralType(C))
281 return 0;
282
Ted Kremenek48fa1362012-10-11 20:58:18 +0000283 const ParmVarDecl *OldValue = D->getParamDecl(0);
284 QualType OldValueTy = OldValue->getType();
285
286 const ParmVarDecl *NewValue = D->getParamDecl(1);
287 QualType NewValueTy = NewValue->getType();
288
289 assert(OldValueTy == NewValueTy);
290
291 const ParmVarDecl *TheValue = D->getParamDecl(2);
292 QualType TheValueTy = TheValue->getType();
293 const PointerType *PT = TheValueTy->getAs<PointerType>();
294 if (!PT)
295 return 0;
296 QualType PointeeTy = PT->getPointeeType();
297
298 ASTMaker M(C);
299 // Construct the comparison.
300 Expr *Comparison =
301 M.makeComparison(
302 M.makeLvalueToRvalue(M.makeDeclRefExpr(OldValue), OldValueTy),
303 M.makeLvalueToRvalue(
304 M.makeDereference(
305 M.makeLvalueToRvalue(M.makeDeclRefExpr(TheValue), TheValueTy),
306 PointeeTy),
307 PointeeTy),
308 BO_EQ);
309
310 // Construct the body of the IfStmt.
311 Stmt *Stmts[2];
312 Stmts[0] =
313 M.makeAssignment(
314 M.makeDereference(
315 M.makeLvalueToRvalue(M.makeDeclRefExpr(TheValue), TheValueTy),
316 PointeeTy),
317 M.makeLvalueToRvalue(M.makeDeclRefExpr(NewValue), NewValueTy),
318 NewValueTy);
Ted Kremenek5dbd9902012-10-12 00:18:19 +0000319
320 Expr *BoolVal = M.makeObjCBool(true);
321 Expr *RetVal = isBoolean ? M.makeIntegralCastToBoolean(BoolVal)
322 : M.makeIntegralCast(BoolVal, ResultTy);
323 Stmts[1] = M.makeReturn(RetVal);
Ted Kremenek48fa1362012-10-11 20:58:18 +0000324 CompoundStmt *Body = M.makeCompound(ArrayRef<Stmt*>(Stmts, 2));
325
326 // Construct the else clause.
Ted Kremenek5dbd9902012-10-12 00:18:19 +0000327 BoolVal = M.makeObjCBool(false);
328 RetVal = isBoolean ? M.makeIntegralCastToBoolean(BoolVal)
329 : M.makeIntegralCast(BoolVal, ResultTy);
330 Stmt *Else = M.makeReturn(RetVal);
Ted Kremenek48fa1362012-10-11 20:58:18 +0000331
332 /// Construct the If.
333 Stmt *If =
334 new (C) IfStmt(C, SourceLocation(), 0, Comparison, Body,
335 SourceLocation(), Else);
336
337 return If;
338}
339
Ted Kremeneka43df952012-09-21 00:09:11 +0000340Stmt *BodyFarm::getBody(const FunctionDecl *D) {
341 D = D->getCanonicalDecl();
342
343 llvm::Optional<Stmt *> &Val = Bodies[D];
344 if (Val.hasValue())
345 return Val.getValue();
346
347 Val = 0;
348
349 if (D->getIdentifier() == 0)
350 return 0;
351
352 StringRef Name = D->getName();
353 if (Name.empty())
354 return 0;
Ted Kremenek48fa1362012-10-11 20:58:18 +0000355
356 FunctionFarmer FF;
357
358 if (Name.startswith("OSAtomicCompareAndSwap") ||
359 Name.startswith("objc_atomicCompareAndSwap")) {
360 FF = create_OSAtomicCompareAndSwap;
361 }
362 else {
363 FF = llvm::StringSwitch<FunctionFarmer>(Name)
364 .Case("dispatch_sync", create_dispatch_sync)
365 .Case("dispatch_once", create_dispatch_once)
366 .Default(NULL);
Ted Kremeneka43df952012-09-21 00:09:11 +0000367 }
368
Ted Kremenek48fa1362012-10-11 20:58:18 +0000369 if (FF) { Val = FF(C, D); }
Ted Kremeneka43df952012-09-21 00:09:11 +0000370 return Val.getValue();
371}
372