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 | |
George Karpenkov | 3d64d6e | 2017-10-23 23:59:52 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/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, |
George Karpenkov | b2a60c6 | 2017-10-17 22:28:18 +0000 | [diff] [blame] | 66 | bool RefersToEnclosingVariableOrCapture = false); |
Ted Kremenek | 7241813 | 2012-09-21 17:54:35 +0000 | [diff] [blame] | 67 | |
Ted Kremenek | dff3553 | 2012-09-21 18:33:52 +0000 | [diff] [blame] | 68 | /// Create a new UnaryOperator representing a dereference. |
| 69 | UnaryOperator *makeDereference(const Expr *Arg, QualType Ty); |
| 70 | |
Ted Kremenek | 69bcb82 | 2012-09-21 18:13:23 +0000 | [diff] [blame] | 71 | /// Create an implicit cast for an integer conversion. |
Ted Kremenek | 6fdefb5 | 2012-10-12 00:18:19 +0000 | [diff] [blame] | 72 | Expr *makeIntegralCast(const Expr *Arg, QualType Ty); |
Ted Kremenek | 69bcb82 | 2012-09-21 18:13:23 +0000 | [diff] [blame] | 73 | |
Ted Kremenek | 089ffd0 | 2012-10-11 20:58:18 +0000 | [diff] [blame] | 74 | /// Create an implicit cast to a builtin boolean type. |
| 75 | ImplicitCastExpr *makeIntegralCastToBoolean(const Expr *Arg); |
| 76 | |
George Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 77 | /// Create an implicit cast for lvalue-to-rvaluate conversions. |
Ted Kremenek | ca90ea5 | 2012-09-21 18:13:27 +0000 | [diff] [blame] | 78 | ImplicitCastExpr *makeLvalueToRvalue(const Expr *Arg, QualType Ty); |
| 79 | |
George Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 80 | /// Make RValue out of variable declaration, creating a temporary |
| 81 | /// DeclRefExpr in the process. |
| 82 | ImplicitCastExpr * |
| 83 | makeLvalueToRvalue(const VarDecl *Decl, |
George Karpenkov | b2a60c6 | 2017-10-17 22:28:18 +0000 | [diff] [blame] | 84 | bool RefersToEnclosingVariableOrCapture = false); |
George Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 85 | |
| 86 | /// Create an implicit cast of the given type. |
| 87 | ImplicitCastExpr *makeImplicitCast(const Expr *Arg, QualType Ty, |
| 88 | CastKind CK = CK_LValueToRValue); |
| 89 | |
Ted Kremenek | 089ffd0 | 2012-10-11 20:58:18 +0000 | [diff] [blame] | 90 | /// Create an Objective-C bool literal. |
| 91 | ObjCBoolLiteralExpr *makeObjCBool(bool Val); |
Jordan Rose | 1a866cd | 2014-01-10 20:06:06 +0000 | [diff] [blame] | 92 | |
| 93 | /// Create an Objective-C ivar reference. |
| 94 | ObjCIvarRefExpr *makeObjCIvarRef(const Expr *Base, const ObjCIvarDecl *IVar); |
Ted Kremenek | 089ffd0 | 2012-10-11 20:58:18 +0000 | [diff] [blame] | 95 | |
| 96 | /// Create a Return statement. |
| 97 | ReturnStmt *makeReturn(const Expr *RetVal); |
| 98 | |
Devin Coughlin | 046833e | 2017-11-06 22:12:19 +0000 | [diff] [blame] | 99 | /// Create an integer literal expression of the given type. |
| 100 | IntegerLiteral *makeIntegerLiteral(uint64_t Value, QualType Ty); |
George Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 101 | |
| 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 Karpenkov | c928e1f | 2017-10-11 20:53:01 +0000 | [diff] [blame] | 109 | ValueDecl *findMemberField(const RecordDecl *RD, StringRef Name); |
George Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 110 | |
Ted Kremenek | 7241813 | 2012-09-21 17:54:35 +0000 | [diff] [blame] | 111 | private: |
| 112 | ASTContext &C; |
| 113 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 114 | } |
Ted Kremenek | 7241813 | 2012-09-21 17:54:35 +0000 | [diff] [blame] | 115 | |
Ted Kremenek | f465dc1 | 2012-09-21 18:33:54 +0000 | [diff] [blame] | 116 | BinaryOperator *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 Nemet | 484aa45 | 2017-03-27 19:17:25 +0000 | [diff] [blame] | 120 | OK_Ordinary, SourceLocation(), FPOptions()); |
Ted Kremenek | f465dc1 | 2012-09-21 18:33:54 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Ted Kremenek | 089ffd0 | 2012-10-11 20:58:18 +0000 | [diff] [blame] | 123 | BinaryOperator *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 Nemet | 484aa45 | 2017-03-27 19:17:25 +0000 | [diff] [blame] | 132 | OK_Ordinary, SourceLocation(), FPOptions()); |
Ted Kremenek | 089ffd0 | 2012-10-11 20:58:18 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | CompoundStmt *ASTMaker::makeCompound(ArrayRef<Stmt *> Stmts) { |
Benjamin Kramer | 0742090 | 2017-12-24 16:24:20 +0000 | [diff] [blame] | 136 | return CompoundStmt::Create(C, Stmts, SourceLocation(), SourceLocation()); |
Ted Kremenek | 089ffd0 | 2012-10-11 20:58:18 +0000 | [diff] [blame] | 137 | } |
| 138 | |
George Karpenkov | b2a60c6 | 2017-10-17 22:28:18 +0000 | [diff] [blame] | 139 | DeclRefExpr *ASTMaker::makeDeclRefExpr( |
| 140 | const VarDecl *D, |
| 141 | bool RefersToEnclosingVariableOrCapture) { |
| 142 | QualType Type = D->getType().getNonReferenceType(); |
George Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 143 | |
| 144 | DeclRefExpr *DR = DeclRefExpr::Create( |
| 145 | C, NestedNameSpecifierLoc(), SourceLocation(), const_cast<VarDecl *>(D), |
| 146 | RefersToEnclosingVariableOrCapture, SourceLocation(), Type, VK_LValue); |
Ted Kremenek | 7241813 | 2012-09-21 17:54:35 +0000 | [diff] [blame] | 147 | return DR; |
| 148 | } |
| 149 | |
Ted Kremenek | dff3553 | 2012-09-21 18:33:52 +0000 | [diff] [blame] | 150 | UnaryOperator *ASTMaker::makeDereference(const Expr *Arg, QualType Ty) { |
| 151 | return new (C) UnaryOperator(const_cast<Expr*>(Arg), UO_Deref, Ty, |
Aaron Ballman | a503855 | 2018-01-09 13:07:03 +0000 | [diff] [blame] | 152 | VK_LValue, OK_Ordinary, SourceLocation(), |
| 153 | /*CanOverflow*/ false); |
Ted Kremenek | dff3553 | 2012-09-21 18:33:52 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Ted Kremenek | ca90ea5 | 2012-09-21 18:13:27 +0000 | [diff] [blame] | 156 | ImplicitCastExpr *ASTMaker::makeLvalueToRvalue(const Expr *Arg, QualType Ty) { |
George Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 157 | return makeImplicitCast(Arg, Ty, CK_LValueToRValue); |
| 158 | } |
| 159 | |
George Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 160 | ImplicitCastExpr * |
| 161 | ASTMaker::makeLvalueToRvalue(const VarDecl *Arg, |
George Karpenkov | b2a60c6 | 2017-10-17 22:28:18 +0000 | [diff] [blame] | 162 | bool RefersToEnclosingVariableOrCapture) { |
| 163 | QualType Type = Arg->getType().getNonReferenceType(); |
George Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 164 | return makeLvalueToRvalue(makeDeclRefExpr(Arg, |
George Karpenkov | b2a60c6 | 2017-10-17 22:28:18 +0000 | [diff] [blame] | 165 | RefersToEnclosingVariableOrCapture), |
George Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 166 | Type); |
| 167 | } |
| 168 | |
| 169 | ImplicitCastExpr *ASTMaker::makeImplicitCast(const Expr *Arg, QualType Ty, |
| 170 | CastKind CK) { |
| 171 | return ImplicitCastExpr::Create(C, Ty, |
George Karpenkov | a132938 | 2017-10-25 00:03:45 +0000 | [diff] [blame] | 172 | /* CastKind=*/ CK, |
| 173 | /* Expr=*/ const_cast<Expr *>(Arg), |
| 174 | /* CXXCastPath=*/ nullptr, |
| 175 | /* ExprValueKind=*/ VK_RValue); |
Ted Kremenek | ca90ea5 | 2012-09-21 18:13:27 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Ted Kremenek | 6fdefb5 | 2012-10-12 00:18:19 +0000 | [diff] [blame] | 178 | Expr *ASTMaker::makeIntegralCast(const Expr *Arg, QualType Ty) { |
| 179 | if (Arg->getType() == Ty) |
| 180 | return const_cast<Expr*>(Arg); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 181 | |
Ted Kremenek | 69bcb82 | 2012-09-21 18:13:23 +0000 | [diff] [blame] | 182 | return ImplicitCastExpr::Create(C, Ty, CK_IntegralCast, |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 183 | const_cast<Expr*>(Arg), nullptr, VK_RValue); |
Ted Kremenek | 69bcb82 | 2012-09-21 18:13:23 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Ted Kremenek | 089ffd0 | 2012-10-11 20:58:18 +0000 | [diff] [blame] | 186 | ImplicitCastExpr *ASTMaker::makeIntegralCastToBoolean(const Expr *Arg) { |
| 187 | return ImplicitCastExpr::Create(C, C.BoolTy, CK_IntegralToBoolean, |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 188 | const_cast<Expr*>(Arg), nullptr, VK_RValue); |
Ted Kremenek | 089ffd0 | 2012-10-11 20:58:18 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | ObjCBoolLiteralExpr *ASTMaker::makeObjCBool(bool Val) { |
| 192 | QualType Ty = C.getBOOLDecl() ? C.getBOOLType() : C.ObjCBuiltinBoolTy; |
| 193 | return new (C) ObjCBoolLiteralExpr(Val, Ty, SourceLocation()); |
| 194 | } |
| 195 | |
Jordan Rose | 1a866cd | 2014-01-10 20:06:06 +0000 | [diff] [blame] | 196 | ObjCIvarRefExpr *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 Kremenek | 089ffd0 | 2012-10-11 20:58:18 +0000 | [diff] [blame] | 205 | ReturnStmt *ASTMaker::makeReturn(const Expr *RetVal) { |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 206 | return new (C) ReturnStmt(SourceLocation(), const_cast<Expr*>(RetVal), |
| 207 | nullptr); |
Ted Kremenek | 089ffd0 | 2012-10-11 20:58:18 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Devin Coughlin | 046833e | 2017-11-06 22:12:19 +0000 | [diff] [blame] | 210 | IntegerLiteral *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 Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | MemberExpr *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 Karpenkov | a132938 | 2017-10-25 00:03:45 +0000 | [diff] [blame] | 224 | /* TemplateArgumentListInfo=*/ nullptr, MemberDecl->getType(), ValueKind, |
George Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 225 | OK_Ordinary); |
| 226 | } |
| 227 | |
George Karpenkov | c928e1f | 2017-10-11 20:53:01 +0000 | [diff] [blame] | 228 | ValueDecl *ASTMaker::findMemberField(const RecordDecl *RD, StringRef Name) { |
George Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 229 | |
| 230 | CXXBasePaths Paths( |
| 231 | /* FindAmbiguities=*/false, |
| 232 | /* RecordPaths=*/false, |
George Karpenkov | a132938 | 2017-10-25 00:03:45 +0000 | [diff] [blame] | 233 | /* DetectVirtual=*/ false); |
George Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 234 | 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 Karpenkov | c928e1f | 2017-10-11 20:53:01 +0000 | [diff] [blame] | 240 | return cast<ValueDecl>(FoundDecl); |
George Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 241 | |
| 242 | return nullptr; |
| 243 | } |
| 244 | |
Ted Kremenek | 2b5c83c | 2012-09-21 17:54:32 +0000 | [diff] [blame] | 245 | //===----------------------------------------------------------------------===// |
| 246 | // Creation functions for faux ASTs. |
| 247 | //===----------------------------------------------------------------------===// |
| 248 | |
| 249 | typedef Stmt *(*FunctionFarmer)(ASTContext &C, const FunctionDecl *D); |
| 250 | |
George Karpenkov | 6dda671 | 2017-10-02 21:01:46 +0000 | [diff] [blame] | 251 | static CallExpr *create_call_once_funcptr_call(ASTContext &C, ASTMaker M, |
| 252 | const ParmVarDecl *Callback, |
| 253 | ArrayRef<Expr *> CallArgs) { |
George Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 254 | |
George Karpenkov | 98e81cd | 2017-10-24 00:13:18 +0000 | [diff] [blame] | 255 | 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 Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 272 | } |
| 273 | |
George Karpenkov | 6dda671 | 2017-10-02 21:01:46 +0000 | [diff] [blame] | 274 | static CallExpr *create_call_once_lambda_call(ASTContext &C, ASTMaker M, |
| 275 | const ParmVarDecl *Callback, |
George Karpenkov | bd4254c | 2017-10-20 23:29:59 +0000 | [diff] [blame] | 276 | CXXRecordDecl *CallbackDecl, |
George Karpenkov | 6dda671 | 2017-10-02 21:01:46 +0000 | [diff] [blame] | 277 | ArrayRef<Expr *> CallArgs) { |
George Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 278 | assert(CallbackDecl != nullptr); |
| 279 | assert(CallbackDecl->isLambda()); |
| 280 | FunctionDecl *callOperatorDecl = CallbackDecl->getLambdaCallOperator(); |
| 281 | assert(callOperatorDecl != nullptr); |
| 282 | |
| 283 | DeclRefExpr *callOperatorDeclRef = |
George Karpenkov | a132938 | 2017-10-25 00:03:45 +0000 | [diff] [blame] | 284 | DeclRefExpr::Create(/* Ctx =*/ C, |
| 285 | /* QualifierLoc =*/ NestedNameSpecifierLoc(), |
| 286 | /* TemplateKWLoc =*/ SourceLocation(), |
George Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 287 | const_cast<FunctionDecl *>(callOperatorDecl), |
George Karpenkov | a132938 | 2017-10-25 00:03:45 +0000 | [diff] [blame] | 288 | /* RefersToEnclosingVariableOrCapture=*/ false, |
| 289 | /* NameLoc =*/ SourceLocation(), |
| 290 | /* T =*/ callOperatorDecl->getType(), |
| 291 | /* VK =*/ VK_LValue); |
George Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 292 | |
George Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 293 | 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 |
| 316 | static 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 Karpenkov | 0354483 | 2017-11-03 00:36:03 +0000 | [diff] [blame] | 327 | |
| 328 | if (!Callback->getType()->isReferenceType()) { |
| 329 | llvm::dbgs() << "libcxx03 std::call_once implementation, skipping.\n"; |
| 330 | return nullptr; |
| 331 | } |
| 332 | if (!Flag->getType()->isReferenceType()) { |
| 333 | llvm::dbgs() << "unknown std::call_once implementation, skipping.\n"; |
| 334 | return nullptr; |
| 335 | } |
| 336 | |
George Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 337 | QualType CallbackType = Callback->getType().getNonReferenceType(); |
George Karpenkov | bd4254c | 2017-10-20 23:29:59 +0000 | [diff] [blame] | 338 | |
| 339 | // Nullable pointer, non-null iff function is a CXXRecordDecl. |
| 340 | CXXRecordDecl *CallbackRecordDecl = CallbackType->getAsCXXRecordDecl(); |
George Karpenkov | 8b53f7c | 2017-10-09 23:20:46 +0000 | [diff] [blame] | 341 | QualType FlagType = Flag->getType().getNonReferenceType(); |
George Karpenkov | c928e1f | 2017-10-11 20:53:01 +0000 | [diff] [blame] | 342 | 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 Karpenkov | 8b53f7c | 2017-10-09 23:20:46 +0000 | [diff] [blame] | 348 | return nullptr; |
| 349 | } |
| 350 | |
George Karpenkov | c928e1f | 2017-10-11 20:53:01 +0000 | [diff] [blame] | 351 | // We initially assume libc++ implementation of call_once, |
| 352 | // where the once_flag struct has a field `__state_`. |
| 353 | ValueDecl *FlagFieldDecl = M.findMemberField(FlagRecordDecl, "__state_"); |
| 354 | |
| 355 | // Otherwise, try libstdc++ implementation, with a field |
| 356 | // `_M_once` |
| 357 | if (!FlagFieldDecl) { |
George Karpenkov | c928e1f | 2017-10-11 20:53:01 +0000 | [diff] [blame] | 358 | FlagFieldDecl = M.findMemberField(FlagRecordDecl, "_M_once"); |
| 359 | } |
| 360 | |
| 361 | if (!FlagFieldDecl) { |
George Karpenkov | 0354483 | 2017-11-03 00:36:03 +0000 | [diff] [blame] | 362 | 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 Karpenkov | 8b53f7c | 2017-10-09 23:20:46 +0000 | [diff] [blame] | 365 | return nullptr; |
| 366 | } |
George Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 367 | |
George Karpenkov | bd4254c | 2017-10-20 23:29:59 +0000 | [diff] [blame] | 368 | 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 Karpenkov | 6dda671 | 2017-10-02 21:01:46 +0000 | [diff] [blame] | 374 | |
George Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 375 | SmallVector<Expr *, 5> CallArgs; |
George Karpenkov | bd4254c | 2017-10-20 23:29:59 +0000 | [diff] [blame] | 376 | const FunctionProtoType *CallbackFunctionType; |
| 377 | if (isLambdaCall) { |
George Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 378 | |
George Karpenkov | 6dda671 | 2017-10-02 21:01:46 +0000 | [diff] [blame] | 379 | // Lambda requires callback itself inserted as a first parameter. |
| 380 | CallArgs.push_back( |
| 381 | M.makeDeclRefExpr(Callback, |
George Karpenkov | a132938 | 2017-10-25 00:03:45 +0000 | [diff] [blame] | 382 | /* RefersToEnclosingVariableOrCapture=*/ true)); |
George Karpenkov | bd4254c | 2017-10-20 23:29:59 +0000 | [diff] [blame] | 383 | CallbackFunctionType = CallbackRecordDecl->getLambdaCallOperator() |
| 384 | ->getType() |
| 385 | ->getAs<FunctionProtoType>(); |
George Karpenkov | 98e81cd | 2017-10-24 00:13:18 +0000 | [diff] [blame] | 386 | } else if (!CallbackType->getPointeeType().isNull()) { |
George Karpenkov | bd4254c | 2017-10-20 23:29:59 +0000 | [diff] [blame] | 387 | CallbackFunctionType = |
| 388 | CallbackType->getPointeeType()->getAs<FunctionProtoType>(); |
George Karpenkov | 98e81cd | 2017-10-24 00:13:18 +0000 | [diff] [blame] | 389 | } else { |
| 390 | CallbackFunctionType = CallbackType->getAs<FunctionProtoType>(); |
George Karpenkov | bd4254c | 2017-10-20 23:29:59 +0000 | [diff] [blame] | 391 | } |
George Karpenkov | 6dda671 | 2017-10-02 21:01:46 +0000 | [diff] [blame] | 392 | |
George Karpenkov | bd4254c | 2017-10-20 23:29:59 +0000 | [diff] [blame] | 393 | 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 Karpenkov | 0354483 | 2017-11-03 00:36:03 +0000 | [diff] [blame] | 398 | 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 Karpenkov | bd4254c | 2017-10-20 23:29:59 +0000 | [diff] [blame] | 401 | 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); |
George Karpenkov | 5920232 | 2018-02-02 01:44:07 +0000 | [diff] [blame] | 409 | if (PDecl && |
| 410 | CallbackFunctionType->getParamType(ParamIdx - 2) |
| 411 | .getNonReferenceType() |
| 412 | .getCanonicalType() != |
| 413 | PDecl->getType().getNonReferenceType().getCanonicalType()) { |
| 414 | DEBUG(llvm::dbgs() << "Types of params of the callback do not match " |
| 415 | << "params passed to std::call_once, " |
| 416 | << "ignoring the call\n"); |
| 417 | return nullptr; |
| 418 | } |
George Karpenkov | bd4254c | 2017-10-20 23:29:59 +0000 | [diff] [blame] | 419 | Expr *ParamExpr = M.makeDeclRefExpr(PDecl); |
| 420 | if (!CallbackFunctionType->getParamType(ParamIdx - 2)->isReferenceType()) { |
| 421 | QualType PTy = PDecl->getType().getNonReferenceType(); |
| 422 | ParamExpr = M.makeLvalueToRvalue(ParamExpr, PTy); |
| 423 | } |
| 424 | CallArgs.push_back(ParamExpr); |
| 425 | } |
George Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 426 | |
| 427 | CallExpr *CallbackCall; |
George Karpenkov | 6dda671 | 2017-10-02 21:01:46 +0000 | [diff] [blame] | 428 | if (isLambdaCall) { |
George Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 429 | |
George Karpenkov | bd4254c | 2017-10-20 23:29:59 +0000 | [diff] [blame] | 430 | CallbackCall = create_call_once_lambda_call(C, M, Callback, |
| 431 | CallbackRecordDecl, CallArgs); |
George Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 432 | } else { |
| 433 | |
| 434 | // Function pointer case. |
| 435 | CallbackCall = create_call_once_funcptr_call(C, M, Callback, CallArgs); |
| 436 | } |
| 437 | |
George Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 438 | DeclRefExpr *FlagDecl = |
| 439 | M.makeDeclRefExpr(Flag, |
George Karpenkov | b2a60c6 | 2017-10-17 22:28:18 +0000 | [diff] [blame] | 440 | /* RefersToEnclosingVariableOrCapture=*/true); |
George Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 441 | |
George Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 442 | |
George Karpenkov | c928e1f | 2017-10-11 20:53:01 +0000 | [diff] [blame] | 443 | MemberExpr *Deref = M.makeMemberExpression(FlagDecl, FlagFieldDecl); |
George Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 444 | assert(Deref->isLValue()); |
| 445 | QualType DerefType = Deref->getType(); |
| 446 | |
| 447 | // Negation predicate. |
| 448 | UnaryOperator *FlagCheck = new (C) UnaryOperator( |
George Karpenkov | a132938 | 2017-10-25 00:03:45 +0000 | [diff] [blame] | 449 | /* input=*/ |
George Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 450 | M.makeImplicitCast(M.makeLvalueToRvalue(Deref, DerefType), DerefType, |
| 451 | CK_IntegralToBoolean), |
George Karpenkov | a132938 | 2017-10-25 00:03:45 +0000 | [diff] [blame] | 452 | /* opc=*/ UO_LNot, |
| 453 | /* QualType=*/ C.IntTy, |
| 454 | /* ExprValueKind=*/ VK_RValue, |
Aaron Ballman | a503855 | 2018-01-09 13:07:03 +0000 | [diff] [blame] | 455 | /* ExprObjectKind=*/ OK_Ordinary, SourceLocation(), |
| 456 | /* CanOverflow*/ false); |
George Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 457 | |
| 458 | // Create assignment. |
| 459 | BinaryOperator *FlagAssignment = M.makeAssignment( |
Devin Coughlin | 046833e | 2017-11-06 22:12:19 +0000 | [diff] [blame] | 460 | Deref, M.makeIntegralCast(M.makeIntegerLiteral(1, C.IntTy), DerefType), |
| 461 | DerefType); |
George Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 462 | |
| 463 | IfStmt *Out = new (C) |
| 464 | IfStmt(C, SourceLocation(), |
George Karpenkov | a132938 | 2017-10-25 00:03:45 +0000 | [diff] [blame] | 465 | /* IsConstexpr=*/ false, |
| 466 | /* init=*/ nullptr, |
| 467 | /* var=*/ nullptr, |
| 468 | /* cond=*/ FlagCheck, |
| 469 | /* then=*/ M.makeCompound({CallbackCall, FlagAssignment})); |
George Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 470 | |
| 471 | return Out; |
| 472 | } |
| 473 | |
Ted Kremenek | d81a4a1 | 2012-09-21 00:52:24 +0000 | [diff] [blame] | 474 | /// Create a fake body for dispatch_once. |
| 475 | static Stmt *create_dispatch_once(ASTContext &C, const FunctionDecl *D) { |
| 476 | // Check if we have at least two parameters. |
| 477 | if (D->param_size() != 2) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 478 | return nullptr; |
Ted Kremenek | d81a4a1 | 2012-09-21 00:52:24 +0000 | [diff] [blame] | 479 | |
| 480 | // Check if the first parameter is a pointer to integer type. |
| 481 | const ParmVarDecl *Predicate = D->getParamDecl(0); |
| 482 | QualType PredicateQPtrTy = Predicate->getType(); |
| 483 | const PointerType *PredicatePtrTy = PredicateQPtrTy->getAs<PointerType>(); |
| 484 | if (!PredicatePtrTy) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 485 | return nullptr; |
Ted Kremenek | d81a4a1 | 2012-09-21 00:52:24 +0000 | [diff] [blame] | 486 | QualType PredicateTy = PredicatePtrTy->getPointeeType(); |
| 487 | if (!PredicateTy->isIntegerType()) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 488 | return nullptr; |
| 489 | |
Ted Kremenek | d81a4a1 | 2012-09-21 00:52:24 +0000 | [diff] [blame] | 490 | // Check if the second parameter is the proper block type. |
| 491 | const ParmVarDecl *Block = D->getParamDecl(1); |
| 492 | QualType Ty = Block->getType(); |
| 493 | if (!isDispatchBlock(Ty)) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 494 | return nullptr; |
| 495 | |
Ted Kremenek | d81a4a1 | 2012-09-21 00:52:24 +0000 | [diff] [blame] | 496 | // Everything checks out. Create a fakse body that checks the predicate, |
| 497 | // sets it, and calls the block. Basically, an AST dump of: |
| 498 | // |
| 499 | // void dispatch_once(dispatch_once_t *predicate, dispatch_block_t block) { |
Devin Coughlin | 046833e | 2017-11-06 22:12:19 +0000 | [diff] [blame] | 500 | // if (*predicate != ~0l) { |
| 501 | // *predicate = ~0l; |
Ted Kremenek | d81a4a1 | 2012-09-21 00:52:24 +0000 | [diff] [blame] | 502 | // block(); |
| 503 | // } |
| 504 | // } |
| 505 | |
Ted Kremenek | 7241813 | 2012-09-21 17:54:35 +0000 | [diff] [blame] | 506 | ASTMaker M(C); |
| 507 | |
Ted Kremenek | d81a4a1 | 2012-09-21 00:52:24 +0000 | [diff] [blame] | 508 | // (1) Create the call. |
George Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 509 | CallExpr *CE = new (C) CallExpr( |
| 510 | /*ASTContext=*/C, |
| 511 | /*StmtClass=*/M.makeLvalueToRvalue(/*Expr=*/Block), |
| 512 | /*args=*/None, |
| 513 | /*QualType=*/C.VoidTy, |
| 514 | /*ExprValueType=*/VK_RValue, |
| 515 | /*SourceLocation=*/SourceLocation()); |
Ted Kremenek | d81a4a1 | 2012-09-21 00:52:24 +0000 | [diff] [blame] | 516 | |
| 517 | // (2) Create the assignment to the predicate. |
Devin Coughlin | 046833e | 2017-11-06 22:12:19 +0000 | [diff] [blame] | 518 | Expr *DoneValue = |
| 519 | new (C) UnaryOperator(M.makeIntegerLiteral(0, C.LongTy), UO_Not, C.LongTy, |
Aaron Ballman | a503855 | 2018-01-09 13:07:03 +0000 | [diff] [blame] | 520 | VK_RValue, OK_Ordinary, SourceLocation(), |
| 521 | /*CanOverflow*/false); |
George Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 522 | |
Ted Kremenek | e7ad535 | 2012-09-21 18:33:56 +0000 | [diff] [blame] | 523 | BinaryOperator *B = |
| 524 | M.makeAssignment( |
| 525 | M.makeDereference( |
| 526 | M.makeLvalueToRvalue( |
| 527 | M.makeDeclRefExpr(Predicate), PredicateQPtrTy), |
| 528 | PredicateTy), |
Devin Coughlin | 046833e | 2017-11-06 22:12:19 +0000 | [diff] [blame] | 529 | M.makeIntegralCast(DoneValue, PredicateTy), |
Ted Kremenek | e7ad535 | 2012-09-21 18:33:56 +0000 | [diff] [blame] | 530 | PredicateTy); |
| 531 | |
Ted Kremenek | d81a4a1 | 2012-09-21 00:52:24 +0000 | [diff] [blame] | 532 | // (3) Create the compound statement. |
Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 533 | Stmt *Stmts[] = { B, CE }; |
| 534 | CompoundStmt *CS = M.makeCompound(Stmts); |
Ted Kremenek | d81a4a1 | 2012-09-21 00:52:24 +0000 | [diff] [blame] | 535 | |
| 536 | // (4) Create the 'if' condition. |
Ted Kremenek | e7ad535 | 2012-09-21 18:33:56 +0000 | [diff] [blame] | 537 | ImplicitCastExpr *LValToRval = |
| 538 | M.makeLvalueToRvalue( |
| 539 | M.makeDereference( |
| 540 | M.makeLvalueToRvalue( |
| 541 | M.makeDeclRefExpr(Predicate), |
| 542 | PredicateQPtrTy), |
| 543 | PredicateTy), |
| 544 | PredicateTy); |
Devin Coughlin | 046833e | 2017-11-06 22:12:19 +0000 | [diff] [blame] | 545 | |
| 546 | Expr *GuardCondition = M.makeComparison(LValToRval, DoneValue, BO_NE); |
Ted Kremenek | d81a4a1 | 2012-09-21 00:52:24 +0000 | [diff] [blame] | 547 | // (5) Create the 'if' statement. |
George Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 548 | IfStmt *If = new (C) IfStmt(C, SourceLocation(), |
George Karpenkov | a132938 | 2017-10-25 00:03:45 +0000 | [diff] [blame] | 549 | /* IsConstexpr=*/ false, |
| 550 | /* init=*/ nullptr, |
| 551 | /* var=*/ nullptr, |
Devin Coughlin | 046833e | 2017-11-06 22:12:19 +0000 | [diff] [blame] | 552 | /* cond=*/ GuardCondition, |
George Karpenkov | a132938 | 2017-10-25 00:03:45 +0000 | [diff] [blame] | 553 | /* then=*/ CS); |
Ted Kremenek | d81a4a1 | 2012-09-21 00:52:24 +0000 | [diff] [blame] | 554 | return If; |
| 555 | } |
| 556 | |
Ted Kremenek | 14f779c | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 557 | /// Create a fake body for dispatch_sync. |
| 558 | static Stmt *create_dispatch_sync(ASTContext &C, const FunctionDecl *D) { |
| 559 | // Check if we have at least two parameters. |
| 560 | if (D->param_size() != 2) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 561 | return nullptr; |
| 562 | |
Ted Kremenek | 14f779c | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 563 | // Check if the second parameter is a block. |
| 564 | const ParmVarDecl *PV = D->getParamDecl(1); |
| 565 | QualType Ty = PV->getType(); |
Ted Kremenek | d81a4a1 | 2012-09-21 00:52:24 +0000 | [diff] [blame] | 566 | if (!isDispatchBlock(Ty)) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 567 | return nullptr; |
| 568 | |
Ted Kremenek | 14f779c | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 569 | // Everything checks out. Create a fake body that just calls the block. |
| 570 | // This is basically just an AST dump of: |
| 571 | // |
| 572 | // void dispatch_sync(dispatch_queue_t queue, void (^block)(void)) { |
| 573 | // block(); |
| 574 | // } |
Ted Kremenek | 7241813 | 2012-09-21 17:54:35 +0000 | [diff] [blame] | 575 | // |
| 576 | ASTMaker M(C); |
| 577 | DeclRefExpr *DR = M.makeDeclRefExpr(PV); |
Ted Kremenek | dff3553 | 2012-09-21 18:33:52 +0000 | [diff] [blame] | 578 | ImplicitCastExpr *ICE = M.makeLvalueToRvalue(DR, Ty); |
Dmitri Gribenko | 44ebbd5 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 579 | CallExpr *CE = new (C) CallExpr(C, ICE, None, C.VoidTy, VK_RValue, |
| 580 | SourceLocation()); |
Ted Kremenek | 14f779c | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 581 | return CE; |
| 582 | } |
| 583 | |
Ted Kremenek | 089ffd0 | 2012-10-11 20:58:18 +0000 | [diff] [blame] | 584 | static Stmt *create_OSAtomicCompareAndSwap(ASTContext &C, const FunctionDecl *D) |
| 585 | { |
| 586 | // There are exactly 3 arguments. |
| 587 | if (D->param_size() != 3) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 588 | return nullptr; |
| 589 | |
Anna Zaks | 064185a | 2013-02-05 19:52:26 +0000 | [diff] [blame] | 590 | // Signature: |
| 591 | // _Bool OSAtomicCompareAndSwapPtr(void *__oldValue, |
| 592 | // void *__newValue, |
| 593 | // void * volatile *__theValue) |
| 594 | // Generate body: |
Ted Kremenek | 089ffd0 | 2012-10-11 20:58:18 +0000 | [diff] [blame] | 595 | // if (oldValue == *theValue) { |
| 596 | // *theValue = newValue; |
| 597 | // return YES; |
| 598 | // } |
| 599 | // else return NO; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 600 | |
| 601 | QualType ResultTy = D->getReturnType(); |
Ted Kremenek | 6fdefb5 | 2012-10-12 00:18:19 +0000 | [diff] [blame] | 602 | bool isBoolean = ResultTy->isBooleanType(); |
| 603 | if (!isBoolean && !ResultTy->isIntegralType(C)) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 604 | return nullptr; |
| 605 | |
Ted Kremenek | 089ffd0 | 2012-10-11 20:58:18 +0000 | [diff] [blame] | 606 | const ParmVarDecl *OldValue = D->getParamDecl(0); |
| 607 | QualType OldValueTy = OldValue->getType(); |
| 608 | |
| 609 | const ParmVarDecl *NewValue = D->getParamDecl(1); |
| 610 | QualType NewValueTy = NewValue->getType(); |
| 611 | |
| 612 | assert(OldValueTy == NewValueTy); |
| 613 | |
| 614 | const ParmVarDecl *TheValue = D->getParamDecl(2); |
| 615 | QualType TheValueTy = TheValue->getType(); |
| 616 | const PointerType *PT = TheValueTy->getAs<PointerType>(); |
| 617 | if (!PT) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 618 | return nullptr; |
Ted Kremenek | 089ffd0 | 2012-10-11 20:58:18 +0000 | [diff] [blame] | 619 | QualType PointeeTy = PT->getPointeeType(); |
| 620 | |
| 621 | ASTMaker M(C); |
| 622 | // Construct the comparison. |
| 623 | Expr *Comparison = |
| 624 | M.makeComparison( |
| 625 | M.makeLvalueToRvalue(M.makeDeclRefExpr(OldValue), OldValueTy), |
| 626 | M.makeLvalueToRvalue( |
| 627 | M.makeDereference( |
| 628 | M.makeLvalueToRvalue(M.makeDeclRefExpr(TheValue), TheValueTy), |
| 629 | PointeeTy), |
| 630 | PointeeTy), |
| 631 | BO_EQ); |
| 632 | |
| 633 | // Construct the body of the IfStmt. |
| 634 | Stmt *Stmts[2]; |
| 635 | Stmts[0] = |
| 636 | M.makeAssignment( |
| 637 | M.makeDereference( |
| 638 | M.makeLvalueToRvalue(M.makeDeclRefExpr(TheValue), TheValueTy), |
| 639 | PointeeTy), |
| 640 | M.makeLvalueToRvalue(M.makeDeclRefExpr(NewValue), NewValueTy), |
| 641 | NewValueTy); |
Ted Kremenek | 6fdefb5 | 2012-10-12 00:18:19 +0000 | [diff] [blame] | 642 | |
| 643 | Expr *BoolVal = M.makeObjCBool(true); |
| 644 | Expr *RetVal = isBoolean ? M.makeIntegralCastToBoolean(BoolVal) |
| 645 | : M.makeIntegralCast(BoolVal, ResultTy); |
| 646 | Stmts[1] = M.makeReturn(RetVal); |
Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 647 | CompoundStmt *Body = M.makeCompound(Stmts); |
Ted Kremenek | 089ffd0 | 2012-10-11 20:58:18 +0000 | [diff] [blame] | 648 | |
| 649 | // Construct the else clause. |
Ted Kremenek | 6fdefb5 | 2012-10-12 00:18:19 +0000 | [diff] [blame] | 650 | BoolVal = M.makeObjCBool(false); |
| 651 | RetVal = isBoolean ? M.makeIntegralCastToBoolean(BoolVal) |
| 652 | : M.makeIntegralCast(BoolVal, ResultTy); |
| 653 | Stmt *Else = M.makeReturn(RetVal); |
Ted Kremenek | 089ffd0 | 2012-10-11 20:58:18 +0000 | [diff] [blame] | 654 | |
| 655 | /// Construct the If. |
Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 656 | Stmt *If = new (C) IfStmt(C, SourceLocation(), false, nullptr, nullptr, |
| 657 | Comparison, Body, SourceLocation(), Else); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 658 | |
Ted Kremenek | 089ffd0 | 2012-10-11 20:58:18 +0000 | [diff] [blame] | 659 | return If; |
| 660 | } |
| 661 | |
Ted Kremenek | 14f779c | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 662 | Stmt *BodyFarm::getBody(const FunctionDecl *D) { |
| 663 | D = D->getCanonicalDecl(); |
| 664 | |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 665 | Optional<Stmt *> &Val = Bodies[D]; |
Ted Kremenek | 14f779c | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 666 | if (Val.hasValue()) |
| 667 | return Val.getValue(); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 668 | |
| 669 | Val = nullptr; |
| 670 | |
| 671 | if (D->getIdentifier() == nullptr) |
| 672 | return nullptr; |
Ted Kremenek | 14f779c | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 673 | |
| 674 | StringRef Name = D->getName(); |
| 675 | if (Name.empty()) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 676 | return nullptr; |
Ted Kremenek | 089ffd0 | 2012-10-11 20:58:18 +0000 | [diff] [blame] | 677 | |
| 678 | FunctionFarmer FF; |
| 679 | |
| 680 | if (Name.startswith("OSAtomicCompareAndSwap") || |
| 681 | Name.startswith("objc_atomicCompareAndSwap")) { |
| 682 | FF = create_OSAtomicCompareAndSwap; |
George Karpenkov | 657a589 | 2017-09-30 00:03:22 +0000 | [diff] [blame] | 683 | } else if (Name == "call_once" && D->getDeclContext()->isStdNamespace()) { |
| 684 | FF = create_call_once; |
| 685 | } else { |
Ted Kremenek | 089ffd0 | 2012-10-11 20:58:18 +0000 | [diff] [blame] | 686 | FF = llvm::StringSwitch<FunctionFarmer>(Name) |
| 687 | .Case("dispatch_sync", create_dispatch_sync) |
| 688 | .Case("dispatch_once", create_dispatch_once) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 689 | .Default(nullptr); |
Ted Kremenek | 14f779c | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Ted Kremenek | 089ffd0 | 2012-10-11 20:58:18 +0000 | [diff] [blame] | 692 | if (FF) { Val = FF(C, D); } |
Ted Kremenek | eeccb30 | 2014-08-27 15:14:15 +0000 | [diff] [blame] | 693 | else if (Injector) { Val = Injector->getBody(D); } |
Ted Kremenek | 14f779c | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 694 | return Val.getValue(); |
| 695 | } |
| 696 | |
Devin Coughlin | b7e810b | 2016-01-26 23:58:48 +0000 | [diff] [blame] | 697 | static const ObjCIvarDecl *findBackingIvar(const ObjCPropertyDecl *Prop) { |
| 698 | const ObjCIvarDecl *IVar = Prop->getPropertyIvarDecl(); |
| 699 | |
| 700 | if (IVar) |
| 701 | return IVar; |
| 702 | |
| 703 | // When a readonly property is shadowed in a class extensions with a |
| 704 | // a readwrite property, the instance variable belongs to the shadowing |
| 705 | // property rather than the shadowed property. If there is no instance |
| 706 | // variable on a readonly property, check to see whether the property is |
| 707 | // shadowed and if so try to get the instance variable from shadowing |
| 708 | // property. |
| 709 | if (!Prop->isReadOnly()) |
| 710 | return nullptr; |
| 711 | |
| 712 | auto *Container = cast<ObjCContainerDecl>(Prop->getDeclContext()); |
| 713 | const ObjCInterfaceDecl *PrimaryInterface = nullptr; |
| 714 | if (auto *InterfaceDecl = dyn_cast<ObjCInterfaceDecl>(Container)) { |
| 715 | PrimaryInterface = InterfaceDecl; |
| 716 | } else if (auto *CategoryDecl = dyn_cast<ObjCCategoryDecl>(Container)) { |
| 717 | PrimaryInterface = CategoryDecl->getClassInterface(); |
| 718 | } else if (auto *ImplDecl = dyn_cast<ObjCImplDecl>(Container)) { |
| 719 | PrimaryInterface = ImplDecl->getClassInterface(); |
| 720 | } else { |
| 721 | return nullptr; |
| 722 | } |
| 723 | |
| 724 | // FindPropertyVisibleInPrimaryClass() looks first in class extensions, so it |
| 725 | // is guaranteed to find the shadowing property, if it exists, rather than |
| 726 | // the shadowed property. |
| 727 | auto *ShadowingProp = PrimaryInterface->FindPropertyVisibleInPrimaryClass( |
Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 728 | Prop->getIdentifier(), Prop->getQueryKind()); |
Devin Coughlin | b7e810b | 2016-01-26 23:58:48 +0000 | [diff] [blame] | 729 | if (ShadowingProp && ShadowingProp != Prop) { |
| 730 | IVar = ShadowingProp->getPropertyIvarDecl(); |
| 731 | } |
| 732 | |
| 733 | return IVar; |
| 734 | } |
| 735 | |
Jordan Rose | 1a866cd | 2014-01-10 20:06:06 +0000 | [diff] [blame] | 736 | static Stmt *createObjCPropertyGetter(ASTContext &Ctx, |
| 737 | const ObjCPropertyDecl *Prop) { |
Jordan Rose | ddf1966 | 2014-01-23 03:59:10 +0000 | [diff] [blame] | 738 | // First, find the backing ivar. |
Devin Coughlin | b7e810b | 2016-01-26 23:58:48 +0000 | [diff] [blame] | 739 | const ObjCIvarDecl *IVar = findBackingIvar(Prop); |
Jordan Rose | 1a866cd | 2014-01-10 20:06:06 +0000 | [diff] [blame] | 740 | if (!IVar) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 741 | return nullptr; |
Jordan Rose | ddf1966 | 2014-01-23 03:59:10 +0000 | [diff] [blame] | 742 | |
| 743 | // Ignore weak variables, which have special behavior. |
Jordan Rose | 1a866cd | 2014-01-10 20:06:06 +0000 | [diff] [blame] | 744 | if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 745 | return nullptr; |
Jordan Rose | a3f2781 | 2014-01-14 17:29:06 +0000 | [diff] [blame] | 746 | |
Jordan Rose | ddf1966 | 2014-01-23 03:59:10 +0000 | [diff] [blame] | 747 | // Look to see if Sema has synthesized a body for us. This happens in |
| 748 | // Objective-C++ because the return value may be a C++ class type with a |
| 749 | // non-trivial copy constructor. We can only do this if we can find the |
| 750 | // @synthesize for this property, though (or if we know it's been auto- |
| 751 | // synthesized). |
Jordan Rose | a3f2781 | 2014-01-14 17:29:06 +0000 | [diff] [blame] | 752 | const ObjCImplementationDecl *ImplDecl = |
| 753 | IVar->getContainingInterface()->getImplementation(); |
| 754 | if (ImplDecl) { |
Aaron Ballman | d85eff4 | 2014-03-14 15:02:45 +0000 | [diff] [blame] | 755 | for (const auto *I : ImplDecl->property_impls()) { |
Jordan Rose | a3f2781 | 2014-01-14 17:29:06 +0000 | [diff] [blame] | 756 | if (I->getPropertyDecl() != Prop) |
| 757 | continue; |
| 758 | |
| 759 | if (I->getGetterCXXConstructor()) { |
| 760 | ASTMaker M(Ctx); |
| 761 | return M.makeReturn(I->getGetterCXXConstructor()); |
| 762 | } |
| 763 | } |
| 764 | } |
| 765 | |
Jordan Rose | ddf1966 | 2014-01-23 03:59:10 +0000 | [diff] [blame] | 766 | // Sanity check that the property is the same type as the ivar, or a |
| 767 | // reference to it, and that it is either an object pointer or trivially |
| 768 | // copyable. |
| 769 | if (!Ctx.hasSameUnqualifiedType(IVar->getType(), |
| 770 | Prop->getType().getNonReferenceType())) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 771 | return nullptr; |
Jordan Rose | ddf1966 | 2014-01-23 03:59:10 +0000 | [diff] [blame] | 772 | if (!IVar->getType()->isObjCLifetimeType() && |
| 773 | !IVar->getType().isTriviallyCopyableType(Ctx)) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 774 | return nullptr; |
Jordan Rose | 1a866cd | 2014-01-10 20:06:06 +0000 | [diff] [blame] | 775 | |
Jordan Rose | ddf1966 | 2014-01-23 03:59:10 +0000 | [diff] [blame] | 776 | // Generate our body: |
| 777 | // return self->_ivar; |
Jordan Rose | 1a866cd | 2014-01-10 20:06:06 +0000 | [diff] [blame] | 778 | ASTMaker M(Ctx); |
| 779 | |
| 780 | const VarDecl *selfVar = Prop->getGetterMethodDecl()->getSelfDecl(); |
Devin Coughlin | aac894f | 2017-01-11 01:02:34 +0000 | [diff] [blame] | 781 | if (!selfVar) |
| 782 | return nullptr; |
Jordan Rose | 1a866cd | 2014-01-10 20:06:06 +0000 | [diff] [blame] | 783 | |
| 784 | Expr *loadedIVar = |
| 785 | M.makeObjCIvarRef( |
| 786 | M.makeLvalueToRvalue( |
| 787 | M.makeDeclRefExpr(selfVar), |
| 788 | selfVar->getType()), |
| 789 | IVar); |
| 790 | |
| 791 | if (!Prop->getType()->isReferenceType()) |
| 792 | loadedIVar = M.makeLvalueToRvalue(loadedIVar, IVar->getType()); |
| 793 | |
| 794 | return M.makeReturn(loadedIVar); |
| 795 | } |
| 796 | |
Jordan Rose | ddf1966 | 2014-01-23 03:59:10 +0000 | [diff] [blame] | 797 | Stmt *BodyFarm::getBody(const ObjCMethodDecl *D) { |
| 798 | // We currently only know how to synthesize property accessors. |
Jordan Rose | 1a866cd | 2014-01-10 20:06:06 +0000 | [diff] [blame] | 799 | if (!D->isPropertyAccessor()) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 800 | return nullptr; |
Jordan Rose | 1a866cd | 2014-01-10 20:06:06 +0000 | [diff] [blame] | 801 | |
| 802 | D = D->getCanonicalDecl(); |
| 803 | |
| 804 | Optional<Stmt *> &Val = Bodies[D]; |
| 805 | if (Val.hasValue()) |
| 806 | return Val.getValue(); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 807 | Val = nullptr; |
Jordan Rose | 1a866cd | 2014-01-10 20:06:06 +0000 | [diff] [blame] | 808 | |
Jordan Rose | ddf1966 | 2014-01-23 03:59:10 +0000 | [diff] [blame] | 809 | const ObjCPropertyDecl *Prop = D->findPropertyDecl(); |
Jordan Rose | 1a866cd | 2014-01-10 20:06:06 +0000 | [diff] [blame] | 810 | if (!Prop) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 811 | return nullptr; |
Jordan Rose | 1a866cd | 2014-01-10 20:06:06 +0000 | [diff] [blame] | 812 | |
Jordan Rose | ddf1966 | 2014-01-23 03:59:10 +0000 | [diff] [blame] | 813 | // For now, we only synthesize getters. |
Devin Coughlin | ef3697e | 2016-02-18 19:37:39 +0000 | [diff] [blame] | 814 | // Synthesizing setters would cause false negatives in the |
| 815 | // RetainCountChecker because the method body would bind the parameter |
| 816 | // to an instance variable, causing it to escape. This would prevent |
| 817 | // warning in the following common scenario: |
| 818 | // |
| 819 | // id foo = [[NSObject alloc] init]; |
| 820 | // self.foo = foo; // We should warn that foo leaks here. |
| 821 | // |
Jordan Rose | 1a866cd | 2014-01-10 20:06:06 +0000 | [diff] [blame] | 822 | if (D->param_size() != 0) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 823 | return nullptr; |
Jordan Rose | 1a866cd | 2014-01-10 20:06:06 +0000 | [diff] [blame] | 824 | |
| 825 | Val = createObjCPropertyGetter(C, Prop); |
| 826 | |
| 827 | return Val.getValue(); |
| 828 | } |