aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorLewis Hyatt <lhyatt@gmail.com>2024-03-22 12:55:27 -0400
committerLewis Hyatt <lhyatt@gcc.gnu.org>2024-10-19 19:25:39 -0400
commit65c5bbe1c92f9c08e99d3a37c136f2ef9804a37f (patch)
treedb81dc45464d28b24ab77dddaf1d8ab1872847c0 /gcc
parent7fa9fd47f161d6b8177439ae2b46a2048a5eaa24 (diff)
downloadgcc-65c5bbe1c92f9c08e99d3a37c136f2ef9804a37f.zip
gcc-65c5bbe1c92f9c08e99d3a37c136f2ef9804a37f.tar.gz
gcc-65c5bbe1c92f9c08e99d3a37c136f2ef9804a37f.tar.bz2
diagnostics: libcpp: Improve locations for _Pragma lexing diagnostics [PR114423]
libcpp is not currently set up to be able to generate valid locations for tokens lexed from a _Pragma string. Instead, after obtaining the tokens, it sets their locations all to the location of the _Pragma operator itself. This makes things like _Pragma("GCC diagnostic") work well enough, but if any diagnostics are issued during lexing, prior to resetting the token locations, those diagnostics get issued at the invalid locations. Fix that up by adding a new field pfile->diagnostic_override_loc that instructs libcpp to issue diagnostics at the alternate location. libcpp/ChangeLog: PR preprocessor/114423 * internal.h (struct cpp_reader): Add DIAGNOSTIC_OVERRIDE_LOC field. * directives.cc (destringize_and_run): Set the new field to the location of the _Pragma operator. * errors.cc (cpp_diagnostic_at): Support DIAGNOSTIC_OVERRIDE_LOC to temporarily issue diagnostics at a different location. (cpp_diagnostic_with_line): Likewise. gcc/testsuite/ChangeLog: PR preprocessor/114423 * c-c++-common/cpp/pragma-diagnostic-loc.c: New test. * c-c++-common/cpp/diagnostic-pragma-1.c: Adjust expected output. * g++.dg/pch/operator-1.C: Likewise.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/testsuite/c-c++-common/cpp/diagnostic-pragma-1.c9
-rw-r--r--gcc/testsuite/c-c++-common/cpp/pragma-diagnostic-loc.c17
-rw-r--r--gcc/testsuite/g++.dg/pch/operator-1.C8
3 files changed, 28 insertions, 6 deletions
diff --git a/gcc/testsuite/c-c++-common/cpp/diagnostic-pragma-1.c b/gcc/testsuite/c-c++-common/cpp/diagnostic-pragma-1.c
index 9867c94..6e37294 100644
--- a/gcc/testsuite/c-c++-common/cpp/diagnostic-pragma-1.c
+++ b/gcc/testsuite/c-c++-common/cpp/diagnostic-pragma-1.c
@@ -3,9 +3,8 @@
#pragma GCC warning "warn-a" // { dg-warning warn-a }
#pragma GCC error "err-b" // { dg-error err-b }
-#define CONST1 _Pragma("GCC warning \"warn-c\"") 1
-#define CONST2 _Pragma("GCC error \"err-d\"") 2
-
-char a[CONST1]; // { dg-warning warn-c }
-char b[CONST2]; // { dg-error err-d }
+#define CONST1 _Pragma("GCC warning \"warn-c\"") 1 // { dg-warning warn-c }
+#define CONST2 _Pragma("GCC error \"err-d\"") 2 // { dg-error err-d }
+char a[CONST1]; // { dg-note "in expansion of macro 'CONST1'" }
+char b[CONST2]; // { dg-note "in expansion of macro 'CONST2'" }
diff --git a/gcc/testsuite/c-c++-common/cpp/pragma-diagnostic-loc.c b/gcc/testsuite/c-c++-common/cpp/pragma-diagnostic-loc.c
new file mode 100644
index 0000000..4ef4042
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/cpp/pragma-diagnostic-loc.c
@@ -0,0 +1,17 @@
+/* { dg-do preprocess } */
+/* PR preprocessor/114423 */
+/* Check that we now issue diagnostics at the location of the _Pragma
+ instead of an invalid location. If we someday manage to issue
+ diagnostics at better locations in the future, this will need
+ updating. */
+_Pragma("GCC warning \"warning1\"") /* { dg-warning "1:warning1" } */
+#define P _Pragma("GCC warning \"warning2\"") /* { dg-warning "11:warning2" } */
+P /* { dg-note "in expansion of macro" } */
+#define S "GCC warning \"warning3\""
+/**/ _Pragma(S) /* { dg-warning "6:warning3" } */
+
+/* This diagnostic uses a different code path (cpp_diagnostic_at() rather
+ than cpp_error_with_line()). Also make sure that the dg-note location
+ does not get overridden to the _Pragma location. */
+#pragma GCC poison xyz /* { dg-note "poisoned here" } */
+/* */ _Pragma("xyz") /* { dg-error "7:attempt to use poisoned" } */
diff --git a/gcc/testsuite/g++.dg/pch/operator-1.C b/gcc/testsuite/g++.dg/pch/operator-1.C
index 290b5f7..1138a96 100644
--- a/gcc/testsuite/g++.dg/pch/operator-1.C
+++ b/gcc/testsuite/g++.dg/pch/operator-1.C
@@ -1,2 +1,8 @@
#include "operator-1.H"
-int main(void){ major(0);} /* { dg-warning "Did not Work" } */
+int main(void){ major(0);} /* { dg-note "in expansion of macro 'major'" } */
+/* Line numbers below pertain to the header file. */
+/* { dg-warning "Did not Work" "" { target *-*-* } 1 } */
+/* { dg-note "in expansion of macro '__glibc_macro_warning1'" "" { target *-*-* } 3 } */
+/* { dg-note "in expansion of macro '__glibc_macro_warning'" "" { target *-*-* } 4 } */
+/* { dg-note "in expansion of macro '__SYSMACROS_DM1'" "" { target *-*-* } 6 } */
+/* { dg-note "in expansion of macro '__SYSMACROS_DM'" "" { target *-*-* } 9 } */