diff --git a/gcc/c-family/c-common.cc b/gcc/c-family/c-common.cc index 655c3aefee6..6e41ceb38e9 100644 --- a/gcc/c-family/c-common.cc +++ b/gcc/c-family/c-common.cc @@ -284,9 +284,11 @@ int c_inhibit_evaluation_warnings; be generated. */ bool in_late_binary_op; -/* Whether lexing has been completed, so subsequent preprocessor - errors should use the compiler's input_location. */ -bool done_lexing = false; +/* Depending on which phase of processing we are in, we may need + to prefer input_location to libcpp's locations. (Specifically, + after the C++ lexer is done lexing tokens, but prior to calling + cpp_finish (), we need to do so. */ +bool override_libcpp_locations; /* Information about how a function name is generated. */ struct fname_var_t @@ -6681,7 +6683,7 @@ c_cpp_diagnostic (cpp_reader *pfile ATTRIBUTE_UNUSED, default: gcc_unreachable (); } - if (done_lexing) + if (override_libcpp_locations) richloc->set_range (0, input_location, SHOW_RANGE_WITH_CARET); diagnostic_set_info_translated (&diagnostic, msg, ap, richloc, dlevel); diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h index f9064393b4e..c06769b6f0b 100644 --- a/gcc/c-family/c-common.h +++ b/gcc/c-family/c-common.h @@ -767,10 +767,12 @@ extern int max_tinst_depth; extern int c_inhibit_evaluation_warnings; -/* Whether lexing has been completed, so subsequent preprocessor - errors should use the compiler's input_location. */ +/* Depending on which phase of processing we are in, we may need + to prefer input_location to libcpp's locations. (Specifically, + after the C++ lexer is done lexing tokens, but prior to calling + cpp_finish (), we need to do so. */ -extern bool done_lexing; +extern bool override_libcpp_locations; /* C types are partitioned into three subsets: object, function, and incomplete types. */ diff --git a/gcc/c-family/c-opts.cc b/gcc/c-family/c-opts.cc index b9f01a65ed7..4e1463689de 100644 --- a/gcc/c-family/c-opts.cc +++ b/gcc/c-family/c-opts.cc @@ -1281,6 +1281,12 @@ c_common_finish (void) } } + /* When we call cpp_finish (), it may generate some diagnostics using + locations it remembered from the preprocessing phase, e.g. for + -Wunused-macros. So inform c_cpp_diagnostic () not to override those + locations with input_location, which would be incorrect now. */ + override_libcpp_locations = false; + /* For performance, avoid tearing down cpplib's internal structures with cpp_destroy (). */ cpp_finish (parse_in, deps_stream); diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index 4f67441eeb1..66e6fde3e28 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -755,7 +755,7 @@ cp_lexer_new_main (void) /* Subsequent preprocessor diagnostics should use compiler diagnostic functions to get the compiler source location. */ - done_lexing = true; + override_libcpp_locations = true; maybe_check_all_macros (parse_in); diff --git a/gcc/testsuite/c-c++-common/pragma-diag-15.c b/gcc/testsuite/c-c++-common/pragma-diag-15.c index d8076b4f93a..8ffff88d5e3 100644 --- a/gcc/testsuite/c-c++-common/pragma-diag-15.c +++ b/gcc/testsuite/c-c++-common/pragma-diag-15.c @@ -9,5 +9,5 @@ because the location of the macro definition is incorrectly set. This is a separate issue, will resolve it in a later patch. */ -#define X /* { dg-warning "-:-Wunused-macros" {} { xfail c++ } } */ +#define X /* { dg-warning "-:-Wunused-macros" } */ #pragma GCC diagnostic ignored "-Wunused-macros"