blob: 7b1122d5b007e0af1f335b3f3a24f4b63fde9e76 [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
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 "BodyFarm.h"
20
21using namespace clang;
22
Ted Kremenek0a4c0982012-09-21 17:54:32 +000023//===----------------------------------------------------------------------===//
24// Helper creation functions for constructing faux ASTs.
25//===----------------------------------------------------------------------===//
Ted Kremeneka43df952012-09-21 00:09:11 +000026
Ted Kremenekcc85d212012-09-21 00:52:24 +000027static bool isDispatchBlock(QualType Ty) {
28 // Is it a block pointer?
29 const BlockPointerType *BPT = Ty->getAs<BlockPointerType>();
30 if (!BPT)
31 return false;
32
33 // Check if the block pointer type takes no arguments and
34 // returns void.
35 const FunctionProtoType *FT =
36 BPT->getPointeeType()->getAs<FunctionProtoType>();
37 if (!FT || !FT->getResultType()->isVoidType() ||
38 FT->getNumArgs() != 0)
39 return false;
40
41 return true;
42}
43
Ted Kremenek0a4c0982012-09-21 17:54:32 +000044//===----------------------------------------------------------------------===//
45// Creation functions for faux ASTs.
46//===----------------------------------------------------------------------===//
47
48typedef Stmt *(*FunctionFarmer)(ASTContext &C, const FunctionDecl *D);
49
Ted Kremenekcc85d212012-09-21 00:52:24 +000050/// Create a fake body for dispatch_once.
51static Stmt *create_dispatch_once(ASTContext &C, const FunctionDecl *D) {
52 // Check if we have at least two parameters.
53 if (D->param_size() != 2)
54 return 0;
55
56 // Check if the first parameter is a pointer to integer type.
57 const ParmVarDecl *Predicate = D->getParamDecl(0);
58 QualType PredicateQPtrTy = Predicate->getType();
59 const PointerType *PredicatePtrTy = PredicateQPtrTy->getAs<PointerType>();
60 if (!PredicatePtrTy)
61 return 0;
62 QualType PredicateTy = PredicatePtrTy->getPointeeType();
63 if (!PredicateTy->isIntegerType())
64 return 0;
65
66 // Check if the second parameter is the proper block type.
67 const ParmVarDecl *Block = D->getParamDecl(1);
68 QualType Ty = Block->getType();
69 if (!isDispatchBlock(Ty))
70 return 0;
71
72 // Everything checks out. Create a fakse body that checks the predicate,
73 // sets it, and calls the block. Basically, an AST dump of:
74 //
75 // void dispatch_once(dispatch_once_t *predicate, dispatch_block_t block) {
76 // if (!*predicate) {
77 // *predicate = 1;
78 // block();
79 // }
80 // }
81
82 // (1) Create the call.
83 DeclRefExpr *DR = DeclRefExpr::CreateEmpty(C, false, false, false, false);
84 DR->setDecl(const_cast<ParmVarDecl*>(Block));
85 DR->setType(Ty);
86 DR->setValueKind(VK_LValue);
87 ImplicitCastExpr *ICE = ImplicitCastExpr::Create(C, Ty, CK_LValueToRValue,
88 DR, 0, VK_RValue);
89 CallExpr *CE = new (C) CallExpr(C, ICE, ArrayRef<Expr*>(), C.VoidTy,
90 VK_RValue, SourceLocation());
91
92 // (2) Create the assignment to the predicate.
93 IntegerLiteral *IL =
94 IntegerLiteral::Create(C, llvm::APInt(C.getTypeSize(C.IntTy), (uint64_t) 1),
95 C.IntTy, SourceLocation());
96 ICE = ImplicitCastExpr::Create(C, PredicateTy, CK_IntegralCast, IL, 0,
97 VK_RValue);
98 DR = DeclRefExpr::CreateEmpty(C, false, false, false, false);
99 DR->setDecl(const_cast<ParmVarDecl*>(Predicate));
100 DR->setType(PredicateQPtrTy);
101 DR->setValueKind(VK_LValue);
102 ImplicitCastExpr *LValToRval =
103 ImplicitCastExpr::Create(C, PredicateQPtrTy, CK_LValueToRValue, DR,
104 0, VK_RValue);
105 UnaryOperator *UO = new (C) UnaryOperator(LValToRval, UO_Deref, PredicateTy,
106 VK_LValue, OK_Ordinary,
107 SourceLocation());
108 BinaryOperator *B = new (C) BinaryOperator(UO, ICE, BO_Assign,
109 PredicateTy, VK_RValue,
110 OK_Ordinary,
111 SourceLocation());
112 // (3) Create the compound statement.
113 Stmt *Stmts[2];
114 Stmts[0] = B;
115 Stmts[1] = CE;
116 CompoundStmt *CS = new (C) CompoundStmt(C, Stmts, 2, SourceLocation(),
117 SourceLocation());
118
119 // (4) Create the 'if' condition.
120 DR = DeclRefExpr::CreateEmpty(C, false, false, false, false);
121 DR->setDecl(const_cast<ParmVarDecl*>(Predicate));
122 DR->setType(PredicateQPtrTy);
123 DR->setValueKind(VK_LValue);
124 LValToRval = ImplicitCastExpr::Create(C, PredicateQPtrTy, CK_LValueToRValue,
125 DR, 0, VK_RValue);
126 UO = new (C) UnaryOperator(LValToRval, UO_Deref, PredicateTy,
127 VK_LValue, OK_Ordinary,
128 SourceLocation());
129 LValToRval = ImplicitCastExpr::Create(C, PredicateTy, CK_LValueToRValue,
130 UO, 0, VK_RValue);
131 UO = new (C) UnaryOperator(LValToRval, UO_LNot, C.IntTy,
132 VK_RValue, OK_Ordinary, SourceLocation());
133
134 // (5) Create the 'if' statement.
135 IfStmt *If = new (C) IfStmt(C, SourceLocation(), 0, UO, CS);
136 return If;
137}
138
139
Ted Kremeneka43df952012-09-21 00:09:11 +0000140
141/// Create a fake body for dispatch_sync.
142static Stmt *create_dispatch_sync(ASTContext &C, const FunctionDecl *D) {
143 // Check if we have at least two parameters.
144 if (D->param_size() != 2)
145 return 0;
146
147 // Check if the second parameter is a block.
148 const ParmVarDecl *PV = D->getParamDecl(1);
149 QualType Ty = PV->getType();
Ted Kremenekcc85d212012-09-21 00:52:24 +0000150 if (!isDispatchBlock(Ty))
Ted Kremeneka43df952012-09-21 00:09:11 +0000151 return 0;
152
153 // Everything checks out. Create a fake body that just calls the block.
154 // This is basically just an AST dump of:
155 //
156 // void dispatch_sync(dispatch_queue_t queue, void (^block)(void)) {
157 // block();
158 // }
159 //
160 DeclRefExpr *DR = DeclRefExpr::CreateEmpty(C, false, false, false, false);
161 DR->setDecl(const_cast<ParmVarDecl*>(PV));
Ted Kremenekcc85d212012-09-21 00:52:24 +0000162 DR->setType(Ty);
Ted Kremeneka43df952012-09-21 00:09:11 +0000163 DR->setValueKind(VK_LValue);
164 ImplicitCastExpr *ICE = ImplicitCastExpr::Create(C, Ty, CK_LValueToRValue,
165 DR, 0, VK_RValue);
166 CallExpr *CE = new (C) CallExpr(C, ICE, ArrayRef<Expr*>(), C.VoidTy,
167 VK_RValue, SourceLocation());
168 return CE;
169}
170
171Stmt *BodyFarm::getBody(const FunctionDecl *D) {
172 D = D->getCanonicalDecl();
173
174 llvm::Optional<Stmt *> &Val = Bodies[D];
175 if (Val.hasValue())
176 return Val.getValue();
177
178 Val = 0;
179
180 if (D->getIdentifier() == 0)
181 return 0;
182
183 StringRef Name = D->getName();
184 if (Name.empty())
185 return 0;
186
187 FunctionFarmer FF =
188 llvm::StringSwitch<FunctionFarmer>(Name)
189 .Case("dispatch_sync", create_dispatch_sync)
Ted Kremenekcc85d212012-09-21 00:52:24 +0000190 .Case("dispatch_once", create_dispatch_once)
Ted Kremeneka43df952012-09-21 00:09:11 +0000191 .Default(NULL);
192
193 if (FF) {
194 Val = FF(C, D);
195 }
196
197 return Val.getValue();
198}
199