[clang] Better diagnostics when assertion fails in `consteval` (PR #130458)
JJ Marr via cfe-commits
cfe-commits at lists.llvm.org
Fri May 9 13:53:52 PDT 2025
https://github.com/jj-marr updated https://github.com/llvm/llvm-project/pull/130458
>From 3b6d28e4f74c1fda73abe3b5972d5ac2a43576de Mon Sep 17 00:00:00 2001
From: JJ Marr <jjmarr at gmail.com>
Date: Sat, 8 Mar 2025 22:00:45 -0500
Subject: [PATCH] Explain which assertion failed during consteval
---
clang/docs/ReleaseNotes.rst | 4 +++
.../clang/Basic/DiagnosticSemaKinds.td | 2 ++
clang/lib/AST/ExprConstant.cpp | 10 ++++--
clang/test/SemaCXX/consteval-assert.cpp | 34 +++++++++++++++++++
4 files changed, 48 insertions(+), 2 deletions(-)
create mode 100644 clang/test/SemaCXX/consteval-assert.cpp
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index eb2e8f2b8a6c0..4c719ebc19221 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -419,6 +419,10 @@ Improvements to Clang's diagnostics
- ``-Winitializer-overrides`` and ``-Wreorder-init-list`` are now grouped under
the ``-Wc99-designator`` diagnostic group, as they also are about the
behavior of the C99 feature as it was introduced into C++20. Fixes #GH47037
+
+- Explanatory note is printed when ``assert`` fails during evaluation of a
+ constant expression. Prior to this, the error inaccurately implied that assert
+ could not be used at all in a constant expression (#GH130458)
Improvements to Clang's time-trace
----------------------------------
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 4c96142e28134..87e982e9c491c 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -162,6 +162,8 @@ def err_ice_too_large : Error<
"integer constant expression evaluates to value %0 that cannot be "
"represented in a %1-bit %select{signed|unsigned}2 integer type">;
def err_expr_not_string_literal : Error<"expression is not a string literal">;
+def note_constexpr_assert_failed : Note<
+ "assertion failed during evaluation of constant expression">;
// Semantic analysis of constant literals.
def ext_predef_outside_function : Warning<
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index f2e49b9ea669e..3ee355d6570ed 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -5975,9 +5975,15 @@ static bool CheckConstexprFunction(EvalInfo &Info, SourceLocation CallLoc,
Definition->hasAttr<MSConstexprAttr>())))
return true;
- if (Info.getLangOpts().CPlusPlus11) {
- const FunctionDecl *DiagDecl = Definition ? Definition : Declaration;
+ const FunctionDecl *DiagDecl = Definition ? Definition : Declaration;
+ if (CallLoc.isMacroID() && (DiagDecl->getName() == "__assert_rtn" ||
+ DiagDecl->getName() == "__assert_fail" ||
+ DiagDecl->getName() == "_wassert")) {
+ Info.FFDiag(CallLoc, diag::note_constexpr_assert_failed);
+ return false;
+ }
+ if (Info.getLangOpts().CPlusPlus11) {
// If this function is not constexpr because it is an inherited
// non-constexpr constructor, diagnose that directly.
auto *CD = dyn_cast<CXXConstructorDecl>(DiagDecl);
diff --git a/clang/test/SemaCXX/consteval-assert.cpp b/clang/test/SemaCXX/consteval-assert.cpp
new file mode 100644
index 0000000000000..b54a38ff26105
--- /dev/null
+++ b/clang/test/SemaCXX/consteval-assert.cpp
@@ -0,0 +1,34 @@
+// RUN: %clang_cc1 -std=c++23 -verify=expected,cxx20_plus -DTEST_LINUX %s
+// RUN: %clang_cc1 -std=c++23 -verify=expected,cxx20_plus -DTEST_WINDOWS %s
+// RUN: %clang_cc1 -std=c++23 -verify=expected,cxx20_plus -DTEST_DARWIN %s
+
+#ifdef __ASSERT_FUNCTION
+#undef __ASSERT_FUNCTION
+#endif
+
+#if defined(TEST_LINUX)
+ extern "C" void __assert_fail(const char*, const char*, unsigned, const char*);
+ #define assert(cond) \
+ ((cond) ? (void)0 : __assert_fail(#cond, __FILE__, __LINE__, __func__))
+#elif defined(TEST_DARWIN)
+ void __assert_rtn(const char *, const char *, int, const char *);
+ #define assert(cond) \
+ (__builtin_expect(!(cond), 0) ? __assert_rtn(__func__, __FILE__, __LINE__, #cond) : (void)0)
+#elif defined(TEST_WINDOWS)
+ void /*__cdecl*/ _wassert(const wchar_t*, const wchar_t*, unsigned);
+ #define _CRT_WIDE_(s) L ## s
+ #define _CRT_WIDE(s) _CRT_WIDE_(s)
+ #define assert(cond) \
+ (void)((!!(cond)) || (_wassert(_CRT_WIDE(#cond), _CRT_WIDE(__FILE__), (unsigned)(__LINE__)), 0))
+#endif
+
+consteval int square(int x) {
+ int result = x * x;
+ assert(result == 42); // expected-note {{assertion failed during evaluation of constant expression}}
+ return result;
+}
+
+void test() {
+ auto val = square(2); // expected-note {{in call to 'square(2)'}} \
+ // expected-error {{call to consteval function 'square' is not a constant expression}}
+}
More information about the cfe-commits
mailing list