Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Clang] No longer require complete types with __builtin_launder #91070

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,8 @@ Bug Fixes in This Version
- Clang will no longer emit a duplicate -Wunused-value warning for an expression
`(A, B)` which evaluates to glvalue `B` that can be converted to non ODR-use. (#GH45783)

- `__builtin_launder` no longer requires a pointer to a complete type. (#GH90949)

Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
5 changes: 3 additions & 2 deletions clang/lib/CodeGen/CGBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2487,8 +2487,9 @@ TypeRequiresBuiltinLaunderImp(const ASTContext &Ctx, QualType Ty,
if (!Seen.insert(Record).second)
return false;

assert(Record->hasDefinition() &&
"Incomplete types should already be diagnosed");
// Assume incomplete types need to be laundered
if (!Record->hasDefinition())
return true;

if (Record->isDynamicClass())
return true;
Expand Down
21 changes: 2 additions & 19 deletions clang/lib/Sema/SemaChecking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2164,15 +2164,8 @@ static ExprResult BuiltinLaunder(Sema &S, CallExpr *TheCall) {
// * The type of the argument if it's not an array or function type,
// Otherwise,
// * The decayed argument type.
QualType ParamTy = [&]() {
QualType ArgTy = TheCall->getArg(0)->getType();
if (const ArrayType *Ty = ArgTy->getAsArrayTypeUnsafe())
return S.Context.getPointerType(Ty->getElementType());
if (ArgTy->isFunctionType()) {
return S.Context.getPointerType(ArgTy);
}
return ArgTy;
}();
QualType ParamTy =
S.Context.getAdjustedParameterType(TheCall->getArg(0)->getType());

TheCall->setType(ParamTy);

Expand All @@ -2191,16 +2184,6 @@ static ExprResult BuiltinLaunder(Sema &S, CallExpr *TheCall) {
return ExprError();
}

// We either have an incomplete class type, or we have a class template
// whose instantiation has not been forced. Example:
//
// template <class T> struct Foo { T value; };
// Foo<int> *p = nullptr;
// auto *d = __builtin_launder(p);
if (S.RequireCompleteType(TheCall->getBeginLoc(), ParamTy->getPointeeType(),
diag::err_incomplete_type))
return ExprError();

assert(ParamTy->getPointeeType()->isObjectType() &&
"Unhandled non-object pointer case");

Expand Down
14 changes: 11 additions & 3 deletions clang/test/AST/Interp/builtin-functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,16 +457,24 @@ void f() {
static_assert(test_in_constexpr(i), "");
}

struct Incomplete; // both-note {{forward declaration}}
struct Incomplete;
struct IncompleteMember {
Incomplete &i;
};
void test_incomplete(Incomplete *i, IncompleteMember *im) {
// both-error@+1 {{incomplete type 'Incomplete' where a complete type is required}}
__builtin_launder(i);
__builtin_launder(i); // OK
__builtin_launder(&i); // OK
__builtin_launder(im); // OK
}
extern Incomplete incomplete;
extern IncompleteMember incomplete_member;
static_assert(test_constexpr_launder(&incomplete) == &incomplete, "");
static_assert(test_constexpr_launder(&incomplete_member) == &incomplete_member, "");
template<typename> struct X { static_assert(false, ""); };
extern X<void> x;
static_assert(__builtin_launder(__builtin_addressof(x)) == __builtin_addressof(x), "");
static_assert((test_constexpr_launder)(__builtin_addressof(x)) == __builtin_addressof(x), "");
template<> struct X<void> {};

void test_noexcept(int *i) {
static_assert(noexcept(__builtin_launder(i)), "");
Expand Down
56 changes: 56 additions & 0 deletions clang/test/CodeGenCXX/builtin-launder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,66 @@ extern "C" void test_builtin_launder_virtual_base(TestVirtualBase *p) {
TestVirtualBase *d = __builtin_launder(p);
}

struct IncompleteNeedsLaunder;

// CHECK-LABEL: define{{.*}} void @test_builtin_launder_incomplete_later_needs_launder
extern "C" void test_builtin_launder_incomplete_later_needs_launder(IncompleteNeedsLaunder *p) {
// CHECK-STRICT-NOT: ret void
// CHECK-STRICT: @llvm.launder.invariant.group

// CHECK-NONSTRICT-NOT: @llvm.launder.invariant.group

// CHECK: ret void
IncompleteNeedsLaunder *d = __builtin_launder(p);
}

struct IncompleteNeedsLaunder {
virtual void foo() {}
};

// CHECK-LABEL: define{{.*}} void @test_builtin_launder_completed_needs_launder
extern "C" void test_builtin_launder_completed_needs_launder(IncompleteNeedsLaunder *p) {
// CHECK-STRICT-NOT: ret void
// CHECK-STRICT: @llvm.launder.invariant.group

// CHECK-NONSTRICT-NOT: @llvm.launder.invariant.group

// CHECK: ret void
IncompleteNeedsLaunder *d = __builtin_launder(p);
}

struct IncompleteDoesntNeedLaunder;

// CHECK-LABEL: define{{.*}} void @test_builtin_launder_incomplete_later_doesnt_needs_launder
extern "C" void test_builtin_launder_incomplete_later_doesnt_needs_launder(IncompleteDoesntNeedLaunder *p) {
// CHECK-STRICT-NOT: ret void
// CHECK-STRICT: @llvm.launder.invariant.group

// CHECK-NONSTRICT-NOT: @llvm.launder.invariant.group

// CHECK: ret void
IncompleteDoesntNeedLaunder *d = __builtin_launder(p);
}

//===----------------------------------------------------------------------===//
// Negative Cases
//===----------------------------------------------------------------------===//

struct IncompleteDoesntNeedLaunder {};

// CHECK-LABEL: define{{.*}} void @test_builtin_launder_completed_doesnt_need_launder
extern "C" void test_builtin_launder_completed_doesnt_need_launder(IncompleteDoesntNeedLaunder *p) {
// CHECK: entry
// CHECK-NOT: llvm.launder.invariant.group
// CHECK-NEXT: %p.addr = alloca ptr, align 8
// CHECK-NEXT: %d = alloca ptr
// CHECK-NEXT: store ptr %p, ptr %p.addr
// CHECK-NEXT: [[TMP:%.*]] = load ptr, ptr %p.addr
// CHECK-NEXT: store ptr [[TMP]], ptr %d
// CHECK-NEXT: ret void
IncompleteDoesntNeedLaunder *d = __builtin_launder(p);
}

// CHECK-LABEL: define{{.*}} void @test_builtin_launder_ommitted_one
extern "C" void test_builtin_launder_ommitted_one(int *p) {
// CHECK: entry
Expand Down
14 changes: 11 additions & 3 deletions clang/test/SemaCXX/builtins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,24 @@ void f() {
static_assert(test_in_constexpr(i), "");
}

struct Incomplete; // expected-note {{forward declaration}}
struct Incomplete;
struct IncompleteMember {
Incomplete &i;
};
void test_incomplete(Incomplete *i, IncompleteMember *im) {
// expected-error@+1 {{incomplete type 'Incomplete' where a complete type is required}}
__builtin_launder(i);
__builtin_launder(i); // OK
__builtin_launder(&i); // OK
__builtin_launder(im); // OK
}
extern Incomplete incomplete;
extern IncompleteMember incomplete_member;
static_assert(test_constexpr_launder(&incomplete) == &incomplete, "");
static_assert(test_constexpr_launder(&incomplete_member) == &incomplete_member, "");
template<typename> struct X { static_assert(false, ""); };
extern X<void> x;
static_assert(__builtin_launder(__builtin_addressof(x)) == __builtin_addressof(x), "");
static_assert((test_constexpr_launder)(__builtin_addressof(x)) == __builtin_addressof(x), "");
template<> struct X<void> {};

void test_noexcept(int *i) {
static_assert(noexcept(__builtin_launder(i)), "");
Expand Down
Loading