BodyFarm.cpp revision b80e5bb7c7c47e06f2ff9c2f9d4b6b138db180e2
1//== 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 23//===----------------------------------------------------------------------===// 24// Helper creation functions for constructing faux ASTs. 25//===----------------------------------------------------------------------===// 26 27static 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 44namespace { 45class ASTMaker { 46public: 47 ASTMaker(ASTContext &C) : C(C) {} 48 49 /// Create a new BinaryOperator representing a simple assignment. 50 BinaryOperator *makeAssignment(const Expr *LHS, const Expr *RHS, QualType Ty); 51 52 /// Create a new DeclRefExpr for the referenced variable. 53 DeclRefExpr *makeDeclRefExpr(const VarDecl *D); 54 55 /// Create a new UnaryOperator representing a dereference. 56 UnaryOperator *makeDereference(const Expr *Arg, QualType Ty); 57 58 /// Create an implicit cast for an integer conversion. 59 ImplicitCastExpr *makeIntegralCast(const Expr *Arg, QualType Ty); 60 61 // Create an implicit cast for lvalue-to-rvaluate conversions. 62 ImplicitCastExpr *makeLvalueToRvalue(const Expr *Arg, QualType Ty); 63 64private: 65 ASTContext &C; 66}; 67} 68 69BinaryOperator *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 76DeclRefExpr *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 89UnaryOperator *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 94ImplicitCastExpr *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 99ImplicitCastExpr *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 104//===----------------------------------------------------------------------===// 105// Creation functions for faux ASTs. 106//===----------------------------------------------------------------------===// 107 108typedef Stmt *(*FunctionFarmer)(ASTContext &C, const FunctionDecl *D); 109 110/// 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 142 ASTMaker M(C); 143 144 // (1) Create the call. 145 DeclRefExpr *DR = M.makeDeclRefExpr(Block); 146 ImplicitCastExpr *ICE = M.makeLvalueToRvalue(DR, Ty); 147 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()); 154 ICE = M.makeIntegralCast(IL, PredicateTy); 155 DR = M.makeDeclRefExpr(Predicate); 156 ImplicitCastExpr *LValToRval = M.makeLvalueToRvalue(DR, PredicateQPtrTy); 157 UnaryOperator *UO = M.makeDereference(LValToRval, PredicateTy); 158 BinaryOperator * B = M.makeAssignment(UO, ICE, PredicateTy); 159 160 // (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. 168 DR = M.makeDeclRefExpr(Predicate); 169 LValToRval = M.makeLvalueToRvalue(DR, PredicateQPtrTy); 170 UO = M.makeDereference(LValToRval, PredicateTy); 171 LValToRval = M.makeLvalueToRvalue(UO, PredicateTy); 172 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 180/// 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(); 189 if (!isDispatchBlock(Ty)) 190 return 0; 191 192 // 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 // } 198 // 199 ASTMaker M(C); 200 DeclRefExpr *DR = M.makeDeclRefExpr(PV); 201 ImplicitCastExpr *ICE = M.makeLvalueToRvalue(DR, Ty); 202 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) 226 .Case("dispatch_once", create_dispatch_once) 227 .Default(NULL); 228 229 if (FF) { 230 Val = FF(C, D); 231 } 232 233 return Val.getValue(); 234} 235 236