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 "BodyFarm.h" 16 #include "clang/AST/ASTContext.h" 17 #include "clang/AST/CXXInheritance.h" 18 #include "clang/AST/Decl.h" 19 #include "clang/AST/Expr.h" 20 #include "clang/AST/ExprCXX.h" 21 #include "clang/AST/ExprObjC.h" 22 #include "clang/AST/NestedNameSpecifier.h" 23 #include "clang/Analysis/CodeInjector.h" 24 #include "clang/Basic/OperatorKinds.h" 25 #include "llvm/ADT/StringSwitch.h" 26 #include "llvm/Support/Debug.h" 27 28 #define DEBUG_TYPE "body-farm" 29 30 using namespace clang; 31 32 //===----------------------------------------------------------------------===// 33 // Helper creation functions for constructing faux ASTs. 34 //===----------------------------------------------------------------------===// 35 36 static bool isDispatchBlock(QualType Ty) { 37 // Is it a block pointer? 38 const BlockPointerType *BPT = Ty->getAs<BlockPointerType>(); 39 if (!BPT) 40 return false; 41 42 // Check if the block pointer type takes no arguments and 43 // returns void. 44 const FunctionProtoType *FT = 45 BPT->getPointeeType()->getAs<FunctionProtoType>(); 46 return FT && FT->getReturnType()->isVoidType() && FT->getNumParams() == 0; 47 } 48 49 namespace { 50 class ASTMaker { 51 public: 52 ASTMaker(ASTContext &C) : C(C) {} 53 54 /// Create a new BinaryOperator representing a simple assignment. 55 BinaryOperator *makeAssignment(const Expr *LHS, const Expr *RHS, QualType Ty); 56 57 /// Create a new BinaryOperator representing a comparison. 58 BinaryOperator *makeComparison(const Expr *LHS, const Expr *RHS, 59 BinaryOperator::Opcode Op); 60 61 /// Create a new compound stmt using the provided statements. 62 CompoundStmt *makeCompound(ArrayRef<Stmt*>); 63 64 /// Create a new DeclRefExpr for the referenced variable. 65 DeclRefExpr *makeDeclRefExpr(const VarDecl *D, 66 bool RefersToEnclosingVariableOrCapture = false, 67 bool GetNonReferenceType = false); 68 69 /// Create a new UnaryOperator representing a dereference. 70 UnaryOperator *makeDereference(const Expr *Arg, QualType Ty); 71 72 /// Create an implicit cast for an integer conversion. 73 Expr *makeIntegralCast(const Expr *Arg, QualType Ty); 74 75 /// Create an implicit cast to a builtin boolean type. 76 ImplicitCastExpr *makeIntegralCastToBoolean(const Expr *Arg); 77 78 /// Create an implicit cast for lvalue-to-rvaluate conversions. 79 ImplicitCastExpr *makeLvalueToRvalue(const Expr *Arg, QualType Ty); 80 81 /// Make RValue out of variable declaration, creating a temporary 82 /// DeclRefExpr in the process. 83 ImplicitCastExpr * 84 makeLvalueToRvalue(const VarDecl *Decl, 85 bool RefersToEnclosingVariableOrCapture = false, 86 bool GetNonReferenceType = false); 87 88 /// Create an implicit cast of the given type. 89 ImplicitCastExpr *makeImplicitCast(const Expr *Arg, QualType Ty, 90 CastKind CK = CK_LValueToRValue); 91 92 /// Create an Objective-C bool literal. 93 ObjCBoolLiteralExpr *makeObjCBool(bool Val); 94 95 /// Create an Objective-C ivar reference. 96 ObjCIvarRefExpr *makeObjCIvarRef(const Expr *Base, const ObjCIvarDecl *IVar); 97 98 /// Create a Return statement. 99 ReturnStmt *makeReturn(const Expr *RetVal); 100 101 /// Create an integer literal. 102 IntegerLiteral *makeIntegerLiteral(uint64_t value); 103 104 /// Create a member expression. 105 MemberExpr *makeMemberExpression(Expr *base, ValueDecl *MemberDecl, 106 bool IsArrow = false, 107 ExprValueKind ValueKind = VK_LValue); 108 109 /// Returns a *first* member field of a record declaration with a given name. 110 /// \return an nullptr if no member with such a name exists. 111 NamedDecl *findMemberField(const CXXRecordDecl *RD, StringRef Name); 112 113 private: 114 ASTContext &C; 115 }; 116 } 117 118 BinaryOperator *ASTMaker::makeAssignment(const Expr *LHS, const Expr *RHS, 119 QualType Ty) { 120 return new (C) BinaryOperator(const_cast<Expr*>(LHS), const_cast<Expr*>(RHS), 121 BO_Assign, Ty, VK_RValue, 122 OK_Ordinary, SourceLocation(), FPOptions()); 123 } 124 125 BinaryOperator *ASTMaker::makeComparison(const Expr *LHS, const Expr *RHS, 126 BinaryOperator::Opcode Op) { 127 assert(BinaryOperator::isLogicalOp(Op) || 128 BinaryOperator::isComparisonOp(Op)); 129 return new (C) BinaryOperator(const_cast<Expr*>(LHS), 130 const_cast<Expr*>(RHS), 131 Op, 132 C.getLogicalOperationType(), 133 VK_RValue, 134 OK_Ordinary, SourceLocation(), FPOptions()); 135 } 136 137 CompoundStmt *ASTMaker::makeCompound(ArrayRef<Stmt *> Stmts) { 138 return new (C) CompoundStmt(C, Stmts, SourceLocation(), SourceLocation()); 139 } 140 141 DeclRefExpr *ASTMaker::makeDeclRefExpr(const VarDecl *D, 142 bool RefersToEnclosingVariableOrCapture, 143 bool GetNonReferenceType) { 144 auto Type = D->getType(); 145 if (GetNonReferenceType) 146 Type = Type.getNonReferenceType(); 147 148 DeclRefExpr *DR = DeclRefExpr::Create( 149 C, NestedNameSpecifierLoc(), SourceLocation(), const_cast<VarDecl *>(D), 150 RefersToEnclosingVariableOrCapture, SourceLocation(), Type, VK_LValue); 151 return DR; 152 } 153 154 UnaryOperator *ASTMaker::makeDereference(const Expr *Arg, QualType Ty) { 155 return new (C) UnaryOperator(const_cast<Expr*>(Arg), UO_Deref, Ty, 156 VK_LValue, OK_Ordinary, SourceLocation()); 157 } 158 159 ImplicitCastExpr *ASTMaker::makeLvalueToRvalue(const Expr *Arg, QualType Ty) { 160 return makeImplicitCast(Arg, Ty, CK_LValueToRValue); 161 } 162 163 ImplicitCastExpr * 164 ASTMaker::makeLvalueToRvalue(const VarDecl *Arg, 165 bool RefersToEnclosingVariableOrCapture, 166 bool GetNonReferenceType) { 167 auto Type = Arg->getType(); 168 if (GetNonReferenceType) 169 Type = Type.getNonReferenceType(); 170 return makeLvalueToRvalue(makeDeclRefExpr(Arg, 171 RefersToEnclosingVariableOrCapture, 172 GetNonReferenceType), 173 Type); 174 } 175 176 ImplicitCastExpr *ASTMaker::makeImplicitCast(const Expr *Arg, QualType Ty, 177 CastKind CK) { 178 return ImplicitCastExpr::Create(C, Ty, 179 /* CastKind= */ CK, 180 /* Expr= */ const_cast<Expr *>(Arg), 181 /* CXXCastPath= */ nullptr, 182 /* ExprValueKind= */ VK_RValue); 183 } 184 185 Expr *ASTMaker::makeIntegralCast(const Expr *Arg, QualType Ty) { 186 if (Arg->getType() == Ty) 187 return const_cast<Expr*>(Arg); 188 189 return ImplicitCastExpr::Create(C, Ty, CK_IntegralCast, 190 const_cast<Expr*>(Arg), nullptr, VK_RValue); 191 } 192 193 ImplicitCastExpr *ASTMaker::makeIntegralCastToBoolean(const Expr *Arg) { 194 return ImplicitCastExpr::Create(C, C.BoolTy, CK_IntegralToBoolean, 195 const_cast<Expr*>(Arg), nullptr, VK_RValue); 196 } 197 198 ObjCBoolLiteralExpr *ASTMaker::makeObjCBool(bool Val) { 199 QualType Ty = C.getBOOLDecl() ? C.getBOOLType() : C.ObjCBuiltinBoolTy; 200 return new (C) ObjCBoolLiteralExpr(Val, Ty, SourceLocation()); 201 } 202 203 ObjCIvarRefExpr *ASTMaker::makeObjCIvarRef(const Expr *Base, 204 const ObjCIvarDecl *IVar) { 205 return new (C) ObjCIvarRefExpr(const_cast<ObjCIvarDecl*>(IVar), 206 IVar->getType(), SourceLocation(), 207 SourceLocation(), const_cast<Expr*>(Base), 208 /*arrow=*/true, /*free=*/false); 209 } 210 211 212 ReturnStmt *ASTMaker::makeReturn(const Expr *RetVal) { 213 return new (C) ReturnStmt(SourceLocation(), const_cast<Expr*>(RetVal), 214 nullptr); 215 } 216 217 IntegerLiteral *ASTMaker::makeIntegerLiteral(uint64_t value) { 218 return IntegerLiteral::Create(C, 219 llvm::APInt( 220 /*numBits=*/C.getTypeSize(C.IntTy), value), 221 /*QualType=*/C.IntTy, SourceLocation()); 222 } 223 224 MemberExpr *ASTMaker::makeMemberExpression(Expr *base, ValueDecl *MemberDecl, 225 bool IsArrow, 226 ExprValueKind ValueKind) { 227 228 DeclAccessPair FoundDecl = DeclAccessPair::make(MemberDecl, AS_public); 229 return MemberExpr::Create( 230 C, base, IsArrow, SourceLocation(), NestedNameSpecifierLoc(), 231 SourceLocation(), MemberDecl, FoundDecl, 232 DeclarationNameInfo(MemberDecl->getDeclName(), SourceLocation()), 233 /* TemplateArgumentListInfo= */ nullptr, MemberDecl->getType(), ValueKind, 234 OK_Ordinary); 235 } 236 237 NamedDecl *ASTMaker::findMemberField(const CXXRecordDecl *RD, StringRef Name) { 238 239 CXXBasePaths Paths( 240 /* FindAmbiguities=*/false, 241 /* RecordPaths=*/false, 242 /* DetectVirtual= */ false); 243 const IdentifierInfo &II = C.Idents.get(Name); 244 DeclarationName DeclName = C.DeclarationNames.getIdentifier(&II); 245 246 DeclContextLookupResult Decls = RD->lookup(DeclName); 247 for (NamedDecl *FoundDecl : Decls) 248 if (!FoundDecl->getDeclContext()->isFunctionOrMethod()) 249 return FoundDecl; 250 251 return nullptr; 252 } 253 254 //===----------------------------------------------------------------------===// 255 // Creation functions for faux ASTs. 256 //===----------------------------------------------------------------------===// 257 258 typedef Stmt *(*FunctionFarmer)(ASTContext &C, const FunctionDecl *D); 259 260 static CallExpr *create_call_once_funcptr_call(ASTContext &C, ASTMaker M, 261 const ParmVarDecl *Callback, 262 ArrayRef<Expr *> CallArgs) { 263 264 return new (C) CallExpr( 265 /*ASTContext=*/C, 266 /*StmtClass=*/M.makeLvalueToRvalue(/*Expr=*/Callback), 267 /*args=*/CallArgs, 268 /*QualType=*/C.VoidTy, 269 /*ExprValueType=*/VK_RValue, 270 /*SourceLocation=*/SourceLocation()); 271 } 272 273 static CallExpr *create_call_once_lambda_call(ASTContext &C, ASTMaker M, 274 const ParmVarDecl *Callback, 275 QualType CallbackType, 276 ArrayRef<Expr *> CallArgs) { 277 278 CXXRecordDecl *CallbackDecl = CallbackType->getAsCXXRecordDecl(); 279 280 assert(CallbackDecl != nullptr); 281 assert(CallbackDecl->isLambda()); 282 FunctionDecl *callOperatorDecl = CallbackDecl->getLambdaCallOperator(); 283 assert(callOperatorDecl != nullptr); 284 285 DeclRefExpr *callOperatorDeclRef = 286 DeclRefExpr::Create(/* Ctx = */ C, 287 /* QualifierLoc = */ NestedNameSpecifierLoc(), 288 /* TemplateKWLoc = */ SourceLocation(), 289 const_cast<FunctionDecl *>(callOperatorDecl), 290 /* RefersToEnclosingVariableOrCapture= */ false, 291 /* NameLoc = */ SourceLocation(), 292 /* T = */ callOperatorDecl->getType(), 293 /* VK = */ VK_LValue); 294 295 return new (C) 296 CXXOperatorCallExpr(/*AstContext=*/C, OO_Call, callOperatorDeclRef, 297 /*args=*/CallArgs, 298 /*QualType=*/C.VoidTy, 299 /*ExprValueType=*/VK_RValue, 300 /*SourceLocation=*/SourceLocation(), FPOptions()); 301 } 302 303 /// Create a fake body for std::call_once. 304 /// Emulates the following function body: 305 /// 306 /// \code 307 /// typedef struct once_flag_s { 308 /// unsigned long __state = 0; 309 /// } once_flag; 310 /// template<class Callable> 311 /// void call_once(once_flag& o, Callable func) { 312 /// if (!o.__state) { 313 /// func(); 314 /// } 315 /// o.__state = 1; 316 /// } 317 /// \endcode 318 static Stmt *create_call_once(ASTContext &C, const FunctionDecl *D) { 319 DEBUG(llvm::dbgs() << "Generating body for call_once\n"); 320 321 // We need at least two parameters. 322 if (D->param_size() < 2) 323 return nullptr; 324 325 ASTMaker M(C); 326 327 const ParmVarDecl *Flag = D->getParamDecl(0); 328 const ParmVarDecl *Callback = D->getParamDecl(1); 329 QualType CallbackType = Callback->getType().getNonReferenceType(); 330 331 bool isLambdaCall = CallbackType->getAsCXXRecordDecl() && 332 CallbackType->getAsCXXRecordDecl()->isLambda(); 333 334 SmallVector<Expr *, 5> CallArgs; 335 336 if (isLambdaCall) 337 // Lambda requires callback itself inserted as a first parameter. 338 CallArgs.push_back( 339 M.makeDeclRefExpr(Callback, 340 /* RefersToEnclosingVariableOrCapture= */ true, 341 /* GetNonReferenceType= */ true)); 342 343 // All arguments past first two ones are passed to the callback. 344 for (unsigned int i = 2; i < D->getNumParams(); i++) 345 CallArgs.push_back(M.makeLvalueToRvalue(D->getParamDecl(i))); 346 347 CallExpr *CallbackCall; 348 if (isLambdaCall) { 349 350 CallbackCall = 351 create_call_once_lambda_call(C, M, Callback, CallbackType, CallArgs); 352 } else { 353 354 // Function pointer case. 355 CallbackCall = create_call_once_funcptr_call(C, M, Callback, CallArgs); 356 } 357 358 QualType FlagType = Flag->getType().getNonReferenceType(); 359 DeclRefExpr *FlagDecl = 360 M.makeDeclRefExpr(Flag, 361 /* RefersToEnclosingVariableOrCapture=*/true, 362 /* GetNonReferenceType=*/true); 363 364 CXXRecordDecl *FlagCXXDecl = FlagType->getAsCXXRecordDecl(); 365 366 // Note: here we are assuming libc++ implementation of call_once, 367 // which has a struct with a field `__state_`. 368 // Body farming might not work for other `call_once` implementations. 369 NamedDecl *FoundDecl = M.findMemberField(FlagCXXDecl, "__state_"); 370 ValueDecl *FieldDecl; 371 if (FoundDecl) { 372 FieldDecl = dyn_cast<ValueDecl>(FoundDecl); 373 } else { 374 DEBUG(llvm::dbgs() << "No field __state_ found on std::once_flag struct, " 375 << "unable to synthesize call_once body, ignoring " 376 << "the call.\n"); 377 return nullptr; 378 } 379 380 MemberExpr *Deref = M.makeMemberExpression(FlagDecl, FieldDecl); 381 assert(Deref->isLValue()); 382 QualType DerefType = Deref->getType(); 383 384 // Negation predicate. 385 UnaryOperator *FlagCheck = new (C) UnaryOperator( 386 /* input= */ 387 M.makeImplicitCast(M.makeLvalueToRvalue(Deref, DerefType), DerefType, 388 CK_IntegralToBoolean), 389 /* opc= */ UO_LNot, 390 /* QualType= */ C.IntTy, 391 /* ExprValueKind= */ VK_RValue, 392 /* ExprObjectKind= */ OK_Ordinary, SourceLocation()); 393 394 // Create assignment. 395 BinaryOperator *FlagAssignment = M.makeAssignment( 396 Deref, M.makeIntegralCast(M.makeIntegerLiteral(1), DerefType), DerefType); 397 398 IfStmt *Out = new (C) 399 IfStmt(C, SourceLocation(), 400 /* IsConstexpr= */ false, 401 /* init= */ nullptr, 402 /* var= */ nullptr, 403 /* cond= */ FlagCheck, 404 /* then= */ M.makeCompound({CallbackCall, FlagAssignment})); 405 406 return Out; 407 } 408 409 /// Create a fake body for dispatch_once. 410 static Stmt *create_dispatch_once(ASTContext &C, const FunctionDecl *D) { 411 // Check if we have at least two parameters. 412 if (D->param_size() != 2) 413 return nullptr; 414 415 // Check if the first parameter is a pointer to integer type. 416 const ParmVarDecl *Predicate = D->getParamDecl(0); 417 QualType PredicateQPtrTy = Predicate->getType(); 418 const PointerType *PredicatePtrTy = PredicateQPtrTy->getAs<PointerType>(); 419 if (!PredicatePtrTy) 420 return nullptr; 421 QualType PredicateTy = PredicatePtrTy->getPointeeType(); 422 if (!PredicateTy->isIntegerType()) 423 return nullptr; 424 425 // Check if the second parameter is the proper block type. 426 const ParmVarDecl *Block = D->getParamDecl(1); 427 QualType Ty = Block->getType(); 428 if (!isDispatchBlock(Ty)) 429 return nullptr; 430 431 // Everything checks out. Create a fakse body that checks the predicate, 432 // sets it, and calls the block. Basically, an AST dump of: 433 // 434 // void dispatch_once(dispatch_once_t *predicate, dispatch_block_t block) { 435 // if (!*predicate) { 436 // *predicate = 1; 437 // block(); 438 // } 439 // } 440 441 ASTMaker M(C); 442 443 // (1) Create the call. 444 CallExpr *CE = new (C) CallExpr( 445 /*ASTContext=*/C, 446 /*StmtClass=*/M.makeLvalueToRvalue(/*Expr=*/Block), 447 /*args=*/None, 448 /*QualType=*/C.VoidTy, 449 /*ExprValueType=*/VK_RValue, 450 /*SourceLocation=*/SourceLocation()); 451 452 // (2) Create the assignment to the predicate. 453 IntegerLiteral *IL = M.makeIntegerLiteral(1); 454 455 BinaryOperator *B = 456 M.makeAssignment( 457 M.makeDereference( 458 M.makeLvalueToRvalue( 459 M.makeDeclRefExpr(Predicate), PredicateQPtrTy), 460 PredicateTy), 461 M.makeIntegralCast(IL, PredicateTy), 462 PredicateTy); 463 464 // (3) Create the compound statement. 465 Stmt *Stmts[] = { B, CE }; 466 CompoundStmt *CS = M.makeCompound(Stmts); 467 468 // (4) Create the 'if' condition. 469 ImplicitCastExpr *LValToRval = 470 M.makeLvalueToRvalue( 471 M.makeDereference( 472 M.makeLvalueToRvalue( 473 M.makeDeclRefExpr(Predicate), 474 PredicateQPtrTy), 475 PredicateTy), 476 PredicateTy); 477 478 UnaryOperator *UO = new (C) UnaryOperator( 479 /* input= */ LValToRval, 480 /* opc= */ UO_LNot, 481 /* QualType= */ C.IntTy, 482 /* ExprValueKind= */ VK_RValue, 483 /* ExprObjectKind= */ OK_Ordinary, SourceLocation()); 484 485 // (5) Create the 'if' statement. 486 IfStmt *If = new (C) IfStmt(C, SourceLocation(), 487 /* IsConstexpr= */ false, 488 /* init= */ nullptr, 489 /* var= */ nullptr, 490 /* cond= */ UO, 491 /* then= */ CS); 492 return If; 493 } 494 495 /// Create a fake body for dispatch_sync. 496 static Stmt *create_dispatch_sync(ASTContext &C, const FunctionDecl *D) { 497 // Check if we have at least two parameters. 498 if (D->param_size() != 2) 499 return nullptr; 500 501 // Check if the second parameter is a block. 502 const ParmVarDecl *PV = D->getParamDecl(1); 503 QualType Ty = PV->getType(); 504 if (!isDispatchBlock(Ty)) 505 return nullptr; 506 507 // Everything checks out. Create a fake body that just calls the block. 508 // This is basically just an AST dump of: 509 // 510 // void dispatch_sync(dispatch_queue_t queue, void (^block)(void)) { 511 // block(); 512 // } 513 // 514 ASTMaker M(C); 515 DeclRefExpr *DR = M.makeDeclRefExpr(PV); 516 ImplicitCastExpr *ICE = M.makeLvalueToRvalue(DR, Ty); 517 CallExpr *CE = new (C) CallExpr(C, ICE, None, C.VoidTy, VK_RValue, 518 SourceLocation()); 519 return CE; 520 } 521 522 static Stmt *create_OSAtomicCompareAndSwap(ASTContext &C, const FunctionDecl *D) 523 { 524 // There are exactly 3 arguments. 525 if (D->param_size() != 3) 526 return nullptr; 527 528 // Signature: 529 // _Bool OSAtomicCompareAndSwapPtr(void *__oldValue, 530 // void *__newValue, 531 // void * volatile *__theValue) 532 // Generate body: 533 // if (oldValue == *theValue) { 534 // *theValue = newValue; 535 // return YES; 536 // } 537 // else return NO; 538 539 QualType ResultTy = D->getReturnType(); 540 bool isBoolean = ResultTy->isBooleanType(); 541 if (!isBoolean && !ResultTy->isIntegralType(C)) 542 return nullptr; 543 544 const ParmVarDecl *OldValue = D->getParamDecl(0); 545 QualType OldValueTy = OldValue->getType(); 546 547 const ParmVarDecl *NewValue = D->getParamDecl(1); 548 QualType NewValueTy = NewValue->getType(); 549 550 assert(OldValueTy == NewValueTy); 551 552 const ParmVarDecl *TheValue = D->getParamDecl(2); 553 QualType TheValueTy = TheValue->getType(); 554 const PointerType *PT = TheValueTy->getAs<PointerType>(); 555 if (!PT) 556 return nullptr; 557 QualType PointeeTy = PT->getPointeeType(); 558 559 ASTMaker M(C); 560 // Construct the comparison. 561 Expr *Comparison = 562 M.makeComparison( 563 M.makeLvalueToRvalue(M.makeDeclRefExpr(OldValue), OldValueTy), 564 M.makeLvalueToRvalue( 565 M.makeDereference( 566 M.makeLvalueToRvalue(M.makeDeclRefExpr(TheValue), TheValueTy), 567 PointeeTy), 568 PointeeTy), 569 BO_EQ); 570 571 // Construct the body of the IfStmt. 572 Stmt *Stmts[2]; 573 Stmts[0] = 574 M.makeAssignment( 575 M.makeDereference( 576 M.makeLvalueToRvalue(M.makeDeclRefExpr(TheValue), TheValueTy), 577 PointeeTy), 578 M.makeLvalueToRvalue(M.makeDeclRefExpr(NewValue), NewValueTy), 579 NewValueTy); 580 581 Expr *BoolVal = M.makeObjCBool(true); 582 Expr *RetVal = isBoolean ? M.makeIntegralCastToBoolean(BoolVal) 583 : M.makeIntegralCast(BoolVal, ResultTy); 584 Stmts[1] = M.makeReturn(RetVal); 585 CompoundStmt *Body = M.makeCompound(Stmts); 586 587 // Construct the else clause. 588 BoolVal = M.makeObjCBool(false); 589 RetVal = isBoolean ? M.makeIntegralCastToBoolean(BoolVal) 590 : M.makeIntegralCast(BoolVal, ResultTy); 591 Stmt *Else = M.makeReturn(RetVal); 592 593 /// Construct the If. 594 Stmt *If = new (C) IfStmt(C, SourceLocation(), false, nullptr, nullptr, 595 Comparison, Body, SourceLocation(), Else); 596 597 return If; 598 } 599 600 Stmt *BodyFarm::getBody(const FunctionDecl *D) { 601 D = D->getCanonicalDecl(); 602 603 Optional<Stmt *> &Val = Bodies[D]; 604 if (Val.hasValue()) 605 return Val.getValue(); 606 607 Val = nullptr; 608 609 if (D->getIdentifier() == nullptr) 610 return nullptr; 611 612 StringRef Name = D->getName(); 613 if (Name.empty()) 614 return nullptr; 615 616 FunctionFarmer FF; 617 618 if (Name.startswith("OSAtomicCompareAndSwap") || 619 Name.startswith("objc_atomicCompareAndSwap")) { 620 FF = create_OSAtomicCompareAndSwap; 621 } else if (Name == "call_once" && D->getDeclContext()->isStdNamespace()) { 622 FF = create_call_once; 623 } else { 624 FF = llvm::StringSwitch<FunctionFarmer>(Name) 625 .Case("dispatch_sync", create_dispatch_sync) 626 .Case("dispatch_once", create_dispatch_once) 627 .Default(nullptr); 628 } 629 630 if (FF) { Val = FF(C, D); } 631 else if (Injector) { Val = Injector->getBody(D); } 632 return Val.getValue(); 633 } 634 635 static const ObjCIvarDecl *findBackingIvar(const ObjCPropertyDecl *Prop) { 636 const ObjCIvarDecl *IVar = Prop->getPropertyIvarDecl(); 637 638 if (IVar) 639 return IVar; 640 641 // When a readonly property is shadowed in a class extensions with a 642 // a readwrite property, the instance variable belongs to the shadowing 643 // property rather than the shadowed property. If there is no instance 644 // variable on a readonly property, check to see whether the property is 645 // shadowed and if so try to get the instance variable from shadowing 646 // property. 647 if (!Prop->isReadOnly()) 648 return nullptr; 649 650 auto *Container = cast<ObjCContainerDecl>(Prop->getDeclContext()); 651 const ObjCInterfaceDecl *PrimaryInterface = nullptr; 652 if (auto *InterfaceDecl = dyn_cast<ObjCInterfaceDecl>(Container)) { 653 PrimaryInterface = InterfaceDecl; 654 } else if (auto *CategoryDecl = dyn_cast<ObjCCategoryDecl>(Container)) { 655 PrimaryInterface = CategoryDecl->getClassInterface(); 656 } else if (auto *ImplDecl = dyn_cast<ObjCImplDecl>(Container)) { 657 PrimaryInterface = ImplDecl->getClassInterface(); 658 } else { 659 return nullptr; 660 } 661 662 // FindPropertyVisibleInPrimaryClass() looks first in class extensions, so it 663 // is guaranteed to find the shadowing property, if it exists, rather than 664 // the shadowed property. 665 auto *ShadowingProp = PrimaryInterface->FindPropertyVisibleInPrimaryClass( 666 Prop->getIdentifier(), Prop->getQueryKind()); 667 if (ShadowingProp && ShadowingProp != Prop) { 668 IVar = ShadowingProp->getPropertyIvarDecl(); 669 } 670 671 return IVar; 672 } 673 674 static Stmt *createObjCPropertyGetter(ASTContext &Ctx, 675 const ObjCPropertyDecl *Prop) { 676 // First, find the backing ivar. 677 const ObjCIvarDecl *IVar = findBackingIvar(Prop); 678 if (!IVar) 679 return nullptr; 680 681 // Ignore weak variables, which have special behavior. 682 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak) 683 return nullptr; 684 685 // Look to see if Sema has synthesized a body for us. This happens in 686 // Objective-C++ because the return value may be a C++ class type with a 687 // non-trivial copy constructor. We can only do this if we can find the 688 // @synthesize for this property, though (or if we know it's been auto- 689 // synthesized). 690 const ObjCImplementationDecl *ImplDecl = 691 IVar->getContainingInterface()->getImplementation(); 692 if (ImplDecl) { 693 for (const auto *I : ImplDecl->property_impls()) { 694 if (I->getPropertyDecl() != Prop) 695 continue; 696 697 if (I->getGetterCXXConstructor()) { 698 ASTMaker M(Ctx); 699 return M.makeReturn(I->getGetterCXXConstructor()); 700 } 701 } 702 } 703 704 // Sanity check that the property is the same type as the ivar, or a 705 // reference to it, and that it is either an object pointer or trivially 706 // copyable. 707 if (!Ctx.hasSameUnqualifiedType(IVar->getType(), 708 Prop->getType().getNonReferenceType())) 709 return nullptr; 710 if (!IVar->getType()->isObjCLifetimeType() && 711 !IVar->getType().isTriviallyCopyableType(Ctx)) 712 return nullptr; 713 714 // Generate our body: 715 // return self->_ivar; 716 ASTMaker M(Ctx); 717 718 const VarDecl *selfVar = Prop->getGetterMethodDecl()->getSelfDecl(); 719 if (!selfVar) 720 return nullptr; 721 722 Expr *loadedIVar = 723 M.makeObjCIvarRef( 724 M.makeLvalueToRvalue( 725 M.makeDeclRefExpr(selfVar), 726 selfVar->getType()), 727 IVar); 728 729 if (!Prop->getType()->isReferenceType()) 730 loadedIVar = M.makeLvalueToRvalue(loadedIVar, IVar->getType()); 731 732 return M.makeReturn(loadedIVar); 733 } 734 735 Stmt *BodyFarm::getBody(const ObjCMethodDecl *D) { 736 // We currently only know how to synthesize property accessors. 737 if (!D->isPropertyAccessor()) 738 return nullptr; 739 740 D = D->getCanonicalDecl(); 741 742 Optional<Stmt *> &Val = Bodies[D]; 743 if (Val.hasValue()) 744 return Val.getValue(); 745 Val = nullptr; 746 747 const ObjCPropertyDecl *Prop = D->findPropertyDecl(); 748 if (!Prop) 749 return nullptr; 750 751 // For now, we only synthesize getters. 752 // Synthesizing setters would cause false negatives in the 753 // RetainCountChecker because the method body would bind the parameter 754 // to an instance variable, causing it to escape. This would prevent 755 // warning in the following common scenario: 756 // 757 // id foo = [[NSObject alloc] init]; 758 // self.foo = foo; // We should warn that foo leaks here. 759 // 760 if (D->param_size() != 0) 761 return nullptr; 762 763 Val = createObjCPropertyGetter(C, Prop); 764 765 return Val.getValue(); 766 } 767 768