From 7a15a656165b75a3cc2433d04e79a92b4b1bd445 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Wed, 5 Apr 2017 10:36:10 -0400
Subject: [PATCH 44/46] FIXME: WIP on erroneous angled-bracket includes

---
 gcc/testsuite/gcc.dg/cpp/bad-angled-include.c   |  7 +++++++
 gcc/testsuite/gcc.dg/cpp/not-a-system-include.h |  1 +
 libcpp/files.c                                  | 25 +++++++++++++++++++------
 libcpp/init.c                                   |  2 +-
 libcpp/internal.h                               |  3 ++-
 5 files changed, 30 insertions(+), 8 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/cpp/bad-angled-include.c
 create mode 100644 gcc/testsuite/gcc.dg/cpp/not-a-system-include.h

diff --git a/gcc/testsuite/gcc.dg/cpp/bad-angled-include.c b/gcc/testsuite/gcc.dg/cpp/bad-angled-include.c
new file mode 100644
index 0000000..fba9b35
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/cpp/bad-angled-include.c
@@ -0,0 +1,7 @@
+/* { dg-do compile } */
+
+#include <not-a-system-include.h> /* { dg-error "not-a-system-include.h: No such file or directory" } */
+
+/* { dg-message "terminated" "terminated" { target *-*-* } 0 } */
+
+int make_non_empty;
diff --git a/gcc/testsuite/gcc.dg/cpp/not-a-system-include.h b/gcc/testsuite/gcc.dg/cpp/not-a-system-include.h
new file mode 100644
index 0000000..5cc7ca2
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/cpp/not-a-system-include.h
@@ -0,0 +1 @@
+/* dummy include */
diff --git a/libcpp/files.c b/libcpp/files.c
index 969a531..50334f1 100644
--- a/libcpp/files.c
+++ b/libcpp/files.c
@@ -507,7 +507,7 @@ _cpp_find_failed (_cpp_file *file)
 _cpp_file *
 _cpp_find_file (cpp_reader *pfile, const char *fname, cpp_dir *start_dir,
 		bool fake, int angle_brackets, bool implicit_preinclude,
-		source_location loc)
+		source_location loc, enum include_type type)
 {
   struct cpp_file_hash_entry *entry;
   void **hash_slot;
@@ -588,7 +588,18 @@ _cpp_find_file (cpp_reader *pfile, const char *fname, cpp_dir *start_dir,
 	      return NULL;
 	    }
 	  else
-	    open_file_failed (pfile, file, angle_brackets, loc);
+	    {
+	      if (angle_brackets)
+		{
+		  /* Try again without angle brackets.  */
+		  if (_cpp_has_header (pfile, fname, 0 /* angle_brackets*/,
+				       type))
+		    fprintf (stderr, "found %s without angle brackets\n",
+			     fname);
+		  /* FIXME: do we leak? */
+		}
+	      open_file_failed (pfile, file, angle_brackets, loc);
+	    }
 	  break;
 	}
 
@@ -1030,7 +1041,7 @@ _cpp_stack_include (cpp_reader *pfile, const char *fname, int angle_brackets,
     return false;
 
   file = _cpp_find_file (pfile, fname, dir, false, angle_brackets,
-			 type == IT_DEFAULT, loc);
+			 type == IT_DEFAULT, loc, type);
   if (type == IT_DEFAULT && file == NULL)
     return false;
 
@@ -1345,7 +1356,8 @@ cpp_clear_file_cache (cpp_reader *pfile)
 void
 _cpp_fake_include (cpp_reader *pfile, const char *fname)
 {
-  _cpp_find_file (pfile, fname, pfile->buffer->file->dir, true, 0, false, 0);
+  _cpp_find_file (pfile, fname, pfile->buffer->file->dir, true, 0, false, 0,
+		  IT_DEFAULT);
 }
 
 /* Not everyone who wants to set system-header-ness on a buffer can
@@ -1463,7 +1475,8 @@ _cpp_compare_file_date (cpp_reader *pfile, const char *fname,
   if (!dir)
     return -1;
 
-  file = _cpp_find_file (pfile, fname, dir, false, angle_brackets, false, 0);
+  file = _cpp_find_file (pfile, fname, dir, false, angle_brackets, false, 0,
+			 IT_INCLUDE);
   if (file->err_no)
     return -1;
 
@@ -2008,7 +2021,7 @@ _cpp_has_header (cpp_reader *pfile, const char *fname, int angle_brackets,
   cpp_dir *start_dir = search_path_head (pfile, fname, angle_brackets, type);
   _cpp_file *file = _cpp_find_file (pfile, fname, start_dir,
 				    /*fake=*/false, angle_brackets,
-				    /*implicit_preinclude=*/false, 0);
+				    /*implicit_preinclude=*/false, 0, type);
   return file->err_no != ENOENT;
 }
 
diff --git a/libcpp/init.c b/libcpp/init.c
index 0679b7f..8b3269f 100644
--- a/libcpp/init.c
+++ b/libcpp/init.c
@@ -630,7 +630,7 @@ cpp_read_main_file (cpp_reader *pfile, const char *fname)
 
   pfile->main_file
     = _cpp_find_file (pfile, fname, &pfile->no_search_path, false, 0, false,
-		      loc);
+		      loc, IT_DEFAULT);
   if (_cpp_find_failed (pfile->main_file))
     return NULL;
 
diff --git a/libcpp/internal.h b/libcpp/internal.h
index f24e85c..0a0c1a5 100644
--- a/libcpp/internal.h
+++ b/libcpp/internal.h
@@ -645,7 +645,8 @@ extern void _cpp_destroy_hashtable (cpp_reader *);
 /* In files.c */
 typedef struct _cpp_file _cpp_file;
 extern _cpp_file *_cpp_find_file (cpp_reader *, const char *, cpp_dir *,
-				  bool, int, bool, source_location);
+				  bool, int, bool, source_location,
+				  enum include_type);
 extern bool _cpp_find_failed (_cpp_file *);
 extern void _cpp_mark_file_once_only (cpp_reader *, struct _cpp_file *);
 extern void _cpp_fake_include (cpp_reader *, const char *);
-- 
1.8.5.3

