Skip to content

Commit

Permalink
Use __cpp_has_attribute() (#928). (#929)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jtv authored Jan 15, 2025
1 parent e17d673 commit 0c390a0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 29 deletions.
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
13 changes: 10 additions & 3 deletions config-tests/PQXX_HAVE_ASSUME.cxx
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
int main(int argc, char **argv)
#if !__has_cpp_attribute(assume)
# error "No support for [[assume]] attribute."
#endif

#include <iostream>


void foo(int i)
{
[[assume(argv != nullptr)]];
return argc - 1;
[[assume(i > 0)]];
std::cout << i << '\n';
}
16 changes: 7 additions & 9 deletions config-tests/PQXX_HAVE_LIKELY.cxx
Original file line number Diff line number Diff line change
@@ -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;
}
40 changes: 23 additions & 17 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -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 <iostream>





void foo(int i)

{

[[assume(argv != nullptr)]];
[[assume(i > 0)]];

return argc - 1;
std::cout << i << '\n';

}

Expand Down Expand Up @@ -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;

}

Expand Down

0 comments on commit 0c390a0

Please sign in to comment.