c++, libcpp: Implement CWG3053

The following patch implements CWG3053 approved in Kona, where it is now
valid not just to #define likely(a) or #define unlikely(a, b, c) but also
to #undef likely or #undef unlikely.

2025-11-10  Jakub Jelinek  <jakub@redhat.com>

libcpp/
	* directives.cc: Implement CWG3053.
	(do_undef): Don't pedwarn or warn about #undef likely or #undef
	unlikely.
gcc/testsuite/
	* g++.dg/warn/Wkeyword-macro-4.C: Don't diagnose for #undef likely
	or #undef unlikely.
	* g++.dg/warn/Wkeyword-macro-5.C: Likewise.
	* g++.dg/warn/Wkeyword-macro-9.C: Likewise.
	* g++.dg/warn/Wkeyword-macro-8.C: Likewise.
	* g++.dg/warn/Wkeyword-macro-10.C: Likewise.
This commit is contained in:
Jakub Jelinek 2025-11-10 11:34:20 +01:00 committed by Jakub Jelinek
parent 52fc9f01da
commit 611fc65050
6 changed files with 17 additions and 12 deletions

View File

@ -17,7 +17,7 @@
#undef inline // { dg-warning "undefining keyword 'inline'" }
#define inline __inline__ __attribute__((__always_inline__)) // { dg-warning "keyword 'inline' defined as macro" }
#define likely(a) a
#undef likely // { dg-warning "undefining keyword 'likely'" "" { target c++20 } }
#undef likely
#define unlikely(a, b, c) a + b + c
#define unlikely(a, b, c) a + b + c
#undef unlikely // { dg-warning "undefining keyword 'unlikely'" "" { target c++20 } }
#undef unlikely

View File

@ -104,9 +104,9 @@
#undef deprecated // { dg-error "undefining keyword 'deprecated'" "" { target c++26 } }
#undef fallthrough // { dg-error "undefining keyword 'fallthrough'" "" { target c++26 } }
#undef indeterminate
#undef likely // { dg-error "undefining keyword 'likely'" "" { target c++26 } }
#undef likely
#undef maybe_unused // { dg-error "undefining keyword 'maybe_unused'" "" { target c++26 } }
#undef nodiscard // { dg-error "undefining keyword 'nodiscard'" "" { target c++26 } }
#undef noreturn // { dg-error "undefining keyword 'noreturn'" "" { target c++26 } }
#undef no_unique_address // { dg-error "undefining keyword 'no_unique_address'" "" { target c++26 } }
#undef unlikely // { dg-error "undefining keyword 'unlikely'" "" { target c++26 } }
#undef unlikely

View File

@ -104,9 +104,9 @@
#undef deprecated // { dg-warning "undefining keyword 'deprecated'" "" { target c++26 } }
#undef fallthrough // { dg-warning "undefining keyword 'fallthrough'" "" { target c++26 } }
#undef indeterminate
#undef likely // { dg-warning "undefining keyword 'likely'" "" { target c++26 } }
#undef likely
#undef maybe_unused // { dg-warning "undefining keyword 'maybe_unused'" "" { target c++26 } }
#undef nodiscard // { dg-warning "undefining keyword 'nodiscard'" "" { target c++26 } }
#undef noreturn // { dg-warning "undefining keyword 'noreturn'" "" { target c++26 } }
#undef no_unique_address // { dg-warning "undefining keyword 'no_unique_address'" "" { target c++26 } }
#undef unlikely // { dg-warning "undefining keyword 'unlikely'" "" { target c++26 } }
#undef unlikely

View File

@ -104,9 +104,9 @@
#undef deprecated // { dg-warning "undefining keyword 'deprecated'" "" { target c++14 } }
#undef fallthrough // { dg-warning "undefining keyword 'fallthrough'" "" { target c++17 } }
#undef indeterminate
#undef likely // { dg-warning "undefining keyword 'likely'" "" { target c++20 } }
#undef likely
#undef maybe_unused // { dg-warning "undefining keyword 'maybe_unused'" "" { target c++17 } }
#undef nodiscard // { dg-warning "undefining keyword 'nodiscard'" "" { target c++17 } }
#undef noreturn // { dg-warning "undefining keyword 'noreturn'" "" { target c++11 } }
#undef no_unique_address // { dg-warning "undefining keyword 'no_unique_address'" "" { target c++20 } }
#undef unlikely // { dg-warning "undefining keyword 'unlikely'" "" { target c++20 } }
#undef unlikely

View File

@ -17,6 +17,6 @@
#undef inline // { dg-error "undefining keyword 'inline'" "" { target c++26 } }
#define inline __inline__ __attribute__((__always_inline__)) // { dg-error "keyword 'inline' defined as macro" "" { target c++26 } }
#define likely(a) a
#undef likely // { dg-error "undefining keyword 'likely'" "" { target c++26 } }
#undef likely
#define unlikely(a, b, c) a + b + c
#define unlikely(a, b, c) a + b + c

View File

@ -740,9 +740,14 @@ do_undef (cpp_reader *pfile)
&& !CPP_OPTION (pfile, suppress_builtin_macro_warnings)
&& cpp_keyword_p (node))
{
if (CPP_OPTION (pfile, cpp_pedantic)
&& CPP_OPTION (pfile, cplusplus)
&& CPP_OPTION (pfile, lang) >= CLK_GNUCXX26)
if (CPP_OPTION (pfile, cplusplus)
&& (strcmp ((const char *) NODE_NAME (node), "likely") == 0
|| strcmp ((const char *) NODE_NAME (node),
"unlikely") == 0))
/* CWG3053: likely and unlikely can be undefined. */;
else if (CPP_OPTION (pfile, cpp_pedantic)
&& CPP_OPTION (pfile, cplusplus)
&& CPP_OPTION (pfile, lang) >= CLK_GNUCXX26)
cpp_pedwarning (pfile, CPP_W_KEYWORD_MACRO,
"undefining keyword %qs", NODE_NAME (node));
else