blob: 54c8ab4430080f8c757498910a3daba67ec57ec5 [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 Kremenek016c33d2012-09-21 17:54:35 +000044namespace {
45class ASTMaker {
46public:
47 ASTMaker(ASTContext &C) : C(C) {}
48
Ted Kremenekb80e5bb2012-09-21 18:33:54 +000049 /// Create a new BinaryOperator representing a simple assignment.
50 BinaryOperator *makeAssignment(const Expr *LHS, const Expr *RHS, QualType Ty);
51
Ted Kremeneka6d62a12012-09-21 18:13:23 +000052 /// Create a new DeclRefExpr for the referenced variable.
Ted Kremenek016c33d2012-09-21 17:54:35 +000053 DeclRefExpr *makeDeclRefExpr(const VarDecl *D);
54
Ted Kremenek0b5c5e42012-09-21 18:33:52 +000055 /// Create a new UnaryOperator representing a dereference.
56 UnaryOperator *makeDereference(const Expr *Arg, QualType Ty);
57
Ted Kremeneka6d62a12012-09-21 18:13:23 +000058 /// Create an implicit cast for an integer conversion.
59 ImplicitCastExpr *makeIntegralCast(const Expr *Arg, QualType Ty);
60
Ted Kremenek9ba05cd2012-09-21 18:13:27 +000061 // Create an implicit cast for lvalue-to-rvaluate conversions.
62 ImplicitCastExpr *makeLvalueToRvalue(const Expr *Arg, QualType Ty);
63
Ted Kremenek016c33d2012-09-21 17:54:35 +000064private:
65 ASTContext &C;
66};
67}
68
Ted Kremenekb80e5bb2012-09-21 18:33:54 +000069BinaryOperator *ASTMaker::makeAssignment(const Expr *LHS, const Expr *RHS,
70 QualType Ty) {
71 return new (C) BinaryOperator(const_cast<Expr*>(LHS), const_cast<Expr*>(RHS),
72 BO_Assign, Ty, VK_RValue,
73 OK_Ordinary, SourceLocation());
74}
75
Ted Kremenek016c33d2012-09-21 17:54:35 +000076DeclRefExpr *ASTMaker::makeDeclRefExpr(const VarDecl *D) {
77 DeclRefExpr *DR =
78 DeclRefExpr::Create(/* Ctx = */ C,
79 /* QualifierLoc = */ NestedNameSpecifierLoc(),
80 /* TemplateKWLoc = */ SourceLocation(),
81 /* D = */ const_cast<VarDecl*>(D),
82 /* isEnclosingLocal = */ false,
83 /* NameLoc = */ SourceLocation(),
84 /* T = */ D->getType(),
85 /* VK = */ VK_LValue);
86 return DR;
87}
88
Ted Kremenek0b5c5e42012-09-21 18:33:52 +000089UnaryOperator *ASTMaker::makeDereference(const Expr *Arg, QualType Ty) {
90 return new (C) UnaryOperator(const_cast<Expr*>(Arg), UO_Deref, Ty,
91 VK_LValue, OK_Ordinary, SourceLocation());
92}
93
Ted Kremenek9ba05cd2012-09-21 18:13:27 +000094ImplicitCastExpr *ASTMaker::makeLvalueToRvalue(const Expr *Arg, QualType Ty) {
95 return ImplicitCastExpr::Create(C, Ty, CK_LValueToRValue,
96 const_cast<Expr*>(Arg), 0, VK_RValue);
97}
98
Ted Kremeneka6d62a12012-09-21 18:13:23 +000099ImplicitCastExpr *ASTMaker::makeIntegralCast(const Expr *Arg, QualType Ty) {
100 return ImplicitCastExpr::Create(C, Ty, CK_IntegralCast,
101 const_cast<Expr*>(Arg), 0, VK_RValue);
102}
103
Ted Kremenek0a4c0982012-09-21 17:54:32 +0000104//===----------------------------------------------------------------------===//
105// Creation functions for faux ASTs.
106//===----------------------------------------------------------------------===//
107
108typedef Stmt *(*FunctionFarmer)(ASTContext &C, const FunctionDecl *D);
109
Ted Kremenekcc85d212012-09-21 00:52:24 +0000110/// Create a fake body for dispatch_once.
111static Stmt *create_dispatch_once(ASTContext &C, const FunctionDecl *D) {
112 // Check if we have at least two parameters.
113 if (D->param_size() != 2)
114 return 0;
115
116 // Check if the first parameter is a pointer to integer type.
117 const ParmVarDecl *Predicate = D->getParamDecl(0);
118 QualType PredicateQPtrTy = Predicate->getType();
119 const PointerType *PredicatePtrTy = PredicateQPtrTy->getAs<PointerType>();
120 if (!PredicatePtrTy)
121 return 0;
122 QualType PredicateTy = PredicatePtrTy->getPointeeType();
123 if (!PredicateTy->isIntegerType())
124 return 0;
125
126 // Check if the second parameter is the proper block type.
127 const ParmVarDecl *Block = D->getParamDecl(1);
128 QualType Ty = Block->getType();
129 if (!isDispatchBlock(Ty))
130 return 0;
131
132 // Everything checks out. Create a fakse body that checks the predicate,
133 // sets it, and calls the block. Basically, an AST dump of:
134 //
135 // void dispatch_once(dispatch_once_t *predicate, dispatch_block_t block) {
136 // if (!*predicate) {
137 // *predicate = 1;
138 // block();
139 // }
140 // }
141
Ted Kremenek016c33d2012-09-21 17:54:35 +0000142 ASTMaker M(C);
143
Ted Kremenekcc85d212012-09-21 00:52:24 +0000144 // (1) Create the call.
Ted Kremenek016c33d2012-09-21 17:54:35 +0000145 DeclRefExpr *DR = M.makeDeclRefExpr(Block);
Ted Kremenek9ba05cd2012-09-21 18:13:27 +0000146 ImplicitCastExpr *ICE = M.makeLvalueToRvalue(DR, Ty);
Ted Kremenekcc85d212012-09-21 00:52:24 +0000147 CallExpr *CE = new (C) CallExpr(C, ICE, ArrayRef<Expr*>(), C.VoidTy,
148 VK_RValue, SourceLocation());
149
150 // (2) Create the assignment to the predicate.
151 IntegerLiteral *IL =
152 IntegerLiteral::Create(C, llvm::APInt(C.getTypeSize(C.IntTy), (uint64_t) 1),
153 C.IntTy, SourceLocation());
Ted Kremeneka6d62a12012-09-21 18:13:23 +0000154 ICE = M.makeIntegralCast(IL, PredicateTy);
Ted Kremenek016c33d2012-09-21 17:54:35 +0000155 DR = M.makeDeclRefExpr(Predicate);
Ted Kremenek9ba05cd2012-09-21 18:13:27 +0000156 ImplicitCastExpr *LValToRval = M.makeLvalueToRvalue(DR, PredicateQPtrTy);
Ted Kremenek0b5c5e42012-09-21 18:33:52 +0000157 UnaryOperator *UO = M.makeDereference(LValToRval, PredicateTy);
Ted Kremenekb80e5bb2012-09-21 18:33:54 +0000158 BinaryOperator * B = M.makeAssignment(UO, ICE, PredicateTy);
159
Ted Kremenekcc85d212012-09-21 00:52:24 +0000160 // (3) Create the compound statement.
161 Stmt *Stmts[2];
162 Stmts[0] = B;
163 Stmts[1] = CE;
164 CompoundStmt *CS = new (C) CompoundStmt(C, Stmts, 2, SourceLocation(),
165 SourceLocation());
166
167 // (4) Create the 'if' condition.
Ted Kremenek016c33d2012-09-21 17:54:35 +0000168 DR = M.makeDeclRefExpr(Predicate);
Ted Kremenek9ba05cd2012-09-21 18:13:27 +0000169 LValToRval = M.makeLvalueToRvalue(DR, PredicateQPtrTy);
Ted Kremenek0b5c5e42012-09-21 18:33:52 +0000170 UO = M.makeDereference(LValToRval, PredicateTy);
Ted Kremenek9ba05cd2012-09-21 18:13:27 +0000171 LValToRval = M.makeLvalueToRvalue(UO, PredicateTy);
Ted Kremenekcc85d212012-09-21 00:52:24 +0000172 UO = new (C) UnaryOperator(LValToRval, UO_LNot, C.IntTy,
173 VK_RValue, OK_Ordinary, SourceLocation());
174
175 // (5) Create the 'if' statement.
176 IfStmt *If = new (C) IfStmt(C, SourceLocation(), 0, UO, CS);
177 return If;
178}
179
Ted Kremeneka43df952012-09-21 00:09:11 +0000180/// Create a fake body for dispatch_sync.
181static Stmt *create_dispatch_sync(ASTContext &C, const FunctionDecl *D) {
182 // Check if we have at least two parameters.
183 if (D->param_size() != 2)
184 return 0;
185
186 // Check if the second parameter is a block.
187 const ParmVarDecl *PV = D->getParamDecl(1);
188 QualType Ty = PV->getType();
Ted Kremenekcc85d212012-09-21 00:52:24 +0000189 if (!isDispatchBlock(Ty))
Ted Kremeneka43df952012-09-21 00:09:11 +0000190 return 0;
Ted Kremenek016c33d2012-09-21 17:54:35 +0000191
Ted Kremeneka43df952012-09-21 00:09:11 +0000192 // Everything checks out. Create a fake body that just calls the block.
193 // This is basically just an AST dump of:
194 //
195 // void dispatch_sync(dispatch_queue_t queue, void (^block)(void)) {
196 // block();
197 // }
Ted Kremenek016c33d2012-09-21 17:54:35 +0000198 //
199 ASTMaker M(C);
200 DeclRefExpr *DR = M.makeDeclRefExpr(PV);
Ted Kremenek0b5c5e42012-09-21 18:33:52 +0000201 ImplicitCastExpr *ICE = M.makeLvalueToRvalue(DR, Ty);
Ted Kremeneka43df952012-09-21 00:09:11 +0000202 CallExpr *CE = new (C) CallExpr(C, ICE, ArrayRef<Expr*>(), C.VoidTy,
203 VK_RValue, SourceLocation());
204 return CE;
205}
206
207Stmt *BodyFarm::getBody(const FunctionDecl *D) {
208 D = D->getCanonicalDecl();
209
210 llvm::Optional<Stmt *> &Val = Bodies[D];
211 if (Val.hasValue())
212 return Val.getValue();
213
214 Val = 0;
215
216 if (D->getIdentifier() == 0)
217 return 0;
218
219 StringRef Name = D->getName();
220 if (Name.empty())
221 return 0;
222
223 FunctionFarmer FF =
224 llvm::StringSwitch<FunctionFarmer>(Name)
225 .Case("dispatch_sync", create_dispatch_sync)
Ted Kremenekcc85d212012-09-21 00:52:24 +0000226 .Case("dispatch_once", create_dispatch_once)
Ted Kremeneka43df952012-09-21 00:09:11 +0000227 .Default(NULL);
228
229 if (FF) {
230 Val = FF(C, D);
231 }
232
233 return Val.getValue();
234}
235