summaryrefslogtreecommitdiff
path: root/libcpp/init.cc
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2025-08-15 22:31:27 +0200
committerJakub Jelinek <jakub@gcc.gnu.org>2025-08-15 22:31:27 +0200
commitcdd015c4ddbb1ae71eea1e44654cee5ca29a6c64 (patch)
tree0ef721a32855f4fc2f282a6f2d2e363728ba900a /libcpp/init.cc
parent87f354ca75361faa2f40b826d632fbc49a082553 (diff)
c++: Warn on #undef/#define of remaining cpp.predefined macros [PR120778]
We already warn on #undef or pedwarn on #define (but not on #define after #undef) of some builtin macros mentioned in cpp.predefined. The C++26 P2843R3 paper changes it from (compile time) undefined behavior to ill-formed. The following patch arranges for warning (for #undef) and pedwarn (on #define) for the remaining cpp.predefined macros. __cpp_* feature test macros only for C++20 which added some of them to cpp.predefined, in earlier C++ versions it was just an extension and for pedantic diagnostic I think we don't need to diagnose anything, __STDCPP_* and __cplusplus macros for all C++ versions where they appeared. Like the earlier posted -Wkeyword-macro diagnostics (which is done regardless whether the identifier is defined as a macro or not, obviously most likely none of the keywords are defined as macros initially), this one also warns on #undef when a macro isn't defined or later #define after #undef. 2025-08-15 Jakub Jelinek <jakub@redhat.com> PR preprocessor/120778 PR target/121520 gcc/c-family/ * c-cppbuiltin.cc (c_cpp_builtins): Implement C++26 DR 2581. Add cpp_define_warn lambda and use it as well as cpp_warn where needed. In the if (c_dialect_cxx ()) block with __cpp_* predefinitions add cpp_define lambda. Formatting fixes. gcc/c/ * c-decl.cc (c_init_decl_processing): Use cpp_warn instead of cpp_lookup and NODE_WARN bit setting. gcc/cp/ * lex.cc (cxx_init): Remove warn_on lambda. Use cpp_warn instead of cpp_lookup and NODE_WARN bit setting or warn_on. gcc/testsuite/ * g++.dg/DRs/dr2581-1.C: New test. * g++.dg/DRs/dr2581-2.C: New test. * c-c++-common/cpp/pr92296-2.c: Expect warnings also on defining special macros after undefining them. libcpp/ * include/cpplib.h (struct cpp_options): Add suppress_builtin_macro_warnings member. (cpp_warn): New inline functions. * init.cc (cpp_create_reader): Clear suppress_builtin_macro_warnings. (cpp_init_builtins): Call cpp_warn on __cplusplus, __STDC__, __STDC_VERSION__, __STDC_MB_MIGHT_NEQ_WC__ and __STDCPP_STRICT_POINTER_SAFETY__ when appropriate. * directives.cc (do_undef): Warn on undefining NODE_WARN macros if not cpp_keyword_p. Don't emit any NODE_WARN related diagnostics if CPP_OPTION (pfile, suppress_builtin_macro_warnings). (cpp_define, _cpp_define_builtin, cpp_undef): Temporarily set CPP_OPTION (pfile, suppress_builtin_macro_warnings) around run_directive calls. * macro.cc (_cpp_create_definition): Warn on defining NODE_WARN macros if they weren't previously defined and not cpp_keyword_p. Ignore NODE_WARN for diagnostics if CPP_OPTION (pfile, suppress_builtin_macro_warnings).
Diffstat (limited to 'libcpp/init.cc')
-rw-r--r--libcpp/init.cc11
1 files changed, 11 insertions, 0 deletions
diff --git a/libcpp/init.cc b/libcpp/init.cc
index e9732afeb2c..eb495e26eff 100644
--- a/libcpp/init.cc
+++ b/libcpp/init.cc
@@ -246,6 +246,7 @@ cpp_create_reader (enum c_lang lang, cpp_hash_table *table,
CPP_OPTION (pfile, dollars_in_ident) = 1;
CPP_OPTION (pfile, warn_dollars) = 1;
CPP_OPTION (pfile, warn_variadic_macros) = 1;
+ CPP_OPTION (pfile, suppress_builtin_macro_warnings) = 0;
CPP_OPTION (pfile, warn_builtin_macro_redefined) = 1;
CPP_OPTION (pfile, cpp_warn_implicit_fallthrough) = 0;
CPP_OPTION (pfile, warn_header_guard) = 0;
@@ -593,6 +594,8 @@ cpp_init_builtins (cpp_reader *pfile, int hosted)
&& (! CPP_OPTION (pfile, stdc_0_in_system_headers)
|| CPP_OPTION (pfile, std)))
_cpp_define_builtin (pfile, "__STDC__ 1");
+ else if (CPP_OPTION (pfile, cplusplus))
+ cpp_warn (pfile, "__STDC__");
if (CPP_OPTION (pfile, cplusplus))
{
@@ -618,6 +621,14 @@ cpp_init_builtins (cpp_reader *pfile, int hosted)
_cpp_define_builtin (pfile, "__cplusplus 201103L");
else
_cpp_define_builtin (pfile, "__cplusplus 199711L");
+ cpp_warn (pfile, "__cplusplus");
+ if (CPP_OPTION (pfile, lang) >= CLK_GNUCXX11)
+ {
+ cpp_warn (pfile, "__STDC_VERSION__");
+ cpp_warn (pfile, "__STDC_MB_MIGHT_NEQ_WC__");
+ if (CPP_OPTION (pfile, lang) < CLK_GNUCXX23)
+ cpp_warn (pfile, "__STDCPP_STRICT_POINTER_SAFETY__");
+ }
}
else if (CPP_OPTION (pfile, lang) == CLK_ASM)
_cpp_define_builtin (pfile, "__ASSEMBLER__ 1");