diff options
| author | Lewis Hyatt <lhyatt@gmail.com> | 2025-02-11 13:45:41 -0500 |
|---|---|---|
| committer | Lewis Hyatt <lhyatt@gcc.gnu.org> | 2025-04-27 22:35:25 -0400 |
| commit | 78673484b4055b93207eee0efd60a434b0bf96ab (patch) | |
| tree | 2072b64344552e1acf6fa4c106d05ab96c272c8f /libcpp | |
| parent | dea7b9a78b11b5ca0c85b971521144ba07a66aca (diff) | |
c-family: Improve location for -Wunknown-pragmas in a _Pragma [PR118838]
The warning for -Wunknown-pragmas is issued at the location provided by
libcpp to the def_pragma() callback. This location is
cpp_reader::directive_line, which is a location for the start of the line
only; it is also not a valid location in case the unknown pragma was lexed
from a _Pragma string. These factors make it impossible to suppress
-Wunknown-pragmas via _Pragma("GCC diagnostic...") directives on the same
source line, as in the PR and the test case. Address that by issuing the
warning at a better location returned by cpp_get_diagnostic_override_loc().
libcpp already maintains this location to handle _Pragma-related diagnostics
internally; it was needed also to make a publicly accessible version of it.
gcc/c-family/ChangeLog:
PR c/118838
* c-lex.cc (cb_def_pragma): Call cpp_get_diagnostic_override_loc()
to get a valid location at which to issue -Wunknown-pragmas, in case
it was triggered from a _Pragma.
libcpp/ChangeLog:
PR c/118838
* errors.cc (cpp_get_diagnostic_override_loc): New function.
* include/cpplib.h (cpp_get_diagnostic_override_loc): Declare.
gcc/testsuite/ChangeLog:
PR c/118838
* c-c++-common/cpp/pragma-diagnostic-loc-2.c: New test.
* g++.dg/gomp/macro-4.C: Adjust expected output.
* gcc.dg/gomp/macro-4.c: Likewise.
* gcc.dg/cpp/Wunknown-pragmas-1.c: Likewise.
Diffstat (limited to 'libcpp')
| -rw-r--r-- | libcpp/errors.cc | 10 | ||||
| -rw-r--r-- | libcpp/include/cpplib.h | 2 |
2 files changed, 12 insertions, 0 deletions
diff --git a/libcpp/errors.cc b/libcpp/errors.cc index 9621c4b66ea..d9efb6acd30 100644 --- a/libcpp/errors.cc +++ b/libcpp/errors.cc @@ -52,6 +52,16 @@ cpp_diagnostic_get_current_location (cpp_reader *pfile) } } +/* Sometimes a diagnostic needs to be generated before libcpp has been able + to generate a valid location for the current token; in that case, the + non-zero location returned by this function is the preferred one to use. */ + +location_t +cpp_get_diagnostic_override_loc (const cpp_reader *pfile) +{ + return pfile->diagnostic_override_loc; +} + /* Print a diagnostic at the given location. */ ATTRIBUTE_CPP_PPDIAG (5, 0) diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h index 3bf33ab4884..7c147ae5847 100644 --- a/libcpp/include/cpplib.h +++ b/libcpp/include/cpplib.h @@ -1169,6 +1169,8 @@ extern const char *cpp_probe_header_unit (cpp_reader *, const char *file, extern const char *cpp_get_narrow_charset_name (cpp_reader *) ATTRIBUTE_PURE; extern const char *cpp_get_wide_charset_name (cpp_reader *) ATTRIBUTE_PURE; +extern location_t cpp_get_diagnostic_override_loc (const cpp_reader *); + /* This function reads the file, but does not start preprocessing. It returns the name of the original file; this is the same as the input file, except for preprocessed input. This will generate at |
