Skip to content

Commit d267daa

Browse files
authored
[clang][bytecode] Diagnose loads from weak variables (#109256)
1 parent 3d5e8e4 commit d267daa

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

clang/lib/AST/ByteCode/Interp.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,13 +560,25 @@ bool CheckGlobalInitialized(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
560560
return false;
561561
}
562562

563+
static bool CheckWeak(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
564+
if (!Ptr.isWeak())
565+
return true;
566+
567+
const auto *VD = Ptr.getDeclDesc()->asVarDecl();
568+
assert(VD);
569+
S.FFDiag(S.Current->getLocation(OpPC), diag::note_constexpr_var_init_weak)
570+
<< VD;
571+
S.Note(VD->getLocation(), diag::note_declared_at);
572+
573+
return false;
574+
}
575+
563576
bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
564577
AccessKinds AK) {
565578
if (!CheckLive(S, OpPC, Ptr, AK))
566579
return false;
567580
if (!CheckConstant(S, OpPC, Ptr))
568581
return false;
569-
570582
if (!CheckDummy(S, OpPC, Ptr, AK))
571583
return false;
572584
if (!CheckExtern(S, OpPC, Ptr))
@@ -579,6 +591,8 @@ bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
579591
return false;
580592
if (!CheckTemporary(S, OpPC, Ptr, AK))
581593
return false;
594+
if (!CheckWeak(S, OpPC, Ptr))
595+
return false;
582596
if (!CheckMutable(S, OpPC, Ptr))
583597
return false;
584598
if (!CheckVolatile(S, OpPC, Ptr, AK))

clang/test/AST/ByteCode/weak.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,11 @@ int ha[(bool)&a]; // both-warning {{variable length arrays in C++ are a Clang ex
77
int ha2[&a == nullptr]; // both-warning {{variable length arrays in C++ are a Clang extension}} \
88
// both-note {{comparison against address of weak declaration '&a' can only be performed at runtime}} \
99
// both-error {{variable length array declaration not allowed at file scope}}
10+
11+
extern const int W1 __attribute__((weak)) = 10; // both-note {{declared here}}
12+
static_assert(W1 == 10, ""); // both-error {{static assertion expression is not an integral constant expression}} \
13+
// both-note {{initializer of weak variable 'W1' is not considered constant because it may be different at runtime}}
14+
15+
extern const int W2 __attribute__((weak)); // both-note {{declared here}}
16+
static_assert(W2 == 10, ""); // both-error {{static assertion expression is not an integral constant expression}} \
17+
// both-note {{initializer of 'W2' is unknown}}

clang/test/SemaCXX/weak-init.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %clang_cc1 %s -verify -fsyntax-only
2+
// RUN: %clang_cc1 %s -verify -fsyntax-only -fexperimental-new-constant-interpreter
23

34
extern const int W1 __attribute__((weak)) = 10; // expected-note {{declared here}}
45

0 commit comments

Comments
 (0)