Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 15 additions & 1 deletion clang/lib/AST/ExprConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13279,6 +13279,9 @@ static bool refersToCompleteObject(const LValue &LVal) {
if (LVal.Designator.Invalid)
return false;

if (LVal.AllowConstexprUnknown)
return false;

if (!LVal.Designator.Entries.empty())
return LVal.Designator.isMostDerivedAnUnsizedArray();

Expand Down Expand Up @@ -13328,7 +13331,7 @@ static bool isUserWritingOffTheEnd(const ASTContext &Ctx, const LValue &LVal) {
return false;
};

return LVal.InvalidBase &&
return (LVal.InvalidBase || LVal.AllowConstexprUnknown) &&
Designator.Entries.size() == Designator.MostDerivedPathLength &&
Designator.MostDerivedIsArrayElement && isFlexibleArrayMember() &&
isDesignatorAtObjectEnd(Ctx, LVal);
Expand Down Expand Up @@ -13396,6 +13399,17 @@ static bool determineEndOffset(EvalInfo &Info, SourceLocation ExprLoc,
if (LVal.InvalidBase)
return false;

// We cannot deterimine the end offset of the enitre object if this is an
// unknown reference.
if (Type == 0 && LVal.AllowConstexprUnknown)
return false;

// We cannot deterimine the end offset of the subobject if this is an
// unknown reference and the subobject designator is invalid (e.g., unsized
// array designator).
if (Type == 1 && LVal.Designator.Invalid && LVal.AllowConstexprUnknown)
return false;

QualType BaseTy = getObjectType(LVal.getLValueBase());
const bool Ret = CheckedHandleSizeof(BaseTy, EndOffset);
addFlexibleArrayMemberInitSize(Info, BaseTy, LVal, EndOffset);
Expand Down
48 changes: 48 additions & 0 deletions clang/test/Sema/builtin-object-size.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -fstrict-flex-arrays=0 -DSTRICT0 -std=c++23 -verify %s
// RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -fstrict-flex-arrays=1 -DSTRICT1 -std=c++23 -verify %s
// RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -fstrict-flex-arrays=2 -DSTRICT2 -std=c++23 -verify %s
// RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -fstrict-flex-arrays=3 -DSTRICT3 -std=c++23 -verify %s

struct EmptyS {
int i;
char a[];
};

template <unsigned N>
struct S {
int i;
char a[N];
};

extern S<2> &s2;
static_assert(__builtin_object_size(s2.a, 0)); // expected-error {{static assertion expression is not an integral constant expression}}
static_assert(__builtin_object_size(s2.a, 1) == 2);
#if defined(STRICT0)
// expected-error@-2 {{static assertion expression is not an integral constant expression}}
#endif
static_assert(__builtin_object_size(s2.a, 2) == 4);
static_assert(__builtin_object_size(s2.a, 3) == 2);

extern S<1> &s1;
static_assert(__builtin_object_size(s1.a, 0)); // expected-error {{static assertion expression is not an integral constant expression}}
static_assert(__builtin_object_size(s1.a, 1) == 1);
#if defined(STRICT0) || defined(STRICT1)
// expected-error@-2 {{static assertion expression is not an integral constant expression}}
#endif
static_assert(__builtin_object_size(s1.a, 2) == 4);
static_assert(__builtin_object_size(s1.a, 3) == 1);

extern S<0> &s0;
static_assert(__builtin_object_size(s0.a, 0)); // expected-error {{static assertion expression is not an integral constant expression}}
static_assert(__builtin_object_size(s0.a, 1) == 0);
#if defined(STRICT0) || defined(STRICT1) || defined(STRICT2)
// expected-error@-2 {{static assertion expression is not an integral constant expression}}
#endif
static_assert(__builtin_object_size(s0.a, 2) == 0);
static_assert(__builtin_object_size(s0.a, 3) == 0);

extern EmptyS &empty;
static_assert(__builtin_object_size(empty.a, 0)); // expected-error {{static assertion expression is not an integral constant expression}}
static_assert(__builtin_object_size(empty.a, 1)); // expected-error {{static assertion expression is not an integral constant expression}}
static_assert(__builtin_object_size(empty.a, 2) == 0);
static_assert(__builtin_object_size(empty.a, 3)); // expected-error {{static assertion expression is not an integral constant expression}}