[clang] [clang] Allow C-style casts in constexpr in MS compatible mode (PR #197005)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Jun 5 08:58:54 PDT 2026
================
@@ -18,6 +18,36 @@ using namespace clang::interp;
State::~State() {}
+// In MSVC compatibility mode we relax constexpr evaluation for the
+// FIELD_OFFSET-style pattern:
+//
+// (LONG_PTR)(&((T*)0)->field)
+//
+// Evaluation of such expressions first produces a ptr-to-int cast
+// diagnostic and may then encounter a null-subobject access while
+// forming the field address. Both diagnostics are considered part of
+// the same accepted pattern and should not prevent constant folding.
+//
+// If a ptr-to-int cast diagnostic was recorded but evaluation later
+// reaches any other failure, discard the recorded diagnostic so the
+// expression is rejected.
+void State::clearDiagIfNeeded(diag::kind DiagId) {
+ if (!Ctx.getLangOpts().MSVCCompat)
+ return;
+ switch (DiagId) {
+ case diag::note_constexpr_invalid_cast_ptrtoint:
+ case diag::note_constexpr_null_subobject:
----------------
eleviant wrote:
I've changed the overall approach, now instead of recording invalid_cast_ptrtoint and null_subobject and clearing them later, we just skip them. This removed a large portion of the code including clearDiagIfNeeded, maybeFoldConstexprWithCast and few others. Also this fixes a bug, with evaluation of template parameter with implicit cast to bool.
https://github.com/llvm/llvm-project/pull/197005
More information about the cfe-commits
mailing list