preprocessor: __has_include_next should not error out [PR80755]

Message ID Y5BdQ2An000WISET@alf.mars
State Accepted
Headers
Series preprocessor: __has_include_next should not error out [PR80755] |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

Helmut Grohne Dec. 7, 2022, 9:30 a.m. UTC
  If __has_include_next reaches the end of the search path, it causes an
error. The use of __has_include_next at the end of the search path is
legal and it should return false instead.

Bootstrapped and tested on x86_64-linux-gnu. Patched cross toolchain for
i686-gnu (hurd) built many packages.

gcc/ChangeLog:

	PR preprocessor/80755
	* libcpp/files.cc (search_path_head): Do not raise an error for
	type IT_INCLUDE_NEXT.
	* libcpp/files.cc (_cpp_has_header): Deal with NULL return from
	search_path_head.

gcc/testsuite/ChangeLog:

	PR preprocessor/80755
	* testsuite/gcc.dg/cpp/pr80755.c: New test.
	* gcc.dg/cpp/inc/pr80755.h: Added support file for test.

Signed-off-by: Helmut Grohne <helmut@subdivi.de>
---
 gcc/testsuite/gcc.dg/cpp/inc/pr80755.h | 2 ++
 gcc/testsuite/gcc.dg/cpp/pr80755.c     | 5 +++++
 libcpp/files.cc                        | 4 +++-
 3 files changed, 10 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.dg/cpp/inc/pr80755.h
 create mode 100644 gcc/testsuite/gcc.dg/cpp/pr80755.c
  

Patch

diff --git a/gcc/testsuite/gcc.dg/cpp/inc/pr80755.h b/gcc/testsuite/gcc.dg/cpp/inc/pr80755.h
new file mode 100644
index 00000000000..32022cc691d
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/cpp/inc/pr80755.h
@@ -0,0 +1,2 @@ 
+#if __has_include_next(<itdoesnotmatterwhetherthisheaderexists>)
+#endif
diff --git a/gcc/testsuite/gcc.dg/cpp/pr80755.c b/gcc/testsuite/gcc.dg/cpp/pr80755.c
new file mode 100644
index 00000000000..34ae995a6c9
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/cpp/pr80755.c
@@ -0,0 +1,5 @@ 
+/* PR preprocessor/80755 */
+/* { dg-do preprocess } */
+/* { dg-options "-idirafter $srcdir/gcc.dg/cpp/inc" } */
+
+#include "pr80755.h"
diff --git a/libcpp/files.cc b/libcpp/files.cc
index a18b1caf48d..606f53ed015 100644
--- a/libcpp/files.cc
+++ b/libcpp/files.cc
@@ -1042,7 +1042,7 @@  search_path_head (cpp_reader *pfile, const char *fname, int angle_brackets,
      path use the normal search logic.  */
   if (type == IT_INCLUDE_NEXT && file->dir
       && file->dir != &pfile->no_search_path)
-    dir = file->dir->next;
+    return file->dir->next;
   else if (angle_brackets)
     dir = pfile->bracket_include;
   else if (type == IT_CMDLINE)
@@ -2145,6 +2145,8 @@  _cpp_has_header (cpp_reader *pfile, const char *fname, int angle_brackets,
 		 enum include_type type)
 {
   cpp_dir *start_dir = search_path_head (pfile, fname, angle_brackets, type);
+  if (!start_dir)
+    return false;
   _cpp_file *file = _cpp_find_file (pfile, fname, start_dir, angle_brackets,
 				    _cpp_FFK_HAS_INCLUDE, 0);
   return file->err_no != ENOENT;