From 0c390a0c5c01b15b2bf608058787e2b20e868314 Mon Sep 17 00:00:00 2001 From: Jeroen Vermeulen Date: Wed, 15 Jan 2025 22:24:34 +0100 Subject: [PATCH] Use `__cpp_has_attribute()` (#928). (#929) Rewrite the feature checks for `[[assume]]`, `[[likely]]`, and `[[unlikely]]` using the C++20 `__cpp_has_attribute()` macro. Unfortunately some compilers will still _warn_ about the attribute ("it's a C++20 extension!") so the checks also need to _try_ the attribute. --- NEWS | 2 ++ config-tests/PQXX_HAVE_ASSUME.cxx | 13 +++++++--- config-tests/PQXX_HAVE_LIKELY.cxx | 16 ++++++------- configure | 40 ++++++++++++++++++------------- 4 files changed, 42 insertions(+), 29 deletions(-) diff --git a/NEWS b/NEWS index 17c79d0da..8d4874c5c 100644 --- a/NEWS +++ b/NEWS @@ -2,6 +2,8 @@ - Fix string conversion buffer budget for arrays containing nulls. (#921) - Remove `-fanalyzer` option again; gcc is still broken. - Oops, no, minimum CMake version is not 3.28, but 3.12! + - Fix buffer overrun in converting array with nulls to string. (#922) + - Fix warnings on compilers that accept `[[assume]]` with a warning. (#928) 7.10.0 - Deprecate `errorhandler`; replace with lambda-friendly "notice handlers." - Deprecate `notification_receiver`; replace with "notification handlers" diff --git a/config-tests/PQXX_HAVE_ASSUME.cxx b/config-tests/PQXX_HAVE_ASSUME.cxx index 41f5d4f02..0267fdc35 100644 --- a/config-tests/PQXX_HAVE_ASSUME.cxx +++ b/config-tests/PQXX_HAVE_ASSUME.cxx @@ -1,5 +1,12 @@ -int main(int argc, char **argv) +#if !__has_cpp_attribute(assume) +# error "No support for [[assume]] attribute." +#endif + +#include + + +void foo(int i) { - [[assume(argv != nullptr)]]; - return argc - 1; + [[assume(i > 0)]]; + std::cout << i << '\n'; } diff --git a/config-tests/PQXX_HAVE_LIKELY.cxx b/config-tests/PQXX_HAVE_LIKELY.cxx index 72a8b1285..4e57e5e87 100644 --- a/config-tests/PQXX_HAVE_LIKELY.cxx +++ b/config-tests/PQXX_HAVE_LIKELY.cxx @@ -1,16 +1,14 @@ // Test for C++20 [[likely]] and [[unlikely]] attributes. // C++20: Assume support. -int main(int argc, char **) -{ -#if __cplusplus < 202002L - deliberately_fail(because, older, C++, standard); +#if !__has_cpp_attribute(likely) +# error "No support for [[likely]] / [[unlikely]] attributes." #endif - int x = 0; - if (argc == 1) [[likely]] - x = 0; +int foo(int i) +{ + if (i > 0) [[likely]] + return 100; else - x = 1; - return x; + return 0; } diff --git a/configure b/configure index ff7c3448a..1eee40098 100755 --- a/configure +++ b/configure @@ -17237,13 +17237,27 @@ printf %s "checking PQXX_HAVE_ASSUME... " >&6; } PQXX_HAVE_ASSUME=yes cat confdefs.h - <<_ACEOF >conftest.$ac_ext /* end confdefs.h. */ -int main(int argc, char **argv) +#if !__has_cpp_attribute(assume) + +# error "No support for [[assume]] attribute." + +#endif + + + +#include + + + + + +void foo(int i) { - [[assume(argv != nullptr)]]; + [[assume(i > 0)]]; - return argc - 1; + std::cout << i << '\n'; } @@ -17583,29 +17597,21 @@ cat confdefs.h - <<_ACEOF >conftest.$ac_ext -int main(int argc, char **) - -{ - -#if __cplusplus < 202002L +#if !__has_cpp_attribute(likely) - deliberately_fail(because, older, C++, standard); +# error "No support for [[likely]] / [[unlikely]] attributes." #endif - int x = 0; - - if (argc == 1) [[likely]] - - x = 0; +int foo(int i) - else +{ - x = 1; + if (i > 0) [[likely]] return 100; - return x; + else return 0; }