blob: e5d3c5ce5bc2a9b09ec00252ea6079e9ffe0598a [file] [log] [blame]
Ted Kremenek14f779c2012-09-21 00:09:11 +00001//== 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
George Karpenkov3d64d6e2017-10-23 23:59:52 +000015#include "clang/Analysis/BodyFarm.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "clang/AST/ASTContext.h"
George Karpenkov657a5892017-09-30 00:03:22 +000017#include "clang/AST/CXXInheritance.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/AST/Decl.h"
19#include "clang/AST/Expr.h"
George Karpenkov657a5892017-09-30 00:03:22 +000020#include "clang/AST/ExprCXX.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/AST/ExprObjC.h"
George Karpenkov657a5892017-09-30 00:03:22 +000022#include "clang/AST/NestedNameSpecifier.h"
Chandler Carruth0d9593d2015-01-14 11:29:14 +000023#include "clang/Analysis/CodeInjector.h"
George Karpenkov657a5892017-09-30 00:03:22 +000024#include "clang/Basic/OperatorKinds.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000025#include "llvm/ADT/StringSwitch.h"
George Karpenkov657a5892017-09-30 00:03:22 +000026#include "llvm/Support/Debug.h"
27
28#define DEBUG_TYPE "body-farm"
Ted Kremenek14f779c2012-09-21 00:09:11 +000029
30using namespace clang;
31
Ted Kremenek2b5c83c2012-09-21 17:54:32 +000032//===----------------------------------------------------------------------===//
33// Helper creation functions for constructing faux ASTs.
34//===----------------------------------------------------------------------===//
Ted Kremenek14f779c2012-09-21 00:09:11 +000035
Ted Kremenekd81a4a12012-09-21 00:52:24 +000036static 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>();
Alexander Kornienko090360d2015-11-06 01:08:38 +000046 return FT && FT->getReturnType()->isVoidType() && FT->getNumParams() == 0;
Ted Kremenekd81a4a12012-09-21 00:52:24 +000047}
48
Ted Kremenek72418132012-09-21 17:54:35 +000049namespace {
50class ASTMaker {
51public:
52 ASTMaker(ASTContext &C) : C(C) {}
53
Ted Kremenekf465dc12012-09-21 18:33:54 +000054 /// Create a new BinaryOperator representing a simple assignment.
55 BinaryOperator *makeAssignment(const Expr *LHS, const Expr *RHS, QualType Ty);
56
Ted Kremenek089ffd02012-10-11 20:58:18 +000057 /// 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
Ted Kremenek69bcb822012-09-21 18:13:23 +000064 /// Create a new DeclRefExpr for the referenced variable.
George Karpenkov657a5892017-09-30 00:03:22 +000065 DeclRefExpr *makeDeclRefExpr(const VarDecl *D,
George Karpenkovb2a60c62017-10-17 22:28:18 +000066 bool RefersToEnclosingVariableOrCapture = false);
Ted Kremenek72418132012-09-21 17:54:35 +000067
Ted Kremenekdff35532012-09-21 18:33:52 +000068 /// Create a new UnaryOperator representing a dereference.
69 UnaryOperator *makeDereference(const Expr *Arg, QualType Ty);
70
Ted Kremenek69bcb822012-09-21 18:13:23 +000071 /// Create an implicit cast for an integer conversion.
Ted Kremenek6fdefb52012-10-12 00:18:19 +000072 Expr *makeIntegralCast(const Expr *Arg, QualType Ty);
Ted Kremenek69bcb822012-09-21 18:13:23 +000073
Ted Kremenek089ffd02012-10-11 20:58:18 +000074 /// Create an implicit cast to a builtin boolean type.
75 ImplicitCastExpr *makeIntegralCastToBoolean(const Expr *Arg);
76
George Karpenkov657a5892017-09-30 00:03:22 +000077 /// Create an implicit cast for lvalue-to-rvaluate conversions.
Ted Kremenekca90ea52012-09-21 18:13:27 +000078 ImplicitCastExpr *makeLvalueToRvalue(const Expr *Arg, QualType Ty);
79
George Karpenkov657a5892017-09-30 00:03:22 +000080 /// Make RValue out of variable declaration, creating a temporary
81 /// DeclRefExpr in the process.
82 ImplicitCastExpr *
83 makeLvalueToRvalue(const VarDecl *Decl,
George Karpenkovb2a60c62017-10-17 22:28:18 +000084 bool RefersToEnclosingVariableOrCapture = false);
George Karpenkov657a5892017-09-30 00:03:22 +000085
86 /// Create an implicit cast of the given type.
87 ImplicitCastExpr *makeImplicitCast(const Expr *Arg, QualType Ty,
88 CastKind CK = CK_LValueToRValue);
89
Ted Kremenek089ffd02012-10-11 20:58:18 +000090 /// Create an Objective-C bool literal.
91 ObjCBoolLiteralExpr *makeObjCBool(bool Val);
Jordan Rose1a866cd2014-01-10 20:06:06 +000092
93 /// Create an Objective-C ivar reference.
94 ObjCIvarRefExpr *makeObjCIvarRef(const Expr *Base, const ObjCIvarDecl *IVar);
Ted Kremenek089ffd02012-10-11 20:58:18 +000095
96 /// Create a Return statement.
97 ReturnStmt *makeReturn(const Expr *RetVal);
98
Devin Coughlin046833e2017-11-06 22:12:19 +000099 /// Create an integer literal expression of the given type.
100 IntegerLiteral *makeIntegerLiteral(uint64_t Value, QualType Ty);
George Karpenkov657a5892017-09-30 00:03:22 +0000101
102 /// Create a member expression.
103 MemberExpr *makeMemberExpression(Expr *base, ValueDecl *MemberDecl,
104 bool IsArrow = false,
105 ExprValueKind ValueKind = VK_LValue);
106
107 /// Returns a *first* member field of a record declaration with a given name.
108 /// \return an nullptr if no member with such a name exists.
George Karpenkovc928e1f2017-10-11 20:53:01 +0000109 ValueDecl *findMemberField(const RecordDecl *RD, StringRef Name);
George Karpenkov657a5892017-09-30 00:03:22 +0000110
Ted Kremenek72418132012-09-21 17:54:35 +0000111private:
112 ASTContext &C;
113};
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000114}
Ted Kremenek72418132012-09-21 17:54:35 +0000115
Ted Kremenekf465dc12012-09-21 18:33:54 +0000116BinaryOperator *ASTMaker::makeAssignment(const Expr *LHS, const Expr *RHS,
117 QualType Ty) {
118 return new (C) BinaryOperator(const_cast<Expr*>(LHS), const_cast<Expr*>(RHS),
119 BO_Assign, Ty, VK_RValue,
Adam Nemet484aa452017-03-27 19:17:25 +0000120 OK_Ordinary, SourceLocation(), FPOptions());
Ted Kremenekf465dc12012-09-21 18:33:54 +0000121}
122
Ted Kremenek089ffd02012-10-11 20:58:18 +0000123BinaryOperator *ASTMaker::makeComparison(const Expr *LHS, const Expr *RHS,
124 BinaryOperator::Opcode Op) {
125 assert(BinaryOperator::isLogicalOp(Op) ||
126 BinaryOperator::isComparisonOp(Op));
127 return new (C) BinaryOperator(const_cast<Expr*>(LHS),
128 const_cast<Expr*>(RHS),
129 Op,
130 C.getLogicalOperationType(),
131 VK_RValue,
Adam Nemet484aa452017-03-27 19:17:25 +0000132 OK_Ordinary, SourceLocation(), FPOptions());
Ted Kremenek089ffd02012-10-11 20:58:18 +0000133}
134
135CompoundStmt *ASTMaker::makeCompound(ArrayRef<Stmt *> Stmts) {
Nico Webera2a0eb92012-12-29 20:03:39 +0000136 return new (C) CompoundStmt(C, Stmts, SourceLocation(), SourceLocation());
Ted Kremenek089ffd02012-10-11 20:58:18 +0000137}
138
George Karpenkovb2a60c62017-10-17 22:28:18 +0000139DeclRefExpr *ASTMaker::makeDeclRefExpr(
140 const VarDecl *D,
141 bool RefersToEnclosingVariableOrCapture) {
142 QualType Type = D->getType().getNonReferenceType();
George Karpenkov657a5892017-09-30 00:03:22 +0000143
144 DeclRefExpr *DR = DeclRefExpr::Create(
145 C, NestedNameSpecifierLoc(), SourceLocation(), const_cast<VarDecl *>(D),
146 RefersToEnclosingVariableOrCapture, SourceLocation(), Type, VK_LValue);
Ted Kremenek72418132012-09-21 17:54:35 +0000147 return DR;
148}
149
Ted Kremenekdff35532012-09-21 18:33:52 +0000150UnaryOperator *ASTMaker::makeDereference(const Expr *Arg, QualType Ty) {
151 return new (C) UnaryOperator(const_cast<Expr*>(Arg), UO_Deref, Ty,
152 VK_LValue, OK_Ordinary, SourceLocation());
153}
154
Ted Kremenekca90ea52012-09-21 18:13:27 +0000155ImplicitCastExpr *ASTMaker::makeLvalueToRvalue(const Expr *Arg, QualType Ty) {
George Karpenkov657a5892017-09-30 00:03:22 +0000156 return makeImplicitCast(Arg, Ty, CK_LValueToRValue);
157}
158
George Karpenkov657a5892017-09-30 00:03:22 +0000159ImplicitCastExpr *
160ASTMaker::makeLvalueToRvalue(const VarDecl *Arg,
George Karpenkovb2a60c62017-10-17 22:28:18 +0000161 bool RefersToEnclosingVariableOrCapture) {
162 QualType Type = Arg->getType().getNonReferenceType();
George Karpenkov657a5892017-09-30 00:03:22 +0000163 return makeLvalueToRvalue(makeDeclRefExpr(Arg,
George Karpenkovb2a60c62017-10-17 22:28:18 +0000164 RefersToEnclosingVariableOrCapture),
George Karpenkov657a5892017-09-30 00:03:22 +0000165 Type);
166}
167
168ImplicitCastExpr *ASTMaker::makeImplicitCast(const Expr *Arg, QualType Ty,
169 CastKind CK) {
170 return ImplicitCastExpr::Create(C, Ty,
George Karpenkova1329382017-10-25 00:03:45 +0000171 /* CastKind=*/ CK,
172 /* Expr=*/ const_cast<Expr *>(Arg),
173 /* CXXCastPath=*/ nullptr,
174 /* ExprValueKind=*/ VK_RValue);
Ted Kremenekca90ea52012-09-21 18:13:27 +0000175}
176
Ted Kremenek6fdefb52012-10-12 00:18:19 +0000177Expr *ASTMaker::makeIntegralCast(const Expr *Arg, QualType Ty) {
178 if (Arg->getType() == Ty)
179 return const_cast<Expr*>(Arg);
Craig Topper25542942014-05-20 04:30:07 +0000180
Ted Kremenek69bcb822012-09-21 18:13:23 +0000181 return ImplicitCastExpr::Create(C, Ty, CK_IntegralCast,
Craig Topper25542942014-05-20 04:30:07 +0000182 const_cast<Expr*>(Arg), nullptr, VK_RValue);
Ted Kremenek69bcb822012-09-21 18:13:23 +0000183}
184
Ted Kremenek089ffd02012-10-11 20:58:18 +0000185ImplicitCastExpr *ASTMaker::makeIntegralCastToBoolean(const Expr *Arg) {
186 return ImplicitCastExpr::Create(C, C.BoolTy, CK_IntegralToBoolean,
Craig Topper25542942014-05-20 04:30:07 +0000187 const_cast<Expr*>(Arg), nullptr, VK_RValue);
Ted Kremenek089ffd02012-10-11 20:58:18 +0000188}
189
190ObjCBoolLiteralExpr *ASTMaker::makeObjCBool(bool Val) {
191 QualType Ty = C.getBOOLDecl() ? C.getBOOLType() : C.ObjCBuiltinBoolTy;
192 return new (C) ObjCBoolLiteralExpr(Val, Ty, SourceLocation());
193}
194
Jordan Rose1a866cd2014-01-10 20:06:06 +0000195ObjCIvarRefExpr *ASTMaker::makeObjCIvarRef(const Expr *Base,
196 const ObjCIvarDecl *IVar) {
197 return new (C) ObjCIvarRefExpr(const_cast<ObjCIvarDecl*>(IVar),
198 IVar->getType(), SourceLocation(),
199 SourceLocation(), const_cast<Expr*>(Base),
200 /*arrow=*/true, /*free=*/false);
201}
202
203
Ted Kremenek089ffd02012-10-11 20:58:18 +0000204ReturnStmt *ASTMaker::makeReturn(const Expr *RetVal) {
Craig Topper25542942014-05-20 04:30:07 +0000205 return new (C) ReturnStmt(SourceLocation(), const_cast<Expr*>(RetVal),
206 nullptr);
Ted Kremenek089ffd02012-10-11 20:58:18 +0000207}
208
Devin Coughlin046833e2017-11-06 22:12:19 +0000209IntegerLiteral *ASTMaker::makeIntegerLiteral(uint64_t Value, QualType Ty) {
210 llvm::APInt APValue = llvm::APInt(C.getTypeSize(Ty), Value);
211 return IntegerLiteral::Create(C, APValue, Ty, SourceLocation());
George Karpenkov657a5892017-09-30 00:03:22 +0000212}
213
214MemberExpr *ASTMaker::makeMemberExpression(Expr *base, ValueDecl *MemberDecl,
215 bool IsArrow,
216 ExprValueKind ValueKind) {
217
218 DeclAccessPair FoundDecl = DeclAccessPair::make(MemberDecl, AS_public);
219 return MemberExpr::Create(
220 C, base, IsArrow, SourceLocation(), NestedNameSpecifierLoc(),
221 SourceLocation(), MemberDecl, FoundDecl,
222 DeclarationNameInfo(MemberDecl->getDeclName(), SourceLocation()),
George Karpenkova1329382017-10-25 00:03:45 +0000223 /* TemplateArgumentListInfo=*/ nullptr, MemberDecl->getType(), ValueKind,
George Karpenkov657a5892017-09-30 00:03:22 +0000224 OK_Ordinary);
225}
226
George Karpenkovc928e1f2017-10-11 20:53:01 +0000227ValueDecl *ASTMaker::findMemberField(const RecordDecl *RD, StringRef Name) {
George Karpenkov657a5892017-09-30 00:03:22 +0000228
229 CXXBasePaths Paths(
230 /* FindAmbiguities=*/false,
231 /* RecordPaths=*/false,
George Karpenkova1329382017-10-25 00:03:45 +0000232 /* DetectVirtual=*/ false);
George Karpenkov657a5892017-09-30 00:03:22 +0000233 const IdentifierInfo &II = C.Idents.get(Name);
234 DeclarationName DeclName = C.DeclarationNames.getIdentifier(&II);
235
236 DeclContextLookupResult Decls = RD->lookup(DeclName);
237 for (NamedDecl *FoundDecl : Decls)
238 if (!FoundDecl->getDeclContext()->isFunctionOrMethod())
George Karpenkovc928e1f2017-10-11 20:53:01 +0000239 return cast<ValueDecl>(FoundDecl);
George Karpenkov657a5892017-09-30 00:03:22 +0000240
241 return nullptr;
242}
243
Ted Kremenek2b5c83c2012-09-21 17:54:32 +0000244//===----------------------------------------------------------------------===//
245// Creation functions for faux ASTs.
246//===----------------------------------------------------------------------===//
247
248typedef Stmt *(*FunctionFarmer)(ASTContext &C, const FunctionDecl *D);
249
George Karpenkov6dda6712017-10-02 21:01:46 +0000250static CallExpr *create_call_once_funcptr_call(ASTContext &C, ASTMaker M,
251 const ParmVarDecl *Callback,
252 ArrayRef<Expr *> CallArgs) {
George Karpenkov657a5892017-09-30 00:03:22 +0000253
George Karpenkov98e81cd2017-10-24 00:13:18 +0000254 QualType Ty = Callback->getType();
255 DeclRefExpr *Call = M.makeDeclRefExpr(Callback);
256 CastKind CK;
257 if (Ty->isRValueReferenceType()) {
258 CK = CK_LValueToRValue;
259 } else {
260 assert(Ty->isLValueReferenceType());
261 CK = CK_FunctionToPointerDecay;
262 Ty = C.getPointerType(Ty.getNonReferenceType());
263 }
264
265 return new (C)
266 CallExpr(C, M.makeImplicitCast(Call, Ty.getNonReferenceType(), CK),
267 /*args=*/CallArgs,
268 /*QualType=*/C.VoidTy,
269 /*ExprValueType=*/VK_RValue,
270 /*SourceLocation=*/SourceLocation());
George Karpenkov657a5892017-09-30 00:03:22 +0000271}
272
George Karpenkov6dda6712017-10-02 21:01:46 +0000273static CallExpr *create_call_once_lambda_call(ASTContext &C, ASTMaker M,
274 const ParmVarDecl *Callback,
George Karpenkovbd4254c2017-10-20 23:29:59 +0000275 CXXRecordDecl *CallbackDecl,
George Karpenkov6dda6712017-10-02 21:01:46 +0000276 ArrayRef<Expr *> CallArgs) {
George Karpenkov657a5892017-09-30 00:03:22 +0000277 assert(CallbackDecl != nullptr);
278 assert(CallbackDecl->isLambda());
279 FunctionDecl *callOperatorDecl = CallbackDecl->getLambdaCallOperator();
280 assert(callOperatorDecl != nullptr);
281
282 DeclRefExpr *callOperatorDeclRef =
George Karpenkova1329382017-10-25 00:03:45 +0000283 DeclRefExpr::Create(/* Ctx =*/ C,
284 /* QualifierLoc =*/ NestedNameSpecifierLoc(),
285 /* TemplateKWLoc =*/ SourceLocation(),
George Karpenkov657a5892017-09-30 00:03:22 +0000286 const_cast<FunctionDecl *>(callOperatorDecl),
George Karpenkova1329382017-10-25 00:03:45 +0000287 /* RefersToEnclosingVariableOrCapture=*/ false,
288 /* NameLoc =*/ SourceLocation(),
289 /* T =*/ callOperatorDecl->getType(),
290 /* VK =*/ VK_LValue);
George Karpenkov657a5892017-09-30 00:03:22 +0000291
George Karpenkov657a5892017-09-30 00:03:22 +0000292 return new (C)
293 CXXOperatorCallExpr(/*AstContext=*/C, OO_Call, callOperatorDeclRef,
294 /*args=*/CallArgs,
295 /*QualType=*/C.VoidTy,
296 /*ExprValueType=*/VK_RValue,
297 /*SourceLocation=*/SourceLocation(), FPOptions());
298}
299
300/// Create a fake body for std::call_once.
301/// Emulates the following function body:
302///
303/// \code
304/// typedef struct once_flag_s {
305/// unsigned long __state = 0;
306/// } once_flag;
307/// template<class Callable>
308/// void call_once(once_flag& o, Callable func) {
309/// if (!o.__state) {
310/// func();
311/// }
312/// o.__state = 1;
313/// }
314/// \endcode
315static Stmt *create_call_once(ASTContext &C, const FunctionDecl *D) {
316 DEBUG(llvm::dbgs() << "Generating body for call_once\n");
317
318 // We need at least two parameters.
319 if (D->param_size() < 2)
320 return nullptr;
321
322 ASTMaker M(C);
323
324 const ParmVarDecl *Flag = D->getParamDecl(0);
325 const ParmVarDecl *Callback = D->getParamDecl(1);
George Karpenkov03544832017-11-03 00:36:03 +0000326
327 if (!Callback->getType()->isReferenceType()) {
328 llvm::dbgs() << "libcxx03 std::call_once implementation, skipping.\n";
329 return nullptr;
330 }
331 if (!Flag->getType()->isReferenceType()) {
332 llvm::dbgs() << "unknown std::call_once implementation, skipping.\n";
333 return nullptr;
334 }
335
George Karpenkov657a5892017-09-30 00:03:22 +0000336 QualType CallbackType = Callback->getType().getNonReferenceType();
George Karpenkovbd4254c2017-10-20 23:29:59 +0000337
338 // Nullable pointer, non-null iff function is a CXXRecordDecl.
339 CXXRecordDecl *CallbackRecordDecl = CallbackType->getAsCXXRecordDecl();
George Karpenkov8b53f7c2017-10-09 23:20:46 +0000340 QualType FlagType = Flag->getType().getNonReferenceType();
George Karpenkovc928e1f2017-10-11 20:53:01 +0000341 auto *FlagRecordDecl = dyn_cast_or_null<RecordDecl>(FlagType->getAsTagDecl());
342
343 if (!FlagRecordDecl) {
344 DEBUG(llvm::dbgs() << "Flag field is not a record: "
345 << "unknown std::call_once implementation, "
346 << "ignoring the call.\n");
George Karpenkov8b53f7c2017-10-09 23:20:46 +0000347 return nullptr;
348 }
349
George Karpenkovc928e1f2017-10-11 20:53:01 +0000350 // We initially assume libc++ implementation of call_once,
351 // where the once_flag struct has a field `__state_`.
352 ValueDecl *FlagFieldDecl = M.findMemberField(FlagRecordDecl, "__state_");
353
354 // Otherwise, try libstdc++ implementation, with a field
355 // `_M_once`
356 if (!FlagFieldDecl) {
George Karpenkovc928e1f2017-10-11 20:53:01 +0000357 FlagFieldDecl = M.findMemberField(FlagRecordDecl, "_M_once");
358 }
359
360 if (!FlagFieldDecl) {
George Karpenkov03544832017-11-03 00:36:03 +0000361 DEBUG(llvm::dbgs() << "No field _M_once or __state_ found on "
362 << "std::once_flag struct: unknown std::call_once "
363 << "implementation, ignoring the call.");
George Karpenkov8b53f7c2017-10-09 23:20:46 +0000364 return nullptr;
365 }
George Karpenkov657a5892017-09-30 00:03:22 +0000366
George Karpenkovbd4254c2017-10-20 23:29:59 +0000367 bool isLambdaCall = CallbackRecordDecl && CallbackRecordDecl->isLambda();
368 if (CallbackRecordDecl && !isLambdaCall) {
369 DEBUG(llvm::dbgs() << "Not supported: synthesizing body for functors when "
370 << "body farming std::call_once, ignoring the call.");
371 return nullptr;
372 }
George Karpenkov6dda6712017-10-02 21:01:46 +0000373
George Karpenkov657a5892017-09-30 00:03:22 +0000374 SmallVector<Expr *, 5> CallArgs;
George Karpenkovbd4254c2017-10-20 23:29:59 +0000375 const FunctionProtoType *CallbackFunctionType;
376 if (isLambdaCall) {
George Karpenkov657a5892017-09-30 00:03:22 +0000377
George Karpenkov6dda6712017-10-02 21:01:46 +0000378 // Lambda requires callback itself inserted as a first parameter.
379 CallArgs.push_back(
380 M.makeDeclRefExpr(Callback,
George Karpenkova1329382017-10-25 00:03:45 +0000381 /* RefersToEnclosingVariableOrCapture=*/ true));
George Karpenkovbd4254c2017-10-20 23:29:59 +0000382 CallbackFunctionType = CallbackRecordDecl->getLambdaCallOperator()
383 ->getType()
384 ->getAs<FunctionProtoType>();
George Karpenkov98e81cd2017-10-24 00:13:18 +0000385 } else if (!CallbackType->getPointeeType().isNull()) {
George Karpenkovbd4254c2017-10-20 23:29:59 +0000386 CallbackFunctionType =
387 CallbackType->getPointeeType()->getAs<FunctionProtoType>();
George Karpenkov98e81cd2017-10-24 00:13:18 +0000388 } else {
389 CallbackFunctionType = CallbackType->getAs<FunctionProtoType>();
George Karpenkovbd4254c2017-10-20 23:29:59 +0000390 }
George Karpenkov6dda6712017-10-02 21:01:46 +0000391
George Karpenkovbd4254c2017-10-20 23:29:59 +0000392 if (!CallbackFunctionType)
393 return nullptr;
394
395 // First two arguments are used for the flag and for the callback.
396 if (D->getNumParams() != CallbackFunctionType->getNumParams() + 2) {
George Karpenkov03544832017-11-03 00:36:03 +0000397 DEBUG(llvm::dbgs() << "Types of params of the callback do not match "
398 << "params passed to std::call_once, "
399 << "ignoring the call\n");
George Karpenkovbd4254c2017-10-20 23:29:59 +0000400 return nullptr;
401 }
402
403 // All arguments past first two ones are passed to the callback,
404 // and we turn lvalues into rvalues if the argument is not passed by
405 // reference.
406 for (unsigned int ParamIdx = 2; ParamIdx < D->getNumParams(); ParamIdx++) {
407 const ParmVarDecl *PDecl = D->getParamDecl(ParamIdx);
408 Expr *ParamExpr = M.makeDeclRefExpr(PDecl);
409 if (!CallbackFunctionType->getParamType(ParamIdx - 2)->isReferenceType()) {
410 QualType PTy = PDecl->getType().getNonReferenceType();
411 ParamExpr = M.makeLvalueToRvalue(ParamExpr, PTy);
412 }
413 CallArgs.push_back(ParamExpr);
414 }
George Karpenkov657a5892017-09-30 00:03:22 +0000415
416 CallExpr *CallbackCall;
George Karpenkov6dda6712017-10-02 21:01:46 +0000417 if (isLambdaCall) {
George Karpenkov657a5892017-09-30 00:03:22 +0000418
George Karpenkovbd4254c2017-10-20 23:29:59 +0000419 CallbackCall = create_call_once_lambda_call(C, M, Callback,
420 CallbackRecordDecl, CallArgs);
George Karpenkov657a5892017-09-30 00:03:22 +0000421 } else {
422
423 // Function pointer case.
424 CallbackCall = create_call_once_funcptr_call(C, M, Callback, CallArgs);
425 }
426
George Karpenkov657a5892017-09-30 00:03:22 +0000427 DeclRefExpr *FlagDecl =
428 M.makeDeclRefExpr(Flag,
George Karpenkovb2a60c62017-10-17 22:28:18 +0000429 /* RefersToEnclosingVariableOrCapture=*/true);
George Karpenkov657a5892017-09-30 00:03:22 +0000430
George Karpenkov657a5892017-09-30 00:03:22 +0000431
George Karpenkovc928e1f2017-10-11 20:53:01 +0000432 MemberExpr *Deref = M.makeMemberExpression(FlagDecl, FlagFieldDecl);
George Karpenkov657a5892017-09-30 00:03:22 +0000433 assert(Deref->isLValue());
434 QualType DerefType = Deref->getType();
435
436 // Negation predicate.
437 UnaryOperator *FlagCheck = new (C) UnaryOperator(
George Karpenkova1329382017-10-25 00:03:45 +0000438 /* input=*/
George Karpenkov657a5892017-09-30 00:03:22 +0000439 M.makeImplicitCast(M.makeLvalueToRvalue(Deref, DerefType), DerefType,
440 CK_IntegralToBoolean),
George Karpenkova1329382017-10-25 00:03:45 +0000441 /* opc=*/ UO_LNot,
442 /* QualType=*/ C.IntTy,
443 /* ExprValueKind=*/ VK_RValue,
444 /* ExprObjectKind=*/ OK_Ordinary, SourceLocation());
George Karpenkov657a5892017-09-30 00:03:22 +0000445
446 // Create assignment.
447 BinaryOperator *FlagAssignment = M.makeAssignment(
Devin Coughlin046833e2017-11-06 22:12:19 +0000448 Deref, M.makeIntegralCast(M.makeIntegerLiteral(1, C.IntTy), DerefType),
449 DerefType);
George Karpenkov657a5892017-09-30 00:03:22 +0000450
451 IfStmt *Out = new (C)
452 IfStmt(C, SourceLocation(),
George Karpenkova1329382017-10-25 00:03:45 +0000453 /* IsConstexpr=*/ false,
454 /* init=*/ nullptr,
455 /* var=*/ nullptr,
456 /* cond=*/ FlagCheck,
457 /* then=*/ M.makeCompound({CallbackCall, FlagAssignment}));
George Karpenkov657a5892017-09-30 00:03:22 +0000458
459 return Out;
460}
461
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000462/// Create a fake body for dispatch_once.
463static Stmt *create_dispatch_once(ASTContext &C, const FunctionDecl *D) {
464 // Check if we have at least two parameters.
465 if (D->param_size() != 2)
Craig Topper25542942014-05-20 04:30:07 +0000466 return nullptr;
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000467
468 // Check if the first parameter is a pointer to integer type.
469 const ParmVarDecl *Predicate = D->getParamDecl(0);
470 QualType PredicateQPtrTy = Predicate->getType();
471 const PointerType *PredicatePtrTy = PredicateQPtrTy->getAs<PointerType>();
472 if (!PredicatePtrTy)
Craig Topper25542942014-05-20 04:30:07 +0000473 return nullptr;
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000474 QualType PredicateTy = PredicatePtrTy->getPointeeType();
475 if (!PredicateTy->isIntegerType())
Craig Topper25542942014-05-20 04:30:07 +0000476 return nullptr;
477
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000478 // Check if the second parameter is the proper block type.
479 const ParmVarDecl *Block = D->getParamDecl(1);
480 QualType Ty = Block->getType();
481 if (!isDispatchBlock(Ty))
Craig Topper25542942014-05-20 04:30:07 +0000482 return nullptr;
483
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000484 // Everything checks out. Create a fakse body that checks the predicate,
485 // sets it, and calls the block. Basically, an AST dump of:
486 //
487 // void dispatch_once(dispatch_once_t *predicate, dispatch_block_t block) {
Devin Coughlin046833e2017-11-06 22:12:19 +0000488 // if (*predicate != ~0l) {
489 // *predicate = ~0l;
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000490 // block();
491 // }
492 // }
493
Ted Kremenek72418132012-09-21 17:54:35 +0000494 ASTMaker M(C);
495
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000496 // (1) Create the call.
George Karpenkov657a5892017-09-30 00:03:22 +0000497 CallExpr *CE = new (C) CallExpr(
498 /*ASTContext=*/C,
499 /*StmtClass=*/M.makeLvalueToRvalue(/*Expr=*/Block),
500 /*args=*/None,
501 /*QualType=*/C.VoidTy,
502 /*ExprValueType=*/VK_RValue,
503 /*SourceLocation=*/SourceLocation());
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000504
505 // (2) Create the assignment to the predicate.
Devin Coughlin046833e2017-11-06 22:12:19 +0000506 Expr *DoneValue =
507 new (C) UnaryOperator(M.makeIntegerLiteral(0, C.LongTy), UO_Not, C.LongTy,
508 VK_RValue, OK_Ordinary, SourceLocation());
George Karpenkov657a5892017-09-30 00:03:22 +0000509
Ted Kremeneke7ad5352012-09-21 18:33:56 +0000510 BinaryOperator *B =
511 M.makeAssignment(
512 M.makeDereference(
513 M.makeLvalueToRvalue(
514 M.makeDeclRefExpr(Predicate), PredicateQPtrTy),
515 PredicateTy),
Devin Coughlin046833e2017-11-06 22:12:19 +0000516 M.makeIntegralCast(DoneValue, PredicateTy),
Ted Kremeneke7ad5352012-09-21 18:33:56 +0000517 PredicateTy);
518
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000519 // (3) Create the compound statement.
Craig Topper5fc8fc22014-08-27 06:28:36 +0000520 Stmt *Stmts[] = { B, CE };
521 CompoundStmt *CS = M.makeCompound(Stmts);
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000522
523 // (4) Create the 'if' condition.
Ted Kremeneke7ad5352012-09-21 18:33:56 +0000524 ImplicitCastExpr *LValToRval =
525 M.makeLvalueToRvalue(
526 M.makeDereference(
527 M.makeLvalueToRvalue(
528 M.makeDeclRefExpr(Predicate),
529 PredicateQPtrTy),
530 PredicateTy),
531 PredicateTy);
Devin Coughlin046833e2017-11-06 22:12:19 +0000532
533 Expr *GuardCondition = M.makeComparison(LValToRval, DoneValue, BO_NE);
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000534 // (5) Create the 'if' statement.
George Karpenkov657a5892017-09-30 00:03:22 +0000535 IfStmt *If = new (C) IfStmt(C, SourceLocation(),
George Karpenkova1329382017-10-25 00:03:45 +0000536 /* IsConstexpr=*/ false,
537 /* init=*/ nullptr,
538 /* var=*/ nullptr,
Devin Coughlin046833e2017-11-06 22:12:19 +0000539 /* cond=*/ GuardCondition,
George Karpenkova1329382017-10-25 00:03:45 +0000540 /* then=*/ CS);
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000541 return If;
542}
543
Ted Kremenek14f779c2012-09-21 00:09:11 +0000544/// Create a fake body for dispatch_sync.
545static Stmt *create_dispatch_sync(ASTContext &C, const FunctionDecl *D) {
546 // Check if we have at least two parameters.
547 if (D->param_size() != 2)
Craig Topper25542942014-05-20 04:30:07 +0000548 return nullptr;
549
Ted Kremenek14f779c2012-09-21 00:09:11 +0000550 // Check if the second parameter is a block.
551 const ParmVarDecl *PV = D->getParamDecl(1);
552 QualType Ty = PV->getType();
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000553 if (!isDispatchBlock(Ty))
Craig Topper25542942014-05-20 04:30:07 +0000554 return nullptr;
555
Ted Kremenek14f779c2012-09-21 00:09:11 +0000556 // Everything checks out. Create a fake body that just calls the block.
557 // This is basically just an AST dump of:
558 //
559 // void dispatch_sync(dispatch_queue_t queue, void (^block)(void)) {
560 // block();
561 // }
Ted Kremenek72418132012-09-21 17:54:35 +0000562 //
563 ASTMaker M(C);
564 DeclRefExpr *DR = M.makeDeclRefExpr(PV);
Ted Kremenekdff35532012-09-21 18:33:52 +0000565 ImplicitCastExpr *ICE = M.makeLvalueToRvalue(DR, Ty);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000566 CallExpr *CE = new (C) CallExpr(C, ICE, None, C.VoidTy, VK_RValue,
567 SourceLocation());
Ted Kremenek14f779c2012-09-21 00:09:11 +0000568 return CE;
569}
570
Ted Kremenek089ffd02012-10-11 20:58:18 +0000571static Stmt *create_OSAtomicCompareAndSwap(ASTContext &C, const FunctionDecl *D)
572{
573 // There are exactly 3 arguments.
574 if (D->param_size() != 3)
Craig Topper25542942014-05-20 04:30:07 +0000575 return nullptr;
576
Anna Zaks064185a2013-02-05 19:52:26 +0000577 // Signature:
578 // _Bool OSAtomicCompareAndSwapPtr(void *__oldValue,
579 // void *__newValue,
580 // void * volatile *__theValue)
581 // Generate body:
Ted Kremenek089ffd02012-10-11 20:58:18 +0000582 // if (oldValue == *theValue) {
583 // *theValue = newValue;
584 // return YES;
585 // }
586 // else return NO;
Alp Toker314cc812014-01-25 16:55:45 +0000587
588 QualType ResultTy = D->getReturnType();
Ted Kremenek6fdefb52012-10-12 00:18:19 +0000589 bool isBoolean = ResultTy->isBooleanType();
590 if (!isBoolean && !ResultTy->isIntegralType(C))
Craig Topper25542942014-05-20 04:30:07 +0000591 return nullptr;
592
Ted Kremenek089ffd02012-10-11 20:58:18 +0000593 const ParmVarDecl *OldValue = D->getParamDecl(0);
594 QualType OldValueTy = OldValue->getType();
595
596 const ParmVarDecl *NewValue = D->getParamDecl(1);
597 QualType NewValueTy = NewValue->getType();
598
599 assert(OldValueTy == NewValueTy);
600
601 const ParmVarDecl *TheValue = D->getParamDecl(2);
602 QualType TheValueTy = TheValue->getType();
603 const PointerType *PT = TheValueTy->getAs<PointerType>();
604 if (!PT)
Craig Topper25542942014-05-20 04:30:07 +0000605 return nullptr;
Ted Kremenek089ffd02012-10-11 20:58:18 +0000606 QualType PointeeTy = PT->getPointeeType();
607
608 ASTMaker M(C);
609 // Construct the comparison.
610 Expr *Comparison =
611 M.makeComparison(
612 M.makeLvalueToRvalue(M.makeDeclRefExpr(OldValue), OldValueTy),
613 M.makeLvalueToRvalue(
614 M.makeDereference(
615 M.makeLvalueToRvalue(M.makeDeclRefExpr(TheValue), TheValueTy),
616 PointeeTy),
617 PointeeTy),
618 BO_EQ);
619
620 // Construct the body of the IfStmt.
621 Stmt *Stmts[2];
622 Stmts[0] =
623 M.makeAssignment(
624 M.makeDereference(
625 M.makeLvalueToRvalue(M.makeDeclRefExpr(TheValue), TheValueTy),
626 PointeeTy),
627 M.makeLvalueToRvalue(M.makeDeclRefExpr(NewValue), NewValueTy),
628 NewValueTy);
Ted Kremenek6fdefb52012-10-12 00:18:19 +0000629
630 Expr *BoolVal = M.makeObjCBool(true);
631 Expr *RetVal = isBoolean ? M.makeIntegralCastToBoolean(BoolVal)
632 : M.makeIntegralCast(BoolVal, ResultTy);
633 Stmts[1] = M.makeReturn(RetVal);
Craig Topper5fc8fc22014-08-27 06:28:36 +0000634 CompoundStmt *Body = M.makeCompound(Stmts);
Ted Kremenek089ffd02012-10-11 20:58:18 +0000635
636 // Construct the else clause.
Ted Kremenek6fdefb52012-10-12 00:18:19 +0000637 BoolVal = M.makeObjCBool(false);
638 RetVal = isBoolean ? M.makeIntegralCastToBoolean(BoolVal)
639 : M.makeIntegralCast(BoolVal, ResultTy);
640 Stmt *Else = M.makeReturn(RetVal);
Ted Kremenek089ffd02012-10-11 20:58:18 +0000641
642 /// Construct the If.
Richard Smitha547eb22016-07-14 00:11:03 +0000643 Stmt *If = new (C) IfStmt(C, SourceLocation(), false, nullptr, nullptr,
644 Comparison, Body, SourceLocation(), Else);
Craig Topper25542942014-05-20 04:30:07 +0000645
Ted Kremenek089ffd02012-10-11 20:58:18 +0000646 return If;
647}
648
Ted Kremenek14f779c2012-09-21 00:09:11 +0000649Stmt *BodyFarm::getBody(const FunctionDecl *D) {
650 D = D->getCanonicalDecl();
651
David Blaikie05785d12013-02-20 22:23:23 +0000652 Optional<Stmt *> &Val = Bodies[D];
Ted Kremenek14f779c2012-09-21 00:09:11 +0000653 if (Val.hasValue())
654 return Val.getValue();
Craig Topper25542942014-05-20 04:30:07 +0000655
656 Val = nullptr;
657
658 if (D->getIdentifier() == nullptr)
659 return nullptr;
Ted Kremenek14f779c2012-09-21 00:09:11 +0000660
661 StringRef Name = D->getName();
662 if (Name.empty())
Craig Topper25542942014-05-20 04:30:07 +0000663 return nullptr;
Ted Kremenek089ffd02012-10-11 20:58:18 +0000664
665 FunctionFarmer FF;
666
667 if (Name.startswith("OSAtomicCompareAndSwap") ||
668 Name.startswith("objc_atomicCompareAndSwap")) {
669 FF = create_OSAtomicCompareAndSwap;
George Karpenkov657a5892017-09-30 00:03:22 +0000670 } else if (Name == "call_once" && D->getDeclContext()->isStdNamespace()) {
671 FF = create_call_once;
672 } else {
Ted Kremenek089ffd02012-10-11 20:58:18 +0000673 FF = llvm::StringSwitch<FunctionFarmer>(Name)
674 .Case("dispatch_sync", create_dispatch_sync)
675 .Case("dispatch_once", create_dispatch_once)
Craig Topper25542942014-05-20 04:30:07 +0000676 .Default(nullptr);
Ted Kremenek14f779c2012-09-21 00:09:11 +0000677 }
678
Ted Kremenek089ffd02012-10-11 20:58:18 +0000679 if (FF) { Val = FF(C, D); }
Ted Kremenekeeccb302014-08-27 15:14:15 +0000680 else if (Injector) { Val = Injector->getBody(D); }
Ted Kremenek14f779c2012-09-21 00:09:11 +0000681 return Val.getValue();
682}
683
Devin Coughlinb7e810b2016-01-26 23:58:48 +0000684static const ObjCIvarDecl *findBackingIvar(const ObjCPropertyDecl *Prop) {
685 const ObjCIvarDecl *IVar = Prop->getPropertyIvarDecl();
686
687 if (IVar)
688 return IVar;
689
690 // When a readonly property is shadowed in a class extensions with a
691 // a readwrite property, the instance variable belongs to the shadowing
692 // property rather than the shadowed property. If there is no instance
693 // variable on a readonly property, check to see whether the property is
694 // shadowed and if so try to get the instance variable from shadowing
695 // property.
696 if (!Prop->isReadOnly())
697 return nullptr;
698
699 auto *Container = cast<ObjCContainerDecl>(Prop->getDeclContext());
700 const ObjCInterfaceDecl *PrimaryInterface = nullptr;
701 if (auto *InterfaceDecl = dyn_cast<ObjCInterfaceDecl>(Container)) {
702 PrimaryInterface = InterfaceDecl;
703 } else if (auto *CategoryDecl = dyn_cast<ObjCCategoryDecl>(Container)) {
704 PrimaryInterface = CategoryDecl->getClassInterface();
705 } else if (auto *ImplDecl = dyn_cast<ObjCImplDecl>(Container)) {
706 PrimaryInterface = ImplDecl->getClassInterface();
707 } else {
708 return nullptr;
709 }
710
711 // FindPropertyVisibleInPrimaryClass() looks first in class extensions, so it
712 // is guaranteed to find the shadowing property, if it exists, rather than
713 // the shadowed property.
714 auto *ShadowingProp = PrimaryInterface->FindPropertyVisibleInPrimaryClass(
Manman Ren5b786402016-01-28 18:49:28 +0000715 Prop->getIdentifier(), Prop->getQueryKind());
Devin Coughlinb7e810b2016-01-26 23:58:48 +0000716 if (ShadowingProp && ShadowingProp != Prop) {
717 IVar = ShadowingProp->getPropertyIvarDecl();
718 }
719
720 return IVar;
721}
722
Jordan Rose1a866cd2014-01-10 20:06:06 +0000723static Stmt *createObjCPropertyGetter(ASTContext &Ctx,
724 const ObjCPropertyDecl *Prop) {
Jordan Roseddf19662014-01-23 03:59:10 +0000725 // First, find the backing ivar.
Devin Coughlinb7e810b2016-01-26 23:58:48 +0000726 const ObjCIvarDecl *IVar = findBackingIvar(Prop);
Jordan Rose1a866cd2014-01-10 20:06:06 +0000727 if (!IVar)
Craig Topper25542942014-05-20 04:30:07 +0000728 return nullptr;
Jordan Roseddf19662014-01-23 03:59:10 +0000729
730 // Ignore weak variables, which have special behavior.
Jordan Rose1a866cd2014-01-10 20:06:06 +0000731 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak)
Craig Topper25542942014-05-20 04:30:07 +0000732 return nullptr;
Jordan Rosea3f27812014-01-14 17:29:06 +0000733
Jordan Roseddf19662014-01-23 03:59:10 +0000734 // Look to see if Sema has synthesized a body for us. This happens in
735 // Objective-C++ because the return value may be a C++ class type with a
736 // non-trivial copy constructor. We can only do this if we can find the
737 // @synthesize for this property, though (or if we know it's been auto-
738 // synthesized).
Jordan Rosea3f27812014-01-14 17:29:06 +0000739 const ObjCImplementationDecl *ImplDecl =
740 IVar->getContainingInterface()->getImplementation();
741 if (ImplDecl) {
Aaron Ballmand85eff42014-03-14 15:02:45 +0000742 for (const auto *I : ImplDecl->property_impls()) {
Jordan Rosea3f27812014-01-14 17:29:06 +0000743 if (I->getPropertyDecl() != Prop)
744 continue;
745
746 if (I->getGetterCXXConstructor()) {
747 ASTMaker M(Ctx);
748 return M.makeReturn(I->getGetterCXXConstructor());
749 }
750 }
751 }
752
Jordan Roseddf19662014-01-23 03:59:10 +0000753 // Sanity check that the property is the same type as the ivar, or a
754 // reference to it, and that it is either an object pointer or trivially
755 // copyable.
756 if (!Ctx.hasSameUnqualifiedType(IVar->getType(),
757 Prop->getType().getNonReferenceType()))
Craig Topper25542942014-05-20 04:30:07 +0000758 return nullptr;
Jordan Roseddf19662014-01-23 03:59:10 +0000759 if (!IVar->getType()->isObjCLifetimeType() &&
760 !IVar->getType().isTriviallyCopyableType(Ctx))
Craig Topper25542942014-05-20 04:30:07 +0000761 return nullptr;
Jordan Rose1a866cd2014-01-10 20:06:06 +0000762
Jordan Roseddf19662014-01-23 03:59:10 +0000763 // Generate our body:
764 // return self->_ivar;
Jordan Rose1a866cd2014-01-10 20:06:06 +0000765 ASTMaker M(Ctx);
766
767 const VarDecl *selfVar = Prop->getGetterMethodDecl()->getSelfDecl();
Devin Coughlinaac894f2017-01-11 01:02:34 +0000768 if (!selfVar)
769 return nullptr;
Jordan Rose1a866cd2014-01-10 20:06:06 +0000770
771 Expr *loadedIVar =
772 M.makeObjCIvarRef(
773 M.makeLvalueToRvalue(
774 M.makeDeclRefExpr(selfVar),
775 selfVar->getType()),
776 IVar);
777
778 if (!Prop->getType()->isReferenceType())
779 loadedIVar = M.makeLvalueToRvalue(loadedIVar, IVar->getType());
780
781 return M.makeReturn(loadedIVar);
782}
783
Jordan Roseddf19662014-01-23 03:59:10 +0000784Stmt *BodyFarm::getBody(const ObjCMethodDecl *D) {
785 // We currently only know how to synthesize property accessors.
Jordan Rose1a866cd2014-01-10 20:06:06 +0000786 if (!D->isPropertyAccessor())
Craig Topper25542942014-05-20 04:30:07 +0000787 return nullptr;
Jordan Rose1a866cd2014-01-10 20:06:06 +0000788
789 D = D->getCanonicalDecl();
790
791 Optional<Stmt *> &Val = Bodies[D];
792 if (Val.hasValue())
793 return Val.getValue();
Craig Topper25542942014-05-20 04:30:07 +0000794 Val = nullptr;
Jordan Rose1a866cd2014-01-10 20:06:06 +0000795
Jordan Roseddf19662014-01-23 03:59:10 +0000796 const ObjCPropertyDecl *Prop = D->findPropertyDecl();
Jordan Rose1a866cd2014-01-10 20:06:06 +0000797 if (!Prop)
Craig Topper25542942014-05-20 04:30:07 +0000798 return nullptr;
Jordan Rose1a866cd2014-01-10 20:06:06 +0000799
Jordan Roseddf19662014-01-23 03:59:10 +0000800 // For now, we only synthesize getters.
Devin Coughlinef3697e2016-02-18 19:37:39 +0000801 // Synthesizing setters would cause false negatives in the
802 // RetainCountChecker because the method body would bind the parameter
803 // to an instance variable, causing it to escape. This would prevent
804 // warning in the following common scenario:
805 //
806 // id foo = [[NSObject alloc] init];
807 // self.foo = foo; // We should warn that foo leaks here.
808 //
Jordan Rose1a866cd2014-01-10 20:06:06 +0000809 if (D->param_size() != 0)
Craig Topper25542942014-05-20 04:30:07 +0000810 return nullptr;
Jordan Rose1a866cd2014-01-10 20:06:06 +0000811
812 Val = createObjCPropertyGetter(C, Prop);
813
814 return Val.getValue();
815}
816