blob: d038cf1ecde54f470b901135abd0fbdd8ae9b51e [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) {
106 return new (C) CompoundStmt(C, const_cast<Stmt**>(Stmts.data()),
107 Stmts.size(),
108 SourceLocation(), SourceLocation());
109}
110
Ted Kremenek016c33d2012-09-21 17:54:35 +0000111DeclRefExpr *ASTMaker::makeDeclRefExpr(const VarDecl *D) {
112 DeclRefExpr *DR =
113 DeclRefExpr::Create(/* Ctx = */ C,
114 /* QualifierLoc = */ NestedNameSpecifierLoc(),
115 /* TemplateKWLoc = */ SourceLocation(),
116 /* D = */ const_cast<VarDecl*>(D),
117 /* isEnclosingLocal = */ false,
118 /* NameLoc = */ SourceLocation(),
119 /* T = */ D->getType(),
120 /* VK = */ VK_LValue);
121 return DR;
122}
123
Ted Kremenek0b5c5e42012-09-21 18:33:52 +0000124UnaryOperator *ASTMaker::makeDereference(const Expr *Arg, QualType Ty) {
125 return new (C) UnaryOperator(const_cast<Expr*>(Arg), UO_Deref, Ty,
126 VK_LValue, OK_Ordinary, SourceLocation());
127}
128
Ted Kremenek9ba05cd2012-09-21 18:13:27 +0000129ImplicitCastExpr *ASTMaker::makeLvalueToRvalue(const Expr *Arg, QualType Ty) {
130 return ImplicitCastExpr::Create(C, Ty, CK_LValueToRValue,
131 const_cast<Expr*>(Arg), 0, VK_RValue);
132}
133
Ted Kremenek5dbd9902012-10-12 00:18:19 +0000134Expr *ASTMaker::makeIntegralCast(const Expr *Arg, QualType Ty) {
135 if (Arg->getType() == Ty)
136 return const_cast<Expr*>(Arg);
137
Ted Kremeneka6d62a12012-09-21 18:13:23 +0000138 return ImplicitCastExpr::Create(C, Ty, CK_IntegralCast,
139 const_cast<Expr*>(Arg), 0, VK_RValue);
140}
141
Ted Kremenek48fa1362012-10-11 20:58:18 +0000142ImplicitCastExpr *ASTMaker::makeIntegralCastToBoolean(const Expr *Arg) {
143 return ImplicitCastExpr::Create(C, C.BoolTy, CK_IntegralToBoolean,
144 const_cast<Expr*>(Arg), 0, VK_RValue);
145}
146
147ObjCBoolLiteralExpr *ASTMaker::makeObjCBool(bool Val) {
148 QualType Ty = C.getBOOLDecl() ? C.getBOOLType() : C.ObjCBuiltinBoolTy;
149 return new (C) ObjCBoolLiteralExpr(Val, Ty, SourceLocation());
150}
151
152ReturnStmt *ASTMaker::makeReturn(const Expr *RetVal) {
153 return new (C) ReturnStmt(SourceLocation(), const_cast<Expr*>(RetVal), 0);
154}
155
Ted Kremenek0a4c0982012-09-21 17:54:32 +0000156//===----------------------------------------------------------------------===//
157// Creation functions for faux ASTs.
158//===----------------------------------------------------------------------===//
159
160typedef Stmt *(*FunctionFarmer)(ASTContext &C, const FunctionDecl *D);
161
Ted Kremenekcc85d212012-09-21 00:52:24 +0000162/// Create a fake body for dispatch_once.
163static Stmt *create_dispatch_once(ASTContext &C, const FunctionDecl *D) {
164 // Check if we have at least two parameters.
165 if (D->param_size() != 2)
166 return 0;
167
168 // Check if the first parameter is a pointer to integer type.
169 const ParmVarDecl *Predicate = D->getParamDecl(0);
170 QualType PredicateQPtrTy = Predicate->getType();
171 const PointerType *PredicatePtrTy = PredicateQPtrTy->getAs<PointerType>();
172 if (!PredicatePtrTy)
173 return 0;
174 QualType PredicateTy = PredicatePtrTy->getPointeeType();
175 if (!PredicateTy->isIntegerType())
176 return 0;
177
178 // Check if the second parameter is the proper block type.
179 const ParmVarDecl *Block = D->getParamDecl(1);
180 QualType Ty = Block->getType();
181 if (!isDispatchBlock(Ty))
182 return 0;
183
184 // Everything checks out. Create a fakse body that checks the predicate,
185 // sets it, and calls the block. Basically, an AST dump of:
186 //
187 // void dispatch_once(dispatch_once_t *predicate, dispatch_block_t block) {
188 // if (!*predicate) {
189 // *predicate = 1;
190 // block();
191 // }
192 // }
193
Ted Kremenek016c33d2012-09-21 17:54:35 +0000194 ASTMaker M(C);
195
Ted Kremenekcc85d212012-09-21 00:52:24 +0000196 // (1) Create the call.
Ted Kremenek016c33d2012-09-21 17:54:35 +0000197 DeclRefExpr *DR = M.makeDeclRefExpr(Block);
Ted Kremenek9ba05cd2012-09-21 18:13:27 +0000198 ImplicitCastExpr *ICE = M.makeLvalueToRvalue(DR, Ty);
Ted Kremenekcc85d212012-09-21 00:52:24 +0000199 CallExpr *CE = new (C) CallExpr(C, ICE, ArrayRef<Expr*>(), C.VoidTy,
200 VK_RValue, SourceLocation());
201
202 // (2) Create the assignment to the predicate.
203 IntegerLiteral *IL =
204 IntegerLiteral::Create(C, llvm::APInt(C.getTypeSize(C.IntTy), (uint64_t) 1),
205 C.IntTy, SourceLocation());
Ted Kremenekfcf8eba2012-09-21 18:33:56 +0000206 BinaryOperator *B =
207 M.makeAssignment(
208 M.makeDereference(
209 M.makeLvalueToRvalue(
210 M.makeDeclRefExpr(Predicate), PredicateQPtrTy),
211 PredicateTy),
212 M.makeIntegralCast(IL, PredicateTy),
213 PredicateTy);
214
Ted Kremenekcc85d212012-09-21 00:52:24 +0000215 // (3) Create the compound statement.
216 Stmt *Stmts[2];
217 Stmts[0] = B;
Ted Kremenek48fa1362012-10-11 20:58:18 +0000218 Stmts[1] = CE;
219 CompoundStmt *CS = M.makeCompound(ArrayRef<Stmt*>(Stmts, 2));
Ted Kremenekcc85d212012-09-21 00:52:24 +0000220
221 // (4) Create the 'if' condition.
Ted Kremenekfcf8eba2012-09-21 18:33:56 +0000222 ImplicitCastExpr *LValToRval =
223 M.makeLvalueToRvalue(
224 M.makeDereference(
225 M.makeLvalueToRvalue(
226 M.makeDeclRefExpr(Predicate),
227 PredicateQPtrTy),
228 PredicateTy),
229 PredicateTy);
230
231 UnaryOperator *UO = new (C) UnaryOperator(LValToRval, UO_LNot, C.IntTy,
232 VK_RValue, OK_Ordinary,
233 SourceLocation());
Ted Kremenekcc85d212012-09-21 00:52:24 +0000234
235 // (5) Create the 'if' statement.
236 IfStmt *If = new (C) IfStmt(C, SourceLocation(), 0, UO, CS);
237 return If;
238}
239
Ted Kremeneka43df952012-09-21 00:09:11 +0000240/// Create a fake body for dispatch_sync.
241static Stmt *create_dispatch_sync(ASTContext &C, const FunctionDecl *D) {
242 // Check if we have at least two parameters.
243 if (D->param_size() != 2)
244 return 0;
245
246 // Check if the second parameter is a block.
247 const ParmVarDecl *PV = D->getParamDecl(1);
248 QualType Ty = PV->getType();
Ted Kremenekcc85d212012-09-21 00:52:24 +0000249 if (!isDispatchBlock(Ty))
Ted Kremeneka43df952012-09-21 00:09:11 +0000250 return 0;
Ted Kremenek016c33d2012-09-21 17:54:35 +0000251
Ted Kremeneka43df952012-09-21 00:09:11 +0000252 // Everything checks out. Create a fake body that just calls the block.
253 // This is basically just an AST dump of:
254 //
255 // void dispatch_sync(dispatch_queue_t queue, void (^block)(void)) {
256 // block();
257 // }
Ted Kremenek016c33d2012-09-21 17:54:35 +0000258 //
259 ASTMaker M(C);
260 DeclRefExpr *DR = M.makeDeclRefExpr(PV);
Ted Kremenek0b5c5e42012-09-21 18:33:52 +0000261 ImplicitCastExpr *ICE = M.makeLvalueToRvalue(DR, Ty);
Ted Kremeneka43df952012-09-21 00:09:11 +0000262 CallExpr *CE = new (C) CallExpr(C, ICE, ArrayRef<Expr*>(), C.VoidTy,
263 VK_RValue, SourceLocation());
264 return CE;
265}
266
Ted Kremenek48fa1362012-10-11 20:58:18 +0000267static Stmt *create_OSAtomicCompareAndSwap(ASTContext &C, const FunctionDecl *D)
268{
269 // There are exactly 3 arguments.
270 if (D->param_size() != 3)
271 return 0;
272
273 // Body for:
274 // if (oldValue == *theValue) {
275 // *theValue = newValue;
276 // return YES;
277 // }
278 // else return NO;
279
Ted Kremenek5dbd9902012-10-12 00:18:19 +0000280 QualType ResultTy = D->getResultType();
281 bool isBoolean = ResultTy->isBooleanType();
282 if (!isBoolean && !ResultTy->isIntegralType(C))
283 return 0;
284
Ted Kremenek48fa1362012-10-11 20:58:18 +0000285 const ParmVarDecl *OldValue = D->getParamDecl(0);
286 QualType OldValueTy = OldValue->getType();
287
288 const ParmVarDecl *NewValue = D->getParamDecl(1);
289 QualType NewValueTy = NewValue->getType();
290
291 assert(OldValueTy == NewValueTy);
292
293 const ParmVarDecl *TheValue = D->getParamDecl(2);
294 QualType TheValueTy = TheValue->getType();
295 const PointerType *PT = TheValueTy->getAs<PointerType>();
296 if (!PT)
297 return 0;
298 QualType PointeeTy = PT->getPointeeType();
299
300 ASTMaker M(C);
301 // Construct the comparison.
302 Expr *Comparison =
303 M.makeComparison(
304 M.makeLvalueToRvalue(M.makeDeclRefExpr(OldValue), OldValueTy),
305 M.makeLvalueToRvalue(
306 M.makeDereference(
307 M.makeLvalueToRvalue(M.makeDeclRefExpr(TheValue), TheValueTy),
308 PointeeTy),
309 PointeeTy),
310 BO_EQ);
311
312 // Construct the body of the IfStmt.
313 Stmt *Stmts[2];
314 Stmts[0] =
315 M.makeAssignment(
316 M.makeDereference(
317 M.makeLvalueToRvalue(M.makeDeclRefExpr(TheValue), TheValueTy),
318 PointeeTy),
319 M.makeLvalueToRvalue(M.makeDeclRefExpr(NewValue), NewValueTy),
320 NewValueTy);
Ted Kremenek5dbd9902012-10-12 00:18:19 +0000321
322 Expr *BoolVal = M.makeObjCBool(true);
323 Expr *RetVal = isBoolean ? M.makeIntegralCastToBoolean(BoolVal)
324 : M.makeIntegralCast(BoolVal, ResultTy);
325 Stmts[1] = M.makeReturn(RetVal);
Ted Kremenek48fa1362012-10-11 20:58:18 +0000326 CompoundStmt *Body = M.makeCompound(ArrayRef<Stmt*>(Stmts, 2));
327
328 // Construct the else clause.
Ted Kremenek5dbd9902012-10-12 00:18:19 +0000329 BoolVal = M.makeObjCBool(false);
330 RetVal = isBoolean ? M.makeIntegralCastToBoolean(BoolVal)
331 : M.makeIntegralCast(BoolVal, ResultTy);
332 Stmt *Else = M.makeReturn(RetVal);
Ted Kremenek48fa1362012-10-11 20:58:18 +0000333
334 /// Construct the If.
335 Stmt *If =
336 new (C) IfStmt(C, SourceLocation(), 0, Comparison, Body,
337 SourceLocation(), Else);
338
339 return If;
340}
341
Ted Kremeneka43df952012-09-21 00:09:11 +0000342Stmt *BodyFarm::getBody(const FunctionDecl *D) {
343 D = D->getCanonicalDecl();
344
345 llvm::Optional<Stmt *> &Val = Bodies[D];
346 if (Val.hasValue())
347 return Val.getValue();
348
349 Val = 0;
350
351 if (D->getIdentifier() == 0)
352 return 0;
353
354 StringRef Name = D->getName();
355 if (Name.empty())
356 return 0;
Ted Kremenek48fa1362012-10-11 20:58:18 +0000357
358 FunctionFarmer FF;
359
360 if (Name.startswith("OSAtomicCompareAndSwap") ||
361 Name.startswith("objc_atomicCompareAndSwap")) {
362 FF = create_OSAtomicCompareAndSwap;
363 }
364 else {
365 FF = llvm::StringSwitch<FunctionFarmer>(Name)
366 .Case("dispatch_sync", create_dispatch_sync)
367 .Case("dispatch_once", create_dispatch_once)
368 .Default(NULL);
Ted Kremeneka43df952012-09-21 00:09:11 +0000369 }
370
Ted Kremenek48fa1362012-10-11 20:58:18 +0000371 if (FF) { Val = FF(C, D); }
Ted Kremeneka43df952012-09-21 00:09:11 +0000372 return Val.getValue();
373}
374