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