Skip to content

Commit 321f831

Browse files
committed
[clang][Interp] Diagnose comparisons with weak pointers
1 parent c836983 commit 321f831

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

clang/lib/AST/Interp/Interp.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,18 @@ inline bool CmpHelperEQ<Pointer>(InterpState &S, CodePtr OpPC, CompareFn Fn) {
801801
return true;
802802
}
803803

804+
for (const auto &P : {LHS, RHS}) {
805+
if (P.isZero())
806+
continue;
807+
if (const ValueDecl *VD = P.getDeclDesc()->asValueDecl();
808+
VD && VD->isWeak()) {
809+
const SourceInfo &Loc = S.Current->getSource(OpPC);
810+
S.FFDiag(Loc, diag::note_constexpr_pointer_weak_comparison)
811+
<< P.toDiagnosticString(S.getCtx());
812+
return false;
813+
}
814+
}
815+
804816
if (!Pointer::hasSameBase(LHS, RHS)) {
805817
S.Stk.push<BoolT>(BoolT::from(Fn(ComparisonCategoryResult::Unordered)));
806818
return true;

clang/test/AST/Interp/weak.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %clang_cc1 -std=c++20 -fexperimental-new-constant-interpreter -verify=expected,both %s
2+
// RUN: %clang_cc1 -std=c++20 -verify=ref,both %s
3+
4+
5+
6+
7+
/// FIXME: The new interpreter also emits the "address of weak declaration" note in the pointer-to-bool case.
8+
9+
[[gnu::weak]] extern int a;
10+
int ha[(bool)&a]; // both-warning {{variable length arrays in C++ are a Clang extension}} \
11+
// expected-note {{comparison against address of weak declaration}} \
12+
// both-error {{variable length array declaration not allowed at file scope}}
13+
int ha2[&a == nullptr]; // both-warning {{variable length arrays in C++ are a Clang extension}} \
14+
// both-note {{comparison against address of weak declaration '&a' can only be performed at runtime}} \
15+
// both-error {{variable length array declaration not allowed at file scope}}

0 commit comments

Comments
 (0)