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