Skip to content

Commit cec2073

Browse files
committed
[clang][Interp] Diagnose comparisions against weak function pointers
1 parent 7cd3268 commit cec2073

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

clang/lib/AST/Interp/Interp.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,17 @@ inline bool CmpHelperEQ<FunctionPointer>(InterpState &S, CodePtr OpPC,
746746
CompareFn Fn) {
747747
const auto &RHS = S.Stk.pop<FunctionPointer>();
748748
const auto &LHS = S.Stk.pop<FunctionPointer>();
749+
750+
// We cannot compare against weak declarations at compile time.
751+
for (const auto &FP : {LHS, RHS}) {
752+
if (!FP.isZero() && FP.getFunction()->getDecl()->isWeak()) {
753+
const SourceInfo &Loc = S.Current->getSource(OpPC);
754+
S.FFDiag(Loc, diag::note_constexpr_pointer_weak_comparison)
755+
<< FP.toDiagnosticString(S.getCtx());
756+
return false;
757+
}
758+
}
759+
749760
S.Stk.push<Boolean>(Boolean::from(Fn(LHS.compare(RHS))));
750761
return true;
751762
}

clang/test/AST/Interp/functions.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,3 +565,13 @@ namespace VariadicOperator {
565565
float &fr = c(10);
566566
}
567567
}
568+
569+
namespace WeakCompare {
570+
[[gnu::weak]]void weak_method();
571+
static_assert(weak_method != nullptr, ""); // both-error {{not an integral constant expression}} \
572+
// both-note {{comparison against address of weak declaration '&weak_method' can only be performed at runtim}}
573+
574+
constexpr auto A = &weak_method;
575+
static_assert(A != nullptr, ""); // both-error {{not an integral constant expression}} \
576+
// both-note {{comparison against address of weak declaration '&weak_method' can only be performed at runtim}}
577+
}

0 commit comments

Comments
 (0)