2012-09-21 08:09:11 +08:00
|
|
|
//== BodyFarm.cpp - Factory for conjuring up fake bodies ----------*- C++ -*-//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// BodyFarm is a factory for creating faux implementations for functions/methods
|
|
|
|
// for analysis purposes.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-10-24 07:59:52 +08:00
|
|
|
#include "clang/Analysis/BodyFarm.h"
|
2012-09-21 08:09:11 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2017-09-30 08:03:22 +08:00
|
|
|
#include "clang/AST/CXXInheritance.h"
|
2012-09-21 08:09:11 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/Expr.h"
|
2017-09-30 08:03:22 +08:00
|
|
|
#include "clang/AST/ExprCXX.h"
|
2012-10-12 04:58:18 +08:00
|
|
|
#include "clang/AST/ExprObjC.h"
|
2017-09-30 08:03:22 +08:00
|
|
|
#include "clang/AST/NestedNameSpecifier.h"
|
2015-01-14 19:29:14 +08:00
|
|
|
#include "clang/Analysis/CodeInjector.h"
|
2017-09-30 08:03:22 +08:00
|
|
|
#include "clang/Basic/OperatorKinds.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
2017-09-30 08:03:22 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "body-farm"
|
2012-09-21 08:09:11 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2012-09-22 01:54:32 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Helper creation functions for constructing faux ASTs.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2012-09-21 08:09:11 +08:00
|
|
|
|
2012-09-21 08:52:24 +08:00
|
|
|
static bool isDispatchBlock(QualType Ty) {
|
|
|
|
// Is it a block pointer?
|
|
|
|
const BlockPointerType *BPT = Ty->getAs<BlockPointerType>();
|
|
|
|
if (!BPT)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check if the block pointer type takes no arguments and
|
|
|
|
// returns void.
|
|
|
|
const FunctionProtoType *FT =
|
|
|
|
BPT->getPointeeType()->getAs<FunctionProtoType>();
|
2015-11-06 09:08:38 +08:00
|
|
|
return FT && FT->getReturnType()->isVoidType() && FT->getNumParams() == 0;
|
2012-09-21 08:52:24 +08:00
|
|
|
}
|
|
|
|
|
2012-09-22 01:54:35 +08:00
|
|
|
namespace {
|
|
|
|
class ASTMaker {
|
|
|
|
public:
|
|
|
|
ASTMaker(ASTContext &C) : C(C) {}
|
|
|
|
|
2012-09-22 02:33:54 +08:00
|
|
|
/// Create a new BinaryOperator representing a simple assignment.
|
|
|
|
BinaryOperator *makeAssignment(const Expr *LHS, const Expr *RHS, QualType Ty);
|
|
|
|
|
2012-10-12 04:58:18 +08:00
|
|
|
/// Create a new BinaryOperator representing a comparison.
|
|
|
|
BinaryOperator *makeComparison(const Expr *LHS, const Expr *RHS,
|
|
|
|
BinaryOperator::Opcode Op);
|
|
|
|
|
|
|
|
/// Create a new compound stmt using the provided statements.
|
|
|
|
CompoundStmt *makeCompound(ArrayRef<Stmt*>);
|
|
|
|
|
2012-09-22 02:13:23 +08:00
|
|
|
/// Create a new DeclRefExpr for the referenced variable.
|
2017-09-30 08:03:22 +08:00
|
|
|
DeclRefExpr *makeDeclRefExpr(const VarDecl *D,
|
2017-10-18 06:28:18 +08:00
|
|
|
bool RefersToEnclosingVariableOrCapture = false);
|
2012-09-22 01:54:35 +08:00
|
|
|
|
2012-09-22 02:33:52 +08:00
|
|
|
/// Create a new UnaryOperator representing a dereference.
|
|
|
|
UnaryOperator *makeDereference(const Expr *Arg, QualType Ty);
|
|
|
|
|
2012-09-22 02:13:23 +08:00
|
|
|
/// Create an implicit cast for an integer conversion.
|
2012-10-12 08:18:19 +08:00
|
|
|
Expr *makeIntegralCast(const Expr *Arg, QualType Ty);
|
2012-09-22 02:13:23 +08:00
|
|
|
|
2012-10-12 04:58:18 +08:00
|
|
|
/// Create an implicit cast to a builtin boolean type.
|
|
|
|
ImplicitCastExpr *makeIntegralCastToBoolean(const Expr *Arg);
|
|
|
|
|
2017-09-30 08:03:22 +08:00
|
|
|
/// Create an implicit cast for lvalue-to-rvaluate conversions.
|
2012-09-22 02:13:27 +08:00
|
|
|
ImplicitCastExpr *makeLvalueToRvalue(const Expr *Arg, QualType Ty);
|
|
|
|
|
2017-09-30 08:03:22 +08:00
|
|
|
/// Make RValue out of variable declaration, creating a temporary
|
|
|
|
/// DeclRefExpr in the process.
|
|
|
|
ImplicitCastExpr *
|
|
|
|
makeLvalueToRvalue(const VarDecl *Decl,
|
2017-10-18 06:28:18 +08:00
|
|
|
bool RefersToEnclosingVariableOrCapture = false);
|
2017-09-30 08:03:22 +08:00
|
|
|
|
|
|
|
/// Create an implicit cast of the given type.
|
|
|
|
ImplicitCastExpr *makeImplicitCast(const Expr *Arg, QualType Ty,
|
|
|
|
CastKind CK = CK_LValueToRValue);
|
|
|
|
|
2012-10-12 04:58:18 +08:00
|
|
|
/// Create an Objective-C bool literal.
|
|
|
|
ObjCBoolLiteralExpr *makeObjCBool(bool Val);
|
2014-01-11 04:06:06 +08:00
|
|
|
|
|
|
|
/// Create an Objective-C ivar reference.
|
|
|
|
ObjCIvarRefExpr *makeObjCIvarRef(const Expr *Base, const ObjCIvarDecl *IVar);
|
2012-10-12 04:58:18 +08:00
|
|
|
|
|
|
|
/// Create a Return statement.
|
|
|
|
ReturnStmt *makeReturn(const Expr *RetVal);
|
|
|
|
|
2017-11-07 06:12:19 +08:00
|
|
|
/// Create an integer literal expression of the given type.
|
|
|
|
IntegerLiteral *makeIntegerLiteral(uint64_t Value, QualType Ty);
|
2017-09-30 08:03:22 +08:00
|
|
|
|
|
|
|
/// Create a member expression.
|
|
|
|
MemberExpr *makeMemberExpression(Expr *base, ValueDecl *MemberDecl,
|
|
|
|
bool IsArrow = false,
|
|
|
|
ExprValueKind ValueKind = VK_LValue);
|
|
|
|
|
|
|
|
/// Returns a *first* member field of a record declaration with a given name.
|
|
|
|
/// \return an nullptr if no member with such a name exists.
|
2017-10-12 04:53:01 +08:00
|
|
|
ValueDecl *findMemberField(const RecordDecl *RD, StringRef Name);
|
2017-09-30 08:03:22 +08:00
|
|
|
|
2012-09-22 01:54:35 +08:00
|
|
|
private:
|
|
|
|
ASTContext &C;
|
|
|
|
};
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2012-09-22 01:54:35 +08:00
|
|
|
|
2012-09-22 02:33:54 +08:00
|
|
|
BinaryOperator *ASTMaker::makeAssignment(const Expr *LHS, const Expr *RHS,
|
|
|
|
QualType Ty) {
|
|
|
|
return new (C) BinaryOperator(const_cast<Expr*>(LHS), const_cast<Expr*>(RHS),
|
|
|
|
BO_Assign, Ty, VK_RValue,
|
2017-03-28 03:17:25 +08:00
|
|
|
OK_Ordinary, SourceLocation(), FPOptions());
|
2012-09-22 02:33:54 +08:00
|
|
|
}
|
|
|
|
|
2012-10-12 04:58:18 +08:00
|
|
|
BinaryOperator *ASTMaker::makeComparison(const Expr *LHS, const Expr *RHS,
|
|
|
|
BinaryOperator::Opcode Op) {
|
|
|
|
assert(BinaryOperator::isLogicalOp(Op) ||
|
|
|
|
BinaryOperator::isComparisonOp(Op));
|
|
|
|
return new (C) BinaryOperator(const_cast<Expr*>(LHS),
|
|
|
|
const_cast<Expr*>(RHS),
|
|
|
|
Op,
|
|
|
|
C.getLogicalOperationType(),
|
|
|
|
VK_RValue,
|
2017-03-28 03:17:25 +08:00
|
|
|
OK_Ordinary, SourceLocation(), FPOptions());
|
2012-10-12 04:58:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CompoundStmt *ASTMaker::makeCompound(ArrayRef<Stmt *> Stmts) {
|
2017-12-25 00:24:20 +08:00
|
|
|
return CompoundStmt::Create(C, Stmts, SourceLocation(), SourceLocation());
|
2012-10-12 04:58:18 +08:00
|
|
|
}
|
|
|
|
|
2017-10-18 06:28:18 +08:00
|
|
|
DeclRefExpr *ASTMaker::makeDeclRefExpr(
|
|
|
|
const VarDecl *D,
|
|
|
|
bool RefersToEnclosingVariableOrCapture) {
|
|
|
|
QualType Type = D->getType().getNonReferenceType();
|
2017-09-30 08:03:22 +08:00
|
|
|
|
|
|
|
DeclRefExpr *DR = DeclRefExpr::Create(
|
|
|
|
C, NestedNameSpecifierLoc(), SourceLocation(), const_cast<VarDecl *>(D),
|
|
|
|
RefersToEnclosingVariableOrCapture, SourceLocation(), Type, VK_LValue);
|
2012-09-22 01:54:35 +08:00
|
|
|
return DR;
|
|
|
|
}
|
|
|
|
|
2012-09-22 02:33:52 +08:00
|
|
|
UnaryOperator *ASTMaker::makeDereference(const Expr *Arg, QualType Ty) {
|
|
|
|
return new (C) UnaryOperator(const_cast<Expr*>(Arg), UO_Deref, Ty,
|
2018-01-09 21:07:03 +08:00
|
|
|
VK_LValue, OK_Ordinary, SourceLocation(),
|
|
|
|
/*CanOverflow*/ false);
|
2012-09-22 02:33:52 +08:00
|
|
|
}
|
|
|
|
|
2012-09-22 02:13:27 +08:00
|
|
|
ImplicitCastExpr *ASTMaker::makeLvalueToRvalue(const Expr *Arg, QualType Ty) {
|
2017-09-30 08:03:22 +08:00
|
|
|
return makeImplicitCast(Arg, Ty, CK_LValueToRValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
ImplicitCastExpr *
|
|
|
|
ASTMaker::makeLvalueToRvalue(const VarDecl *Arg,
|
2017-10-18 06:28:18 +08:00
|
|
|
bool RefersToEnclosingVariableOrCapture) {
|
|
|
|
QualType Type = Arg->getType().getNonReferenceType();
|
2017-09-30 08:03:22 +08:00
|
|
|
return makeLvalueToRvalue(makeDeclRefExpr(Arg,
|
2017-10-18 06:28:18 +08:00
|
|
|
RefersToEnclosingVariableOrCapture),
|
2017-09-30 08:03:22 +08:00
|
|
|
Type);
|
|
|
|
}
|
|
|
|
|
|
|
|
ImplicitCastExpr *ASTMaker::makeImplicitCast(const Expr *Arg, QualType Ty,
|
|
|
|
CastKind CK) {
|
|
|
|
return ImplicitCastExpr::Create(C, Ty,
|
2017-10-25 08:03:45 +08:00
|
|
|
/* CastKind=*/ CK,
|
|
|
|
/* Expr=*/ const_cast<Expr *>(Arg),
|
|
|
|
/* CXXCastPath=*/ nullptr,
|
|
|
|
/* ExprValueKind=*/ VK_RValue);
|
2012-09-22 02:13:27 +08:00
|
|
|
}
|
|
|
|
|
2012-10-12 08:18:19 +08:00
|
|
|
Expr *ASTMaker::makeIntegralCast(const Expr *Arg, QualType Ty) {
|
|
|
|
if (Arg->getType() == Ty)
|
|
|
|
return const_cast<Expr*>(Arg);
|
2014-05-20 12:30:07 +08:00
|
|
|
|
2012-09-22 02:13:23 +08:00
|
|
|
return ImplicitCastExpr::Create(C, Ty, CK_IntegralCast,
|
2014-05-20 12:30:07 +08:00
|
|
|
const_cast<Expr*>(Arg), nullptr, VK_RValue);
|
2012-09-22 02:13:23 +08:00
|
|
|
}
|
|
|
|
|
2012-10-12 04:58:18 +08:00
|
|
|
ImplicitCastExpr *ASTMaker::makeIntegralCastToBoolean(const Expr *Arg) {
|
|
|
|
return ImplicitCastExpr::Create(C, C.BoolTy, CK_IntegralToBoolean,
|
2014-05-20 12:30:07 +08:00
|
|
|
const_cast<Expr*>(Arg), nullptr, VK_RValue);
|
2012-10-12 04:58:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ObjCBoolLiteralExpr *ASTMaker::makeObjCBool(bool Val) {
|
|
|
|
QualType Ty = C.getBOOLDecl() ? C.getBOOLType() : C.ObjCBuiltinBoolTy;
|
|
|
|
return new (C) ObjCBoolLiteralExpr(Val, Ty, SourceLocation());
|
|
|
|
}
|
|
|
|
|
2014-01-11 04:06:06 +08:00
|
|
|
ObjCIvarRefExpr *ASTMaker::makeObjCIvarRef(const Expr *Base,
|
|
|
|
const ObjCIvarDecl *IVar) {
|
|
|
|
return new (C) ObjCIvarRefExpr(const_cast<ObjCIvarDecl*>(IVar),
|
|
|
|
IVar->getType(), SourceLocation(),
|
|
|
|
SourceLocation(), const_cast<Expr*>(Base),
|
|
|
|
/*arrow=*/true, /*free=*/false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-12 04:58:18 +08:00
|
|
|
ReturnStmt *ASTMaker::makeReturn(const Expr *RetVal) {
|
2014-05-20 12:30:07 +08:00
|
|
|
return new (C) ReturnStmt(SourceLocation(), const_cast<Expr*>(RetVal),
|
|
|
|
nullptr);
|
2012-10-12 04:58:18 +08:00
|
|
|
}
|
|
|
|
|
2017-11-07 06:12:19 +08:00
|
|
|
IntegerLiteral *ASTMaker::makeIntegerLiteral(uint64_t Value, QualType Ty) {
|
|
|
|
llvm::APInt APValue = llvm::APInt(C.getTypeSize(Ty), Value);
|
|
|
|
return IntegerLiteral::Create(C, APValue, Ty, SourceLocation());
|
2017-09-30 08:03:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
MemberExpr *ASTMaker::makeMemberExpression(Expr *base, ValueDecl *MemberDecl,
|
|
|
|
bool IsArrow,
|
|
|
|
ExprValueKind ValueKind) {
|
|
|
|
|
|
|
|
DeclAccessPair FoundDecl = DeclAccessPair::make(MemberDecl, AS_public);
|
|
|
|
return MemberExpr::Create(
|
|
|
|
C, base, IsArrow, SourceLocation(), NestedNameSpecifierLoc(),
|
|
|
|
SourceLocation(), MemberDecl, FoundDecl,
|
|
|
|
DeclarationNameInfo(MemberDecl->getDeclName(), SourceLocation()),
|
2017-10-25 08:03:45 +08:00
|
|
|
/* TemplateArgumentListInfo=*/ nullptr, MemberDecl->getType(), ValueKind,
|
2017-09-30 08:03:22 +08:00
|
|
|
OK_Ordinary);
|
|
|
|
}
|
|
|
|
|
2017-10-12 04:53:01 +08:00
|
|
|
ValueDecl *ASTMaker::findMemberField(const RecordDecl *RD, StringRef Name) {
|
2017-09-30 08:03:22 +08:00
|
|
|
|
|
|
|
CXXBasePaths Paths(
|
|
|
|
/* FindAmbiguities=*/false,
|
|
|
|
/* RecordPaths=*/false,
|
2017-10-25 08:03:45 +08:00
|
|
|
/* DetectVirtual=*/ false);
|
2017-09-30 08:03:22 +08:00
|
|
|
const IdentifierInfo &II = C.Idents.get(Name);
|
|
|
|
DeclarationName DeclName = C.DeclarationNames.getIdentifier(&II);
|
|
|
|
|
|
|
|
DeclContextLookupResult Decls = RD->lookup(DeclName);
|
|
|
|
for (NamedDecl *FoundDecl : Decls)
|
|
|
|
if (!FoundDecl->getDeclContext()->isFunctionOrMethod())
|
2017-10-12 04:53:01 +08:00
|
|
|
return cast<ValueDecl>(FoundDecl);
|
2017-09-30 08:03:22 +08:00
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-09-22 01:54:32 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Creation functions for faux ASTs.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
typedef Stmt *(*FunctionFarmer)(ASTContext &C, const FunctionDecl *D);
|
|
|
|
|
2017-10-03 05:01:46 +08:00
|
|
|
static CallExpr *create_call_once_funcptr_call(ASTContext &C, ASTMaker M,
|
|
|
|
const ParmVarDecl *Callback,
|
|
|
|
ArrayRef<Expr *> CallArgs) {
|
2017-09-30 08:03:22 +08:00
|
|
|
|
2017-10-24 08:13:18 +08:00
|
|
|
QualType Ty = Callback->getType();
|
|
|
|
DeclRefExpr *Call = M.makeDeclRefExpr(Callback);
|
|
|
|
CastKind CK;
|
|
|
|
if (Ty->isRValueReferenceType()) {
|
|
|
|
CK = CK_LValueToRValue;
|
|
|
|
} else {
|
|
|
|
assert(Ty->isLValueReferenceType());
|
|
|
|
CK = CK_FunctionToPointerDecay;
|
|
|
|
Ty = C.getPointerType(Ty.getNonReferenceType());
|
|
|
|
}
|
|
|
|
|
|
|
|
return new (C)
|
|
|
|
CallExpr(C, M.makeImplicitCast(Call, Ty.getNonReferenceType(), CK),
|
|
|
|
/*args=*/CallArgs,
|
|
|
|
/*QualType=*/C.VoidTy,
|
|
|
|
/*ExprValueType=*/VK_RValue,
|
|
|
|
/*SourceLocation=*/SourceLocation());
|
2017-09-30 08:03:22 +08:00
|
|
|
}
|
|
|
|
|
2017-10-03 05:01:46 +08:00
|
|
|
static CallExpr *create_call_once_lambda_call(ASTContext &C, ASTMaker M,
|
|
|
|
const ParmVarDecl *Callback,
|
2017-10-21 07:29:59 +08:00
|
|
|
CXXRecordDecl *CallbackDecl,
|
2017-10-03 05:01:46 +08:00
|
|
|
ArrayRef<Expr *> CallArgs) {
|
2017-09-30 08:03:22 +08:00
|
|
|
assert(CallbackDecl != nullptr);
|
|
|
|
assert(CallbackDecl->isLambda());
|
|
|
|
FunctionDecl *callOperatorDecl = CallbackDecl->getLambdaCallOperator();
|
|
|
|
assert(callOperatorDecl != nullptr);
|
|
|
|
|
|
|
|
DeclRefExpr *callOperatorDeclRef =
|
2017-10-25 08:03:45 +08:00
|
|
|
DeclRefExpr::Create(/* Ctx =*/ C,
|
|
|
|
/* QualifierLoc =*/ NestedNameSpecifierLoc(),
|
|
|
|
/* TemplateKWLoc =*/ SourceLocation(),
|
2017-09-30 08:03:22 +08:00
|
|
|
const_cast<FunctionDecl *>(callOperatorDecl),
|
2017-10-25 08:03:45 +08:00
|
|
|
/* RefersToEnclosingVariableOrCapture=*/ false,
|
|
|
|
/* NameLoc =*/ SourceLocation(),
|
|
|
|
/* T =*/ callOperatorDecl->getType(),
|
|
|
|
/* VK =*/ VK_LValue);
|
2017-09-30 08:03:22 +08:00
|
|
|
|
|
|
|
return new (C)
|
|
|
|
CXXOperatorCallExpr(/*AstContext=*/C, OO_Call, callOperatorDeclRef,
|
|
|
|
/*args=*/CallArgs,
|
|
|
|
/*QualType=*/C.VoidTy,
|
|
|
|
/*ExprValueType=*/VK_RValue,
|
|
|
|
/*SourceLocation=*/SourceLocation(), FPOptions());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a fake body for std::call_once.
|
|
|
|
/// Emulates the following function body:
|
|
|
|
///
|
|
|
|
/// \code
|
|
|
|
/// typedef struct once_flag_s {
|
|
|
|
/// unsigned long __state = 0;
|
|
|
|
/// } once_flag;
|
|
|
|
/// template<class Callable>
|
|
|
|
/// void call_once(once_flag& o, Callable func) {
|
|
|
|
/// if (!o.__state) {
|
|
|
|
/// func();
|
|
|
|
/// }
|
|
|
|
/// o.__state = 1;
|
|
|
|
/// }
|
|
|
|
/// \endcode
|
|
|
|
static Stmt *create_call_once(ASTContext &C, const FunctionDecl *D) {
|
|
|
|
DEBUG(llvm::dbgs() << "Generating body for call_once\n");
|
|
|
|
|
|
|
|
// We need at least two parameters.
|
|
|
|
if (D->param_size() < 2)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
ASTMaker M(C);
|
|
|
|
|
|
|
|
const ParmVarDecl *Flag = D->getParamDecl(0);
|
|
|
|
const ParmVarDecl *Callback = D->getParamDecl(1);
|
2017-11-03 08:36:03 +08:00
|
|
|
|
|
|
|
if (!Callback->getType()->isReferenceType()) {
|
|
|
|
llvm::dbgs() << "libcxx03 std::call_once implementation, skipping.\n";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (!Flag->getType()->isReferenceType()) {
|
|
|
|
llvm::dbgs() << "unknown std::call_once implementation, skipping.\n";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-09-30 08:03:22 +08:00
|
|
|
QualType CallbackType = Callback->getType().getNonReferenceType();
|
2017-10-21 07:29:59 +08:00
|
|
|
|
|
|
|
// Nullable pointer, non-null iff function is a CXXRecordDecl.
|
|
|
|
CXXRecordDecl *CallbackRecordDecl = CallbackType->getAsCXXRecordDecl();
|
2017-10-10 07:20:46 +08:00
|
|
|
QualType FlagType = Flag->getType().getNonReferenceType();
|
2017-10-12 04:53:01 +08:00
|
|
|
auto *FlagRecordDecl = dyn_cast_or_null<RecordDecl>(FlagType->getAsTagDecl());
|
|
|
|
|
|
|
|
if (!FlagRecordDecl) {
|
|
|
|
DEBUG(llvm::dbgs() << "Flag field is not a record: "
|
|
|
|
<< "unknown std::call_once implementation, "
|
|
|
|
<< "ignoring the call.\n");
|
2017-10-10 07:20:46 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-10-12 04:53:01 +08:00
|
|
|
// We initially assume libc++ implementation of call_once,
|
|
|
|
// where the once_flag struct has a field `__state_`.
|
|
|
|
ValueDecl *FlagFieldDecl = M.findMemberField(FlagRecordDecl, "__state_");
|
|
|
|
|
|
|
|
// Otherwise, try libstdc++ implementation, with a field
|
|
|
|
// `_M_once`
|
|
|
|
if (!FlagFieldDecl) {
|
|
|
|
FlagFieldDecl = M.findMemberField(FlagRecordDecl, "_M_once");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!FlagFieldDecl) {
|
2017-11-03 08:36:03 +08:00
|
|
|
DEBUG(llvm::dbgs() << "No field _M_once or __state_ found on "
|
|
|
|
<< "std::once_flag struct: unknown std::call_once "
|
|
|
|
<< "implementation, ignoring the call.");
|
2017-10-10 07:20:46 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2017-09-30 08:03:22 +08:00
|
|
|
|
2017-10-21 07:29:59 +08:00
|
|
|
bool isLambdaCall = CallbackRecordDecl && CallbackRecordDecl->isLambda();
|
|
|
|
if (CallbackRecordDecl && !isLambdaCall) {
|
|
|
|
DEBUG(llvm::dbgs() << "Not supported: synthesizing body for functors when "
|
|
|
|
<< "body farming std::call_once, ignoring the call.");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-10-03 05:01:46 +08:00
|
|
|
|
2017-09-30 08:03:22 +08:00
|
|
|
SmallVector<Expr *, 5> CallArgs;
|
2017-10-21 07:29:59 +08:00
|
|
|
const FunctionProtoType *CallbackFunctionType;
|
|
|
|
if (isLambdaCall) {
|
2017-09-30 08:03:22 +08:00
|
|
|
|
2017-10-03 05:01:46 +08:00
|
|
|
// Lambda requires callback itself inserted as a first parameter.
|
|
|
|
CallArgs.push_back(
|
|
|
|
M.makeDeclRefExpr(Callback,
|
2017-10-25 08:03:45 +08:00
|
|
|
/* RefersToEnclosingVariableOrCapture=*/ true));
|
2017-10-21 07:29:59 +08:00
|
|
|
CallbackFunctionType = CallbackRecordDecl->getLambdaCallOperator()
|
|
|
|
->getType()
|
|
|
|
->getAs<FunctionProtoType>();
|
2017-10-24 08:13:18 +08:00
|
|
|
} else if (!CallbackType->getPointeeType().isNull()) {
|
2017-10-21 07:29:59 +08:00
|
|
|
CallbackFunctionType =
|
|
|
|
CallbackType->getPointeeType()->getAs<FunctionProtoType>();
|
2017-10-24 08:13:18 +08:00
|
|
|
} else {
|
|
|
|
CallbackFunctionType = CallbackType->getAs<FunctionProtoType>();
|
2017-10-21 07:29:59 +08:00
|
|
|
}
|
2017-10-03 05:01:46 +08:00
|
|
|
|
2017-10-21 07:29:59 +08:00
|
|
|
if (!CallbackFunctionType)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// First two arguments are used for the flag and for the callback.
|
|
|
|
if (D->getNumParams() != CallbackFunctionType->getNumParams() + 2) {
|
2017-11-03 08:36:03 +08:00
|
|
|
DEBUG(llvm::dbgs() << "Types of params of the callback do not match "
|
|
|
|
<< "params passed to std::call_once, "
|
|
|
|
<< "ignoring the call\n");
|
2017-10-21 07:29:59 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// All arguments past first two ones are passed to the callback,
|
|
|
|
// and we turn lvalues into rvalues if the argument is not passed by
|
|
|
|
// reference.
|
|
|
|
for (unsigned int ParamIdx = 2; ParamIdx < D->getNumParams(); ParamIdx++) {
|
|
|
|
const ParmVarDecl *PDecl = D->getParamDecl(ParamIdx);
|
|
|
|
Expr *ParamExpr = M.makeDeclRefExpr(PDecl);
|
|
|
|
if (!CallbackFunctionType->getParamType(ParamIdx - 2)->isReferenceType()) {
|
|
|
|
QualType PTy = PDecl->getType().getNonReferenceType();
|
|
|
|
ParamExpr = M.makeLvalueToRvalue(ParamExpr, PTy);
|
|
|
|
}
|
|
|
|
CallArgs.push_back(ParamExpr);
|
|
|
|
}
|
2017-09-30 08:03:22 +08:00
|
|
|
|
|
|
|
CallExpr *CallbackCall;
|
2017-10-03 05:01:46 +08:00
|
|
|
if (isLambdaCall) {
|
2017-09-30 08:03:22 +08:00
|
|
|
|
2017-10-21 07:29:59 +08:00
|
|
|
CallbackCall = create_call_once_lambda_call(C, M, Callback,
|
|
|
|
CallbackRecordDecl, CallArgs);
|
2017-09-30 08:03:22 +08:00
|
|
|
} else {
|
|
|
|
|
|
|
|
// Function pointer case.
|
|
|
|
CallbackCall = create_call_once_funcptr_call(C, M, Callback, CallArgs);
|
|
|
|
}
|
|
|
|
|
|
|
|
DeclRefExpr *FlagDecl =
|
|
|
|
M.makeDeclRefExpr(Flag,
|
2017-10-18 06:28:18 +08:00
|
|
|
/* RefersToEnclosingVariableOrCapture=*/true);
|
2017-09-30 08:03:22 +08:00
|
|
|
|
|
|
|
|
2017-10-12 04:53:01 +08:00
|
|
|
MemberExpr *Deref = M.makeMemberExpression(FlagDecl, FlagFieldDecl);
|
2017-09-30 08:03:22 +08:00
|
|
|
assert(Deref->isLValue());
|
|
|
|
QualType DerefType = Deref->getType();
|
|
|
|
|
|
|
|
// Negation predicate.
|
|
|
|
UnaryOperator *FlagCheck = new (C) UnaryOperator(
|
2017-10-25 08:03:45 +08:00
|
|
|
/* input=*/
|
2017-09-30 08:03:22 +08:00
|
|
|
M.makeImplicitCast(M.makeLvalueToRvalue(Deref, DerefType), DerefType,
|
|
|
|
CK_IntegralToBoolean),
|
2017-10-25 08:03:45 +08:00
|
|
|
/* opc=*/ UO_LNot,
|
|
|
|
/* QualType=*/ C.IntTy,
|
|
|
|
/* ExprValueKind=*/ VK_RValue,
|
2018-01-09 21:07:03 +08:00
|
|
|
/* ExprObjectKind=*/ OK_Ordinary, SourceLocation(),
|
|
|
|
/* CanOverflow*/ false);
|
2017-09-30 08:03:22 +08:00
|
|
|
|
|
|
|
// Create assignment.
|
|
|
|
BinaryOperator *FlagAssignment = M.makeAssignment(
|
2017-11-07 06:12:19 +08:00
|
|
|
Deref, M.makeIntegralCast(M.makeIntegerLiteral(1, C.IntTy), DerefType),
|
|
|
|
DerefType);
|
2017-09-30 08:03:22 +08:00
|
|
|
|
|
|
|
IfStmt *Out = new (C)
|
|
|
|
IfStmt(C, SourceLocation(),
|
2017-10-25 08:03:45 +08:00
|
|
|
/* IsConstexpr=*/ false,
|
|
|
|
/* init=*/ nullptr,
|
|
|
|
/* var=*/ nullptr,
|
|
|
|
/* cond=*/ FlagCheck,
|
|
|
|
/* then=*/ M.makeCompound({CallbackCall, FlagAssignment}));
|
2017-09-30 08:03:22 +08:00
|
|
|
|
|
|
|
return Out;
|
|
|
|
}
|
|
|
|
|
2012-09-21 08:52:24 +08:00
|
|
|
/// Create a fake body for dispatch_once.
|
|
|
|
static Stmt *create_dispatch_once(ASTContext &C, const FunctionDecl *D) {
|
|
|
|
// Check if we have at least two parameters.
|
|
|
|
if (D->param_size() != 2)
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
2012-09-21 08:52:24 +08:00
|
|
|
|
|
|
|
// Check if the first parameter is a pointer to integer type.
|
|
|
|
const ParmVarDecl *Predicate = D->getParamDecl(0);
|
|
|
|
QualType PredicateQPtrTy = Predicate->getType();
|
|
|
|
const PointerType *PredicatePtrTy = PredicateQPtrTy->getAs<PointerType>();
|
|
|
|
if (!PredicatePtrTy)
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
2012-09-21 08:52:24 +08:00
|
|
|
QualType PredicateTy = PredicatePtrTy->getPointeeType();
|
|
|
|
if (!PredicateTy->isIntegerType())
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
|
|
|
|
2012-09-21 08:52:24 +08:00
|
|
|
// Check if the second parameter is the proper block type.
|
|
|
|
const ParmVarDecl *Block = D->getParamDecl(1);
|
|
|
|
QualType Ty = Block->getType();
|
|
|
|
if (!isDispatchBlock(Ty))
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
|
|
|
|
2012-09-21 08:52:24 +08:00
|
|
|
// Everything checks out. Create a fakse body that checks the predicate,
|
|
|
|
// sets it, and calls the block. Basically, an AST dump of:
|
|
|
|
//
|
|
|
|
// void dispatch_once(dispatch_once_t *predicate, dispatch_block_t block) {
|
2017-11-07 06:12:19 +08:00
|
|
|
// if (*predicate != ~0l) {
|
|
|
|
// *predicate = ~0l;
|
2012-09-21 08:52:24 +08:00
|
|
|
// block();
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
2012-09-22 01:54:35 +08:00
|
|
|
ASTMaker M(C);
|
|
|
|
|
2012-09-21 08:52:24 +08:00
|
|
|
// (1) Create the call.
|
2017-09-30 08:03:22 +08:00
|
|
|
CallExpr *CE = new (C) CallExpr(
|
|
|
|
/*ASTContext=*/C,
|
|
|
|
/*StmtClass=*/M.makeLvalueToRvalue(/*Expr=*/Block),
|
|
|
|
/*args=*/None,
|
|
|
|
/*QualType=*/C.VoidTy,
|
|
|
|
/*ExprValueType=*/VK_RValue,
|
|
|
|
/*SourceLocation=*/SourceLocation());
|
2012-09-21 08:52:24 +08:00
|
|
|
|
|
|
|
// (2) Create the assignment to the predicate.
|
2017-11-07 06:12:19 +08:00
|
|
|
Expr *DoneValue =
|
|
|
|
new (C) UnaryOperator(M.makeIntegerLiteral(0, C.LongTy), UO_Not, C.LongTy,
|
2018-01-09 21:07:03 +08:00
|
|
|
VK_RValue, OK_Ordinary, SourceLocation(),
|
|
|
|
/*CanOverflow*/false);
|
2017-09-30 08:03:22 +08:00
|
|
|
|
2012-09-22 02:33:56 +08:00
|
|
|
BinaryOperator *B =
|
|
|
|
M.makeAssignment(
|
|
|
|
M.makeDereference(
|
|
|
|
M.makeLvalueToRvalue(
|
|
|
|
M.makeDeclRefExpr(Predicate), PredicateQPtrTy),
|
|
|
|
PredicateTy),
|
2017-11-07 06:12:19 +08:00
|
|
|
M.makeIntegralCast(DoneValue, PredicateTy),
|
2012-09-22 02:33:56 +08:00
|
|
|
PredicateTy);
|
|
|
|
|
2012-09-21 08:52:24 +08:00
|
|
|
// (3) Create the compound statement.
|
2014-08-27 14:28:36 +08:00
|
|
|
Stmt *Stmts[] = { B, CE };
|
|
|
|
CompoundStmt *CS = M.makeCompound(Stmts);
|
2012-09-21 08:52:24 +08:00
|
|
|
|
|
|
|
// (4) Create the 'if' condition.
|
2012-09-22 02:33:56 +08:00
|
|
|
ImplicitCastExpr *LValToRval =
|
|
|
|
M.makeLvalueToRvalue(
|
|
|
|
M.makeDereference(
|
|
|
|
M.makeLvalueToRvalue(
|
|
|
|
M.makeDeclRefExpr(Predicate),
|
|
|
|
PredicateQPtrTy),
|
|
|
|
PredicateTy),
|
|
|
|
PredicateTy);
|
2017-11-07 06:12:19 +08:00
|
|
|
|
|
|
|
Expr *GuardCondition = M.makeComparison(LValToRval, DoneValue, BO_NE);
|
2012-09-21 08:52:24 +08:00
|
|
|
// (5) Create the 'if' statement.
|
2017-09-30 08:03:22 +08:00
|
|
|
IfStmt *If = new (C) IfStmt(C, SourceLocation(),
|
2017-10-25 08:03:45 +08:00
|
|
|
/* IsConstexpr=*/ false,
|
|
|
|
/* init=*/ nullptr,
|
|
|
|
/* var=*/ nullptr,
|
2017-11-07 06:12:19 +08:00
|
|
|
/* cond=*/ GuardCondition,
|
2017-10-25 08:03:45 +08:00
|
|
|
/* then=*/ CS);
|
2012-09-21 08:52:24 +08:00
|
|
|
return If;
|
|
|
|
}
|
|
|
|
|
2012-09-21 08:09:11 +08:00
|
|
|
/// Create a fake body for dispatch_sync.
|
|
|
|
static Stmt *create_dispatch_sync(ASTContext &C, const FunctionDecl *D) {
|
|
|
|
// Check if we have at least two parameters.
|
|
|
|
if (D->param_size() != 2)
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
|
|
|
|
2012-09-21 08:09:11 +08:00
|
|
|
// Check if the second parameter is a block.
|
|
|
|
const ParmVarDecl *PV = D->getParamDecl(1);
|
|
|
|
QualType Ty = PV->getType();
|
2012-09-21 08:52:24 +08:00
|
|
|
if (!isDispatchBlock(Ty))
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
|
|
|
|
2012-09-21 08:09:11 +08:00
|
|
|
// Everything checks out. Create a fake body that just calls the block.
|
|
|
|
// This is basically just an AST dump of:
|
|
|
|
//
|
|
|
|
// void dispatch_sync(dispatch_queue_t queue, void (^block)(void)) {
|
|
|
|
// block();
|
|
|
|
// }
|
2012-09-22 01:54:35 +08:00
|
|
|
//
|
|
|
|
ASTMaker M(C);
|
|
|
|
DeclRefExpr *DR = M.makeDeclRefExpr(PV);
|
2012-09-22 02:33:52 +08:00
|
|
|
ImplicitCastExpr *ICE = M.makeLvalueToRvalue(DR, Ty);
|
2013-05-05 08:41:58 +08:00
|
|
|
CallExpr *CE = new (C) CallExpr(C, ICE, None, C.VoidTy, VK_RValue,
|
|
|
|
SourceLocation());
|
2012-09-21 08:09:11 +08:00
|
|
|
return CE;
|
|
|
|
}
|
|
|
|
|
2012-10-12 04:58:18 +08:00
|
|
|
static Stmt *create_OSAtomicCompareAndSwap(ASTContext &C, const FunctionDecl *D)
|
|
|
|
{
|
|
|
|
// There are exactly 3 arguments.
|
|
|
|
if (D->param_size() != 3)
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
|
|
|
|
2013-02-06 03:52:26 +08:00
|
|
|
// Signature:
|
|
|
|
// _Bool OSAtomicCompareAndSwapPtr(void *__oldValue,
|
|
|
|
// void *__newValue,
|
|
|
|
// void * volatile *__theValue)
|
|
|
|
// Generate body:
|
2012-10-12 04:58:18 +08:00
|
|
|
// if (oldValue == *theValue) {
|
|
|
|
// *theValue = newValue;
|
|
|
|
// return YES;
|
|
|
|
// }
|
|
|
|
// else return NO;
|
2014-01-26 00:55:45 +08:00
|
|
|
|
|
|
|
QualType ResultTy = D->getReturnType();
|
2012-10-12 08:18:19 +08:00
|
|
|
bool isBoolean = ResultTy->isBooleanType();
|
|
|
|
if (!isBoolean && !ResultTy->isIntegralType(C))
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
|
|
|
|
2012-10-12 04:58:18 +08:00
|
|
|
const ParmVarDecl *OldValue = D->getParamDecl(0);
|
|
|
|
QualType OldValueTy = OldValue->getType();
|
|
|
|
|
|
|
|
const ParmVarDecl *NewValue = D->getParamDecl(1);
|
|
|
|
QualType NewValueTy = NewValue->getType();
|
|
|
|
|
|
|
|
assert(OldValueTy == NewValueTy);
|
|
|
|
|
|
|
|
const ParmVarDecl *TheValue = D->getParamDecl(2);
|
|
|
|
QualType TheValueTy = TheValue->getType();
|
|
|
|
const PointerType *PT = TheValueTy->getAs<PointerType>();
|
|
|
|
if (!PT)
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
2012-10-12 04:58:18 +08:00
|
|
|
QualType PointeeTy = PT->getPointeeType();
|
|
|
|
|
|
|
|
ASTMaker M(C);
|
|
|
|
// Construct the comparison.
|
|
|
|
Expr *Comparison =
|
|
|
|
M.makeComparison(
|
|
|
|
M.makeLvalueToRvalue(M.makeDeclRefExpr(OldValue), OldValueTy),
|
|
|
|
M.makeLvalueToRvalue(
|
|
|
|
M.makeDereference(
|
|
|
|
M.makeLvalueToRvalue(M.makeDeclRefExpr(TheValue), TheValueTy),
|
|
|
|
PointeeTy),
|
|
|
|
PointeeTy),
|
|
|
|
BO_EQ);
|
|
|
|
|
|
|
|
// Construct the body of the IfStmt.
|
|
|
|
Stmt *Stmts[2];
|
|
|
|
Stmts[0] =
|
|
|
|
M.makeAssignment(
|
|
|
|
M.makeDereference(
|
|
|
|
M.makeLvalueToRvalue(M.makeDeclRefExpr(TheValue), TheValueTy),
|
|
|
|
PointeeTy),
|
|
|
|
M.makeLvalueToRvalue(M.makeDeclRefExpr(NewValue), NewValueTy),
|
|
|
|
NewValueTy);
|
2012-10-12 08:18:19 +08:00
|
|
|
|
|
|
|
Expr *BoolVal = M.makeObjCBool(true);
|
|
|
|
Expr *RetVal = isBoolean ? M.makeIntegralCastToBoolean(BoolVal)
|
|
|
|
: M.makeIntegralCast(BoolVal, ResultTy);
|
|
|
|
Stmts[1] = M.makeReturn(RetVal);
|
2014-08-27 14:28:36 +08:00
|
|
|
CompoundStmt *Body = M.makeCompound(Stmts);
|
2012-10-12 04:58:18 +08:00
|
|
|
|
|
|
|
// Construct the else clause.
|
2012-10-12 08:18:19 +08:00
|
|
|
BoolVal = M.makeObjCBool(false);
|
|
|
|
RetVal = isBoolean ? M.makeIntegralCastToBoolean(BoolVal)
|
|
|
|
: M.makeIntegralCast(BoolVal, ResultTy);
|
|
|
|
Stmt *Else = M.makeReturn(RetVal);
|
2012-10-12 04:58:18 +08:00
|
|
|
|
|
|
|
/// Construct the If.
|
2016-07-14 08:11:03 +08:00
|
|
|
Stmt *If = new (C) IfStmt(C, SourceLocation(), false, nullptr, nullptr,
|
|
|
|
Comparison, Body, SourceLocation(), Else);
|
2014-05-20 12:30:07 +08:00
|
|
|
|
2012-10-12 04:58:18 +08:00
|
|
|
return If;
|
|
|
|
}
|
|
|
|
|
2012-09-21 08:09:11 +08:00
|
|
|
Stmt *BodyFarm::getBody(const FunctionDecl *D) {
|
|
|
|
D = D->getCanonicalDecl();
|
|
|
|
|
2013-02-21 06:23:23 +08:00
|
|
|
Optional<Stmt *> &Val = Bodies[D];
|
2012-09-21 08:09:11 +08:00
|
|
|
if (Val.hasValue())
|
|
|
|
return Val.getValue();
|
2014-05-20 12:30:07 +08:00
|
|
|
|
|
|
|
Val = nullptr;
|
|
|
|
|
|
|
|
if (D->getIdentifier() == nullptr)
|
|
|
|
return nullptr;
|
2012-09-21 08:09:11 +08:00
|
|
|
|
|
|
|
StringRef Name = D->getName();
|
|
|
|
if (Name.empty())
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
2012-10-12 04:58:18 +08:00
|
|
|
|
|
|
|
FunctionFarmer FF;
|
|
|
|
|
|
|
|
if (Name.startswith("OSAtomicCompareAndSwap") ||
|
|
|
|
Name.startswith("objc_atomicCompareAndSwap")) {
|
|
|
|
FF = create_OSAtomicCompareAndSwap;
|
2017-09-30 08:03:22 +08:00
|
|
|
} else if (Name == "call_once" && D->getDeclContext()->isStdNamespace()) {
|
|
|
|
FF = create_call_once;
|
|
|
|
} else {
|
2012-10-12 04:58:18 +08:00
|
|
|
FF = llvm::StringSwitch<FunctionFarmer>(Name)
|
|
|
|
.Case("dispatch_sync", create_dispatch_sync)
|
|
|
|
.Case("dispatch_once", create_dispatch_once)
|
2014-05-20 12:30:07 +08:00
|
|
|
.Default(nullptr);
|
2012-09-21 08:09:11 +08:00
|
|
|
}
|
|
|
|
|
2012-10-12 04:58:18 +08:00
|
|
|
if (FF) { Val = FF(C, D); }
|
Add support for the static analyzer to synthesize function implementations from external model files.
Currently the analyzer lazily models some functions using 'BodyFarm',
which constructs a fake function implementation that the analyzer
can simulate that approximates the semantics of the function when
it is called. BodyFarm does this by constructing the AST for
such definitions on-the-fly. One strength of BodyFarm
is that all symbols and types referenced by synthesized function
bodies are contextual adapted to the containing translation unit.
The downside is that these ASTs are hardcoded in Clang's own
source code.
A more scalable model is to allow these models to be defined as source
code in separate "model" files and have the analyzer use those
definitions lazily when a function body is needed. Among other things,
it will allow more customization of the analyzer for specific APIs
and platforms.
This patch provides the initial infrastructure for this feature.
It extends BodyFarm to use an abstract API 'CodeInjector' that can be
used to synthesize function bodies. That 'CodeInjector' is
implemented using a new 'ModelInjector' in libFrontend, which lazily
parses a model file and injects the ASTs into the current translation
unit.
Models are currently found by specifying a 'model-path' as an
analyzer option; if no path is specified the CodeInjector is not
used, thus defaulting to the current behavior in the analyzer.
Models currently contain a single function definition, and can
be found by finding the file <function name>.model. This is an
initial starting point for something more rich, but it bootstraps
this feature for future evolution.
This patch was contributed by Gábor Horváth as part of his
Google Summer of Code project.
Some notes:
- This introduces the notion of a "model file" into
FrontendAction and the Preprocessor. This nomenclature
is specific to the static analyzer, but possibly could be
generalized. Essentially these are sources pulled in
exogenously from the principal translation.
Preprocessor gets a 'InitializeForModelFile' and
'FinalizeForModelFile' which could possibly be hoisted out
of Preprocessor if Preprocessor exposed a new API to
change the PragmaHandlers and some other internal pieces. This
can be revisited.
FrontendAction gets a 'isModelParsingAction()' predicate function
used to allow a new FrontendAction to recycle the Preprocessor
and ASTContext. This name could probably be made something
more general (i.e., not tied to 'model files') at the expense
of losing the intent of why it exists. This can be revisited.
- This is a moderate sized patch; it has gone through some amount of
offline code review. Most of the changes to the non-analyzer
parts are fairly small, and would make little sense without
the analyzer changes.
- Most of the analyzer changes are plumbing, with the interesting
behavior being introduced by ModelInjector.cpp and
ModelConsumer.cpp.
- The new functionality introduced by this change is off-by-default.
It requires an analyzer config option to enable.
llvm-svn: 216550
2014-08-27 23:14:15 +08:00
|
|
|
else if (Injector) { Val = Injector->getBody(D); }
|
2012-09-21 08:09:11 +08:00
|
|
|
return Val.getValue();
|
|
|
|
}
|
|
|
|
|
2016-01-27 07:58:48 +08:00
|
|
|
static const ObjCIvarDecl *findBackingIvar(const ObjCPropertyDecl *Prop) {
|
|
|
|
const ObjCIvarDecl *IVar = Prop->getPropertyIvarDecl();
|
|
|
|
|
|
|
|
if (IVar)
|
|
|
|
return IVar;
|
|
|
|
|
|
|
|
// When a readonly property is shadowed in a class extensions with a
|
|
|
|
// a readwrite property, the instance variable belongs to the shadowing
|
|
|
|
// property rather than the shadowed property. If there is no instance
|
|
|
|
// variable on a readonly property, check to see whether the property is
|
|
|
|
// shadowed and if so try to get the instance variable from shadowing
|
|
|
|
// property.
|
|
|
|
if (!Prop->isReadOnly())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
auto *Container = cast<ObjCContainerDecl>(Prop->getDeclContext());
|
|
|
|
const ObjCInterfaceDecl *PrimaryInterface = nullptr;
|
|
|
|
if (auto *InterfaceDecl = dyn_cast<ObjCInterfaceDecl>(Container)) {
|
|
|
|
PrimaryInterface = InterfaceDecl;
|
|
|
|
} else if (auto *CategoryDecl = dyn_cast<ObjCCategoryDecl>(Container)) {
|
|
|
|
PrimaryInterface = CategoryDecl->getClassInterface();
|
|
|
|
} else if (auto *ImplDecl = dyn_cast<ObjCImplDecl>(Container)) {
|
|
|
|
PrimaryInterface = ImplDecl->getClassInterface();
|
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindPropertyVisibleInPrimaryClass() looks first in class extensions, so it
|
|
|
|
// is guaranteed to find the shadowing property, if it exists, rather than
|
|
|
|
// the shadowed property.
|
|
|
|
auto *ShadowingProp = PrimaryInterface->FindPropertyVisibleInPrimaryClass(
|
2016-01-29 02:49:28 +08:00
|
|
|
Prop->getIdentifier(), Prop->getQueryKind());
|
2016-01-27 07:58:48 +08:00
|
|
|
if (ShadowingProp && ShadowingProp != Prop) {
|
|
|
|
IVar = ShadowingProp->getPropertyIvarDecl();
|
|
|
|
}
|
|
|
|
|
|
|
|
return IVar;
|
|
|
|
}
|
|
|
|
|
2014-01-11 04:06:06 +08:00
|
|
|
static Stmt *createObjCPropertyGetter(ASTContext &Ctx,
|
|
|
|
const ObjCPropertyDecl *Prop) {
|
2014-01-23 11:59:10 +08:00
|
|
|
// First, find the backing ivar.
|
2016-01-27 07:58:48 +08:00
|
|
|
const ObjCIvarDecl *IVar = findBackingIvar(Prop);
|
2014-01-11 04:06:06 +08:00
|
|
|
if (!IVar)
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
2014-01-23 11:59:10 +08:00
|
|
|
|
|
|
|
// Ignore weak variables, which have special behavior.
|
2014-01-11 04:06:06 +08:00
|
|
|
if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak)
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
2014-01-15 01:29:06 +08:00
|
|
|
|
2014-01-23 11:59:10 +08:00
|
|
|
// Look to see if Sema has synthesized a body for us. This happens in
|
|
|
|
// Objective-C++ because the return value may be a C++ class type with a
|
|
|
|
// non-trivial copy constructor. We can only do this if we can find the
|
|
|
|
// @synthesize for this property, though (or if we know it's been auto-
|
|
|
|
// synthesized).
|
2014-01-15 01:29:06 +08:00
|
|
|
const ObjCImplementationDecl *ImplDecl =
|
|
|
|
IVar->getContainingInterface()->getImplementation();
|
|
|
|
if (ImplDecl) {
|
2014-03-14 23:02:45 +08:00
|
|
|
for (const auto *I : ImplDecl->property_impls()) {
|
2014-01-15 01:29:06 +08:00
|
|
|
if (I->getPropertyDecl() != Prop)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (I->getGetterCXXConstructor()) {
|
|
|
|
ASTMaker M(Ctx);
|
|
|
|
return M.makeReturn(I->getGetterCXXConstructor());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-23 11:59:10 +08:00
|
|
|
// Sanity check that the property is the same type as the ivar, or a
|
|
|
|
// reference to it, and that it is either an object pointer or trivially
|
|
|
|
// copyable.
|
|
|
|
if (!Ctx.hasSameUnqualifiedType(IVar->getType(),
|
|
|
|
Prop->getType().getNonReferenceType()))
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
2014-01-23 11:59:10 +08:00
|
|
|
if (!IVar->getType()->isObjCLifetimeType() &&
|
|
|
|
!IVar->getType().isTriviallyCopyableType(Ctx))
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
2014-01-11 04:06:06 +08:00
|
|
|
|
2014-01-23 11:59:10 +08:00
|
|
|
// Generate our body:
|
|
|
|
// return self->_ivar;
|
2014-01-11 04:06:06 +08:00
|
|
|
ASTMaker M(Ctx);
|
|
|
|
|
|
|
|
const VarDecl *selfVar = Prop->getGetterMethodDecl()->getSelfDecl();
|
2017-01-11 09:02:34 +08:00
|
|
|
if (!selfVar)
|
|
|
|
return nullptr;
|
2014-01-11 04:06:06 +08:00
|
|
|
|
|
|
|
Expr *loadedIVar =
|
|
|
|
M.makeObjCIvarRef(
|
|
|
|
M.makeLvalueToRvalue(
|
|
|
|
M.makeDeclRefExpr(selfVar),
|
|
|
|
selfVar->getType()),
|
|
|
|
IVar);
|
|
|
|
|
|
|
|
if (!Prop->getType()->isReferenceType())
|
|
|
|
loadedIVar = M.makeLvalueToRvalue(loadedIVar, IVar->getType());
|
|
|
|
|
|
|
|
return M.makeReturn(loadedIVar);
|
|
|
|
}
|
|
|
|
|
2014-01-23 11:59:10 +08:00
|
|
|
Stmt *BodyFarm::getBody(const ObjCMethodDecl *D) {
|
|
|
|
// We currently only know how to synthesize property accessors.
|
2014-01-11 04:06:06 +08:00
|
|
|
if (!D->isPropertyAccessor())
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
2014-01-11 04:06:06 +08:00
|
|
|
|
|
|
|
D = D->getCanonicalDecl();
|
|
|
|
|
|
|
|
Optional<Stmt *> &Val = Bodies[D];
|
|
|
|
if (Val.hasValue())
|
|
|
|
return Val.getValue();
|
2014-05-20 12:30:07 +08:00
|
|
|
Val = nullptr;
|
2014-01-11 04:06:06 +08:00
|
|
|
|
2014-01-23 11:59:10 +08:00
|
|
|
const ObjCPropertyDecl *Prop = D->findPropertyDecl();
|
2014-01-11 04:06:06 +08:00
|
|
|
if (!Prop)
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
2014-01-11 04:06:06 +08:00
|
|
|
|
2014-01-23 11:59:10 +08:00
|
|
|
// For now, we only synthesize getters.
|
2016-02-19 03:37:39 +08:00
|
|
|
// Synthesizing setters would cause false negatives in the
|
|
|
|
// RetainCountChecker because the method body would bind the parameter
|
|
|
|
// to an instance variable, causing it to escape. This would prevent
|
|
|
|
// warning in the following common scenario:
|
|
|
|
//
|
|
|
|
// id foo = [[NSObject alloc] init];
|
|
|
|
// self.foo = foo; // We should warn that foo leaks here.
|
|
|
|
//
|
2014-01-11 04:06:06 +08:00
|
|
|
if (D->param_size() != 0)
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
2014-01-11 04:06:06 +08:00
|
|
|
|
|
|
|
Val = createObjCPropertyGetter(C, Prop);
|
|
|
|
|
|
|
|
return Val.getValue();
|
|
|
|
}
|
|
|
|
|