Author: Shu-Chun Weng
Date: 2022-10-03T14:28:06-07:00
New Revision: 3933c43d9008bb1b151156da92827a109e7963b6
URL:
https://github.com/llvm/llvm-project/commit/3933c43d9008bb1b151156da92827a109e7963b6
DIFF:
https://github.com/llvm/llvm-project/commit/3933c43d9008bb1b151156da92827a109e7963b6.diff
LOG: [clang] Add cc1 option -fctor-dtor-return-this
This option forces constructors and non-deleting destructors to return
`this` pointer in C++ ABI (except for Microsoft ABI, on which this flag
has no effect).
This is similar to ARM32, Apple ARM64, or Fuchsia C++ ABI, but can be
applied to any target triple.
Differential Revision: https://reviews.llvm.org/D119209
Added:
Modified:
clang/include/clang/Basic/CodeGenOptions.def
clang/include/clang/Driver/Options.td
clang/lib/CodeGen/CGCXXABI.h
clang/lib/CodeGen/ItaniumCXXABI.cpp
clang/test/CodeGenCXX/constructor-destructor-return-this.cpp
Removed:
diff --git a/clang/include/clang/Basic/CodeGenOptions.def
b/clang/include/clang/Basic/CodeGenOptions.def
index ddeabfc9d5451..62d0c936c60a5 100644
--- a/clang/include/clang/Basic/CodeGenOptions.def
+++ b/clang/include/clang/Basic/CodeGenOptions.def
@@ -492,6 +492,10 @@ ENUM_CODEGENOPT(ZeroCallUsedRegs,
llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind,
/// Whether to use opaque pointers.
CODEGENOPT(OpaquePointers, 1, 0)
+/// Modify C++ ABI to returning `this` pointer from constructors and
+/// non-deleting destructors. (No effect on Microsoft ABI.)
+CODEGENOPT(CtorDtorReturnThis, 1, 0)
+
#undef CODEGENOPT
#undef ENUM_CODEGENOPT
#undef VALUE_CODEGENOPT
diff --git a/clang/include/clang/Driver/Options.td
b/clang/include/clang/Driver/Options.td
index 655bf0d58d8e1..b6130f3410830 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -5604,6 +5604,11 @@ def ehcontguard : Flag<["-"], "ehcontguard">,
def fdenormal_fp_math_f32_EQ : Joined<["-"], "fdenormal-fp-math-f32=">,
Group;
+def fctor_dtor_return_this : Flag<["-"], "fctor-dtor-return-this">,
+ HelpText<"Change the C++ ABI to returning `this` pointer from constructors "
+ "and non-deleting destructors. (No effect on Microsoft ABI)">,
+ MarshallingInfoFlag>;
+
} // let Flags = [CC1Option, NoDriverOption]
//===--===//
diff --git a/clang/lib/CodeGen/CGCXXABI.h b/clang/lib/CodeGen/CGCXXABI.h
index 0768e6581acb8..a600768b20746 100644
--- a/clang/lib/CodeGen/CGCXXABI.h
+++ b/clang/lib/CodeGen/CGCXXABI.h
@@ -105,6 +105,10 @@ class CGCXXABI {
/// final class will have been taken care of by the caller.
virtual bool isThisCompleteObject(GlobalDecl GD) const = 0;
+ virtual bool constructorsAndDestructorsReturnThis() const {
+return CGM.getCodeGenOpts().CtorDtorReturnThis;
+ }
+
public:
virtual ~CGCXXABI();
@@ -120,7 +124,13 @@ class CGCXXABI {
///
/// There currently is no way to indicate if a destructor returns 'this'
/// when called virtually, and code generation does not support the case.
- virtual bool HasThisReturn(GlobalDecl GD) const { return false; }
+ virtual bool HasThisReturn(GlobalDecl GD) const {
+if (isa(GD.getDecl()) ||
+(isa(GD.getDecl()) &&
+ GD.getDtorType() != Dtor_Deleting))
+ return constructorsAndDestructorsReturnThis();
+return false;
+ }
virtual bool hasMostDerivedReturn(GlobalDecl GD) const { return false; }
diff --git a/clang/lib/CodeGen/ItaniumCXXABI.cpp
b/clang/lib/CodeGen/ItaniumCXXABI.cpp
index cb97af7ab11ab..c84faf468ea22 100644
--- a/clang/lib/CodeGen/ItaniumCXXABI.cpp
+++ b/clang/lib/CodeGen/ItaniumCXXABI.cpp
@@ -433,11 +433,7 @@ class ARMCXXABI : public ItaniumCXXABI {
ItaniumCXXABI(CGM, /*UseARMMethodPtrABI=*/true,
/*UseARMGuardVarABI=*/true) {}
- bool HasThisReturn(GlobalDecl GD) const override {
-return (isa(GD.getDecl()) || (
- isa(GD.getDecl()) &&
- GD.getDtorType() != Dtor_Deleting));
- }
+ bool constructorsAndDestructorsReturnThis() const override { return true; }
void EmitReturnFromThunk(CodeGenFunction &CGF, RValue RV,
QualType ResTy) override;
@@ -468,11 +464,7 @@ class FuchsiaCXXABI final : public ItaniumCXXABI {
: ItaniumCXXABI(CGM) {}
private:
- bool HasThisReturn(GlobalDecl GD) const override {
-return isa(GD.getDecl()) ||
- (isa(GD.getDecl()) &&
-GD.getDtorType() != Dtor_Deleting);
- }
+ bool constructorsAndDestructorsReturnThis() const override { return true; }
};
class WebAssemblyCXXABI final : public ItaniumCXXABI {
@@ -486,11 +478,7 @@ class WebAssemblyCXXABI final : public ItaniumCXXABI {
llvm::Value *Exn) override;
private:
- bool HasThisReturn(GlobalDecl GD) const override {
-return isa(GD.getDecl()) ||
- (isa(G