[clang] cec2073 - [clang][Interp] Diagnose comparisions against weak function pointers
Timm Bäder via cfe-commits
cfe-commits at lists.llvm.org
Mon Mar 4 20:44:30 PST 2024
Author: Timm Bäder
Date: 2024-03-05T05:44:09+01:00
New Revision: cec2073f8e82c2d72a7246300aaa7b2a85ca4012
URL: https://github.com/llvm/llvm-project/commit/cec2073f8e82c2d72a7246300aaa7b2a85ca4012
DIFF: https://github.com/llvm/llvm-project/commit/cec2073f8e82c2d72a7246300aaa7b2a85ca4012.diff
LOG: [clang][Interp] Diagnose comparisions against weak function pointers
Added:
Modified:
clang/lib/AST/Interp/Interp.h
clang/test/AST/Interp/functions.cpp
Removed:
################################################################################
diff --git a/clang/lib/AST/Interp/Interp.h b/clang/lib/AST/Interp/Interp.h
index 3d49f73a567621..548405a53dfac8 100644
--- a/clang/lib/AST/Interp/Interp.h
+++ b/clang/lib/AST/Interp/Interp.h
@@ -746,6 +746,17 @@ inline bool CmpHelperEQ<FunctionPointer>(InterpState &S, CodePtr OpPC,
CompareFn Fn) {
const auto &RHS = S.Stk.pop<FunctionPointer>();
const auto &LHS = S.Stk.pop<FunctionPointer>();
+
+ // We cannot compare against weak declarations at compile time.
+ for (const auto &FP : {LHS, RHS}) {
+ if (!FP.isZero() && FP.getFunction()->getDecl()->isWeak()) {
+ const SourceInfo &Loc = S.Current->getSource(OpPC);
+ S.FFDiag(Loc, diag::note_constexpr_pointer_weak_comparison)
+ << FP.toDiagnosticString(S.getCtx());
+ return false;
+ }
+ }
+
S.Stk.push<Boolean>(Boolean::from(Fn(LHS.compare(RHS))));
return true;
}
diff --git a/clang/test/AST/Interp/functions.cpp b/clang/test/AST/Interp/functions.cpp
index 38f761f563bef7..67fd9036d81e76 100644
--- a/clang/test/AST/Interp/functions.cpp
+++ b/clang/test/AST/Interp/functions.cpp
@@ -565,3 +565,13 @@ namespace VariadicOperator {
float &fr = c(10);
}
}
+
+namespace WeakCompare {
+ [[gnu::weak]]void weak_method();
+ static_assert(weak_method != nullptr, ""); // both-error {{not an integral constant expression}} \
+ // both-note {{comparison against address of weak declaration '&weak_method' can only be performed at runtim}}
+
+ constexpr auto A = &weak_method;
+ static_assert(A != nullptr, ""); // both-error {{not an integral constant expression}} \
+ // both-note {{comparison against address of weak declaration '&weak_method' can only be performed at runtim}}
+}
More information about the cfe-commits
mailing list