mirror of
https://gcc.gnu.org/git/gcc.git
synced 2026-02-22 03:46:53 -05:00
libcpp: Improve location for macro names [PR66290]
When libcpp reports diagnostics whose locus is a macro name (such as for -Wunused-macros), it uses the location in the cpp_macro object that was stored by _cpp_new_macro. This is currently set to pfile->directive_line, which contains the line number only and no column information. This patch changes the stored location to the src_loc for the token defining the macro name, which includes the location and range information. libcpp/ChangeLog: PR c++/66290 * macro.cc (_cpp_create_definition): Add location argument. * internal.h (_cpp_create_definition): Adjust prototype. * directives.cc (do_define): Pass new location argument to _cpp_create_definition. (do_undef): Stop passing inferior location to cpp_warning_with_line; the default from cpp_warning is better. (cpp_pop_definition): Pass new location argument to _cpp_create_definition. * pch.cc (cpp_read_state): Likewise. gcc/testsuite/ChangeLog: PR c++/66290 * c-c++-common/cpp/macro-ranges.c: New test. * c-c++-common/cpp/line-2.c: Adapt to check for column information on macro-related libcpp warnings. * c-c++-common/cpp/line-3.c: Likewise. * c-c++-common/cpp/macro-arg-count-1.c: Likewise. * c-c++-common/cpp/pr58844-1.c: Likewise. * c-c++-common/cpp/pr58844-2.c: Likewise. * c-c++-common/cpp/warning-zero-location.c: Likewise. * c-c++-common/pragma-diag-14.c: Likewise. * c-c++-common/pragma-diag-15.c: Likewise. * g++.dg/modules/macro-2_d.C: Likewise. * g++.dg/modules/macro-4_d.C: Likewise. * g++.dg/modules/macro-4_e.C: Likewise. * g++.dg/spellcheck-macro-ordering.C: Likewise. * gcc.dg/builtin-redefine.c: Likewise. * gcc.dg/cpp/Wunused.c: Likewise. * gcc.dg/cpp/redef2.c: Likewise. * gcc.dg/cpp/redef3.c: Likewise. * gcc.dg/cpp/redef4.c: Likewise. * gcc.dg/cpp/ucnid-11-utf8.c: Likewise. * gcc.dg/cpp/ucnid-11.c: Likewise. * gcc.dg/cpp/undef2.c: Likewise. * gcc.dg/cpp/warn-redefined-2.c: Likewise. * gcc.dg/cpp/warn-redefined.c: Likewise. * gcc.dg/cpp/warn-unused-macros-2.c: Likewise. * gcc.dg/cpp/warn-unused-macros.c: Likewise.
This commit is contained in:
@@ -655,6 +655,10 @@ do_define (cpp_reader *pfile)
|
||||
|
||||
if (node)
|
||||
{
|
||||
/* This is a better location than pfile->directive_line to store
|
||||
as the macro location. */
|
||||
const location_t name_loc = cpp_diagnostic_get_current_location (pfile);
|
||||
|
||||
/* If we have been requested to expand comments into macros,
|
||||
then re-enable saving of comments. */
|
||||
pfile->state.save_comments =
|
||||
@@ -663,7 +667,7 @@ do_define (cpp_reader *pfile)
|
||||
if (pfile->cb.before_define)
|
||||
pfile->cb.before_define (pfile);
|
||||
|
||||
if (_cpp_create_definition (pfile, node))
|
||||
if (_cpp_create_definition (pfile, node, name_loc))
|
||||
if (pfile->cb.define)
|
||||
pfile->cb.define (pfile, pfile->directive_line, node);
|
||||
|
||||
@@ -694,9 +698,8 @@ do_undef (cpp_reader *pfile)
|
||||
"undefining \"%s\"", NODE_NAME (node));
|
||||
else if (cpp_builtin_macro_p (node)
|
||||
&& CPP_OPTION (pfile, warn_builtin_macro_redefined))
|
||||
cpp_warning_with_line (pfile, CPP_W_BUILTIN_MACRO_REDEFINED,
|
||||
pfile->directive_line, 0,
|
||||
"undefining \"%s\"", NODE_NAME (node));
|
||||
cpp_warning (pfile, CPP_W_BUILTIN_MACRO_REDEFINED,
|
||||
"undefining \"%s\"", NODE_NAME (node));
|
||||
|
||||
if (node->value.macro
|
||||
&& CPP_OPTION (pfile, warn_unused_macros))
|
||||
@@ -2642,7 +2645,7 @@ cpp_pop_definition (cpp_reader *pfile, struct def_pragma_macro *c)
|
||||
{
|
||||
_cpp_clean_line (pfile);
|
||||
nbuf->sysp = 1;
|
||||
if (!_cpp_create_definition (pfile, h))
|
||||
if (!_cpp_create_definition (pfile, h, 0))
|
||||
abort ();
|
||||
_cpp_pop_buffer (pfile);
|
||||
}
|
||||
|
||||
@@ -686,7 +686,7 @@ inline bool _cpp_maybe_notify_macro_use (cpp_reader *pfile, cpp_hashnode *node,
|
||||
}
|
||||
extern cpp_macro *_cpp_new_macro (cpp_reader *, cpp_macro_kind, void *);
|
||||
extern void _cpp_free_definition (cpp_hashnode *);
|
||||
extern bool _cpp_create_definition (cpp_reader *, cpp_hashnode *);
|
||||
extern bool _cpp_create_definition (cpp_reader *, cpp_hashnode *, location_t);
|
||||
extern void _cpp_pop_context (cpp_reader *);
|
||||
extern void _cpp_push_text_context (cpp_reader *, cpp_hashnode *,
|
||||
const unsigned char *, size_t);
|
||||
|
||||
@@ -3819,7 +3819,8 @@ _cpp_new_macro (cpp_reader *pfile, cpp_macro_kind kind, void *placement)
|
||||
|
||||
/* Parse a macro and save its expansion. Returns nonzero on success. */
|
||||
bool
|
||||
_cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
|
||||
_cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node,
|
||||
location_t name_loc)
|
||||
{
|
||||
cpp_macro *macro;
|
||||
|
||||
@@ -3831,6 +3832,13 @@ _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
|
||||
if (!macro)
|
||||
return false;
|
||||
|
||||
/* _cpp_new_macro () has set macro->line to pfile->directive_line, which
|
||||
denotes the line containing the #define with no column information. If
|
||||
provided, change to name_loc, which will be the token src_loc for the
|
||||
macro name, including the location and range information. */
|
||||
if (name_loc)
|
||||
macro->line = name_loc;
|
||||
|
||||
if (cpp_macro_p (node))
|
||||
{
|
||||
if (CPP_OPTION (pfile, warn_unused_macros))
|
||||
@@ -3844,7 +3852,7 @@ _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
|
||||
|
||||
bool warned =
|
||||
cpp_pedwarning_with_line (pfile, reason,
|
||||
pfile->directive_line, 0,
|
||||
macro->line, 0,
|
||||
"\"%s\" redefined", NODE_NAME (node));
|
||||
|
||||
if (warned && cpp_user_macro_p (node))
|
||||
|
||||
@@ -838,7 +838,7 @@ cpp_read_state (cpp_reader *r, const char *name, FILE *f,
|
||||
!= NULL)
|
||||
{
|
||||
_cpp_clean_line (r);
|
||||
if (!_cpp_create_definition (r, h))
|
||||
if (!_cpp_create_definition (r, h, 0))
|
||||
abort ();
|
||||
_cpp_pop_buffer (r);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user