Ted Kremenek | 14f779c | 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 | |
Ted Kremenek | 14f779c | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 15 | #include "BodyFarm.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
| 17 | #include "clang/AST/Decl.h" |
| 18 | #include "clang/AST/Expr.h" |
| 19 | #include "clang/AST/ExprObjC.h" |
Chandler Carruth | 0d9593d | 2015-01-14 11:29:14 +0000 | [diff] [blame] | 20 | #include "clang/Analysis/CodeInjector.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringSwitch.h" |
Ted Kremenek | 14f779c | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace clang; |
| 24 | |
Ted Kremenek | 2b5c83c | 2012-09-21 17:54:32 +0000 | [diff] [blame] | 25 | //===----------------------------------------------------------------------===// |
| 26 | // Helper creation functions for constructing faux ASTs. |
| 27 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 14f779c | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 28 | |
Ted Kremenek | d81a4a1 | 2012-09-21 00:52:24 +0000 | [diff] [blame] | 29 | static bool isDispatchBlock(QualType Ty) { |
| 30 | // Is it a block pointer? |
| 31 | const BlockPointerType *BPT = Ty->getAs<BlockPointerType>(); |
| 32 | if (!BPT) |
| 33 | return false; |
| 34 | |
| 35 | // Check if the block pointer type takes no arguments and |
| 36 | // returns void. |
| 37 | const FunctionProtoType *FT = |
| 38 | BPT->getPointeeType()->getAs<FunctionProtoType>(); |
Alexander Kornienko | 090360d | 2015-11-06 01:08:38 +0000 | [diff] [blame] | 39 | return FT && FT->getReturnType()->isVoidType() && FT->getNumParams() == 0; |
Ted Kremenek | d81a4a1 | 2012-09-21 00:52:24 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Ted Kremenek | 7241813 | 2012-09-21 17:54:35 +0000 | [diff] [blame] | 42 | namespace { |
| 43 | class ASTMaker { |
| 44 | public: |
| 45 | ASTMaker(ASTContext &C) : C(C) {} |
| 46 | |
Ted Kremenek | f465dc1 | 2012-09-21 18:33:54 +0000 | [diff] [blame] | 47 | /// Create a new BinaryOperator representing a simple assignment. |
| 48 | BinaryOperator *makeAssignment(const Expr *LHS, const Expr *RHS, QualType Ty); |
| 49 | |
Ted Kremenek | 089ffd0 | 2012-10-11 20:58:18 +0000 | [diff] [blame] | 50 | /// Create a new BinaryOperator representing a comparison. |
| 51 | BinaryOperator *makeComparison(const Expr *LHS, const Expr *RHS, |
| 52 | BinaryOperator::Opcode Op); |
| 53 | |
| 54 | /// Create a new compound stmt using the provided statements. |
| 55 | CompoundStmt *makeCompound(ArrayRef<Stmt*>); |
| 56 | |
Ted Kremenek | 69bcb82 | 2012-09-21 18:13:23 +0000 | [diff] [blame] | 57 | /// Create a new DeclRefExpr for the referenced variable. |
Ted Kremenek | 7241813 | 2012-09-21 17:54:35 +0000 | [diff] [blame] | 58 | DeclRefExpr *makeDeclRefExpr(const VarDecl *D); |
| 59 | |
Ted Kremenek | dff3553 | 2012-09-21 18:33:52 +0000 | [diff] [blame] | 60 | /// Create a new UnaryOperator representing a dereference. |
| 61 | UnaryOperator *makeDereference(const Expr *Arg, QualType Ty); |
| 62 | |
Ted Kremenek | 69bcb82 | 2012-09-21 18:13:23 +0000 | [diff] [blame] | 63 | /// Create an implicit cast for an integer conversion. |
Ted Kremenek | 6fdefb5 | 2012-10-12 00:18:19 +0000 | [diff] [blame] | 64 | Expr *makeIntegralCast(const Expr *Arg, QualType Ty); |
Ted Kremenek | 69bcb82 | 2012-09-21 18:13:23 +0000 | [diff] [blame] | 65 | |
Ted Kremenek | 089ffd0 | 2012-10-11 20:58:18 +0000 | [diff] [blame] | 66 | /// Create an implicit cast to a builtin boolean type. |
| 67 | ImplicitCastExpr *makeIntegralCastToBoolean(const Expr *Arg); |
| 68 | |
Ted Kremenek | ca90ea5 | 2012-09-21 18:13:27 +0000 | [diff] [blame] | 69 | // Create an implicit cast for lvalue-to-rvaluate conversions. |
| 70 | ImplicitCastExpr *makeLvalueToRvalue(const Expr *Arg, QualType Ty); |
| 71 | |
Ted Kremenek | 089ffd0 | 2012-10-11 20:58:18 +0000 | [diff] [blame] | 72 | /// Create an Objective-C bool literal. |
| 73 | ObjCBoolLiteralExpr *makeObjCBool(bool Val); |
Jordan Rose | 1a866cd | 2014-01-10 20:06:06 +0000 | [diff] [blame] | 74 | |
| 75 | /// Create an Objective-C ivar reference. |
| 76 | ObjCIvarRefExpr *makeObjCIvarRef(const Expr *Base, const ObjCIvarDecl *IVar); |
Ted Kremenek | 089ffd0 | 2012-10-11 20:58:18 +0000 | [diff] [blame] | 77 | |
| 78 | /// Create a Return statement. |
| 79 | ReturnStmt *makeReturn(const Expr *RetVal); |
| 80 | |
Ted Kremenek | 7241813 | 2012-09-21 17:54:35 +0000 | [diff] [blame] | 81 | private: |
| 82 | ASTContext &C; |
| 83 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 84 | } |
Ted Kremenek | 7241813 | 2012-09-21 17:54:35 +0000 | [diff] [blame] | 85 | |
Ted Kremenek | f465dc1 | 2012-09-21 18:33:54 +0000 | [diff] [blame] | 86 | BinaryOperator *ASTMaker::makeAssignment(const Expr *LHS, const Expr *RHS, |
| 87 | QualType Ty) { |
| 88 | return new (C) BinaryOperator(const_cast<Expr*>(LHS), const_cast<Expr*>(RHS), |
| 89 | BO_Assign, Ty, VK_RValue, |
Lang Hames | 5de91cc | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 90 | OK_Ordinary, SourceLocation(), false); |
Ted Kremenek | f465dc1 | 2012-09-21 18:33:54 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Ted Kremenek | 089ffd0 | 2012-10-11 20:58:18 +0000 | [diff] [blame] | 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), |
| 99 | Op, |
| 100 | C.getLogicalOperationType(), |
| 101 | VK_RValue, |
| 102 | OK_Ordinary, SourceLocation(), false); |
| 103 | } |
| 104 | |
| 105 | CompoundStmt *ASTMaker::makeCompound(ArrayRef<Stmt *> Stmts) { |
Nico Weber | a2a0eb9 | 2012-12-29 20:03:39 +0000 | [diff] [blame] | 106 | return new (C) CompoundStmt(C, Stmts, SourceLocation(), SourceLocation()); |
Ted Kremenek | 089ffd0 | 2012-10-11 20:58:18 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Ted Kremenek | 7241813 | 2012-09-21 17:54:35 +0000 | [diff] [blame] | 109 | DeclRefExpr *ASTMaker::makeDeclRefExpr(const VarDecl *D) { |
| 110 | DeclRefExpr *DR = |
| 111 | DeclRefExpr::Create(/* Ctx = */ C, |
| 112 | /* QualifierLoc = */ NestedNameSpecifierLoc(), |
| 113 | /* TemplateKWLoc = */ SourceLocation(), |
| 114 | /* D = */ const_cast<VarDecl*>(D), |
Alexey Bataev | 19acc3d | 2015-01-12 10:17:46 +0000 | [diff] [blame] | 115 | /* RefersToEnclosingVariableOrCapture = */ false, |
Ted Kremenek | 7241813 | 2012-09-21 17:54:35 +0000 | [diff] [blame] | 116 | /* NameLoc = */ SourceLocation(), |
| 117 | /* T = */ D->getType(), |
| 118 | /* VK = */ VK_LValue); |
| 119 | return DR; |
| 120 | } |
| 121 | |
Ted Kremenek | dff3553 | 2012-09-21 18:33:52 +0000 | [diff] [blame] | 122 | UnaryOperator *ASTMaker::makeDereference(const Expr *Arg, QualType Ty) { |
| 123 | return new (C) UnaryOperator(const_cast<Expr*>(Arg), UO_Deref, Ty, |
| 124 | VK_LValue, OK_Ordinary, SourceLocation()); |
| 125 | } |
| 126 | |
Ted Kremenek | ca90ea5 | 2012-09-21 18:13:27 +0000 | [diff] [blame] | 127 | ImplicitCastExpr *ASTMaker::makeLvalueToRvalue(const Expr *Arg, QualType Ty) { |
| 128 | return ImplicitCastExpr::Create(C, Ty, CK_LValueToRValue, |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 129 | const_cast<Expr*>(Arg), nullptr, VK_RValue); |
Ted Kremenek | ca90ea5 | 2012-09-21 18:13:27 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Ted Kremenek | 6fdefb5 | 2012-10-12 00:18:19 +0000 | [diff] [blame] | 132 | Expr *ASTMaker::makeIntegralCast(const Expr *Arg, QualType Ty) { |
| 133 | if (Arg->getType() == Ty) |
| 134 | return const_cast<Expr*>(Arg); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 135 | |
Ted Kremenek | 69bcb82 | 2012-09-21 18:13:23 +0000 | [diff] [blame] | 136 | return ImplicitCastExpr::Create(C, Ty, CK_IntegralCast, |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 137 | const_cast<Expr*>(Arg), nullptr, VK_RValue); |
Ted Kremenek | 69bcb82 | 2012-09-21 18:13:23 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Ted Kremenek | 089ffd0 | 2012-10-11 20:58:18 +0000 | [diff] [blame] | 140 | ImplicitCastExpr *ASTMaker::makeIntegralCastToBoolean(const Expr *Arg) { |
| 141 | return ImplicitCastExpr::Create(C, C.BoolTy, CK_IntegralToBoolean, |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 142 | const_cast<Expr*>(Arg), nullptr, VK_RValue); |
Ted Kremenek | 089ffd0 | 2012-10-11 20:58:18 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | ObjCBoolLiteralExpr *ASTMaker::makeObjCBool(bool Val) { |
| 146 | QualType Ty = C.getBOOLDecl() ? C.getBOOLType() : C.ObjCBuiltinBoolTy; |
| 147 | return new (C) ObjCBoolLiteralExpr(Val, Ty, SourceLocation()); |
| 148 | } |
| 149 | |
Jordan Rose | 1a866cd | 2014-01-10 20:06:06 +0000 | [diff] [blame] | 150 | ObjCIvarRefExpr *ASTMaker::makeObjCIvarRef(const Expr *Base, |
| 151 | const ObjCIvarDecl *IVar) { |
| 152 | return new (C) ObjCIvarRefExpr(const_cast<ObjCIvarDecl*>(IVar), |
| 153 | IVar->getType(), SourceLocation(), |
| 154 | SourceLocation(), const_cast<Expr*>(Base), |
| 155 | /*arrow=*/true, /*free=*/false); |
| 156 | } |
| 157 | |
| 158 | |
Ted Kremenek | 089ffd0 | 2012-10-11 20:58:18 +0000 | [diff] [blame] | 159 | ReturnStmt *ASTMaker::makeReturn(const Expr *RetVal) { |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 160 | return new (C) ReturnStmt(SourceLocation(), const_cast<Expr*>(RetVal), |
| 161 | nullptr); |
Ted Kremenek | 089ffd0 | 2012-10-11 20:58:18 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Ted Kremenek | 2b5c83c | 2012-09-21 17:54:32 +0000 | [diff] [blame] | 164 | //===----------------------------------------------------------------------===// |
| 165 | // Creation functions for faux ASTs. |
| 166 | //===----------------------------------------------------------------------===// |
| 167 | |
| 168 | typedef Stmt *(*FunctionFarmer)(ASTContext &C, const FunctionDecl *D); |
| 169 | |
Ted Kremenek | d81a4a1 | 2012-09-21 00:52:24 +0000 | [diff] [blame] | 170 | /// Create a fake body for dispatch_once. |
| 171 | static Stmt *create_dispatch_once(ASTContext &C, const FunctionDecl *D) { |
| 172 | // Check if we have at least two parameters. |
| 173 | if (D->param_size() != 2) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 174 | return nullptr; |
Ted Kremenek | d81a4a1 | 2012-09-21 00:52:24 +0000 | [diff] [blame] | 175 | |
| 176 | // Check if the first parameter is a pointer to integer type. |
| 177 | const ParmVarDecl *Predicate = D->getParamDecl(0); |
| 178 | QualType PredicateQPtrTy = Predicate->getType(); |
| 179 | const PointerType *PredicatePtrTy = PredicateQPtrTy->getAs<PointerType>(); |
| 180 | if (!PredicatePtrTy) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 181 | return nullptr; |
Ted Kremenek | d81a4a1 | 2012-09-21 00:52:24 +0000 | [diff] [blame] | 182 | QualType PredicateTy = PredicatePtrTy->getPointeeType(); |
| 183 | if (!PredicateTy->isIntegerType()) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 184 | return nullptr; |
| 185 | |
Ted Kremenek | d81a4a1 | 2012-09-21 00:52:24 +0000 | [diff] [blame] | 186 | // Check if the second parameter is the proper block type. |
| 187 | const ParmVarDecl *Block = D->getParamDecl(1); |
| 188 | QualType Ty = Block->getType(); |
| 189 | if (!isDispatchBlock(Ty)) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 190 | return nullptr; |
| 191 | |
Ted Kremenek | d81a4a1 | 2012-09-21 00:52:24 +0000 | [diff] [blame] | 192 | // Everything checks out. Create a fakse body that checks the predicate, |
| 193 | // sets it, and calls the block. Basically, an AST dump of: |
| 194 | // |
| 195 | // void dispatch_once(dispatch_once_t *predicate, dispatch_block_t block) { |
| 196 | // if (!*predicate) { |
| 197 | // *predicate = 1; |
| 198 | // block(); |
| 199 | // } |
| 200 | // } |
| 201 | |
Ted Kremenek | 7241813 | 2012-09-21 17:54:35 +0000 | [diff] [blame] | 202 | ASTMaker M(C); |
| 203 | |
Ted Kremenek | d81a4a1 | 2012-09-21 00:52:24 +0000 | [diff] [blame] | 204 | // (1) Create the call. |
Ted Kremenek | 7241813 | 2012-09-21 17:54:35 +0000 | [diff] [blame] | 205 | DeclRefExpr *DR = M.makeDeclRefExpr(Block); |
Ted Kremenek | ca90ea5 | 2012-09-21 18:13:27 +0000 | [diff] [blame] | 206 | ImplicitCastExpr *ICE = M.makeLvalueToRvalue(DR, Ty); |
Dmitri Gribenko | 44ebbd5 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 207 | CallExpr *CE = new (C) CallExpr(C, ICE, None, C.VoidTy, VK_RValue, |
| 208 | SourceLocation()); |
Ted Kremenek | d81a4a1 | 2012-09-21 00:52:24 +0000 | [diff] [blame] | 209 | |
| 210 | // (2) Create the assignment to the predicate. |
| 211 | IntegerLiteral *IL = |
| 212 | IntegerLiteral::Create(C, llvm::APInt(C.getTypeSize(C.IntTy), (uint64_t) 1), |
| 213 | C.IntTy, SourceLocation()); |
Ted Kremenek | e7ad535 | 2012-09-21 18:33:56 +0000 | [diff] [blame] | 214 | BinaryOperator *B = |
| 215 | M.makeAssignment( |
| 216 | M.makeDereference( |
| 217 | M.makeLvalueToRvalue( |
| 218 | M.makeDeclRefExpr(Predicate), PredicateQPtrTy), |
| 219 | PredicateTy), |
| 220 | M.makeIntegralCast(IL, PredicateTy), |
| 221 | PredicateTy); |
| 222 | |
Ted Kremenek | d81a4a1 | 2012-09-21 00:52:24 +0000 | [diff] [blame] | 223 | // (3) Create the compound statement. |
Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 224 | Stmt *Stmts[] = { B, CE }; |
| 225 | CompoundStmt *CS = M.makeCompound(Stmts); |
Ted Kremenek | d81a4a1 | 2012-09-21 00:52:24 +0000 | [diff] [blame] | 226 | |
| 227 | // (4) Create the 'if' condition. |
Ted Kremenek | e7ad535 | 2012-09-21 18:33:56 +0000 | [diff] [blame] | 228 | ImplicitCastExpr *LValToRval = |
| 229 | M.makeLvalueToRvalue( |
| 230 | M.makeDereference( |
| 231 | M.makeLvalueToRvalue( |
| 232 | M.makeDeclRefExpr(Predicate), |
| 233 | PredicateQPtrTy), |
| 234 | PredicateTy), |
| 235 | PredicateTy); |
| 236 | |
| 237 | UnaryOperator *UO = new (C) UnaryOperator(LValToRval, UO_LNot, C.IntTy, |
| 238 | VK_RValue, OK_Ordinary, |
| 239 | SourceLocation()); |
Ted Kremenek | d81a4a1 | 2012-09-21 00:52:24 +0000 | [diff] [blame] | 240 | |
| 241 | // (5) Create the 'if' statement. |
Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 242 | IfStmt *If = new (C) IfStmt(C, SourceLocation(), false, nullptr, nullptr, |
| 243 | UO, CS); |
Ted Kremenek | d81a4a1 | 2012-09-21 00:52:24 +0000 | [diff] [blame] | 244 | return If; |
| 245 | } |
| 246 | |
Ted Kremenek | 14f779c | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 247 | /// Create a fake body for dispatch_sync. |
| 248 | static Stmt *create_dispatch_sync(ASTContext &C, const FunctionDecl *D) { |
| 249 | // Check if we have at least two parameters. |
| 250 | if (D->param_size() != 2) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 251 | return nullptr; |
| 252 | |
Ted Kremenek | 14f779c | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 253 | // Check if the second parameter is a block. |
| 254 | const ParmVarDecl *PV = D->getParamDecl(1); |
| 255 | QualType Ty = PV->getType(); |
Ted Kremenek | d81a4a1 | 2012-09-21 00:52:24 +0000 | [diff] [blame] | 256 | if (!isDispatchBlock(Ty)) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 257 | return nullptr; |
| 258 | |
Ted Kremenek | 14f779c | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 259 | // Everything checks out. Create a fake body that just calls the block. |
| 260 | // This is basically just an AST dump of: |
| 261 | // |
| 262 | // void dispatch_sync(dispatch_queue_t queue, void (^block)(void)) { |
| 263 | // block(); |
| 264 | // } |
Ted Kremenek | 7241813 | 2012-09-21 17:54:35 +0000 | [diff] [blame] | 265 | // |
| 266 | ASTMaker M(C); |
| 267 | DeclRefExpr *DR = M.makeDeclRefExpr(PV); |
Ted Kremenek | dff3553 | 2012-09-21 18:33:52 +0000 | [diff] [blame] | 268 | ImplicitCastExpr *ICE = M.makeLvalueToRvalue(DR, Ty); |
Dmitri Gribenko | 44ebbd5 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 269 | CallExpr *CE = new (C) CallExpr(C, ICE, None, C.VoidTy, VK_RValue, |
| 270 | SourceLocation()); |
Ted Kremenek | 14f779c | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 271 | return CE; |
| 272 | } |
| 273 | |
Ted Kremenek | 089ffd0 | 2012-10-11 20:58:18 +0000 | [diff] [blame] | 274 | static Stmt *create_OSAtomicCompareAndSwap(ASTContext &C, const FunctionDecl *D) |
| 275 | { |
| 276 | // There are exactly 3 arguments. |
| 277 | if (D->param_size() != 3) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 278 | return nullptr; |
| 279 | |
Anna Zaks | 064185a | 2013-02-05 19:52:26 +0000 | [diff] [blame] | 280 | // Signature: |
| 281 | // _Bool OSAtomicCompareAndSwapPtr(void *__oldValue, |
| 282 | // void *__newValue, |
| 283 | // void * volatile *__theValue) |
| 284 | // Generate body: |
Ted Kremenek | 089ffd0 | 2012-10-11 20:58:18 +0000 | [diff] [blame] | 285 | // if (oldValue == *theValue) { |
| 286 | // *theValue = newValue; |
| 287 | // return YES; |
| 288 | // } |
| 289 | // else return NO; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 290 | |
| 291 | QualType ResultTy = D->getReturnType(); |
Ted Kremenek | 6fdefb5 | 2012-10-12 00:18:19 +0000 | [diff] [blame] | 292 | bool isBoolean = ResultTy->isBooleanType(); |
| 293 | if (!isBoolean && !ResultTy->isIntegralType(C)) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 294 | return nullptr; |
| 295 | |
Ted Kremenek | 089ffd0 | 2012-10-11 20:58:18 +0000 | [diff] [blame] | 296 | const ParmVarDecl *OldValue = D->getParamDecl(0); |
| 297 | QualType OldValueTy = OldValue->getType(); |
| 298 | |
| 299 | const ParmVarDecl *NewValue = D->getParamDecl(1); |
| 300 | QualType NewValueTy = NewValue->getType(); |
| 301 | |
| 302 | assert(OldValueTy == NewValueTy); |
| 303 | |
| 304 | const ParmVarDecl *TheValue = D->getParamDecl(2); |
| 305 | QualType TheValueTy = TheValue->getType(); |
| 306 | const PointerType *PT = TheValueTy->getAs<PointerType>(); |
| 307 | if (!PT) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 308 | return nullptr; |
Ted Kremenek | 089ffd0 | 2012-10-11 20:58:18 +0000 | [diff] [blame] | 309 | QualType PointeeTy = PT->getPointeeType(); |
| 310 | |
| 311 | ASTMaker M(C); |
| 312 | // Construct the comparison. |
| 313 | Expr *Comparison = |
| 314 | M.makeComparison( |
| 315 | M.makeLvalueToRvalue(M.makeDeclRefExpr(OldValue), OldValueTy), |
| 316 | M.makeLvalueToRvalue( |
| 317 | M.makeDereference( |
| 318 | M.makeLvalueToRvalue(M.makeDeclRefExpr(TheValue), TheValueTy), |
| 319 | PointeeTy), |
| 320 | PointeeTy), |
| 321 | BO_EQ); |
| 322 | |
| 323 | // Construct the body of the IfStmt. |
| 324 | Stmt *Stmts[2]; |
| 325 | Stmts[0] = |
| 326 | M.makeAssignment( |
| 327 | M.makeDereference( |
| 328 | M.makeLvalueToRvalue(M.makeDeclRefExpr(TheValue), TheValueTy), |
| 329 | PointeeTy), |
| 330 | M.makeLvalueToRvalue(M.makeDeclRefExpr(NewValue), NewValueTy), |
| 331 | NewValueTy); |
Ted Kremenek | 6fdefb5 | 2012-10-12 00:18:19 +0000 | [diff] [blame] | 332 | |
| 333 | Expr *BoolVal = M.makeObjCBool(true); |
| 334 | Expr *RetVal = isBoolean ? M.makeIntegralCastToBoolean(BoolVal) |
| 335 | : M.makeIntegralCast(BoolVal, ResultTy); |
| 336 | Stmts[1] = M.makeReturn(RetVal); |
Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 337 | CompoundStmt *Body = M.makeCompound(Stmts); |
Ted Kremenek | 089ffd0 | 2012-10-11 20:58:18 +0000 | [diff] [blame] | 338 | |
| 339 | // Construct the else clause. |
Ted Kremenek | 6fdefb5 | 2012-10-12 00:18:19 +0000 | [diff] [blame] | 340 | BoolVal = M.makeObjCBool(false); |
| 341 | RetVal = isBoolean ? M.makeIntegralCastToBoolean(BoolVal) |
| 342 | : M.makeIntegralCast(BoolVal, ResultTy); |
| 343 | Stmt *Else = M.makeReturn(RetVal); |
Ted Kremenek | 089ffd0 | 2012-10-11 20:58:18 +0000 | [diff] [blame] | 344 | |
| 345 | /// Construct the If. |
Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 346 | Stmt *If = new (C) IfStmt(C, SourceLocation(), false, nullptr, nullptr, |
| 347 | Comparison, Body, SourceLocation(), Else); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 348 | |
Ted Kremenek | 089ffd0 | 2012-10-11 20:58:18 +0000 | [diff] [blame] | 349 | return If; |
| 350 | } |
| 351 | |
Ted Kremenek | 14f779c | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 352 | Stmt *BodyFarm::getBody(const FunctionDecl *D) { |
| 353 | D = D->getCanonicalDecl(); |
| 354 | |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 355 | Optional<Stmt *> &Val = Bodies[D]; |
Ted Kremenek | 14f779c | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 356 | if (Val.hasValue()) |
| 357 | return Val.getValue(); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 358 | |
| 359 | Val = nullptr; |
| 360 | |
| 361 | if (D->getIdentifier() == nullptr) |
| 362 | return nullptr; |
Ted Kremenek | 14f779c | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 363 | |
| 364 | StringRef Name = D->getName(); |
| 365 | if (Name.empty()) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 366 | return nullptr; |
Ted Kremenek | 089ffd0 | 2012-10-11 20:58:18 +0000 | [diff] [blame] | 367 | |
| 368 | FunctionFarmer FF; |
| 369 | |
| 370 | if (Name.startswith("OSAtomicCompareAndSwap") || |
| 371 | Name.startswith("objc_atomicCompareAndSwap")) { |
| 372 | FF = create_OSAtomicCompareAndSwap; |
| 373 | } |
| 374 | else { |
| 375 | FF = llvm::StringSwitch<FunctionFarmer>(Name) |
| 376 | .Case("dispatch_sync", create_dispatch_sync) |
| 377 | .Case("dispatch_once", create_dispatch_once) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 378 | .Default(nullptr); |
Ted Kremenek | 14f779c | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Ted Kremenek | 089ffd0 | 2012-10-11 20:58:18 +0000 | [diff] [blame] | 381 | if (FF) { Val = FF(C, D); } |
Ted Kremenek | eeccb30 | 2014-08-27 15:14:15 +0000 | [diff] [blame] | 382 | else if (Injector) { Val = Injector->getBody(D); } |
Ted Kremenek | 14f779c | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 383 | return Val.getValue(); |
| 384 | } |
| 385 | |
Devin Coughlin | b7e810b | 2016-01-26 23:58:48 +0000 | [diff] [blame] | 386 | static const ObjCIvarDecl *findBackingIvar(const ObjCPropertyDecl *Prop) { |
| 387 | const ObjCIvarDecl *IVar = Prop->getPropertyIvarDecl(); |
| 388 | |
| 389 | if (IVar) |
| 390 | return IVar; |
| 391 | |
| 392 | // When a readonly property is shadowed in a class extensions with a |
| 393 | // a readwrite property, the instance variable belongs to the shadowing |
| 394 | // property rather than the shadowed property. If there is no instance |
| 395 | // variable on a readonly property, check to see whether the property is |
| 396 | // shadowed and if so try to get the instance variable from shadowing |
| 397 | // property. |
| 398 | if (!Prop->isReadOnly()) |
| 399 | return nullptr; |
| 400 | |
| 401 | auto *Container = cast<ObjCContainerDecl>(Prop->getDeclContext()); |
| 402 | const ObjCInterfaceDecl *PrimaryInterface = nullptr; |
| 403 | if (auto *InterfaceDecl = dyn_cast<ObjCInterfaceDecl>(Container)) { |
| 404 | PrimaryInterface = InterfaceDecl; |
| 405 | } else if (auto *CategoryDecl = dyn_cast<ObjCCategoryDecl>(Container)) { |
| 406 | PrimaryInterface = CategoryDecl->getClassInterface(); |
| 407 | } else if (auto *ImplDecl = dyn_cast<ObjCImplDecl>(Container)) { |
| 408 | PrimaryInterface = ImplDecl->getClassInterface(); |
| 409 | } else { |
| 410 | return nullptr; |
| 411 | } |
| 412 | |
| 413 | // FindPropertyVisibleInPrimaryClass() looks first in class extensions, so it |
| 414 | // is guaranteed to find the shadowing property, if it exists, rather than |
| 415 | // the shadowed property. |
| 416 | auto *ShadowingProp = PrimaryInterface->FindPropertyVisibleInPrimaryClass( |
Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 417 | Prop->getIdentifier(), Prop->getQueryKind()); |
Devin Coughlin | b7e810b | 2016-01-26 23:58:48 +0000 | [diff] [blame] | 418 | if (ShadowingProp && ShadowingProp != Prop) { |
| 419 | IVar = ShadowingProp->getPropertyIvarDecl(); |
| 420 | } |
| 421 | |
| 422 | return IVar; |
| 423 | } |
| 424 | |
Jordan Rose | 1a866cd | 2014-01-10 20:06:06 +0000 | [diff] [blame] | 425 | static Stmt *createObjCPropertyGetter(ASTContext &Ctx, |
| 426 | const ObjCPropertyDecl *Prop) { |
Jordan Rose | ddf1966 | 2014-01-23 03:59:10 +0000 | [diff] [blame] | 427 | // First, find the backing ivar. |
Devin Coughlin | b7e810b | 2016-01-26 23:58:48 +0000 | [diff] [blame] | 428 | const ObjCIvarDecl *IVar = findBackingIvar(Prop); |
Jordan Rose | 1a866cd | 2014-01-10 20:06:06 +0000 | [diff] [blame] | 429 | if (!IVar) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 430 | return nullptr; |
Jordan Rose | ddf1966 | 2014-01-23 03:59:10 +0000 | [diff] [blame] | 431 | |
| 432 | // Ignore weak variables, which have special behavior. |
Jordan Rose | 1a866cd | 2014-01-10 20:06:06 +0000 | [diff] [blame] | 433 | if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 434 | return nullptr; |
Jordan Rose | a3f2781 | 2014-01-14 17:29:06 +0000 | [diff] [blame] | 435 | |
Jordan Rose | ddf1966 | 2014-01-23 03:59:10 +0000 | [diff] [blame] | 436 | // Look to see if Sema has synthesized a body for us. This happens in |
| 437 | // Objective-C++ because the return value may be a C++ class type with a |
| 438 | // non-trivial copy constructor. We can only do this if we can find the |
| 439 | // @synthesize for this property, though (or if we know it's been auto- |
| 440 | // synthesized). |
Jordan Rose | a3f2781 | 2014-01-14 17:29:06 +0000 | [diff] [blame] | 441 | const ObjCImplementationDecl *ImplDecl = |
| 442 | IVar->getContainingInterface()->getImplementation(); |
| 443 | if (ImplDecl) { |
Aaron Ballman | d85eff4 | 2014-03-14 15:02:45 +0000 | [diff] [blame] | 444 | for (const auto *I : ImplDecl->property_impls()) { |
Jordan Rose | a3f2781 | 2014-01-14 17:29:06 +0000 | [diff] [blame] | 445 | if (I->getPropertyDecl() != Prop) |
| 446 | continue; |
| 447 | |
| 448 | if (I->getGetterCXXConstructor()) { |
| 449 | ASTMaker M(Ctx); |
| 450 | return M.makeReturn(I->getGetterCXXConstructor()); |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | |
Jordan Rose | ddf1966 | 2014-01-23 03:59:10 +0000 | [diff] [blame] | 455 | // Sanity check that the property is the same type as the ivar, or a |
| 456 | // reference to it, and that it is either an object pointer or trivially |
| 457 | // copyable. |
| 458 | if (!Ctx.hasSameUnqualifiedType(IVar->getType(), |
| 459 | Prop->getType().getNonReferenceType())) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 460 | return nullptr; |
Jordan Rose | ddf1966 | 2014-01-23 03:59:10 +0000 | [diff] [blame] | 461 | if (!IVar->getType()->isObjCLifetimeType() && |
| 462 | !IVar->getType().isTriviallyCopyableType(Ctx)) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 463 | return nullptr; |
Jordan Rose | 1a866cd | 2014-01-10 20:06:06 +0000 | [diff] [blame] | 464 | |
Jordan Rose | ddf1966 | 2014-01-23 03:59:10 +0000 | [diff] [blame] | 465 | // Generate our body: |
| 466 | // return self->_ivar; |
Jordan Rose | 1a866cd | 2014-01-10 20:06:06 +0000 | [diff] [blame] | 467 | ASTMaker M(Ctx); |
| 468 | |
| 469 | const VarDecl *selfVar = Prop->getGetterMethodDecl()->getSelfDecl(); |
Devin Coughlin | aac894f | 2017-01-11 01:02:34 +0000 | [diff] [blame^] | 470 | if (!selfVar) |
| 471 | return nullptr; |
Jordan Rose | 1a866cd | 2014-01-10 20:06:06 +0000 | [diff] [blame] | 472 | |
| 473 | Expr *loadedIVar = |
| 474 | M.makeObjCIvarRef( |
| 475 | M.makeLvalueToRvalue( |
| 476 | M.makeDeclRefExpr(selfVar), |
| 477 | selfVar->getType()), |
| 478 | IVar); |
| 479 | |
| 480 | if (!Prop->getType()->isReferenceType()) |
| 481 | loadedIVar = M.makeLvalueToRvalue(loadedIVar, IVar->getType()); |
| 482 | |
| 483 | return M.makeReturn(loadedIVar); |
| 484 | } |
| 485 | |
Jordan Rose | ddf1966 | 2014-01-23 03:59:10 +0000 | [diff] [blame] | 486 | Stmt *BodyFarm::getBody(const ObjCMethodDecl *D) { |
| 487 | // We currently only know how to synthesize property accessors. |
Jordan Rose | 1a866cd | 2014-01-10 20:06:06 +0000 | [diff] [blame] | 488 | if (!D->isPropertyAccessor()) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 489 | return nullptr; |
Jordan Rose | 1a866cd | 2014-01-10 20:06:06 +0000 | [diff] [blame] | 490 | |
| 491 | D = D->getCanonicalDecl(); |
| 492 | |
| 493 | Optional<Stmt *> &Val = Bodies[D]; |
| 494 | if (Val.hasValue()) |
| 495 | return Val.getValue(); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 496 | Val = nullptr; |
Jordan Rose | 1a866cd | 2014-01-10 20:06:06 +0000 | [diff] [blame] | 497 | |
Jordan Rose | ddf1966 | 2014-01-23 03:59:10 +0000 | [diff] [blame] | 498 | const ObjCPropertyDecl *Prop = D->findPropertyDecl(); |
Jordan Rose | 1a866cd | 2014-01-10 20:06:06 +0000 | [diff] [blame] | 499 | if (!Prop) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 500 | return nullptr; |
Jordan Rose | 1a866cd | 2014-01-10 20:06:06 +0000 | [diff] [blame] | 501 | |
Jordan Rose | ddf1966 | 2014-01-23 03:59:10 +0000 | [diff] [blame] | 502 | // For now, we only synthesize getters. |
Devin Coughlin | ef3697e | 2016-02-18 19:37:39 +0000 | [diff] [blame] | 503 | // Synthesizing setters would cause false negatives in the |
| 504 | // RetainCountChecker because the method body would bind the parameter |
| 505 | // to an instance variable, causing it to escape. This would prevent |
| 506 | // warning in the following common scenario: |
| 507 | // |
| 508 | // id foo = [[NSObject alloc] init]; |
| 509 | // self.foo = foo; // We should warn that foo leaks here. |
| 510 | // |
Jordan Rose | 1a866cd | 2014-01-10 20:06:06 +0000 | [diff] [blame] | 511 | if (D->param_size() != 0) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 512 | return nullptr; |
Jordan Rose | 1a866cd | 2014-01-10 20:06:06 +0000 | [diff] [blame] | 513 | |
| 514 | Val = createObjCPropertyGetter(C, Prop); |
| 515 | |
| 516 | return Val.getValue(); |
| 517 | } |
| 518 | |