Skip to content

Commit 86b6300

Browse files
committed
[clang][Interp] Allow getFieldPtr ops on null pointers in C
1 parent ea23761 commit 86b6300

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

clang/lib/AST/Interp/Interp.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1224,7 +1224,8 @@ inline bool GetPtrGlobal(InterpState &S, CodePtr OpPC, uint32_t I) {
12241224
inline bool GetPtrField(InterpState &S, CodePtr OpPC, uint32_t Off) {
12251225
const Pointer &Ptr = S.Stk.pop<Pointer>();
12261226

1227-
if (S.inConstantContext() && !CheckNull(S, OpPC, Ptr, CSK_Field))
1227+
if (S.getLangOpts().CPlusPlus && S.inConstantContext() &&
1228+
!CheckNull(S, OpPC, Ptr, CSK_Field))
12281229
return false;
12291230

12301231
if (CheckDummy(S, OpPC, Ptr)) {

clang/test/AST/Interp/c.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ int a2[(intptr_t)&((struct y*)0)->y]; // all-warning {{folded to constant array}
7979
const struct y *yy = (struct y*)0;
8080
const intptr_t L = (intptr_t)(&(yy->y)); // all-error {{not a compile-time constant}}
8181

82+
_Static_assert((long)&((struct y*)0)->y > 0, ""); // pedantic-ref-warning {{GNU extension}} \
83+
// pedantic-ref-note {{this conversion is not allowed in a constant expression}} \
84+
// pedantic-expected-warning {{GNU extension}} \
85+
// pedantic-expected-note {{this conversion is not allowed in a constant expression}}
86+
8287
const ptrdiff_t m = &m + 137 - &m;
8388
_Static_assert(m == 137, ""); // pedantic-ref-warning {{GNU extension}} \
8489
// pedantic-expected-warning {{GNU extension}}

0 commit comments

Comments
 (0)