From d5b0632bdf78f481859a0013838f974180877661 Mon Sep 17 00:00:00 2001
From: Mike Gulick <mgulick@mathworks.com>
Date: Thu, 30 Nov 2017 18:35:48 -0500
Subject: [PATCH 43/49] PR preprocessor/83173: New test

2017-12-01  Mike Gulick  <mgulick@mathworks.com>

	PR preprocessor/83173
	* gcc.dg/plugin/pr83173.c: New test.
	* gcc.dg/plugin/pr83173.h: Header for pr83173.c
	* gcc.dg/plugin/pr83173-1.h: Header for pr83173.c
	* gcc.dg/plugin/pr83173-2.h: Header for pr83173.c
	* gcc.dg/plugin/location_overflow_pp_plugin.c: New plugin to
	override line_table->highest_location for preprocessor.
---
 .../gcc.dg/plugin/location_overflow_pp_plugin.c    | 44 ++++++++++++++++++++++
 gcc/testsuite/gcc.dg/plugin/plugin.exp             |  1 +
 gcc/testsuite/gcc.dg/plugin/pr83173-1.h            |  2 +
 gcc/testsuite/gcc.dg/plugin/pr83173-2.h            |  2 +
 gcc/testsuite/gcc.dg/plugin/pr83173.c              | 21 +++++++++++
 gcc/testsuite/gcc.dg/plugin/pr83173.h              |  2 +
 6 files changed, 72 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/plugin/location_overflow_pp_plugin.c
 create mode 100644 gcc/testsuite/gcc.dg/plugin/pr83173-1.h
 create mode 100644 gcc/testsuite/gcc.dg/plugin/pr83173-2.h
 create mode 100644 gcc/testsuite/gcc.dg/plugin/pr83173.c
 create mode 100644 gcc/testsuite/gcc.dg/plugin/pr83173.h

diff --git a/gcc/testsuite/gcc.dg/plugin/location_overflow_pp_plugin.c b/gcc/testsuite/gcc.dg/plugin/location_overflow_pp_plugin.c
new file mode 100644
index 0000000..ba5a795
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/plugin/location_overflow_pp_plugin.c
@@ -0,0 +1,44 @@
+/* Plugin for testing how gracefully we degrade in the face of very
+   large source files.  */
+
+#include "config.h"
+#include "gcc-plugin.h"
+#include "system.h"
+#include "coretypes.h"
+#include "diagnostic.h"
+
+int plugin_is_GPL_compatible;
+
+static location_t base_location;
+
+/* Callback handler for the PLUGIN_PRAGMAS event.  This is used to set the
+   initial line table offset for the preprocessor, to make it appear as if we
+   had parsed a very large file.  PRAGMA_START_UNIT is not suitable here as is
+   not invoked during the preprocessor stage.  */
+
+static void
+on_pragma_registration (void *gcc_data, void *user_data)
+{
+  line_table->highest_location = base_location;
+}
+
+int
+plugin_init (struct plugin_name_args *plugin_info,
+	     struct plugin_gcc_version * /*version */ )
+{
+  /* Read VALUE from -fplugin-arg-location_overflow_pp_plugin-value=<VALUE>
+     in hexadecimal form into base_location.  */
+  for (int i = 0; i < plugin_info->argc; i++)
+    {
+      if (0 == strcmp (plugin_info->argv[i].key, "value"))
+	base_location = strtol (plugin_info->argv[i].value, NULL, 16);
+    }
+
+  if (!base_location)
+    error_at (UNKNOWN_LOCATION, "missing plugin argument");
+
+  register_callback (plugin_info->base_name,
+		     PLUGIN_PRAGMAS, on_pragma_registration, NULL);
+
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/plugin/plugin.exp b/gcc/testsuite/gcc.dg/plugin/plugin.exp
index 5443635..2542dd0 100644
--- a/gcc/testsuite/gcc.dg/plugin/plugin.exp
+++ b/gcc/testsuite/gcc.dg/plugin/plugin.exp
@@ -102,6 +102,7 @@ set plugin_test_list [list \
     { location_overflow_plugin.c \
 	  location-overflow-test-1.c \
 	  location-overflow-test-2.c } \
+    { location_overflow_pp_plugin.c pr83173.c } \
     { must_tail_call_plugin.c \
 	  must-tail-call-1.c \
 	  must-tail-call-2.c } \
diff --git a/gcc/testsuite/gcc.dg/plugin/pr83173-1.h b/gcc/testsuite/gcc.dg/plugin/pr83173-1.h
new file mode 100644
index 0000000..bf05d56
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/plugin/pr83173-1.h
@@ -0,0 +1,2 @@
+#pragma once
+#define PR83173_1_H
diff --git a/gcc/testsuite/gcc.dg/plugin/pr83173-2.h b/gcc/testsuite/gcc.dg/plugin/pr83173-2.h
new file mode 100644
index 0000000..dd0fc94
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/plugin/pr83173-2.h
@@ -0,0 +1,2 @@
+#pragma once
+#define PR83173_2_H
diff --git a/gcc/testsuite/gcc.dg/plugin/pr83173.c b/gcc/testsuite/gcc.dg/plugin/pr83173.c
new file mode 100644
index 0000000..ff1858a
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/plugin/pr83173.c
@@ -0,0 +1,21 @@
+/*
+  { dg-options "-fplugin-arg-location_overflow_pp_plugin-value=0x60000001" }
+  { dg-additional-files "pr83173.h" "pr83173-1.h" "pr83173-2.h" }
+  { dg-do preprocess }
+*/
+
+#include "pr83173.h"
+
+int
+main ()
+{
+  return 0;
+}
+
+/*
+  The preprocessor output should contain '# 1 "path/to/pr83173-1.h" 1', but
+  should not contain any other references to pr83183-1.h.
+
+  { dg-final { scan-file pr83173.i "# 1 \[^\r\n\]+pr83173-1\.h\" 1" } }
+  { dg-final { scan-file-not pr83173.i "# (?!1 \[^\r\n\]+pr83173-1\.h\" 1)\[0-9\]+ \[^\r\n\]+pr83173-1\.h\"" } }
+*/
diff --git a/gcc/testsuite/gcc.dg/plugin/pr83173.h b/gcc/testsuite/gcc.dg/plugin/pr83173.h
new file mode 100644
index 0000000..a998a77
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/plugin/pr83173.h
@@ -0,0 +1,2 @@
+#include "pr83173-1.h"
+#include "pr83173-2.h"
-- 
1.8.5.3

