1 //== BodyFarm.cpp - Factory for conjuring up fake bodies ----------*- C++ -*-//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // BodyFarm is a factory for creating faux implementations for functions/methods
11 // for analysis purposes.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/ADT/StringSwitch.h"
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/Expr.h"
18 #include "clang/AST/Decl.h"
19 #include "clang/AST/ExprObjC.h"
22 using namespace clang;
24 //===----------------------------------------------------------------------===//
25 // Helper creation functions for constructing faux ASTs.
26 //===----------------------------------------------------------------------===//
28 static bool isDispatchBlock(QualType Ty) {
29 // Is it a block pointer?
30 const BlockPointerType *BPT = Ty->getAs<BlockPointerType>();
34 // Check if the block pointer type takes no arguments and
36 const FunctionProtoType *FT =
37 BPT->getPointeeType()->getAs<FunctionProtoType>();
38 if (!FT || !FT->getResultType()->isVoidType() ||
39 FT->getNumArgs() != 0)
48 ASTMaker(ASTContext &C) : C(C) {}
50 /// Create a new BinaryOperator representing a simple assignment.
51 BinaryOperator *makeAssignment(const Expr *LHS, const Expr *RHS, QualType Ty);
53 /// Create a new BinaryOperator representing a comparison.
54 BinaryOperator *makeComparison(const Expr *LHS, const Expr *RHS,
55 BinaryOperator::Opcode Op);
57 /// Create a new compound stmt using the provided statements.
58 CompoundStmt *makeCompound(ArrayRef<Stmt*>);
60 /// Create a new DeclRefExpr for the referenced variable.
61 DeclRefExpr *makeDeclRefExpr(const VarDecl *D);
63 /// Create a new UnaryOperator representing a dereference.
64 UnaryOperator *makeDereference(const Expr *Arg, QualType Ty);
66 /// Create an implicit cast for an integer conversion.
67 Expr *makeIntegralCast(const Expr *Arg, QualType Ty);
69 /// Create an implicit cast to a builtin boolean type.
70 ImplicitCastExpr *makeIntegralCastToBoolean(const Expr *Arg);
72 // Create an implicit cast for lvalue-to-rvaluate conversions.
73 ImplicitCastExpr *makeLvalueToRvalue(const Expr *Arg, QualType Ty);
75 /// Create an Objective-C bool literal.
76 ObjCBoolLiteralExpr *makeObjCBool(bool Val);
78 /// Create a Return statement.
79 ReturnStmt *makeReturn(const Expr *RetVal);
86 BinaryOperator *ASTMaker::makeAssignment(const Expr *LHS, const Expr *RHS,
88 return new (C) BinaryOperator(const_cast<Expr*>(LHS), const_cast<Expr*>(RHS),
89 BO_Assign, Ty, VK_RValue,
90 OK_Ordinary, SourceLocation(), false);
93 BinaryOperator *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),
100 C.getLogicalOperationType(),
102 OK_Ordinary, SourceLocation(), false);
105 CompoundStmt *ASTMaker::makeCompound(ArrayRef<Stmt *> Stmts) {
106 return new (C) CompoundStmt(C, const_cast<Stmt**>(Stmts.data()),
108 SourceLocation(), SourceLocation());
111 DeclRefExpr *ASTMaker::makeDeclRefExpr(const VarDecl *D) {
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);
124 UnaryOperator *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());
129 ImplicitCastExpr *ASTMaker::makeLvalueToRvalue(const Expr *Arg, QualType Ty) {
130 return ImplicitCastExpr::Create(C, Ty, CK_LValueToRValue,
131 const_cast<Expr*>(Arg), 0, VK_RValue);
134 Expr *ASTMaker::makeIntegralCast(const Expr *Arg, QualType Ty) {
135 if (Arg->getType() == Ty)
136 return const_cast<Expr*>(Arg);
138 return ImplicitCastExpr::Create(C, Ty, CK_IntegralCast,
139 const_cast<Expr*>(Arg), 0, VK_RValue);
142 ImplicitCastExpr *ASTMaker::makeIntegralCastToBoolean(const Expr *Arg) {
143 return ImplicitCastExpr::Create(C, C.BoolTy, CK_IntegralToBoolean,
144 const_cast<Expr*>(Arg), 0, VK_RValue);
147 ObjCBoolLiteralExpr *ASTMaker::makeObjCBool(bool Val) {
148 QualType Ty = C.getBOOLDecl() ? C.getBOOLType() : C.ObjCBuiltinBoolTy;
149 return new (C) ObjCBoolLiteralExpr(Val, Ty, SourceLocation());
152 ReturnStmt *ASTMaker::makeReturn(const Expr *RetVal) {
153 return new (C) ReturnStmt(SourceLocation(), const_cast<Expr*>(RetVal), 0);
156 //===----------------------------------------------------------------------===//
157 // Creation functions for faux ASTs.
158 //===----------------------------------------------------------------------===//
160 typedef Stmt *(*FunctionFarmer)(ASTContext &C, const FunctionDecl *D);
162 /// Create a fake body for dispatch_once.
163 static Stmt *create_dispatch_once(ASTContext &C, const FunctionDecl *D) {
164 // Check if we have at least two parameters.
165 if (D->param_size() != 2)
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>();
174 QualType PredicateTy = PredicatePtrTy->getPointeeType();
175 if (!PredicateTy->isIntegerType())
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))
184 // Everything checks out. Create a fakse body that checks the predicate,
185 // sets it, and calls the block. Basically, an AST dump of:
187 // void dispatch_once(dispatch_once_t *predicate, dispatch_block_t block) {
188 // if (!*predicate) {
196 // (1) Create the call.
197 DeclRefExpr *DR = M.makeDeclRefExpr(Block);
198 ImplicitCastExpr *ICE = M.makeLvalueToRvalue(DR, Ty);
199 CallExpr *CE = new (C) CallExpr(C, ICE, ArrayRef<Expr*>(), C.VoidTy,
200 VK_RValue, SourceLocation());
202 // (2) Create the assignment to the predicate.
204 IntegerLiteral::Create(C, llvm::APInt(C.getTypeSize(C.IntTy), (uint64_t) 1),
205 C.IntTy, SourceLocation());
209 M.makeLvalueToRvalue(
210 M.makeDeclRefExpr(Predicate), PredicateQPtrTy),
212 M.makeIntegralCast(IL, PredicateTy),
215 // (3) Create the compound statement.
219 CompoundStmt *CS = M.makeCompound(ArrayRef<Stmt*>(Stmts, 2));
221 // (4) Create the 'if' condition.
222 ImplicitCastExpr *LValToRval =
223 M.makeLvalueToRvalue(
225 M.makeLvalueToRvalue(
226 M.makeDeclRefExpr(Predicate),
231 UnaryOperator *UO = new (C) UnaryOperator(LValToRval, UO_LNot, C.IntTy,
232 VK_RValue, OK_Ordinary,
235 // (5) Create the 'if' statement.
236 IfStmt *If = new (C) IfStmt(C, SourceLocation(), 0, UO, CS);
240 /// Create a fake body for dispatch_sync.
241 static Stmt *create_dispatch_sync(ASTContext &C, const FunctionDecl *D) {
242 // Check if we have at least two parameters.
243 if (D->param_size() != 2)
246 // Check if the second parameter is a block.
247 const ParmVarDecl *PV = D->getParamDecl(1);
248 QualType Ty = PV->getType();
249 if (!isDispatchBlock(Ty))
252 // Everything checks out. Create a fake body that just calls the block.
253 // This is basically just an AST dump of:
255 // void dispatch_sync(dispatch_queue_t queue, void (^block)(void)) {
260 DeclRefExpr *DR = M.makeDeclRefExpr(PV);
261 ImplicitCastExpr *ICE = M.makeLvalueToRvalue(DR, Ty);
262 CallExpr *CE = new (C) CallExpr(C, ICE, ArrayRef<Expr*>(), C.VoidTy,
263 VK_RValue, SourceLocation());
267 static Stmt *create_OSAtomicCompareAndSwap(ASTContext &C, const FunctionDecl *D)
269 // There are exactly 3 arguments.
270 if (D->param_size() != 3)
274 // if (oldValue == *theValue) {
275 // *theValue = newValue;
280 QualType ResultTy = D->getResultType();
281 bool isBoolean = ResultTy->isBooleanType();
282 if (!isBoolean && !ResultTy->isIntegralType(C))
285 const ParmVarDecl *OldValue = D->getParamDecl(0);
286 QualType OldValueTy = OldValue->getType();
288 const ParmVarDecl *NewValue = D->getParamDecl(1);
289 QualType NewValueTy = NewValue->getType();
291 assert(OldValueTy == NewValueTy);
293 const ParmVarDecl *TheValue = D->getParamDecl(2);
294 QualType TheValueTy = TheValue->getType();
295 const PointerType *PT = TheValueTy->getAs<PointerType>();
298 QualType PointeeTy = PT->getPointeeType();
301 // Construct the comparison.
304 M.makeLvalueToRvalue(M.makeDeclRefExpr(OldValue), OldValueTy),
305 M.makeLvalueToRvalue(
307 M.makeLvalueToRvalue(M.makeDeclRefExpr(TheValue), TheValueTy),
312 // Construct the body of the IfStmt.
317 M.makeLvalueToRvalue(M.makeDeclRefExpr(TheValue), TheValueTy),
319 M.makeLvalueToRvalue(M.makeDeclRefExpr(NewValue), NewValueTy),
322 Expr *BoolVal = M.makeObjCBool(true);
323 Expr *RetVal = isBoolean ? M.makeIntegralCastToBoolean(BoolVal)
324 : M.makeIntegralCast(BoolVal, ResultTy);
325 Stmts[1] = M.makeReturn(RetVal);
326 CompoundStmt *Body = M.makeCompound(ArrayRef<Stmt*>(Stmts, 2));
328 // Construct the else clause.
329 BoolVal = M.makeObjCBool(false);
330 RetVal = isBoolean ? M.makeIntegralCastToBoolean(BoolVal)
331 : M.makeIntegralCast(BoolVal, ResultTy);
332 Stmt *Else = M.makeReturn(RetVal);
334 /// Construct the If.
336 new (C) IfStmt(C, SourceLocation(), 0, Comparison, Body,
337 SourceLocation(), Else);
342 Stmt *BodyFarm::getBody(const FunctionDecl *D) {
343 D = D->getCanonicalDecl();
345 llvm::Optional<Stmt *> &Val = Bodies[D];
347 return Val.getValue();
351 if (D->getIdentifier() == 0)
354 StringRef Name = D->getName();
360 if (Name.startswith("OSAtomicCompareAndSwap") ||
361 Name.startswith("objc_atomicCompareAndSwap")) {
362 FF = create_OSAtomicCompareAndSwap;
365 FF = llvm::StringSwitch<FunctionFarmer>(Name)
366 .Case("dispatch_sync", create_dispatch_sync)
367 .Case("dispatch_once", create_dispatch_once)
371 if (FF) { Val = FF(C, D); }
372 return Val.getValue();