[2/2] libcpp: Fix incorrect line information for traditional cpp and #include [PR100904]

Message ID 20250211084747.149547-2-quic_apinski@quicinc.com
State New
Headers
Series [1/2] libcpp: Fix handling of `deferred` pragmas with -traditional [PR79516] |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gcc_check--master-arm success Test passed
linaro-tcwg-bot/tcwg_simplebootstrap_build--master-aarch64-bootstrap success Build passed
linaro-tcwg-bot/tcwg_simplebootstrap_build--master-arm-bootstrap success Build passed
linaro-tcwg-bot/tcwg_gcc_check--master-aarch64 success Test passed

Commit Message

Andrew Pinski Feb. 11, 2025, 8:47 a.m. UTC
  After r7-1651-gac81cf0b2bf5efdd7, the location for the error for #include would
be the location on the token. Except in traditional cpp, the location information
for directives is all messed up because first libcpp processes the directive line in traditional
and copies it to a new buffer and then does the lexing using the ISO lexer. This means the location
information for the tokens are wrong and should just grab the location of the directive line instead.
This patch does exactly that. Uses directive line location for traditional cpp when parsing the include.

Bootstrapped and tested on x86_64-linux-gnu.

	PR preprocessor/100904

libcpp/ChangeLog:

	* directives.cc (parse_include): Use the directive line location
	for the location in traditional cpp mode instead of the location
	of the token.

gcc/testsuite/ChangeLog:

	* gcc.dg/cpp/missing-header-trad-1.c: New test.

Signed-off-by: Andrew Pinski <quic_apinski@quicinc.com>
---
 gcc/testsuite/gcc.dg/cpp/missing-header-trad-1.c | 10 ++++++++++
 libcpp/directives.cc                             |  9 ++++++++-
 2 files changed, 18 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.dg/cpp/missing-header-trad-1.c
  

Patch

diff --git a/gcc/testsuite/gcc.dg/cpp/missing-header-trad-1.c b/gcc/testsuite/gcc.dg/cpp/missing-header-trad-1.c
new file mode 100644
index 00000000000..d77cc5fe228
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/cpp/missing-header-trad-1.c
@@ -0,0 +1,10 @@ 
+/* { dg-do preprocess } */
+/* { dg-options "-traditional-cpp" } */
+
+/* PR preprocessor/100904 */
+/* Make sure we error out on the correct line
+   for traditional cpp. */
+
+#include "nonexistent.h" /* { dg-error "-: nonexistent.h"  } */
+
+/* { dg-message "terminated" "terminated" { target *-*-* } 0 } */
diff --git a/libcpp/directives.cc b/libcpp/directives.cc
index 9c0f77ab017..d4a5ab1cbec 100644
--- a/libcpp/directives.cc
+++ b/libcpp/directives.cc
@@ -841,7 +841,14 @@  parse_include (cpp_reader *pfile, int *pangle_brackets,
 
   /* Allow macro expansion.  */
   header = get_token_no_padding (pfile);
-  *location = header->src_loc;
+
+  /* The location for traditional is the directive line as the
+     token line information for the temporary buffer.  */
+  if (CPP_OPTION (pfile, traditional))
+    *location = pfile->directive_line;
+  else
+    *location = header->src_loc;
+
   if ((header->type == CPP_STRING && header->val.str.text[0] != 'R')
       || header->type == CPP_HEADER_NAME)
     {