blob: 492d01f6c98e86f143bd42e3e4bec1defcfb457d [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) {
Benjamin Kramer07420902017-12-24 16:24:20 +0000136 return CompoundStmt::Create(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,
Aaron Ballmana5038552018-01-09 13:07:03 +0000152 VK_LValue, OK_Ordinary, SourceLocation(),
153 /*CanOverflow*/ false);
Ted Kremenekdff35532012-09-21 18:33:52 +0000154}
155
Ted Kremenekca90ea52012-09-21 18:13:27 +0000156ImplicitCastExpr *ASTMaker::makeLvalueToRvalue(const Expr *Arg, QualType Ty) {
George Karpenkov657a5892017-09-30 00:03:22 +0000157 return makeImplicitCast(Arg, Ty, CK_LValueToRValue);
158}
159
George Karpenkov657a5892017-09-30 00:03:22 +0000160ImplicitCastExpr *
161ASTMaker::makeLvalueToRvalue(const VarDecl *Arg,
George Karpenkovb2a60c62017-10-17 22:28:18 +0000162 bool RefersToEnclosingVariableOrCapture) {
163 QualType Type = Arg->getType().getNonReferenceType();
George Karpenkov657a5892017-09-30 00:03:22 +0000164 return makeLvalueToRvalue(makeDeclRefExpr(Arg,
George Karpenkovb2a60c62017-10-17 22:28:18 +0000165 RefersToEnclosingVariableOrCapture),
George Karpenkov657a5892017-09-30 00:03:22 +0000166 Type);
167}
168
169ImplicitCastExpr *ASTMaker::makeImplicitCast(const Expr *Arg, QualType Ty,
170 CastKind CK) {
171 return ImplicitCastExpr::Create(C, Ty,
George Karpenkova1329382017-10-25 00:03:45 +0000172 /* CastKind=*/ CK,
173 /* Expr=*/ const_cast<Expr *>(Arg),
174 /* CXXCastPath=*/ nullptr,
175 /* ExprValueKind=*/ VK_RValue);
Ted Kremenekca90ea52012-09-21 18:13:27 +0000176}
177
Ted Kremenek6fdefb52012-10-12 00:18:19 +0000178Expr *ASTMaker::makeIntegralCast(const Expr *Arg, QualType Ty) {
179 if (Arg->getType() == Ty)
180 return const_cast<Expr*>(Arg);
Craig Topper25542942014-05-20 04:30:07 +0000181
Ted Kremenek69bcb822012-09-21 18:13:23 +0000182 return ImplicitCastExpr::Create(C, Ty, CK_IntegralCast,
Craig Topper25542942014-05-20 04:30:07 +0000183 const_cast<Expr*>(Arg), nullptr, VK_RValue);
Ted Kremenek69bcb822012-09-21 18:13:23 +0000184}
185
Ted Kremenek089ffd02012-10-11 20:58:18 +0000186ImplicitCastExpr *ASTMaker::makeIntegralCastToBoolean(const Expr *Arg) {
187 return ImplicitCastExpr::Create(C, C.BoolTy, CK_IntegralToBoolean,
Craig Topper25542942014-05-20 04:30:07 +0000188 const_cast<Expr*>(Arg), nullptr, VK_RValue);
Ted Kremenek089ffd02012-10-11 20:58:18 +0000189}
190
191ObjCBoolLiteralExpr *ASTMaker::makeObjCBool(bool Val) {
192 QualType Ty = C.getBOOLDecl() ? C.getBOOLType() : C.ObjCBuiltinBoolTy;
193 return new (C) ObjCBoolLiteralExpr(Val, Ty, SourceLocation());
194}
195
Jordan Rose1a866cd2014-01-10 20:06:06 +0000196ObjCIvarRefExpr *ASTMaker::makeObjCIvarRef(const Expr *Base,
197 const ObjCIvarDecl *IVar) {
198 return new (C) ObjCIvarRefExpr(const_cast<ObjCIvarDecl*>(IVar),
199 IVar->getType(), SourceLocation(),
200 SourceLocation(), const_cast<Expr*>(Base),
201 /*arrow=*/true, /*free=*/false);
202}
203
204
Ted Kremenek089ffd02012-10-11 20:58:18 +0000205ReturnStmt *ASTMaker::makeReturn(const Expr *RetVal) {
Craig Topper25542942014-05-20 04:30:07 +0000206 return new (C) ReturnStmt(SourceLocation(), const_cast<Expr*>(RetVal),
207 nullptr);
Ted Kremenek089ffd02012-10-11 20:58:18 +0000208}
209
Devin Coughlin046833e2017-11-06 22:12:19 +0000210IntegerLiteral *ASTMaker::makeIntegerLiteral(uint64_t Value, QualType Ty) {
211 llvm::APInt APValue = llvm::APInt(C.getTypeSize(Ty), Value);
212 return IntegerLiteral::Create(C, APValue, Ty, SourceLocation());
George Karpenkov657a5892017-09-30 00:03:22 +0000213}
214
215MemberExpr *ASTMaker::makeMemberExpression(Expr *base, ValueDecl *MemberDecl,
216 bool IsArrow,
217 ExprValueKind ValueKind) {
218
219 DeclAccessPair FoundDecl = DeclAccessPair::make(MemberDecl, AS_public);
220 return MemberExpr::Create(
221 C, base, IsArrow, SourceLocation(), NestedNameSpecifierLoc(),
222 SourceLocation(), MemberDecl, FoundDecl,
223 DeclarationNameInfo(MemberDecl->getDeclName(), SourceLocation()),
George Karpenkova1329382017-10-25 00:03:45 +0000224 /* TemplateArgumentListInfo=*/ nullptr, MemberDecl->getType(), ValueKind,
George Karpenkov657a5892017-09-30 00:03:22 +0000225 OK_Ordinary);
226}
227
George Karpenkovc928e1f2017-10-11 20:53:01 +0000228ValueDecl *ASTMaker::findMemberField(const RecordDecl *RD, StringRef Name) {
George Karpenkov657a5892017-09-30 00:03:22 +0000229
230 CXXBasePaths Paths(
231 /* FindAmbiguities=*/false,
232 /* RecordPaths=*/false,
George Karpenkova1329382017-10-25 00:03:45 +0000233 /* DetectVirtual=*/ false);
George Karpenkov657a5892017-09-30 00:03:22 +0000234 const IdentifierInfo &II = C.Idents.get(Name);
235 DeclarationName DeclName = C.DeclarationNames.getIdentifier(&II);
236
237 DeclContextLookupResult Decls = RD->lookup(DeclName);
238 for (NamedDecl *FoundDecl : Decls)
239 if (!FoundDecl->getDeclContext()->isFunctionOrMethod())
George Karpenkovc928e1f2017-10-11 20:53:01 +0000240 return cast<ValueDecl>(FoundDecl);
George Karpenkov657a5892017-09-30 00:03:22 +0000241
242 return nullptr;
243}
244
Ted Kremenek2b5c83c2012-09-21 17:54:32 +0000245//===----------------------------------------------------------------------===//
246// Creation functions for faux ASTs.
247//===----------------------------------------------------------------------===//
248
249typedef Stmt *(*FunctionFarmer)(ASTContext &C, const FunctionDecl *D);
250
George Karpenkov6dda6712017-10-02 21:01:46 +0000251static CallExpr *create_call_once_funcptr_call(ASTContext &C, ASTMaker M,
252 const ParmVarDecl *Callback,
253 ArrayRef<Expr *> CallArgs) {
George Karpenkov657a5892017-09-30 00:03:22 +0000254
George Karpenkov98e81cd2017-10-24 00:13:18 +0000255 QualType Ty = Callback->getType();
256 DeclRefExpr *Call = M.makeDeclRefExpr(Callback);
257 CastKind CK;
258 if (Ty->isRValueReferenceType()) {
259 CK = CK_LValueToRValue;
260 } else {
261 assert(Ty->isLValueReferenceType());
262 CK = CK_FunctionToPointerDecay;
263 Ty = C.getPointerType(Ty.getNonReferenceType());
264 }
265
266 return new (C)
267 CallExpr(C, M.makeImplicitCast(Call, Ty.getNonReferenceType(), CK),
268 /*args=*/CallArgs,
269 /*QualType=*/C.VoidTy,
270 /*ExprValueType=*/VK_RValue,
271 /*SourceLocation=*/SourceLocation());
George Karpenkov657a5892017-09-30 00:03:22 +0000272}
273
George Karpenkov6dda6712017-10-02 21:01:46 +0000274static CallExpr *create_call_once_lambda_call(ASTContext &C, ASTMaker M,
275 const ParmVarDecl *Callback,
George Karpenkovbd4254c2017-10-20 23:29:59 +0000276 CXXRecordDecl *CallbackDecl,
George Karpenkov6dda6712017-10-02 21:01:46 +0000277 ArrayRef<Expr *> CallArgs) {
George Karpenkov657a5892017-09-30 00:03:22 +0000278 assert(CallbackDecl != nullptr);
279 assert(CallbackDecl->isLambda());
280 FunctionDecl *callOperatorDecl = CallbackDecl->getLambdaCallOperator();
281 assert(callOperatorDecl != nullptr);
282
283 DeclRefExpr *callOperatorDeclRef =
George Karpenkova1329382017-10-25 00:03:45 +0000284 DeclRefExpr::Create(/* Ctx =*/ C,
285 /* QualifierLoc =*/ NestedNameSpecifierLoc(),
286 /* TemplateKWLoc =*/ SourceLocation(),
George Karpenkov657a5892017-09-30 00:03:22 +0000287 const_cast<FunctionDecl *>(callOperatorDecl),
George Karpenkova1329382017-10-25 00:03:45 +0000288 /* RefersToEnclosingVariableOrCapture=*/ false,
289 /* NameLoc =*/ SourceLocation(),
290 /* T =*/ callOperatorDecl->getType(),
291 /* VK =*/ VK_LValue);
George Karpenkov657a5892017-09-30 00:03:22 +0000292
George Karpenkov657a5892017-09-30 00:03:22 +0000293 return new (C)
294 CXXOperatorCallExpr(/*AstContext=*/C, OO_Call, callOperatorDeclRef,
295 /*args=*/CallArgs,
296 /*QualType=*/C.VoidTy,
297 /*ExprValueType=*/VK_RValue,
298 /*SourceLocation=*/SourceLocation(), FPOptions());
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
316static Stmt *create_call_once(ASTContext &C, const FunctionDecl *D) {
317 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);
George Karpenkov03544832017-11-03 00:36:03 +0000327
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
George Karpenkov657a5892017-09-30 00:03:22 +0000337 QualType CallbackType = Callback->getType().getNonReferenceType();
George Karpenkovbd4254c2017-10-20 23:29:59 +0000338
339 // Nullable pointer, non-null iff function is a CXXRecordDecl.
340 CXXRecordDecl *CallbackRecordDecl = CallbackType->getAsCXXRecordDecl();
George Karpenkov8b53f7c2017-10-09 23:20:46 +0000341 QualType FlagType = Flag->getType().getNonReferenceType();
George Karpenkovc928e1f2017-10-11 20:53:01 +0000342 auto *FlagRecordDecl = dyn_cast_or_null<RecordDecl>(FlagType->getAsTagDecl());
343
344 if (!FlagRecordDecl) {
345 DEBUG(llvm::dbgs() << "Flag field is not a record: "
346 << "unknown std::call_once implementation, "
347 << "ignoring the call.\n");
George Karpenkov8b53f7c2017-10-09 23:20:46 +0000348 return nullptr;
349 }
350
George Karpenkovc928e1f2017-10-11 20:53:01 +0000351 // 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) {
George Karpenkovc928e1f2017-10-11 20:53:01 +0000358 FlagFieldDecl = M.findMemberField(FlagRecordDecl, "_M_once");
359 }
360
361 if (!FlagFieldDecl) {
George Karpenkov03544832017-11-03 00:36:03 +0000362 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.");
George Karpenkov8b53f7c2017-10-09 23:20:46 +0000365 return nullptr;
366 }
George Karpenkov657a5892017-09-30 00:03:22 +0000367
George Karpenkovbd4254c2017-10-20 23:29:59 +0000368 bool isLambdaCall = CallbackRecordDecl && CallbackRecordDecl->isLambda();
369 if (CallbackRecordDecl && !isLambdaCall) {
370 DEBUG(llvm::dbgs() << "Not supported: synthesizing body for functors when "
371 << "body farming std::call_once, ignoring the call.");
372 return nullptr;
373 }
George Karpenkov6dda6712017-10-02 21:01:46 +0000374
George Karpenkov657a5892017-09-30 00:03:22 +0000375 SmallVector<Expr *, 5> CallArgs;
George Karpenkovbd4254c2017-10-20 23:29:59 +0000376 const FunctionProtoType *CallbackFunctionType;
377 if (isLambdaCall) {
George Karpenkov657a5892017-09-30 00:03:22 +0000378
George Karpenkov6dda6712017-10-02 21:01:46 +0000379 // Lambda requires callback itself inserted as a first parameter.
380 CallArgs.push_back(
381 M.makeDeclRefExpr(Callback,
George Karpenkova1329382017-10-25 00:03:45 +0000382 /* RefersToEnclosingVariableOrCapture=*/ true));
George Karpenkovbd4254c2017-10-20 23:29:59 +0000383 CallbackFunctionType = CallbackRecordDecl->getLambdaCallOperator()
384 ->getType()
385 ->getAs<FunctionProtoType>();
George Karpenkov98e81cd2017-10-24 00:13:18 +0000386 } else if (!CallbackType->getPointeeType().isNull()) {
George Karpenkovbd4254c2017-10-20 23:29:59 +0000387 CallbackFunctionType =
388 CallbackType->getPointeeType()->getAs<FunctionProtoType>();
George Karpenkov98e81cd2017-10-24 00:13:18 +0000389 } else {
390 CallbackFunctionType = CallbackType->getAs<FunctionProtoType>();
George Karpenkovbd4254c2017-10-20 23:29:59 +0000391 }
George Karpenkov6dda6712017-10-02 21:01:46 +0000392
George Karpenkovbd4254c2017-10-20 23:29:59 +0000393 if (!CallbackFunctionType)
394 return nullptr;
395
396 // First two arguments are used for the flag and for the callback.
397 if (D->getNumParams() != CallbackFunctionType->getNumParams() + 2) {
George Karpenkov03544832017-11-03 00:36:03 +0000398 DEBUG(llvm::dbgs() << "Types of params of the callback do not match "
399 << "params passed to std::call_once, "
400 << "ignoring the call\n");
George Karpenkovbd4254c2017-10-20 23:29:59 +0000401 return nullptr;
402 }
403
404 // All arguments past first two ones are passed to the callback,
405 // and we turn lvalues into rvalues if the argument is not passed by
406 // reference.
407 for (unsigned int ParamIdx = 2; ParamIdx < D->getNumParams(); ParamIdx++) {
408 const ParmVarDecl *PDecl = D->getParamDecl(ParamIdx);
409 Expr *ParamExpr = M.makeDeclRefExpr(PDecl);
410 if (!CallbackFunctionType->getParamType(ParamIdx - 2)->isReferenceType()) {
411 QualType PTy = PDecl->getType().getNonReferenceType();
412 ParamExpr = M.makeLvalueToRvalue(ParamExpr, PTy);
413 }
414 CallArgs.push_back(ParamExpr);
415 }
George Karpenkov657a5892017-09-30 00:03:22 +0000416
417 CallExpr *CallbackCall;
George Karpenkov6dda6712017-10-02 21:01:46 +0000418 if (isLambdaCall) {
George Karpenkov657a5892017-09-30 00:03:22 +0000419
George Karpenkovbd4254c2017-10-20 23:29:59 +0000420 CallbackCall = create_call_once_lambda_call(C, M, Callback,
421 CallbackRecordDecl, CallArgs);
George Karpenkov657a5892017-09-30 00:03:22 +0000422 } else {
423
424 // Function pointer case.
425 CallbackCall = create_call_once_funcptr_call(C, M, Callback, CallArgs);
426 }
427
George Karpenkov657a5892017-09-30 00:03:22 +0000428 DeclRefExpr *FlagDecl =
429 M.makeDeclRefExpr(Flag,
George Karpenkovb2a60c62017-10-17 22:28:18 +0000430 /* RefersToEnclosingVariableOrCapture=*/true);
George Karpenkov657a5892017-09-30 00:03:22 +0000431
George Karpenkov657a5892017-09-30 00:03:22 +0000432
George Karpenkovc928e1f2017-10-11 20:53:01 +0000433 MemberExpr *Deref = M.makeMemberExpression(FlagDecl, FlagFieldDecl);
George Karpenkov657a5892017-09-30 00:03:22 +0000434 assert(Deref->isLValue());
435 QualType DerefType = Deref->getType();
436
437 // Negation predicate.
438 UnaryOperator *FlagCheck = new (C) UnaryOperator(
George Karpenkova1329382017-10-25 00:03:45 +0000439 /* input=*/
George Karpenkov657a5892017-09-30 00:03:22 +0000440 M.makeImplicitCast(M.makeLvalueToRvalue(Deref, DerefType), DerefType,
441 CK_IntegralToBoolean),
George Karpenkova1329382017-10-25 00:03:45 +0000442 /* opc=*/ UO_LNot,
443 /* QualType=*/ C.IntTy,
444 /* ExprValueKind=*/ VK_RValue,
Aaron Ballmana5038552018-01-09 13:07:03 +0000445 /* ExprObjectKind=*/ OK_Ordinary, SourceLocation(),
446 /* CanOverflow*/ false);
George Karpenkov657a5892017-09-30 00:03:22 +0000447
448 // Create assignment.
449 BinaryOperator *FlagAssignment = M.makeAssignment(
Devin Coughlin046833e2017-11-06 22:12:19 +0000450 Deref, M.makeIntegralCast(M.makeIntegerLiteral(1, C.IntTy), DerefType),
451 DerefType);
George Karpenkov657a5892017-09-30 00:03:22 +0000452
453 IfStmt *Out = new (C)
454 IfStmt(C, SourceLocation(),
George Karpenkova1329382017-10-25 00:03:45 +0000455 /* IsConstexpr=*/ false,
456 /* init=*/ nullptr,
457 /* var=*/ nullptr,
458 /* cond=*/ FlagCheck,
459 /* then=*/ M.makeCompound({CallbackCall, FlagAssignment}));
George Karpenkov657a5892017-09-30 00:03:22 +0000460
461 return Out;
462}
463
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000464/// Create a fake body for dispatch_once.
465static Stmt *create_dispatch_once(ASTContext &C, const FunctionDecl *D) {
466 // Check if we have at least two parameters.
467 if (D->param_size() != 2)
Craig Topper25542942014-05-20 04:30:07 +0000468 return nullptr;
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000469
470 // Check if the first parameter is a pointer to integer type.
471 const ParmVarDecl *Predicate = D->getParamDecl(0);
472 QualType PredicateQPtrTy = Predicate->getType();
473 const PointerType *PredicatePtrTy = PredicateQPtrTy->getAs<PointerType>();
474 if (!PredicatePtrTy)
Craig Topper25542942014-05-20 04:30:07 +0000475 return nullptr;
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000476 QualType PredicateTy = PredicatePtrTy->getPointeeType();
477 if (!PredicateTy->isIntegerType())
Craig Topper25542942014-05-20 04:30:07 +0000478 return nullptr;
479
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000480 // Check if the second parameter is the proper block type.
481 const ParmVarDecl *Block = D->getParamDecl(1);
482 QualType Ty = Block->getType();
483 if (!isDispatchBlock(Ty))
Craig Topper25542942014-05-20 04:30:07 +0000484 return nullptr;
485
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000486 // Everything checks out. Create a fakse body that checks the predicate,
487 // sets it, and calls the block. Basically, an AST dump of:
488 //
489 // void dispatch_once(dispatch_once_t *predicate, dispatch_block_t block) {
Devin Coughlin046833e2017-11-06 22:12:19 +0000490 // if (*predicate != ~0l) {
491 // *predicate = ~0l;
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000492 // block();
493 // }
494 // }
495
Ted Kremenek72418132012-09-21 17:54:35 +0000496 ASTMaker M(C);
497
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000498 // (1) Create the call.
George Karpenkov657a5892017-09-30 00:03:22 +0000499 CallExpr *CE = new (C) CallExpr(
500 /*ASTContext=*/C,
501 /*StmtClass=*/M.makeLvalueToRvalue(/*Expr=*/Block),
502 /*args=*/None,
503 /*QualType=*/C.VoidTy,
504 /*ExprValueType=*/VK_RValue,
505 /*SourceLocation=*/SourceLocation());
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000506
507 // (2) Create the assignment to the predicate.
Devin Coughlin046833e2017-11-06 22:12:19 +0000508 Expr *DoneValue =
509 new (C) UnaryOperator(M.makeIntegerLiteral(0, C.LongTy), UO_Not, C.LongTy,
Aaron Ballmana5038552018-01-09 13:07:03 +0000510 VK_RValue, OK_Ordinary, SourceLocation(),
511 /*CanOverflow*/false);
George Karpenkov657a5892017-09-30 00:03:22 +0000512
Ted Kremeneke7ad5352012-09-21 18:33:56 +0000513 BinaryOperator *B =
514 M.makeAssignment(
515 M.makeDereference(
516 M.makeLvalueToRvalue(
517 M.makeDeclRefExpr(Predicate), PredicateQPtrTy),
518 PredicateTy),
Devin Coughlin046833e2017-11-06 22:12:19 +0000519 M.makeIntegralCast(DoneValue, PredicateTy),
Ted Kremeneke7ad5352012-09-21 18:33:56 +0000520 PredicateTy);
521
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000522 // (3) Create the compound statement.
Craig Topper5fc8fc22014-08-27 06:28:36 +0000523 Stmt *Stmts[] = { B, CE };
524 CompoundStmt *CS = M.makeCompound(Stmts);
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000525
526 // (4) Create the 'if' condition.
Ted Kremeneke7ad5352012-09-21 18:33:56 +0000527 ImplicitCastExpr *LValToRval =
528 M.makeLvalueToRvalue(
529 M.makeDereference(
530 M.makeLvalueToRvalue(
531 M.makeDeclRefExpr(Predicate),
532 PredicateQPtrTy),
533 PredicateTy),
534 PredicateTy);
Devin Coughlin046833e2017-11-06 22:12:19 +0000535
536 Expr *GuardCondition = M.makeComparison(LValToRval, DoneValue, BO_NE);
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000537 // (5) Create the 'if' statement.
George Karpenkov657a5892017-09-30 00:03:22 +0000538 IfStmt *If = new (C) IfStmt(C, SourceLocation(),
George Karpenkova1329382017-10-25 00:03:45 +0000539 /* IsConstexpr=*/ false,
540 /* init=*/ nullptr,
541 /* var=*/ nullptr,
Devin Coughlin046833e2017-11-06 22:12:19 +0000542 /* cond=*/ GuardCondition,
George Karpenkova1329382017-10-25 00:03:45 +0000543 /* then=*/ CS);
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000544 return If;
545}
546
Ted Kremenek14f779c2012-09-21 00:09:11 +0000547/// Create a fake body for dispatch_sync.
548static Stmt *create_dispatch_sync(ASTContext &C, const FunctionDecl *D) {
549 // Check if we have at least two parameters.
550 if (D->param_size() != 2)
Craig Topper25542942014-05-20 04:30:07 +0000551 return nullptr;
552
Ted Kremenek14f779c2012-09-21 00:09:11 +0000553 // Check if the second parameter is a block.
554 const ParmVarDecl *PV = D->getParamDecl(1);
555 QualType Ty = PV->getType();
Ted Kremenekd81a4a12012-09-21 00:52:24 +0000556 if (!isDispatchBlock(Ty))
Craig Topper25542942014-05-20 04:30:07 +0000557 return nullptr;
558
Ted Kremenek14f779c2012-09-21 00:09:11 +0000559 // Everything checks out. Create a fake body that just calls the block.
560 // This is basically just an AST dump of:
561 //
562 // void dispatch_sync(dispatch_queue_t queue, void (^block)(void)) {
563 // block();
564 // }
Ted Kremenek72418132012-09-21 17:54:35 +0000565 //
566 ASTMaker M(C);
567 DeclRefExpr *DR = M.makeDeclRefExpr(PV);
Ted Kremenekdff35532012-09-21 18:33:52 +0000568 ImplicitCastExpr *ICE = M.makeLvalueToRvalue(DR, Ty);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000569 CallExpr *CE = new (C) CallExpr(C, ICE, None, C.VoidTy, VK_RValue,
570 SourceLocation());
Ted Kremenek14f779c2012-09-21 00:09:11 +0000571 return CE;
572}
573
Ted Kremenek089ffd02012-10-11 20:58:18 +0000574static Stmt *create_OSAtomicCompareAndSwap(ASTContext &C, const FunctionDecl *D)
575{
576 // There are exactly 3 arguments.
577 if (D->param_size() != 3)
Craig Topper25542942014-05-20 04:30:07 +0000578 return nullptr;
579
Anna Zaks064185a2013-02-05 19:52:26 +0000580 // Signature:
581 // _Bool OSAtomicCompareAndSwapPtr(void *__oldValue,
582 // void *__newValue,
583 // void * volatile *__theValue)
584 // Generate body:
Ted Kremenek089ffd02012-10-11 20:58:18 +0000585 // if (oldValue == *theValue) {
586 // *theValue = newValue;
587 // return YES;
588 // }
589 // else return NO;
Alp Toker314cc812014-01-25 16:55:45 +0000590
591 QualType ResultTy = D->getReturnType();
Ted Kremenek6fdefb52012-10-12 00:18:19 +0000592 bool isBoolean = ResultTy->isBooleanType();
593 if (!isBoolean && !ResultTy->isIntegralType(C))
Craig Topper25542942014-05-20 04:30:07 +0000594 return nullptr;
595
Ted Kremenek089ffd02012-10-11 20:58:18 +0000596 const ParmVarDecl *OldValue = D->getParamDecl(0);
597 QualType OldValueTy = OldValue->getType();
598
599 const ParmVarDecl *NewValue = D->getParamDecl(1);
600 QualType NewValueTy = NewValue->getType();
601
602 assert(OldValueTy == NewValueTy);
603
604 const ParmVarDecl *TheValue = D->getParamDecl(2);
605 QualType TheValueTy = TheValue->getType();
606 const PointerType *PT = TheValueTy->getAs<PointerType>();
607 if (!PT)
Craig Topper25542942014-05-20 04:30:07 +0000608 return nullptr;
Ted Kremenek089ffd02012-10-11 20:58:18 +0000609 QualType PointeeTy = PT->getPointeeType();
610
611 ASTMaker M(C);
612 // Construct the comparison.
613 Expr *Comparison =
614 M.makeComparison(
615 M.makeLvalueToRvalue(M.makeDeclRefExpr(OldValue), OldValueTy),
616 M.makeLvalueToRvalue(
617 M.makeDereference(
618 M.makeLvalueToRvalue(M.makeDeclRefExpr(TheValue), TheValueTy),
619 PointeeTy),
620 PointeeTy),
621 BO_EQ);
622
623 // Construct the body of the IfStmt.
624 Stmt *Stmts[2];
625 Stmts[0] =
626 M.makeAssignment(
627 M.makeDereference(
628 M.makeLvalueToRvalue(M.makeDeclRefExpr(TheValue), TheValueTy),
629 PointeeTy),
630 M.makeLvalueToRvalue(M.makeDeclRefExpr(NewValue), NewValueTy),
631 NewValueTy);
Ted Kremenek6fdefb52012-10-12 00:18:19 +0000632
633 Expr *BoolVal = M.makeObjCBool(true);
634 Expr *RetVal = isBoolean ? M.makeIntegralCastToBoolean(BoolVal)
635 : M.makeIntegralCast(BoolVal, ResultTy);
636 Stmts[1] = M.makeReturn(RetVal);
Craig Topper5fc8fc22014-08-27 06:28:36 +0000637 CompoundStmt *Body = M.makeCompound(Stmts);
Ted Kremenek089ffd02012-10-11 20:58:18 +0000638
639 // Construct the else clause.
Ted Kremenek6fdefb52012-10-12 00:18:19 +0000640 BoolVal = M.makeObjCBool(false);
641 RetVal = isBoolean ? M.makeIntegralCastToBoolean(BoolVal)
642 : M.makeIntegralCast(BoolVal, ResultTy);
643 Stmt *Else = M.makeReturn(RetVal);
Ted Kremenek089ffd02012-10-11 20:58:18 +0000644
645 /// Construct the If.
Richard Smitha547eb22016-07-14 00:11:03 +0000646 Stmt *If = new (C) IfStmt(C, SourceLocation(), false, nullptr, nullptr,
647 Comparison, Body, SourceLocation(), Else);
Craig Topper25542942014-05-20 04:30:07 +0000648
Ted Kremenek089ffd02012-10-11 20:58:18 +0000649 return If;
650}
651
Ted Kremenek14f779c2012-09-21 00:09:11 +0000652Stmt *BodyFarm::getBody(const FunctionDecl *D) {
653 D = D->getCanonicalDecl();
654
David Blaikie05785d12013-02-20 22:23:23 +0000655 Optional<Stmt *> &Val = Bodies[D];
Ted Kremenek14f779c2012-09-21 00:09:11 +0000656 if (Val.hasValue())
657 return Val.getValue();
Craig Topper25542942014-05-20 04:30:07 +0000658
659 Val = nullptr;
660
661 if (D->getIdentifier() == nullptr)
662 return nullptr;
Ted Kremenek14f779c2012-09-21 00:09:11 +0000663
664 StringRef Name = D->getName();
665 if (Name.empty())
Craig Topper25542942014-05-20 04:30:07 +0000666 return nullptr;
Ted Kremenek089ffd02012-10-11 20:58:18 +0000667
668 FunctionFarmer FF;
669
670 if (Name.startswith("OSAtomicCompareAndSwap") ||
671 Name.startswith("objc_atomicCompareAndSwap")) {
672 FF = create_OSAtomicCompareAndSwap;
George Karpenkov657a5892017-09-30 00:03:22 +0000673 } else if (Name == "call_once" && D->getDeclContext()->isStdNamespace()) {
674 FF = create_call_once;
675 } else {
Ted Kremenek089ffd02012-10-11 20:58:18 +0000676 FF = llvm::StringSwitch<FunctionFarmer>(Name)
677 .Case("dispatch_sync", create_dispatch_sync)
678 .Case("dispatch_once", create_dispatch_once)
Craig Topper25542942014-05-20 04:30:07 +0000679 .Default(nullptr);
Ted Kremenek14f779c2012-09-21 00:09:11 +0000680 }
681
Ted Kremenek089ffd02012-10-11 20:58:18 +0000682 if (FF) { Val = FF(C, D); }
Ted Kremenekeeccb302014-08-27 15:14:15 +0000683 else if (Injector) { Val = Injector->getBody(D); }
Ted Kremenek14f779c2012-09-21 00:09:11 +0000684 return Val.getValue();
685}
686
Devin Coughlinb7e810b2016-01-26 23:58:48 +0000687static const ObjCIvarDecl *findBackingIvar(const ObjCPropertyDecl *Prop) {
688 const ObjCIvarDecl *IVar = Prop->getPropertyIvarDecl();
689
690 if (IVar)
691 return IVar;
692
693 // When a readonly property is shadowed in a class extensions with a
694 // a readwrite property, the instance variable belongs to the shadowing
695 // property rather than the shadowed property. If there is no instance
696 // variable on a readonly property, check to see whether the property is
697 // shadowed and if so try to get the instance variable from shadowing
698 // property.
699 if (!Prop->isReadOnly())
700 return nullptr;
701
702 auto *Container = cast<ObjCContainerDecl>(Prop->getDeclContext());
703 const ObjCInterfaceDecl *PrimaryInterface = nullptr;
704 if (auto *InterfaceDecl = dyn_cast<ObjCInterfaceDecl>(Container)) {
705 PrimaryInterface = InterfaceDecl;
706 } else if (auto *CategoryDecl = dyn_cast<ObjCCategoryDecl>(Container)) {
707 PrimaryInterface = CategoryDecl->getClassInterface();
708 } else if (auto *ImplDecl = dyn_cast<ObjCImplDecl>(Container)) {
709 PrimaryInterface = ImplDecl->getClassInterface();
710 } else {
711 return nullptr;
712 }
713
714 // FindPropertyVisibleInPrimaryClass() looks first in class extensions, so it
715 // is guaranteed to find the shadowing property, if it exists, rather than
716 // the shadowed property.
717 auto *ShadowingProp = PrimaryInterface->FindPropertyVisibleInPrimaryClass(
Manman Ren5b786402016-01-28 18:49:28 +0000718 Prop->getIdentifier(), Prop->getQueryKind());
Devin Coughlinb7e810b2016-01-26 23:58:48 +0000719 if (ShadowingProp && ShadowingProp != Prop) {
720 IVar = ShadowingProp->getPropertyIvarDecl();
721 }
722
723 return IVar;
724}
725
Jordan Rose1a866cd2014-01-10 20:06:06 +0000726static Stmt *createObjCPropertyGetter(ASTContext &Ctx,
727 const ObjCPropertyDecl *Prop) {
Jordan Roseddf19662014-01-23 03:59:10 +0000728 // First, find the backing ivar.
Devin Coughlinb7e810b2016-01-26 23:58:48 +0000729 const ObjCIvarDecl *IVar = findBackingIvar(Prop);
Jordan Rose1a866cd2014-01-10 20:06:06 +0000730 if (!IVar)
Craig Topper25542942014-05-20 04:30:07 +0000731 return nullptr;
Jordan Roseddf19662014-01-23 03:59:10 +0000732
733 // Ignore weak variables, which have special behavior.
Jordan Rose1a866cd2014-01-10 20:06:06 +0000734 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak)
Craig Topper25542942014-05-20 04:30:07 +0000735 return nullptr;
Jordan Rosea3f27812014-01-14 17:29:06 +0000736
Jordan Roseddf19662014-01-23 03:59:10 +0000737 // Look to see if Sema has synthesized a body for us. This happens in
738 // Objective-C++ because the return value may be a C++ class type with a
739 // non-trivial copy constructor. We can only do this if we can find the
740 // @synthesize for this property, though (or if we know it's been auto-
741 // synthesized).
Jordan Rosea3f27812014-01-14 17:29:06 +0000742 const ObjCImplementationDecl *ImplDecl =
743 IVar->getContainingInterface()->getImplementation();
744 if (ImplDecl) {
Aaron Ballmand85eff42014-03-14 15:02:45 +0000745 for (const auto *I : ImplDecl->property_impls()) {
Jordan Rosea3f27812014-01-14 17:29:06 +0000746 if (I->getPropertyDecl() != Prop)
747 continue;
748
749 if (I->getGetterCXXConstructor()) {
750 ASTMaker M(Ctx);
751 return M.makeReturn(I->getGetterCXXConstructor());
752 }
753 }
754 }
755
Jordan Roseddf19662014-01-23 03:59:10 +0000756 // Sanity check that the property is the same type as the ivar, or a
757 // reference to it, and that it is either an object pointer or trivially
758 // copyable.
759 if (!Ctx.hasSameUnqualifiedType(IVar->getType(),
760 Prop->getType().getNonReferenceType()))
Craig Topper25542942014-05-20 04:30:07 +0000761 return nullptr;
Jordan Roseddf19662014-01-23 03:59:10 +0000762 if (!IVar->getType()->isObjCLifetimeType() &&
763 !IVar->getType().isTriviallyCopyableType(Ctx))
Craig Topper25542942014-05-20 04:30:07 +0000764 return nullptr;
Jordan Rose1a866cd2014-01-10 20:06:06 +0000765
Jordan Roseddf19662014-01-23 03:59:10 +0000766 // Generate our body:
767 // return self->_ivar;
Jordan Rose1a866cd2014-01-10 20:06:06 +0000768 ASTMaker M(Ctx);
769
770 const VarDecl *selfVar = Prop->getGetterMethodDecl()->getSelfDecl();
Devin Coughlinaac894f2017-01-11 01:02:34 +0000771 if (!selfVar)
772 return nullptr;
Jordan Rose1a866cd2014-01-10 20:06:06 +0000773
774 Expr *loadedIVar =
775 M.makeObjCIvarRef(
776 M.makeLvalueToRvalue(
777 M.makeDeclRefExpr(selfVar),
778 selfVar->getType()),
779 IVar);
780
781 if (!Prop->getType()->isReferenceType())
782 loadedIVar = M.makeLvalueToRvalue(loadedIVar, IVar->getType());
783
784 return M.makeReturn(loadedIVar);
785}
786
Jordan Roseddf19662014-01-23 03:59:10 +0000787Stmt *BodyFarm::getBody(const ObjCMethodDecl *D) {
788 // We currently only know how to synthesize property accessors.
Jordan Rose1a866cd2014-01-10 20:06:06 +0000789 if (!D->isPropertyAccessor())
Craig Topper25542942014-05-20 04:30:07 +0000790 return nullptr;
Jordan Rose1a866cd2014-01-10 20:06:06 +0000791
792 D = D->getCanonicalDecl();
793
794 Optional<Stmt *> &Val = Bodies[D];
795 if (Val.hasValue())
796 return Val.getValue();
Craig Topper25542942014-05-20 04:30:07 +0000797 Val = nullptr;
Jordan Rose1a866cd2014-01-10 20:06:06 +0000798
Jordan Roseddf19662014-01-23 03:59:10 +0000799 const ObjCPropertyDecl *Prop = D->findPropertyDecl();
Jordan Rose1a866cd2014-01-10 20:06:06 +0000800 if (!Prop)
Craig Topper25542942014-05-20 04:30:07 +0000801 return nullptr;
Jordan Rose1a866cd2014-01-10 20:06:06 +0000802
Jordan Roseddf19662014-01-23 03:59:10 +0000803 // For now, we only synthesize getters.
Devin Coughlinef3697e2016-02-18 19:37:39 +0000804 // Synthesizing setters would cause false negatives in the
805 // RetainCountChecker because the method body would bind the parameter
806 // to an instance variable, causing it to escape. This would prevent
807 // warning in the following common scenario:
808 //
809 // id foo = [[NSObject alloc] init];
810 // self.foo = foo; // We should warn that foo leaks here.
811 //
Jordan Rose1a866cd2014-01-10 20:06:06 +0000812 if (D->param_size() != 0)
Craig Topper25542942014-05-20 04:30:07 +0000813 return nullptr;
Jordan Rose1a866cd2014-01-10 20:06:06 +0000814
815 Val = createObjCPropertyGetter(C, Prop);
816
817 return Val.getValue();
818}
819