Skip to content

[clang][bytecode] Diagnose loads from weak variables #109256

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

Merged
merged 1 commit into from
Sep 19, 2024
Merged
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
16 changes: 15 additions & 1 deletion clang/lib/AST/ByteCode/Interp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,13 +560,25 @@ bool CheckGlobalInitialized(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
return false;
}

static bool CheckWeak(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
if (!Ptr.isWeak())
return true;

const auto *VD = Ptr.getDeclDesc()->asVarDecl();
assert(VD);
S.FFDiag(S.Current->getLocation(OpPC), diag::note_constexpr_var_init_weak)
<< VD;
S.Note(VD->getLocation(), diag::note_declared_at);

return false;
}

bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
AccessKinds AK) {
if (!CheckLive(S, OpPC, Ptr, AK))
return false;
if (!CheckConstant(S, OpPC, Ptr))
return false;

if (!CheckDummy(S, OpPC, Ptr, AK))
return false;
if (!CheckExtern(S, OpPC, Ptr))
Expand All @@ -579,6 +591,8 @@ bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
return false;
if (!CheckTemporary(S, OpPC, Ptr, AK))
return false;
if (!CheckWeak(S, OpPC, Ptr))
return false;
if (!CheckMutable(S, OpPC, Ptr))
return false;
if (!CheckVolatile(S, OpPC, Ptr, AK))
Expand Down
8 changes: 8 additions & 0 deletions clang/test/AST/ByteCode/weak.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@ int ha[(bool)&a]; // both-warning {{variable length arrays in C++ are a Clang ex
int ha2[&a == nullptr]; // both-warning {{variable length arrays in C++ are a Clang extension}} \
// both-note {{comparison against address of weak declaration '&a' can only be performed at runtime}} \
// both-error {{variable length array declaration not allowed at file scope}}

extern const int W1 __attribute__((weak)) = 10; // both-note {{declared here}}
static_assert(W1 == 10, ""); // both-error {{static assertion expression is not an integral constant expression}} \
// both-note {{initializer of weak variable 'W1' is not considered constant because it may be different at runtime}}

extern const int W2 __attribute__((weak)); // both-note {{declared here}}
static_assert(W2 == 10, ""); // both-error {{static assertion expression is not an integral constant expression}} \
// both-note {{initializer of 'W2' is unknown}}
1 change: 1 addition & 0 deletions clang/test/SemaCXX/weak-init.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// RUN: %clang_cc1 %s -verify -fsyntax-only
// RUN: %clang_cc1 %s -verify -fsyntax-only -fexperimental-new-constant-interpreter

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

Expand Down
Loading