Skip to content

Commit 4294841

Browse files
authored
[clang][ExprConst] Can't be past an invalid LValue designator (llvm#84293)
For the test case in C, both `LV.getLValueOffset()` and `Ctx.getTypeSizeInChars(Ty)` are zero, so we return `true` from `isOnePastTheEndOfCompleteObject()` and ultimately diagnose this as being one past the end, but the diagnostic doesn't make sense.
1 parent 1d9fb2e commit 4294841

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

clang/lib/AST/ExprConstant.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9253,7 +9253,8 @@ bool PointerExprEvaluator::VisitCastExpr(const CastExpr *E) {
92539253
Info.getLangOpts().CPlusPlus26)) {
92549254
// Permitted.
92559255
} else {
9256-
if (SubExpr->getType()->isVoidPointerType()) {
9256+
if (SubExpr->getType()->isVoidPointerType() &&
9257+
Info.getLangOpts().CPlusPlus) {
92579258
if (HasValidResult)
92589259
CCEDiag(E, diag::note_constexpr_invalid_void_star_cast)
92599260
<< SubExpr->getType() << Info.getLangOpts().CPlusPlus26
@@ -12942,6 +12943,10 @@ static bool isOnePastTheEndOfCompleteObject(const ASTContext &Ctx,
1294212943
if (Ty->isIncompleteType())
1294312944
return true;
1294412945

12946+
// Can't be past the end of an invalid object.
12947+
if (LV.getLValueDesignator().Invalid)
12948+
return false;
12949+
1294512950
// We're a past-the-end pointer if we point to the byte after the object,
1294612951
// no matter what our type or path is.
1294712952
auto Size = Ctx.getTypeSizeInChars(Ty);

clang/test/AST/Interp/c.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ _Static_assert((&a - 100) != 0, ""); // pedantic-ref-warning {{is a GNU extensio
6666
// pedantic-ref-note {{-100 of non-array}} \
6767
// pedantic-expected-note {{-100 of non-array}}
6868
/// extern variable of a composite type.
69-
/// FIXME: The 'cast from void*' note is missing in the new interpreter.
69+
/// FIXME: The 'this conversion is not allowed' note is missing in the new interpreter.
7070
extern struct Test50S Test50;
7171
_Static_assert(&Test50 != (void*)0, ""); // all-warning {{always true}} \
7272
// pedantic-ref-warning {{is a GNU extension}} \
73-
// pedantic-ref-note {{cast from 'void *' is not allowed}} \
73+
// pedantic-ref-note {{this conversion is not allowed in a constant expression}} \
7474
// pedantic-expected-warning {{is a GNU extension}}
7575

7676
struct y {int x,y;};

clang/test/Sema/const-eval.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,7 @@ void PR21945(void) { int i = (({}), 0l); }
134134

135135
void PR24622(void);
136136
struct PR24622 {} pr24622;
137-
EVAL_EXPR(52, &pr24622 == (void *)&PR24622); // expected-error {{not an integer constant expression}}
138-
// expected-note@-1 {{past the end}}
137+
EVAL_EXPR(52, &pr24622 == (void *)&PR24622);
139138

140139
// We evaluate these by providing 2s' complement semantics in constant
141140
// expressions, like we do for integers.

clang/test/Sema/constexpr-void-cast.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %clang_cc1 -x c -fsyntax-only %s -verify=c
2+
// RUN: %clang_cc1 -x c -fsyntax-only %s -pedantic -verify=c-pedantic
3+
//
4+
// RUN: %clang_cc1 -x c++ -fsyntax-only %s -verify=cxx
5+
// RUN: %clang_cc1 -x c++ -fsyntax-only %s -pedantic -verify=cxx-pedantic
6+
7+
// c-no-diagnostics
8+
// cxx-no-diagnostics
9+
10+
void f(void);
11+
struct S {char c;} s;
12+
_Static_assert(&s != (void *)&f, ""); // c-pedantic-warning {{not an integer constant expression}} \
13+
// c-pedantic-note {{this conversion is not allowed in a constant expression}} \
14+
// cxx-pedantic-warning {{'_Static_assert' is a C11 extension}}

0 commit comments

Comments
 (0)