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