2012-09-21 00:09:11 +00:00
|
|
|
//== BodyFarm.cpp - Factory for conjuring up fake bodies ----------*- C++ -*-//
|
|
|
|
//
|
2019-01-19 08:50:56 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-09-21 00:09:11 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// BodyFarm is a factory for creating faux implementations for functions/methods
|
|
|
|
// for analysis purposes.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-10-23 23:59:52 +00:00
|
|
|
#include "clang/Analysis/BodyFarm.h"
|
2012-09-21 00:09:11 +00:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2017-09-30 00:03:22 +00:00
|
|
|
#include "clang/AST/CXXInheritance.h"
|
2012-09-21 00:09:11 +00:00
|
|
|
#include "clang/AST/Decl.h"
|
2012-12-04 09:13:33 +00:00
|
|
|
#include "clang/AST/Expr.h"
|
2017-09-30 00:03:22 +00:00
|
|
|
#include "clang/AST/ExprCXX.h"
|
2012-10-11 20:58:18 +00:00
|
|
|
#include "clang/AST/ExprObjC.h"
|
2017-09-30 00:03:22 +00:00
|
|
|
#include "clang/AST/NestedNameSpecifier.h"
|
2015-01-14 11:29:14 +00:00
|
|
|
#include "clang/Analysis/CodeInjector.h"
|
Treat `std::move`, `forward`, etc. as builtins.
This is extended to all `std::` functions that take a reference to a
value and return a reference (or pointer) to that same value: `move`,
`forward`, `move_if_noexcept`, `as_const`, `addressof`, and the
libstdc++-specific function `__addressof`.
We still require these functions to be declared before they can be used,
but don't instantiate their definitions unless their addresses are
taken. Instead, code generation, constant evaluation, and static
analysis are given direct knowledge of their effect.
This change aims to reduce various costs associated with these functions
-- per-instantiation memory costs, compile time and memory costs due to
creating out-of-line copies and inlining them, code size at -O0, and so
on -- so that they are not substantially more expensive than a cast.
Most of these improvements are very small, but I measured a 3% decrease
in -O0 object file size for a simple C++ source file using the standard
library after this change.
We now automatically infer the `const` and `nothrow` attributes on these
now-builtin functions, in particular meaning that we get a warning for
an unused call to one of these functions.
In C++20 onwards, we disallow taking the addresses of these functions,
per the C++20 "addressable function" rule. In earlier language modes, a
compatibility warning is produced but the address can still be taken.
The same infrastructure is extended to the existing MSVC builtin
`__GetExceptionInfo`, which is now only recognized in namespace `std`
like it always should have been.
This is a re-commit of
fc3090109643af8d2da9822d0f99c84742b9c877,
a571f82a50416b767fd3cce0fb5027bb5dfec58c,
64c045e25b8471bbb572bd29159c294a82a86a2, and
de6ddaeef3aaa8a9ae3663c12cdb57d9afc0f906,
and reverts aa643f455a5362de7189eac630050d2c8aefe8f2.
This change also includes a workaround for users using libc++ 3.1 and
earlier (!!), as apparently happens on AIX, where std::move sometimes
returns by value.
Reviewed By: aaron.ballman
Differential Revision: https://reviews.llvm.org/D123345
Revert "Fixup D123950 to address revert of D123345"
This reverts commit aa643f455a5362de7189eac630050d2c8aefe8f2.
2022-04-20 17:13:56 -07:00
|
|
|
#include "clang/Basic/Builtins.h"
|
2017-09-30 00:03:22 +00:00
|
|
|
#include "clang/Basic/OperatorKinds.h"
|
2012-12-04 09:13:33 +00:00
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
2017-09-30 00:03:22 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2023-01-14 11:07:21 -08:00
|
|
|
#include <optional>
|
2017-09-30 00:03:22 +00:00
|
|
|
|
|
|
|
#define DEBUG_TYPE "body-farm"
|
2012-09-21 00:09:11 +00:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2012-09-21 17:54:32 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Helper creation functions for constructing faux ASTs.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2012-09-21 00:09:11 +00:00
|
|
|
|
2012-09-21 00:52:24 +00: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 01:08:38 +00:00
|
|
|
return FT && FT->getReturnType()->isVoidType() && FT->getNumParams() == 0;
|
2012-09-21 00:52:24 +00:00
|
|
|
}
|
|
|
|
|
2012-09-21 17:54:35 +00:00
|
|
|
namespace {
|
|
|
|
class ASTMaker {
|
|
|
|
public:
|
|
|
|
ASTMaker(ASTContext &C) : C(C) {}
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2012-09-21 18:33:54 +00:00
|
|
|
/// Create a new BinaryOperator representing a simple assignment.
|
|
|
|
BinaryOperator *makeAssignment(const Expr *LHS, const Expr *RHS, QualType Ty);
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2012-10-11 20:58:18 +00:00
|
|
|
/// Create a new BinaryOperator representing a comparison.
|
|
|
|
BinaryOperator *makeComparison(const Expr *LHS, const Expr *RHS,
|
|
|
|
BinaryOperator::Opcode Op);
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2012-10-11 20:58:18 +00:00
|
|
|
/// Create a new compound stmt using the provided statements.
|
|
|
|
CompoundStmt *makeCompound(ArrayRef<Stmt*>);
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2012-09-21 18:13:23 +00:00
|
|
|
/// Create a new DeclRefExpr for the referenced variable.
|
2017-09-30 00:03:22 +00:00
|
|
|
DeclRefExpr *makeDeclRefExpr(const VarDecl *D,
|
2017-10-17 22:28:18 +00:00
|
|
|
bool RefersToEnclosingVariableOrCapture = false);
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2012-09-21 18:33:52 +00:00
|
|
|
/// Create a new UnaryOperator representing a dereference.
|
|
|
|
UnaryOperator *makeDereference(const Expr *Arg, QualType Ty);
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2012-09-21 18:13:23 +00:00
|
|
|
/// Create an implicit cast for an integer conversion.
|
2012-10-12 00:18:19 +00:00
|
|
|
Expr *makeIntegralCast(const Expr *Arg, QualType Ty);
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2012-10-11 20:58:18 +00:00
|
|
|
/// Create an implicit cast to a builtin boolean type.
|
|
|
|
ImplicitCastExpr *makeIntegralCastToBoolean(const Expr *Arg);
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2017-09-30 00:03:22 +00:00
|
|
|
/// Create an implicit cast for lvalue-to-rvaluate conversions.
|
2012-09-21 18:13:27 +00:00
|
|
|
ImplicitCastExpr *makeLvalueToRvalue(const Expr *Arg, QualType Ty);
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2017-09-30 00:03:22 +00:00
|
|
|
/// Make RValue out of variable declaration, creating a temporary
|
|
|
|
/// DeclRefExpr in the process.
|
|
|
|
ImplicitCastExpr *
|
|
|
|
makeLvalueToRvalue(const VarDecl *Decl,
|
2017-10-17 22:28:18 +00:00
|
|
|
bool RefersToEnclosingVariableOrCapture = false);
|
2017-09-30 00:03:22 +00:00
|
|
|
|
|
|
|
/// Create an implicit cast of the given type.
|
|
|
|
ImplicitCastExpr *makeImplicitCast(const Expr *Arg, QualType Ty,
|
|
|
|
CastKind CK = CK_LValueToRValue);
|
|
|
|
|
Treat `std::move`, `forward`, etc. as builtins.
This is extended to all `std::` functions that take a reference to a
value and return a reference (or pointer) to that same value: `move`,
`forward`, `move_if_noexcept`, `as_const`, `addressof`, and the
libstdc++-specific function `__addressof`.
We still require these functions to be declared before they can be used,
but don't instantiate their definitions unless their addresses are
taken. Instead, code generation, constant evaluation, and static
analysis are given direct knowledge of their effect.
This change aims to reduce various costs associated with these functions
-- per-instantiation memory costs, compile time and memory costs due to
creating out-of-line copies and inlining them, code size at -O0, and so
on -- so that they are not substantially more expensive than a cast.
Most of these improvements are very small, but I measured a 3% decrease
in -O0 object file size for a simple C++ source file using the standard
library after this change.
We now automatically infer the `const` and `nothrow` attributes on these
now-builtin functions, in particular meaning that we get a warning for
an unused call to one of these functions.
In C++20 onwards, we disallow taking the addresses of these functions,
per the C++20 "addressable function" rule. In earlier language modes, a
compatibility warning is produced but the address can still be taken.
The same infrastructure is extended to the existing MSVC builtin
`__GetExceptionInfo`, which is now only recognized in namespace `std`
like it always should have been.
This is a re-commit of
fc3090109643af8d2da9822d0f99c84742b9c877,
a571f82a50416b767fd3cce0fb5027bb5dfec58c,
64c045e25b8471bbb572bd29159c294a82a86a2, and
de6ddaeef3aaa8a9ae3663c12cdb57d9afc0f906,
and reverts aa643f455a5362de7189eac630050d2c8aefe8f2.
This change also includes a workaround for users using libc++ 3.1 and
earlier (!!), as apparently happens on AIX, where std::move sometimes
returns by value.
Reviewed By: aaron.ballman
Differential Revision: https://reviews.llvm.org/D123345
Revert "Fixup D123950 to address revert of D123345"
This reverts commit aa643f455a5362de7189eac630050d2c8aefe8f2.
2022-04-20 17:13:56 -07:00
|
|
|
/// Create a cast to reference type.
|
|
|
|
CastExpr *makeReferenceCast(const Expr *Arg, QualType Ty);
|
|
|
|
|
2012-10-11 20:58:18 +00:00
|
|
|
/// Create an Objective-C bool literal.
|
|
|
|
ObjCBoolLiteralExpr *makeObjCBool(bool Val);
|
2014-01-10 20:06:06 +00:00
|
|
|
|
|
|
|
/// Create an Objective-C ivar reference.
|
|
|
|
ObjCIvarRefExpr *makeObjCIvarRef(const Expr *Base, const ObjCIvarDecl *IVar);
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2012-10-11 20:58:18 +00:00
|
|
|
/// Create a Return statement.
|
|
|
|
ReturnStmt *makeReturn(const Expr *RetVal);
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2017-11-06 22:12:19 +00:00
|
|
|
/// Create an integer literal expression of the given type.
|
|
|
|
IntegerLiteral *makeIntegerLiteral(uint64_t Value, QualType Ty);
|
2017-09-30 00:03:22 +00: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-11 20:53:01 +00:00
|
|
|
ValueDecl *findMemberField(const RecordDecl *RD, StringRef Name);
|
2017-09-30 00:03:22 +00:00
|
|
|
|
2012-09-21 17:54:35 +00:00
|
|
|
private:
|
|
|
|
ASTContext &C;
|
|
|
|
};
|
2015-06-22 23:07:51 +00:00
|
|
|
}
|
2012-09-21 17:54:35 +00:00
|
|
|
|
2012-09-21 18:33:54 +00:00
|
|
|
BinaryOperator *ASTMaker::makeAssignment(const Expr *LHS, const Expr *RHS,
|
|
|
|
QualType Ty) {
|
2020-04-10 13:34:46 -07:00
|
|
|
return BinaryOperator::Create(
|
|
|
|
C, const_cast<Expr *>(LHS), const_cast<Expr *>(RHS), BO_Assign, Ty,
|
2021-06-04 23:15:23 +02:00
|
|
|
VK_PRValue, OK_Ordinary, SourceLocation(), FPOptionsOverride());
|
2012-09-21 18:33:54 +00:00
|
|
|
}
|
|
|
|
|
2012-10-11 20:58:18 +00:00
|
|
|
BinaryOperator *ASTMaker::makeComparison(const Expr *LHS, const Expr *RHS,
|
|
|
|
BinaryOperator::Opcode Op) {
|
|
|
|
assert(BinaryOperator::isLogicalOp(Op) ||
|
|
|
|
BinaryOperator::isComparisonOp(Op));
|
2020-06-26 09:23:45 -07:00
|
|
|
return BinaryOperator::Create(
|
|
|
|
C, const_cast<Expr *>(LHS), const_cast<Expr *>(RHS), Op,
|
2021-06-04 23:15:23 +02:00
|
|
|
C.getLogicalOperationType(), VK_PRValue, OK_Ordinary, SourceLocation(),
|
2020-06-26 09:23:45 -07:00
|
|
|
FPOptionsOverride());
|
2012-10-11 20:58:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CompoundStmt *ASTMaker::makeCompound(ArrayRef<Stmt *> Stmts) {
|
[FPEnv] Allow CompoundStmt to keep FP options
This is a recommit of b822efc7404bf09ccfdc1ab7657475026966c3b2,
reverted in dc34d8df4c48b3a8f474360970cae8a58e6c84f0. The commit caused
fails because the test ast-print-fp-pragmas.c did not specify particular
target, and it failed on targets which do not support constrained
intrinsics. The original commit message is below.
AST does not have special nodes for pragmas. Instead a pragma modifies
some state variables of Sema, which in turn results in modified
attributes of AST nodes. This technique applies to floating point
operations as well. Every AST node that can depend on FP options keeps
current set of them.
This technique works well for options like exception behavior or fast
math options. They represent instructions to the compiler how to modify
code generation for the affected nodes. However treatment of FP control
modes has problems with this technique. Modifying FP control mode
(like rounding direction) usually requires operations on hardware, like
writing to control registers. It must be done prior to the first
operation that depends on the control mode. In particular, such
operations are required for implementation of `pragma STDC FENV_ROUND`,
compiler should set up necessary rounding direction at the beginning of
compound statement where the pragma occurs. As there is no representation
for pragmas in AST, the code generation becomes a complicated task in
this case.
To solve this issue FP options are kept inside CompoundStmt. Unlike to FP
options in expressions, these does not affect any operation on FP values,
but only inform the codegen about the FP options that act in the body of
the statement. As all pragmas that modify FP environment may occurs only
at the start of compound statement or at global level, such solution
works for all relevant pragmas. The options are kept as a difference
from the options in the enclosing compound statement or default options,
it helps codegen to set only changed control modes.
Differential Revision: https://reviews.llvm.org/D123952
2022-07-01 18:32:26 +07:00
|
|
|
return CompoundStmt::Create(C, Stmts, FPOptionsOverride(), SourceLocation(),
|
|
|
|
SourceLocation());
|
2012-10-11 20:58:18 +00:00
|
|
|
}
|
|
|
|
|
2017-10-17 22:28:18 +00:00
|
|
|
DeclRefExpr *ASTMaker::makeDeclRefExpr(
|
|
|
|
const VarDecl *D,
|
|
|
|
bool RefersToEnclosingVariableOrCapture) {
|
|
|
|
QualType Type = D->getType().getNonReferenceType();
|
2017-09-30 00:03:22 +00:00
|
|
|
|
|
|
|
DeclRefExpr *DR = DeclRefExpr::Create(
|
|
|
|
C, NestedNameSpecifierLoc(), SourceLocation(), const_cast<VarDecl *>(D),
|
|
|
|
RefersToEnclosingVariableOrCapture, SourceLocation(), Type, VK_LValue);
|
2012-09-21 17:54:35 +00:00
|
|
|
return DR;
|
|
|
|
}
|
|
|
|
|
2012-09-21 18:33:52 +00:00
|
|
|
UnaryOperator *ASTMaker::makeDereference(const Expr *Arg, QualType Ty) {
|
2020-05-01 10:32:06 -07:00
|
|
|
return UnaryOperator::Create(C, const_cast<Expr *>(Arg), UO_Deref, Ty,
|
2018-01-09 13:07:03 +00:00
|
|
|
VK_LValue, OK_Ordinary, SourceLocation(),
|
2020-06-26 09:23:45 -07:00
|
|
|
/*CanOverflow*/ false, FPOptionsOverride());
|
2012-09-21 18:33:52 +00:00
|
|
|
}
|
|
|
|
|
2012-09-21 18:13:27 +00:00
|
|
|
ImplicitCastExpr *ASTMaker::makeLvalueToRvalue(const Expr *Arg, QualType Ty) {
|
2017-09-30 00:03:22 +00:00
|
|
|
return makeImplicitCast(Arg, Ty, CK_LValueToRValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
ImplicitCastExpr *
|
|
|
|
ASTMaker::makeLvalueToRvalue(const VarDecl *Arg,
|
2017-10-17 22:28:18 +00:00
|
|
|
bool RefersToEnclosingVariableOrCapture) {
|
|
|
|
QualType Type = Arg->getType().getNonReferenceType();
|
2017-09-30 00:03:22 +00:00
|
|
|
return makeLvalueToRvalue(makeDeclRefExpr(Arg,
|
2017-10-17 22:28:18 +00:00
|
|
|
RefersToEnclosingVariableOrCapture),
|
2017-09-30 00:03:22 +00:00
|
|
|
Type);
|
|
|
|
}
|
|
|
|
|
|
|
|
ImplicitCastExpr *ASTMaker::makeImplicitCast(const Expr *Arg, QualType Ty,
|
|
|
|
CastKind CK) {
|
|
|
|
return ImplicitCastExpr::Create(C, Ty,
|
2020-09-12 21:54:14 +07:00
|
|
|
/* CastKind=*/CK,
|
|
|
|
/* Expr=*/const_cast<Expr *>(Arg),
|
|
|
|
/* CXXCastPath=*/nullptr,
|
2021-06-04 23:15:23 +02:00
|
|
|
/* ExprValueKind=*/VK_PRValue,
|
2020-09-12 21:54:14 +07:00
|
|
|
/* FPFeatures */ FPOptionsOverride());
|
2012-09-21 18:13:27 +00:00
|
|
|
}
|
|
|
|
|
Treat `std::move`, `forward`, etc. as builtins.
This is extended to all `std::` functions that take a reference to a
value and return a reference (or pointer) to that same value: `move`,
`forward`, `move_if_noexcept`, `as_const`, `addressof`, and the
libstdc++-specific function `__addressof`.
We still require these functions to be declared before they can be used,
but don't instantiate their definitions unless their addresses are
taken. Instead, code generation, constant evaluation, and static
analysis are given direct knowledge of their effect.
This change aims to reduce various costs associated with these functions
-- per-instantiation memory costs, compile time and memory costs due to
creating out-of-line copies and inlining them, code size at -O0, and so
on -- so that they are not substantially more expensive than a cast.
Most of these improvements are very small, but I measured a 3% decrease
in -O0 object file size for a simple C++ source file using the standard
library after this change.
We now automatically infer the `const` and `nothrow` attributes on these
now-builtin functions, in particular meaning that we get a warning for
an unused call to one of these functions.
In C++20 onwards, we disallow taking the addresses of these functions,
per the C++20 "addressable function" rule. In earlier language modes, a
compatibility warning is produced but the address can still be taken.
The same infrastructure is extended to the existing MSVC builtin
`__GetExceptionInfo`, which is now only recognized in namespace `std`
like it always should have been.
This is a re-commit of
fc3090109643af8d2da9822d0f99c84742b9c877,
a571f82a50416b767fd3cce0fb5027bb5dfec58c,
64c045e25b8471bbb572bd29159c294a82a86a2, and
de6ddaeef3aaa8a9ae3663c12cdb57d9afc0f906,
and reverts aa643f455a5362de7189eac630050d2c8aefe8f2.
This change also includes a workaround for users using libc++ 3.1 and
earlier (!!), as apparently happens on AIX, where std::move sometimes
returns by value.
Reviewed By: aaron.ballman
Differential Revision: https://reviews.llvm.org/D123345
Revert "Fixup D123950 to address revert of D123345"
This reverts commit aa643f455a5362de7189eac630050d2c8aefe8f2.
2022-04-20 17:13:56 -07:00
|
|
|
CastExpr *ASTMaker::makeReferenceCast(const Expr *Arg, QualType Ty) {
|
|
|
|
assert(Ty->isReferenceType());
|
|
|
|
return CXXStaticCastExpr::Create(
|
|
|
|
C, Ty.getNonReferenceType(),
|
|
|
|
Ty->isLValueReferenceType() ? VK_LValue : VK_XValue, CK_NoOp,
|
|
|
|
const_cast<Expr *>(Arg), /*CXXCastPath=*/nullptr,
|
|
|
|
/*Written=*/C.getTrivialTypeSourceInfo(Ty), FPOptionsOverride(),
|
|
|
|
SourceLocation(), SourceLocation(), SourceRange());
|
|
|
|
}
|
|
|
|
|
2012-10-12 00:18:19 +00:00
|
|
|
Expr *ASTMaker::makeIntegralCast(const Expr *Arg, QualType Ty) {
|
|
|
|
if (Arg->getType() == Ty)
|
|
|
|
return const_cast<Expr*>(Arg);
|
2020-09-12 21:54:14 +07:00
|
|
|
return makeImplicitCast(Arg, Ty, CK_IntegralCast);
|
2012-09-21 18:13:23 +00:00
|
|
|
}
|
|
|
|
|
2012-10-11 20:58:18 +00:00
|
|
|
ImplicitCastExpr *ASTMaker::makeIntegralCastToBoolean(const Expr *Arg) {
|
2020-09-12 21:54:14 +07:00
|
|
|
return makeImplicitCast(Arg, C.BoolTy, CK_IntegralToBoolean);
|
2012-10-11 20:58:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ObjCBoolLiteralExpr *ASTMaker::makeObjCBool(bool Val) {
|
|
|
|
QualType Ty = C.getBOOLDecl() ? C.getBOOLType() : C.ObjCBuiltinBoolTy;
|
|
|
|
return new (C) ObjCBoolLiteralExpr(Val, Ty, SourceLocation());
|
|
|
|
}
|
|
|
|
|
2014-01-10 20:06:06 +00: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-11 20:58:18 +00:00
|
|
|
ReturnStmt *ASTMaker::makeReturn(const Expr *RetVal) {
|
2018-10-30 14:40:49 +00:00
|
|
|
return ReturnStmt::Create(C, SourceLocation(), const_cast<Expr *>(RetVal),
|
|
|
|
/* NRVOCandidate=*/nullptr);
|
2012-10-11 20:58:18 +00:00
|
|
|
}
|
|
|
|
|
2017-11-06 22:12:19 +00: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 00:03:22 +00: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 00:03:45 +00:00
|
|
|
/* TemplateArgumentListInfo=*/ nullptr, MemberDecl->getType(), ValueKind,
|
2019-06-11 17:50:36 +00:00
|
|
|
OK_Ordinary, NOUR_None);
|
2017-09-30 00:03:22 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 20:53:01 +00:00
|
|
|
ValueDecl *ASTMaker::findMemberField(const RecordDecl *RD, StringRef Name) {
|
2017-09-30 00:03:22 +00:00
|
|
|
|
|
|
|
CXXBasePaths Paths(
|
|
|
|
/* FindAmbiguities=*/false,
|
|
|
|
/* RecordPaths=*/false,
|
2017-10-25 00:03:45 +00:00
|
|
|
/* DetectVirtual=*/ false);
|
2017-09-30 00:03:22 +00: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-11 20:53:01 +00:00
|
|
|
return cast<ValueDecl>(FoundDecl);
|
2017-09-30 00:03:22 +00:00
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-09-21 17:54:32 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Creation functions for faux ASTs.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
typedef Stmt *(*FunctionFarmer)(ASTContext &C, const FunctionDecl *D);
|
|
|
|
|
2017-10-02 21:01:46 +00:00
|
|
|
static CallExpr *create_call_once_funcptr_call(ASTContext &C, ASTMaker M,
|
|
|
|
const ParmVarDecl *Callback,
|
|
|
|
ArrayRef<Expr *> CallArgs) {
|
2017-09-30 00:03:22 +00:00
|
|
|
|
2017-10-24 00:13:18 +00:00
|
|
|
QualType Ty = Callback->getType();
|
|
|
|
DeclRefExpr *Call = M.makeDeclRefExpr(Callback);
|
2018-05-16 00:29:13 +00:00
|
|
|
Expr *SubExpr;
|
2017-10-24 00:13:18 +00:00
|
|
|
if (Ty->isRValueReferenceType()) {
|
2018-05-16 00:29:13 +00:00
|
|
|
SubExpr = M.makeImplicitCast(
|
|
|
|
Call, Ty.getNonReferenceType(), CK_LValueToRValue);
|
|
|
|
} else if (Ty->isLValueReferenceType() &&
|
|
|
|
Call->getType()->isFunctionType()) {
|
2017-10-24 00:13:18 +00:00
|
|
|
Ty = C.getPointerType(Ty.getNonReferenceType());
|
2018-05-16 00:29:13 +00:00
|
|
|
SubExpr = M.makeImplicitCast(Call, Ty, CK_FunctionToPointerDecay);
|
|
|
|
} else if (Ty->isLValueReferenceType()
|
|
|
|
&& Call->getType()->isPointerType()
|
|
|
|
&& Call->getType()->getPointeeType()->isFunctionType()){
|
|
|
|
SubExpr = Call;
|
|
|
|
} else {
|
|
|
|
llvm_unreachable("Unexpected state");
|
2017-10-24 00:13:18 +00:00
|
|
|
}
|
|
|
|
|
2021-06-04 23:15:23 +02:00
|
|
|
return CallExpr::Create(C, SubExpr, CallArgs, C.VoidTy, VK_PRValue,
|
2020-07-24 12:04:19 +07:00
|
|
|
SourceLocation(), FPOptionsOverride());
|
2017-09-30 00:03:22 +00:00
|
|
|
}
|
|
|
|
|
2017-10-02 21:01:46 +00:00
|
|
|
static CallExpr *create_call_once_lambda_call(ASTContext &C, ASTMaker M,
|
|
|
|
const ParmVarDecl *Callback,
|
2017-10-20 23:29:59 +00:00
|
|
|
CXXRecordDecl *CallbackDecl,
|
2017-10-02 21:01:46 +00:00
|
|
|
ArrayRef<Expr *> CallArgs) {
|
2017-09-30 00:03:22 +00:00
|
|
|
assert(CallbackDecl != nullptr);
|
|
|
|
assert(CallbackDecl->isLambda());
|
|
|
|
FunctionDecl *callOperatorDecl = CallbackDecl->getLambdaCallOperator();
|
|
|
|
assert(callOperatorDecl != nullptr);
|
|
|
|
|
|
|
|
DeclRefExpr *callOperatorDeclRef =
|
2017-10-25 00:03:45 +00:00
|
|
|
DeclRefExpr::Create(/* Ctx =*/ C,
|
|
|
|
/* QualifierLoc =*/ NestedNameSpecifierLoc(),
|
|
|
|
/* TemplateKWLoc =*/ SourceLocation(),
|
2017-09-30 00:03:22 +00:00
|
|
|
const_cast<FunctionDecl *>(callOperatorDecl),
|
2017-10-25 00:03:45 +00:00
|
|
|
/* RefersToEnclosingVariableOrCapture=*/ false,
|
|
|
|
/* NameLoc =*/ SourceLocation(),
|
|
|
|
/* T =*/ callOperatorDecl->getType(),
|
|
|
|
/* VK =*/ VK_LValue);
|
2017-09-30 00:03:22 +00:00
|
|
|
|
2018-12-21 15:20:32 +00:00
|
|
|
return CXXOperatorCallExpr::Create(
|
|
|
|
/*AstContext=*/C, OO_Call, callOperatorDeclRef,
|
2019-07-16 04:46:31 +00:00
|
|
|
/*Args=*/CallArgs,
|
2018-12-21 15:20:32 +00:00
|
|
|
/*QualType=*/C.VoidTy,
|
2021-06-04 23:15:23 +02:00
|
|
|
/*ExprValueType=*/VK_PRValue,
|
2020-04-10 13:34:46 -07:00
|
|
|
/*SourceLocation=*/SourceLocation(),
|
2020-06-26 09:23:45 -07:00
|
|
|
/*FPFeatures=*/FPOptionsOverride());
|
2017-09-30 00:03:22 +00:00
|
|
|
}
|
|
|
|
|
Treat `std::move`, `forward`, etc. as builtins.
This is extended to all `std::` functions that take a reference to a
value and return a reference (or pointer) to that same value: `move`,
`forward`, `move_if_noexcept`, `as_const`, `addressof`, and the
libstdc++-specific function `__addressof`.
We still require these functions to be declared before they can be used,
but don't instantiate their definitions unless their addresses are
taken. Instead, code generation, constant evaluation, and static
analysis are given direct knowledge of their effect.
This change aims to reduce various costs associated with these functions
-- per-instantiation memory costs, compile time and memory costs due to
creating out-of-line copies and inlining them, code size at -O0, and so
on -- so that they are not substantially more expensive than a cast.
Most of these improvements are very small, but I measured a 3% decrease
in -O0 object file size for a simple C++ source file using the standard
library after this change.
We now automatically infer the `const` and `nothrow` attributes on these
now-builtin functions, in particular meaning that we get a warning for
an unused call to one of these functions.
In C++20 onwards, we disallow taking the addresses of these functions,
per the C++20 "addressable function" rule. In earlier language modes, a
compatibility warning is produced but the address can still be taken.
The same infrastructure is extended to the existing MSVC builtin
`__GetExceptionInfo`, which is now only recognized in namespace `std`
like it always should have been.
This is a re-commit of
fc3090109643af8d2da9822d0f99c84742b9c877,
a571f82a50416b767fd3cce0fb5027bb5dfec58c,
64c045e25b8471bbb572bd29159c294a82a86a2, and
de6ddaeef3aaa8a9ae3663c12cdb57d9afc0f906,
and reverts aa643f455a5362de7189eac630050d2c8aefe8f2.
This change also includes a workaround for users using libc++ 3.1 and
earlier (!!), as apparently happens on AIX, where std::move sometimes
returns by value.
Reviewed By: aaron.ballman
Differential Revision: https://reviews.llvm.org/D123345
Revert "Fixup D123950 to address revert of D123345"
This reverts commit aa643f455a5362de7189eac630050d2c8aefe8f2.
2022-04-20 17:13:56 -07:00
|
|
|
/// Create a fake body for 'std::move' or 'std::forward'. This is just:
|
|
|
|
///
|
|
|
|
/// \code
|
|
|
|
/// return static_cast<return_type>(param);
|
|
|
|
/// \endcode
|
|
|
|
static Stmt *create_std_move_forward(ASTContext &C, const FunctionDecl *D) {
|
|
|
|
LLVM_DEBUG(llvm::dbgs() << "Generating body for std::move / std::forward\n");
|
|
|
|
|
|
|
|
ASTMaker M(C);
|
|
|
|
|
|
|
|
QualType ReturnType = D->getType()->castAs<FunctionType>()->getReturnType();
|
|
|
|
Expr *Param = M.makeDeclRefExpr(D->getParamDecl(0));
|
|
|
|
Expr *Cast = M.makeReferenceCast(Param, ReturnType);
|
|
|
|
return M.makeReturn(Cast);
|
|
|
|
}
|
|
|
|
|
2017-09-30 00:03:22 +00:00
|
|
|
/// 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) {
|
2018-05-15 13:30:56 +00:00
|
|
|
LLVM_DEBUG(llvm::dbgs() << "Generating body for call_once\n");
|
2017-09-30 00:03:22 +00:00
|
|
|
|
|
|
|
// 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 00:36:03 +00: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 00:03:22 +00:00
|
|
|
QualType CallbackType = Callback->getType().getNonReferenceType();
|
2017-10-20 23:29:59 +00:00
|
|
|
|
|
|
|
// Nullable pointer, non-null iff function is a CXXRecordDecl.
|
|
|
|
CXXRecordDecl *CallbackRecordDecl = CallbackType->getAsCXXRecordDecl();
|
2017-10-09 23:20:46 +00:00
|
|
|
QualType FlagType = Flag->getType().getNonReferenceType();
|
2018-07-28 02:16:13 +00:00
|
|
|
auto *FlagRecordDecl = FlagType->getAsRecordDecl();
|
2017-10-11 20:53:01 +00:00
|
|
|
|
|
|
|
if (!FlagRecordDecl) {
|
2018-05-15 13:30:56 +00:00
|
|
|
LLVM_DEBUG(llvm::dbgs() << "Flag field is not a record: "
|
|
|
|
<< "unknown std::call_once implementation, "
|
|
|
|
<< "ignoring the call.\n");
|
2017-10-09 23:20:46 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-10-11 20:53:01 +00: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) {
|
2018-05-15 13:30:56 +00:00
|
|
|
LLVM_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-09 23:20:46 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2017-09-30 00:03:22 +00:00
|
|
|
|
2017-10-20 23:29:59 +00:00
|
|
|
bool isLambdaCall = CallbackRecordDecl && CallbackRecordDecl->isLambda();
|
|
|
|
if (CallbackRecordDecl && !isLambdaCall) {
|
2018-05-15 13:30:56 +00:00
|
|
|
LLVM_DEBUG(llvm::dbgs()
|
|
|
|
<< "Not supported: synthesizing body for functors when "
|
|
|
|
<< "body farming std::call_once, ignoring the call.");
|
2017-10-20 23:29:59 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2017-10-02 21:01:46 +00:00
|
|
|
|
2017-09-30 00:03:22 +00:00
|
|
|
SmallVector<Expr *, 5> CallArgs;
|
2017-10-20 23:29:59 +00:00
|
|
|
const FunctionProtoType *CallbackFunctionType;
|
|
|
|
if (isLambdaCall) {
|
2017-09-30 00:03:22 +00:00
|
|
|
|
2017-10-02 21:01:46 +00:00
|
|
|
// Lambda requires callback itself inserted as a first parameter.
|
|
|
|
CallArgs.push_back(
|
|
|
|
M.makeDeclRefExpr(Callback,
|
2017-10-25 00:03:45 +00:00
|
|
|
/* RefersToEnclosingVariableOrCapture=*/ true));
|
2017-10-20 23:29:59 +00:00
|
|
|
CallbackFunctionType = CallbackRecordDecl->getLambdaCallOperator()
|
|
|
|
->getType()
|
|
|
|
->getAs<FunctionProtoType>();
|
2017-10-24 00:13:18 +00:00
|
|
|
} else if (!CallbackType->getPointeeType().isNull()) {
|
2017-10-20 23:29:59 +00:00
|
|
|
CallbackFunctionType =
|
|
|
|
CallbackType->getPointeeType()->getAs<FunctionProtoType>();
|
2017-10-24 00:13:18 +00:00
|
|
|
} else {
|
|
|
|
CallbackFunctionType = CallbackType->getAs<FunctionProtoType>();
|
2017-10-20 23:29:59 +00:00
|
|
|
}
|
2017-10-02 21:01:46 +00:00
|
|
|
|
2017-10-20 23:29:59 +00:00
|
|
|
if (!CallbackFunctionType)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// First two arguments are used for the flag and for the callback.
|
|
|
|
if (D->getNumParams() != CallbackFunctionType->getNumParams() + 2) {
|
2018-05-15 13:30:56 +00:00
|
|
|
LLVM_DEBUG(llvm::dbgs() << "Types of params of the callback do not match "
|
|
|
|
<< "params passed to std::call_once, "
|
|
|
|
<< "ignoring the call\n");
|
2017-10-20 23:29:59 +00: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);
|
2019-08-28 21:19:58 +00:00
|
|
|
assert(PDecl);
|
|
|
|
if (CallbackFunctionType->getParamType(ParamIdx - 2)
|
2018-02-02 01:44:07 +00:00
|
|
|
.getNonReferenceType()
|
|
|
|
.getCanonicalType() !=
|
|
|
|
PDecl->getType().getNonReferenceType().getCanonicalType()) {
|
2018-05-15 13:30:56 +00:00
|
|
|
LLVM_DEBUG(llvm::dbgs() << "Types of params of the callback do not match "
|
|
|
|
<< "params passed to std::call_once, "
|
|
|
|
<< "ignoring the call\n");
|
2018-02-02 01:44:07 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2017-10-20 23:29:59 +00:00
|
|
|
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 00:03:22 +00:00
|
|
|
|
|
|
|
CallExpr *CallbackCall;
|
2017-10-02 21:01:46 +00:00
|
|
|
if (isLambdaCall) {
|
2017-09-30 00:03:22 +00:00
|
|
|
|
2017-10-20 23:29:59 +00:00
|
|
|
CallbackCall = create_call_once_lambda_call(C, M, Callback,
|
|
|
|
CallbackRecordDecl, CallArgs);
|
2017-09-30 00:03:22 +00:00
|
|
|
} else {
|
|
|
|
|
|
|
|
// Function pointer case.
|
|
|
|
CallbackCall = create_call_once_funcptr_call(C, M, Callback, CallArgs);
|
|
|
|
}
|
|
|
|
|
|
|
|
DeclRefExpr *FlagDecl =
|
|
|
|
M.makeDeclRefExpr(Flag,
|
2017-10-17 22:28:18 +00:00
|
|
|
/* RefersToEnclosingVariableOrCapture=*/true);
|
2017-09-30 00:03:22 +00:00
|
|
|
|
|
|
|
|
2017-10-11 20:53:01 +00:00
|
|
|
MemberExpr *Deref = M.makeMemberExpression(FlagDecl, FlagFieldDecl);
|
2017-09-30 00:03:22 +00:00
|
|
|
assert(Deref->isLValue());
|
|
|
|
QualType DerefType = Deref->getType();
|
|
|
|
|
|
|
|
// Negation predicate.
|
2020-05-01 10:32:06 -07:00
|
|
|
UnaryOperator *FlagCheck = UnaryOperator::Create(
|
|
|
|
C,
|
2017-10-25 00:03:45 +00:00
|
|
|
/* input=*/
|
2017-09-30 00:03:22 +00:00
|
|
|
M.makeImplicitCast(M.makeLvalueToRvalue(Deref, DerefType), DerefType,
|
|
|
|
CK_IntegralToBoolean),
|
2020-05-01 10:32:06 -07:00
|
|
|
/* opc=*/UO_LNot,
|
|
|
|
/* QualType=*/C.IntTy,
|
2021-06-04 23:15:23 +02:00
|
|
|
/* ExprValueKind=*/VK_PRValue,
|
2020-05-01 10:32:06 -07:00
|
|
|
/* ExprObjectKind=*/OK_Ordinary, SourceLocation(),
|
2020-06-26 09:23:45 -07:00
|
|
|
/* CanOverflow*/ false, FPOptionsOverride());
|
2017-09-30 00:03:22 +00:00
|
|
|
|
|
|
|
// Create assignment.
|
|
|
|
BinaryOperator *FlagAssignment = M.makeAssignment(
|
2017-11-06 22:12:19 +00:00
|
|
|
Deref, M.makeIntegralCast(M.makeIntegerLiteral(1, C.IntTy), DerefType),
|
|
|
|
DerefType);
|
2017-09-30 00:03:22 +00:00
|
|
|
|
2018-10-27 21:12:20 +00:00
|
|
|
auto *Out =
|
2021-10-05 08:02:53 -04:00
|
|
|
IfStmt::Create(C, SourceLocation(), IfStatementKind::Ordinary,
|
2019-07-16 04:46:31 +00:00
|
|
|
/* Init=*/nullptr,
|
|
|
|
/* Var=*/nullptr,
|
|
|
|
/* Cond=*/FlagCheck,
|
2020-08-10 16:29:33 -07:00
|
|
|
/* LPL=*/SourceLocation(),
|
|
|
|
/* RPL=*/SourceLocation(),
|
2019-07-16 04:46:31 +00:00
|
|
|
/* Then=*/M.makeCompound({CallbackCall, FlagAssignment}));
|
2017-09-30 00:03:22 +00:00
|
|
|
|
|
|
|
return Out;
|
|
|
|
}
|
|
|
|
|
2012-09-21 00:52:24 +00: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 04:30:07 +00:00
|
|
|
return nullptr;
|
2012-09-21 00:52:24 +00: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 04:30:07 +00:00
|
|
|
return nullptr;
|
2012-09-21 00:52:24 +00:00
|
|
|
QualType PredicateTy = PredicatePtrTy->getPointeeType();
|
|
|
|
if (!PredicateTy->isIntegerType())
|
2014-05-20 04:30:07 +00:00
|
|
|
return nullptr;
|
|
|
|
|
2012-09-21 00:52:24 +00: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 04:30:07 +00:00
|
|
|
return nullptr;
|
|
|
|
|
2012-09-21 00:52:24 +00: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-06 22:12:19 +00:00
|
|
|
// if (*predicate != ~0l) {
|
|
|
|
// *predicate = ~0l;
|
2012-09-21 00:52:24 +00:00
|
|
|
// block();
|
|
|
|
// }
|
|
|
|
// }
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2012-09-21 17:54:35 +00:00
|
|
|
ASTMaker M(C);
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2012-09-21 00:52:24 +00:00
|
|
|
// (1) Create the call.
|
2018-12-21 15:20:32 +00:00
|
|
|
CallExpr *CE = CallExpr::Create(
|
2017-09-30 00:03:22 +00:00
|
|
|
/*ASTContext=*/C,
|
|
|
|
/*StmtClass=*/M.makeLvalueToRvalue(/*Expr=*/Block),
|
2022-12-03 11:34:25 -08:00
|
|
|
/*Args=*/std::nullopt,
|
2017-09-30 00:03:22 +00:00
|
|
|
/*QualType=*/C.VoidTy,
|
2021-06-04 23:15:23 +02:00
|
|
|
/*ExprValueType=*/VK_PRValue,
|
2020-07-24 12:04:19 +07:00
|
|
|
/*SourceLocation=*/SourceLocation(), FPOptionsOverride());
|
2012-09-21 00:52:24 +00:00
|
|
|
|
|
|
|
// (2) Create the assignment to the predicate.
|
2017-11-06 22:12:19 +00:00
|
|
|
Expr *DoneValue =
|
2020-05-01 10:32:06 -07:00
|
|
|
UnaryOperator::Create(C, M.makeIntegerLiteral(0, C.LongTy), UO_Not,
|
2021-06-04 23:15:23 +02:00
|
|
|
C.LongTy, VK_PRValue, OK_Ordinary, SourceLocation(),
|
2020-06-26 09:23:45 -07:00
|
|
|
/*CanOverflow*/ false, FPOptionsOverride());
|
2017-09-30 00:03:22 +00:00
|
|
|
|
2012-09-21 18:33:56 +00:00
|
|
|
BinaryOperator *B =
|
|
|
|
M.makeAssignment(
|
|
|
|
M.makeDereference(
|
|
|
|
M.makeLvalueToRvalue(
|
|
|
|
M.makeDeclRefExpr(Predicate), PredicateQPtrTy),
|
|
|
|
PredicateTy),
|
2017-11-06 22:12:19 +00:00
|
|
|
M.makeIntegralCast(DoneValue, PredicateTy),
|
2012-09-21 18:33:56 +00:00
|
|
|
PredicateTy);
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2012-09-21 00:52:24 +00:00
|
|
|
// (3) Create the compound statement.
|
2014-08-27 06:28:36 +00:00
|
|
|
Stmt *Stmts[] = { B, CE };
|
|
|
|
CompoundStmt *CS = M.makeCompound(Stmts);
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2012-09-21 00:52:24 +00:00
|
|
|
// (4) Create the 'if' condition.
|
2012-09-21 18:33:56 +00:00
|
|
|
ImplicitCastExpr *LValToRval =
|
|
|
|
M.makeLvalueToRvalue(
|
|
|
|
M.makeDereference(
|
|
|
|
M.makeLvalueToRvalue(
|
|
|
|
M.makeDeclRefExpr(Predicate),
|
|
|
|
PredicateQPtrTy),
|
|
|
|
PredicateTy),
|
|
|
|
PredicateTy);
|
2017-11-06 22:12:19 +00:00
|
|
|
|
|
|
|
Expr *GuardCondition = M.makeComparison(LValToRval, DoneValue, BO_NE);
|
2012-09-21 00:52:24 +00:00
|
|
|
// (5) Create the 'if' statement.
|
2021-10-05 08:02:53 -04:00
|
|
|
auto *If = IfStmt::Create(C, SourceLocation(), IfStatementKind::Ordinary,
|
2019-07-16 04:46:31 +00:00
|
|
|
/* Init=*/nullptr,
|
|
|
|
/* Var=*/nullptr,
|
|
|
|
/* Cond=*/GuardCondition,
|
2020-08-10 16:29:33 -07:00
|
|
|
/* LPL=*/SourceLocation(),
|
|
|
|
/* RPL=*/SourceLocation(),
|
2019-07-16 04:46:31 +00:00
|
|
|
/* Then=*/CS);
|
2012-09-21 00:52:24 +00:00
|
|
|
return If;
|
|
|
|
}
|
|
|
|
|
2012-09-21 00:09:11 +00: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 04:30:07 +00:00
|
|
|
return nullptr;
|
|
|
|
|
2012-09-21 00:09:11 +00:00
|
|
|
// Check if the second parameter is a block.
|
|
|
|
const ParmVarDecl *PV = D->getParamDecl(1);
|
|
|
|
QualType Ty = PV->getType();
|
2012-09-21 00:52:24 +00:00
|
|
|
if (!isDispatchBlock(Ty))
|
2014-05-20 04:30:07 +00:00
|
|
|
return nullptr;
|
|
|
|
|
2012-09-21 00:09:11 +00: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();
|
|
|
|
// }
|
2018-07-30 19:24:48 +00:00
|
|
|
//
|
2012-09-21 17:54:35 +00:00
|
|
|
ASTMaker M(C);
|
|
|
|
DeclRefExpr *DR = M.makeDeclRefExpr(PV);
|
2012-09-21 18:33:52 +00:00
|
|
|
ImplicitCastExpr *ICE = M.makeLvalueToRvalue(DR, Ty);
|
2022-12-03 11:34:25 -08:00
|
|
|
CallExpr *CE = CallExpr::Create(C, ICE, std::nullopt, C.VoidTy, VK_PRValue,
|
2020-07-24 12:04:19 +07:00
|
|
|
SourceLocation(), FPOptionsOverride());
|
2012-09-21 00:09:11 +00:00
|
|
|
return CE;
|
|
|
|
}
|
|
|
|
|
2012-10-11 20:58:18 +00:00
|
|
|
static Stmt *create_OSAtomicCompareAndSwap(ASTContext &C, const FunctionDecl *D)
|
|
|
|
{
|
|
|
|
// There are exactly 3 arguments.
|
|
|
|
if (D->param_size() != 3)
|
2014-05-20 04:30:07 +00:00
|
|
|
return nullptr;
|
|
|
|
|
2013-02-05 19:52:26 +00:00
|
|
|
// Signature:
|
|
|
|
// _Bool OSAtomicCompareAndSwapPtr(void *__oldValue,
|
|
|
|
// void *__newValue,
|
|
|
|
// void * volatile *__theValue)
|
|
|
|
// Generate body:
|
2012-10-11 20:58:18 +00:00
|
|
|
// if (oldValue == *theValue) {
|
|
|
|
// *theValue = newValue;
|
|
|
|
// return YES;
|
|
|
|
// }
|
|
|
|
// else return NO;
|
2014-01-25 16:55:45 +00:00
|
|
|
|
|
|
|
QualType ResultTy = D->getReturnType();
|
2012-10-12 00:18:19 +00:00
|
|
|
bool isBoolean = ResultTy->isBooleanType();
|
|
|
|
if (!isBoolean && !ResultTy->isIntegralType(C))
|
2014-05-20 04:30:07 +00:00
|
|
|
return nullptr;
|
|
|
|
|
2012-10-11 20:58:18 +00:00
|
|
|
const ParmVarDecl *OldValue = D->getParamDecl(0);
|
|
|
|
QualType OldValueTy = OldValue->getType();
|
|
|
|
|
|
|
|
const ParmVarDecl *NewValue = D->getParamDecl(1);
|
|
|
|
QualType NewValueTy = NewValue->getType();
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2012-10-11 20:58:18 +00:00
|
|
|
assert(OldValueTy == NewValueTy);
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2012-10-11 20:58:18 +00:00
|
|
|
const ParmVarDecl *TheValue = D->getParamDecl(2);
|
|
|
|
QualType TheValueTy = TheValue->getType();
|
|
|
|
const PointerType *PT = TheValueTy->getAs<PointerType>();
|
|
|
|
if (!PT)
|
2014-05-20 04:30:07 +00:00
|
|
|
return nullptr;
|
2012-10-11 20:58:18 +00:00
|
|
|
QualType PointeeTy = PT->getPointeeType();
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2012-10-11 20:58:18 +00:00
|
|
|
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);
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2012-10-12 00:18:19 +00:00
|
|
|
Expr *BoolVal = M.makeObjCBool(true);
|
|
|
|
Expr *RetVal = isBoolean ? M.makeIntegralCastToBoolean(BoolVal)
|
|
|
|
: M.makeIntegralCast(BoolVal, ResultTy);
|
|
|
|
Stmts[1] = M.makeReturn(RetVal);
|
2014-08-27 06:28:36 +00:00
|
|
|
CompoundStmt *Body = M.makeCompound(Stmts);
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2012-10-11 20:58:18 +00:00
|
|
|
// Construct the else clause.
|
2012-10-12 00:18:19 +00:00
|
|
|
BoolVal = M.makeObjCBool(false);
|
|
|
|
RetVal = isBoolean ? M.makeIntegralCastToBoolean(BoolVal)
|
|
|
|
: M.makeIntegralCast(BoolVal, ResultTy);
|
|
|
|
Stmt *Else = M.makeReturn(RetVal);
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2012-10-11 20:58:18 +00:00
|
|
|
/// Construct the If.
|
2020-08-10 16:29:33 -07:00
|
|
|
auto *If =
|
2021-10-05 08:02:53 -04:00
|
|
|
IfStmt::Create(C, SourceLocation(), IfStatementKind::Ordinary,
|
2020-08-10 16:29:33 -07:00
|
|
|
/* Init=*/nullptr,
|
|
|
|
/* Var=*/nullptr, Comparison,
|
|
|
|
/* LPL=*/SourceLocation(),
|
|
|
|
/* RPL=*/SourceLocation(), Body, SourceLocation(), Else);
|
2014-05-20 04:30:07 +00:00
|
|
|
|
2018-07-30 19:24:48 +00:00
|
|
|
return If;
|
2012-10-11 20:58:18 +00:00
|
|
|
}
|
|
|
|
|
2012-09-21 00:09:11 +00:00
|
|
|
Stmt *BodyFarm::getBody(const FunctionDecl *D) {
|
2023-01-14 12:31:01 -08:00
|
|
|
std::optional<Stmt *> &Val = Bodies[D];
|
2022-06-25 22:26:24 -07:00
|
|
|
if (Val)
|
2022-12-17 06:37:59 +00:00
|
|
|
return *Val;
|
2014-05-20 04:30:07 +00:00
|
|
|
|
|
|
|
Val = nullptr;
|
|
|
|
|
|
|
|
if (D->getIdentifier() == nullptr)
|
|
|
|
return nullptr;
|
2012-09-21 00:09:11 +00:00
|
|
|
|
|
|
|
StringRef Name = D->getName();
|
|
|
|
if (Name.empty())
|
2014-05-20 04:30:07 +00:00
|
|
|
return nullptr;
|
2012-10-11 20:58:18 +00:00
|
|
|
|
|
|
|
FunctionFarmer FF;
|
|
|
|
|
Treat `std::move`, `forward`, etc. as builtins.
This is extended to all `std::` functions that take a reference to a
value and return a reference (or pointer) to that same value: `move`,
`forward`, `move_if_noexcept`, `as_const`, `addressof`, and the
libstdc++-specific function `__addressof`.
We still require these functions to be declared before they can be used,
but don't instantiate their definitions unless their addresses are
taken. Instead, code generation, constant evaluation, and static
analysis are given direct knowledge of their effect.
This change aims to reduce various costs associated with these functions
-- per-instantiation memory costs, compile time and memory costs due to
creating out-of-line copies and inlining them, code size at -O0, and so
on -- so that they are not substantially more expensive than a cast.
Most of these improvements are very small, but I measured a 3% decrease
in -O0 object file size for a simple C++ source file using the standard
library after this change.
We now automatically infer the `const` and `nothrow` attributes on these
now-builtin functions, in particular meaning that we get a warning for
an unused call to one of these functions.
In C++20 onwards, we disallow taking the addresses of these functions,
per the C++20 "addressable function" rule. In earlier language modes, a
compatibility warning is produced but the address can still be taken.
The same infrastructure is extended to the existing MSVC builtin
`__GetExceptionInfo`, which is now only recognized in namespace `std`
like it always should have been.
This is a re-commit of
fc3090109643af8d2da9822d0f99c84742b9c877,
a571f82a50416b767fd3cce0fb5027bb5dfec58c,
64c045e25b8471bbb572bd29159c294a82a86a2, and
de6ddaeef3aaa8a9ae3663c12cdb57d9afc0f906,
and reverts aa643f455a5362de7189eac630050d2c8aefe8f2.
This change also includes a workaround for users using libc++ 3.1 and
earlier (!!), as apparently happens on AIX, where std::move sometimes
returns by value.
Reviewed By: aaron.ballman
Differential Revision: https://reviews.llvm.org/D123345
Revert "Fixup D123950 to address revert of D123345"
This reverts commit aa643f455a5362de7189eac630050d2c8aefe8f2.
2022-04-20 17:13:56 -07:00
|
|
|
if (unsigned BuiltinID = D->getBuiltinID()) {
|
|
|
|
switch (BuiltinID) {
|
|
|
|
case Builtin::BIas_const:
|
|
|
|
case Builtin::BIforward:
|
2023-01-29 00:04:51 +00:00
|
|
|
case Builtin::BIforward_like:
|
Treat `std::move`, `forward`, etc. as builtins.
This is extended to all `std::` functions that take a reference to a
value and return a reference (or pointer) to that same value: `move`,
`forward`, `move_if_noexcept`, `as_const`, `addressof`, and the
libstdc++-specific function `__addressof`.
We still require these functions to be declared before they can be used,
but don't instantiate their definitions unless their addresses are
taken. Instead, code generation, constant evaluation, and static
analysis are given direct knowledge of their effect.
This change aims to reduce various costs associated with these functions
-- per-instantiation memory costs, compile time and memory costs due to
creating out-of-line copies and inlining them, code size at -O0, and so
on -- so that they are not substantially more expensive than a cast.
Most of these improvements are very small, but I measured a 3% decrease
in -O0 object file size for a simple C++ source file using the standard
library after this change.
We now automatically infer the `const` and `nothrow` attributes on these
now-builtin functions, in particular meaning that we get a warning for
an unused call to one of these functions.
In C++20 onwards, we disallow taking the addresses of these functions,
per the C++20 "addressable function" rule. In earlier language modes, a
compatibility warning is produced but the address can still be taken.
The same infrastructure is extended to the existing MSVC builtin
`__GetExceptionInfo`, which is now only recognized in namespace `std`
like it always should have been.
This is a re-commit of
fc3090109643af8d2da9822d0f99c84742b9c877,
a571f82a50416b767fd3cce0fb5027bb5dfec58c,
64c045e25b8471bbb572bd29159c294a82a86a2, and
de6ddaeef3aaa8a9ae3663c12cdb57d9afc0f906,
and reverts aa643f455a5362de7189eac630050d2c8aefe8f2.
This change also includes a workaround for users using libc++ 3.1 and
earlier (!!), as apparently happens on AIX, where std::move sometimes
returns by value.
Reviewed By: aaron.ballman
Differential Revision: https://reviews.llvm.org/D123345
Revert "Fixup D123950 to address revert of D123345"
This reverts commit aa643f455a5362de7189eac630050d2c8aefe8f2.
2022-04-20 17:13:56 -07:00
|
|
|
case Builtin::BImove:
|
|
|
|
case Builtin::BImove_if_noexcept:
|
|
|
|
FF = create_std_move_forward;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
FF = nullptr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (Name.startswith("OSAtomicCompareAndSwap") ||
|
|
|
|
Name.startswith("objc_atomicCompareAndSwap")) {
|
2012-10-11 20:58:18 +00:00
|
|
|
FF = create_OSAtomicCompareAndSwap;
|
2017-09-30 00:03:22 +00:00
|
|
|
} else if (Name == "call_once" && D->getDeclContext()->isStdNamespace()) {
|
|
|
|
FF = create_call_once;
|
|
|
|
} else {
|
2012-10-11 20:58:18 +00:00
|
|
|
FF = llvm::StringSwitch<FunctionFarmer>(Name)
|
|
|
|
.Case("dispatch_sync", create_dispatch_sync)
|
|
|
|
.Case("dispatch_once", create_dispatch_once)
|
2014-05-20 04:30:07 +00:00
|
|
|
.Default(nullptr);
|
2012-09-21 00:09:11 +00:00
|
|
|
}
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2012-10-11 20:58:18 +00: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 15:14:15 +00:00
|
|
|
else if (Injector) { Val = Injector->getBody(D); }
|
2022-06-20 22:59:26 -07:00
|
|
|
return *Val;
|
2012-09-21 00:09:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 23:58:48 +00: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-28 18:49:28 +00:00
|
|
|
Prop->getIdentifier(), Prop->getQueryKind());
|
2016-01-26 23:58:48 +00:00
|
|
|
if (ShadowingProp && ShadowingProp != Prop) {
|
|
|
|
IVar = ShadowingProp->getPropertyIvarDecl();
|
|
|
|
}
|
|
|
|
|
|
|
|
return IVar;
|
|
|
|
}
|
|
|
|
|
2014-01-10 20:06:06 +00:00
|
|
|
static Stmt *createObjCPropertyGetter(ASTContext &Ctx,
|
2019-11-21 18:13:48 -08:00
|
|
|
const ObjCMethodDecl *MD) {
|
2021-03-23 18:48:58 +03:00
|
|
|
// First, find the backing ivar.
|
2019-11-21 18:13:48 -08:00
|
|
|
const ObjCIvarDecl *IVar = nullptr;
|
2021-03-23 18:48:58 +03:00
|
|
|
const ObjCPropertyDecl *Prop = nullptr;
|
2019-11-21 18:13:48 -08:00
|
|
|
|
2019-12-04 16:26:16 -08:00
|
|
|
// Property accessor stubs sometimes do not correspond to any property decl
|
|
|
|
// in the current interface (but in a superclass). They still have a
|
|
|
|
// corresponding property impl decl in this case.
|
2019-11-21 18:13:48 -08:00
|
|
|
if (MD->isSynthesizedAccessorStub()) {
|
|
|
|
const ObjCInterfaceDecl *IntD = MD->getClassInterface();
|
|
|
|
const ObjCImplementationDecl *ImpD = IntD->getImplementation();
|
2021-03-23 18:48:58 +03:00
|
|
|
for (const auto *PI : ImpD->property_impls()) {
|
|
|
|
if (const ObjCPropertyDecl *Candidate = PI->getPropertyDecl()) {
|
|
|
|
if (Candidate->getGetterName() == MD->getSelector()) {
|
|
|
|
Prop = Candidate;
|
|
|
|
IVar = Prop->getPropertyIvarDecl();
|
|
|
|
}
|
2019-12-04 16:26:16 -08:00
|
|
|
}
|
2019-11-21 18:13:48 -08:00
|
|
|
}
|
|
|
|
}
|
2014-01-23 03:59:10 +00:00
|
|
|
|
2019-11-21 18:13:48 -08:00
|
|
|
if (!IVar) {
|
2021-03-23 18:48:58 +03:00
|
|
|
Prop = MD->findPropertyDecl();
|
2019-11-21 18:13:48 -08:00
|
|
|
IVar = findBackingIvar(Prop);
|
2021-03-23 18:48:58 +03:00
|
|
|
}
|
2019-11-21 18:13:48 -08:00
|
|
|
|
2021-03-23 18:48:58 +03:00
|
|
|
if (!IVar || !Prop)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// Ignore weak variables, which have special behavior.
|
|
|
|
if (Prop->getPropertyAttributes() & ObjCPropertyAttribute::kind_weak)
|
|
|
|
return nullptr;
|
2014-01-14 17:29:06 +00:00
|
|
|
|
2021-03-23 18:48:58 +03: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).
|
|
|
|
const ObjCImplementationDecl *ImplDecl =
|
2019-11-21 18:13:48 -08:00
|
|
|
IVar->getContainingInterface()->getImplementation();
|
2021-03-23 18:48:58 +03:00
|
|
|
if (ImplDecl) {
|
|
|
|
for (const auto *I : ImplDecl->property_impls()) {
|
|
|
|
if (I->getPropertyDecl() != Prop)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (I->getGetterCXXConstructor()) {
|
|
|
|
ASTMaker M(Ctx);
|
|
|
|
return M.makeReturn(I->getGetterCXXConstructor());
|
2014-01-14 17:29:06 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-21 18:13:48 -08:00
|
|
|
}
|
2014-01-10 20:06:06 +00:00
|
|
|
|
2021-11-19 14:50:09 -05:00
|
|
|
// We expect 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.
|
2021-03-23 18:48:58 +03:00
|
|
|
if (!Ctx.hasSameUnqualifiedType(IVar->getType(),
|
|
|
|
Prop->getType().getNonReferenceType()))
|
|
|
|
return nullptr;
|
|
|
|
if (!IVar->getType()->isObjCLifetimeType() &&
|
|
|
|
!IVar->getType().isTriviallyCopyableType(Ctx))
|
|
|
|
return nullptr;
|
|
|
|
|
2014-01-23 03:59:10 +00:00
|
|
|
// Generate our body:
|
|
|
|
// return self->_ivar;
|
2014-01-10 20:06:06 +00:00
|
|
|
ASTMaker M(Ctx);
|
|
|
|
|
2019-11-21 18:13:48 -08:00
|
|
|
const VarDecl *selfVar = MD->getSelfDecl();
|
2017-01-11 01:02:34 +00:00
|
|
|
if (!selfVar)
|
|
|
|
return nullptr;
|
2014-01-10 20:06:06 +00:00
|
|
|
|
2021-03-23 18:48:58 +03:00
|
|
|
Expr *loadedIVar = M.makeObjCIvarRef(
|
|
|
|
M.makeLvalueToRvalue(M.makeDeclRefExpr(selfVar), selfVar->getType()),
|
2014-01-10 20:06:06 +00:00
|
|
|
IVar);
|
|
|
|
|
2019-11-21 18:13:48 -08:00
|
|
|
if (!MD->getReturnType()->isReferenceType())
|
2014-01-10 20:06:06 +00:00
|
|
|
loadedIVar = M.makeLvalueToRvalue(loadedIVar, IVar->getType());
|
|
|
|
|
|
|
|
return M.makeReturn(loadedIVar);
|
|
|
|
}
|
|
|
|
|
2014-01-23 03:59:10 +00:00
|
|
|
Stmt *BodyFarm::getBody(const ObjCMethodDecl *D) {
|
|
|
|
// We currently only know how to synthesize property accessors.
|
2014-01-10 20:06:06 +00:00
|
|
|
if (!D->isPropertyAccessor())
|
2014-05-20 04:30:07 +00:00
|
|
|
return nullptr;
|
2014-01-10 20:06:06 +00:00
|
|
|
|
|
|
|
D = D->getCanonicalDecl();
|
|
|
|
|
2019-01-18 22:52:13 +00:00
|
|
|
// We should not try to synthesize explicitly redefined accessors.
|
|
|
|
// We do not know for sure how they behave.
|
|
|
|
if (!D->isImplicit())
|
|
|
|
return nullptr;
|
|
|
|
|
2023-01-14 12:31:01 -08:00
|
|
|
std::optional<Stmt *> &Val = Bodies[D];
|
2022-06-25 22:26:24 -07:00
|
|
|
if (Val)
|
2022-12-17 06:37:59 +00:00
|
|
|
return *Val;
|
2014-05-20 04:30:07 +00:00
|
|
|
Val = nullptr;
|
2014-01-10 20:06:06 +00:00
|
|
|
|
2014-01-23 03:59:10 +00:00
|
|
|
// For now, we only synthesize getters.
|
2016-02-18 19:37:39 +00: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-10 20:06:06 +00:00
|
|
|
if (D->param_size() != 0)
|
2014-05-20 04:30:07 +00:00
|
|
|
return nullptr;
|
2014-01-10 20:06:06 +00:00
|
|
|
|
2019-11-04 14:28:14 -08:00
|
|
|
// If the property was defined in an extension, search the extensions for
|
|
|
|
// overrides.
|
|
|
|
const ObjCInterfaceDecl *OID = D->getClassInterface();
|
|
|
|
if (dyn_cast<ObjCInterfaceDecl>(D->getParent()) != OID)
|
|
|
|
for (auto *Ext : OID->known_extensions()) {
|
|
|
|
auto *OMD = Ext->getInstanceMethod(D->getSelector());
|
|
|
|
if (OMD && !OMD->isImplicit())
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-11-21 18:13:48 -08:00
|
|
|
Val = createObjCPropertyGetter(C, D);
|
2014-01-10 20:06:06 +00:00
|
|
|
|
2022-06-20 22:59:26 -07:00
|
|
|
return *Val;
|
2014-01-10 20:06:06 +00:00
|
|
|
}
|