diff options
Diffstat (limited to 'libcpp')
-rw-r--r-- | libcpp/ChangeLog | 12 | ||||
-rw-r--r-- | libcpp/directives.cc | 21 | ||||
-rw-r--r-- | libcpp/include/cpplib.h | 17 | ||||
-rw-r--r-- | libcpp/macro.cc | 35 |
4 files changed, 76 insertions, 9 deletions
diff --git a/libcpp/ChangeLog b/libcpp/ChangeLog index b5188fd..7deda4b 100644 --- a/libcpp/ChangeLog +++ b/libcpp/ChangeLog @@ -1,3 +1,15 @@ +2025-08-05 Jakub Jelinek <jakub@redhat.com> + + PR preprocessor/120778 + * macro.cc (paste_tokens): Use %< and %> instead of \" in + diagnostics around %.*s. + +2025-08-04 Jakub Jelinek <jakub@redhat.com> + + PR preprocessor/120778 + * macro.cc (stringify_arg): For C++26 emit a pedarn instead of warning + for \ at the end of stringification. + 2025-08-03 Jakub Jelinek <jakub@redhat.com> PR c++/120845 diff --git a/libcpp/directives.cc b/libcpp/directives.cc index 4d06caa..47fb8fb 100644 --- a/libcpp/directives.cc +++ b/libcpp/directives.cc @@ -734,13 +734,30 @@ do_undef (cpp_reader *pfile) if (pfile->cb.undef) pfile->cb.undef (pfile, pfile->directive_line, node); + /* Handle -Wkeyword-macro registered identifiers. */ + bool diagnosed = false; + if (CPP_OPTION (pfile, cpp_warn_keyword_macro) && cpp_keyword_p (node)) + { + 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 + cpp_warning (pfile, CPP_W_KEYWORD_MACRO, + "undefining keyword %qs", NODE_NAME (node)); + diagnosed = true; + } /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified identifier is not currently defined as a macro name. */ if (cpp_macro_p (node)) { if (node->flags & NODE_WARN) - cpp_error (pfile, CPP_DL_WARNING, - "undefining %qs", NODE_NAME (node)); + { + if (!diagnosed) + cpp_error (pfile, CPP_DL_WARNING, + "undefining %qs", NODE_NAME (node)); + } else if (cpp_builtin_macro_p (node) && CPP_OPTION (pfile, warn_builtin_macro_redefined)) cpp_warning (pfile, CPP_W_BUILTIN_MACRO_REDEFINED, diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h index 75efdcd..bbd88e5 100644 --- a/libcpp/include/cpplib.h +++ b/libcpp/include/cpplib.h @@ -620,6 +620,9 @@ struct cpp_options /* True if -finput-charset= option has been used explicitly. */ bool cpp_input_charset_explicit; + /* True if -Wkeyword-macro. */ + bool cpp_warn_keyword_macro; + /* -Wleading-whitespace= value. */ unsigned char cpp_warn_leading_whitespace; @@ -757,7 +760,8 @@ enum cpp_warning_reason { CPP_W_HEADER_GUARD, CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER, CPP_W_LEADING_WHITESPACE, - CPP_W_TRAILING_WHITESPACE + CPP_W_TRAILING_WHITESPACE, + CPP_W_KEYWORD_MACRO }; /* Callback for header lookup for HEADER, which is the name of a @@ -1250,6 +1254,17 @@ inline bool cpp_fun_like_macro_p (cpp_hashnode *node) return cpp_user_macro_p (node) && node->value.macro->fun_like; } +/* Return true for nodes marked for -Wkeyword-macro diagnostics. */ +inline bool cpp_keyword_p (cpp_hashnode *node) +{ + /* As keywords are marked identifiers which don't start with underscore + or start with underscore followed by capital letter (except for + _Pragma). */ + return ((node->flags & NODE_WARN) + && (NODE_NAME (node)[0] != '_' + || (NODE_NAME (node)[1] != '_' && NODE_NAME (node)[1] != 'P'))); +} + extern const unsigned char *cpp_macro_definition (cpp_reader *, cpp_hashnode *); extern const unsigned char *cpp_macro_definition (cpp_reader *, cpp_hashnode *, const cpp_macro *); diff --git a/libcpp/macro.cc b/libcpp/macro.cc index d869b02..a47e1fe 100644 --- a/libcpp/macro.cc +++ b/libcpp/macro.cc @@ -1071,7 +1071,7 @@ paste_tokens (cpp_reader *pfile, location_t location, /* Mandatory error for all apart from assembler. */ if (CPP_OPTION (pfile, lang) != CLK_ASM) cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0, - "pasting \"%.*s\" and \"%.*s\" does not give " + "pasting %<%.*s%> and %<%.*s%> does not give " "a valid preprocessing token", (int) (lhsend - buf), buf, (int) (end - rhsstart), rhsstart); @@ -3411,7 +3411,11 @@ warn_of_redefinition (cpp_reader *pfile, cpp_hashnode *node, { /* Some redefinitions need to be warned about regardless. */ if (node->flags & NODE_WARN) - return true; + { + /* Ignore NODE_WARN on -Wkeyword-macro registered identifiers though. */ + if (!CPP_OPTION (pfile, cpp_warn_keyword_macro) || !cpp_keyword_p (node)) + return true; + } /* Suppress warnings for builtins that lack the NODE_WARN flag, unless Wbuiltin-macro-redefined. */ @@ -3949,6 +3953,25 @@ _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node, if (name_loc) macro->line = name_loc; + /* Handle -Wkeyword-macro registered identifiers. */ + if (CPP_OPTION (pfile, cpp_warn_keyword_macro) && cpp_keyword_p (node)) + { + if (macro->fun_like + && CPP_OPTION (pfile, cplusplus) + && (strcmp ((const char *) NODE_NAME (node), "likely") == 0 + || strcmp ((const char *) NODE_NAME (node), "unlikely") == 0)) + /* likely and unlikely can be defined as function-like macros. */; + else if (CPP_OPTION (pfile, cpp_pedantic) + && CPP_OPTION (pfile, cplusplus) + && CPP_OPTION (pfile, lang) >= CLK_GNUCXX26) + cpp_pedwarning_with_line (pfile, CPP_W_KEYWORD_MACRO, macro->line, 0, + "keyword %qs defined as macro", + NODE_NAME (node)); + else + cpp_warning_with_line (pfile, CPP_W_KEYWORD_MACRO, macro->line, 0, + "keyword %qs defined as macro", + NODE_NAME (node)); + } if (cpp_macro_p (node)) { if (CPP_OPTION (pfile, warn_unused_macros)) @@ -3957,12 +3980,12 @@ _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node, if (warn_of_redefinition (pfile, node, macro)) { const enum cpp_warning_reason reason - = (cpp_builtin_macro_p (node) && !(node->flags & NODE_WARN)) - ? CPP_W_BUILTIN_MACRO_REDEFINED : CPP_W_NONE; + = (cpp_builtin_macro_p (node) && !(node->flags & NODE_WARN) + ? CPP_W_BUILTIN_MACRO_REDEFINED : CPP_W_NONE); bool warned - = cpp_pedwarning_with_line (pfile, reason, macro->line, 0, - "%qs redefined", NODE_NAME (node)); + = cpp_pedwarning_with_line (pfile, reason, macro->line, 0, + "%qs redefined", NODE_NAME (node)); if (warned && cpp_user_macro_p (node)) cpp_error_with_line (pfile, CPP_DL_NOTE, node->value.macro->line, |