public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Helmut Grohne <helmut@subdivi.de>
To: gcc-patches@gcc.gnu.org
Subject: [PATCH] preprocessor: __has_include_next should not error out [PR80755]
Date: Wed, 7 Dec 2022 10:30:43 +0100	[thread overview]
Message-ID: <Y5BdQ2An000WISET@alf.mars> (raw)

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

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;
-- 
2.38.1



                 reply	other threads:[~2022-12-07  9:31 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Y5BdQ2An000WISET@alf.mars \
    --to=helmut@subdivi.de \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).