re PR preprocessor/90581 (provide an option to adjust the maximum depth of nested...
authorQing Zhao <qing.zhao@oracle.com>
Tue, 2 Jul 2019 20:23:30 +0000 (20:23 +0000)
committerQing Zhao <qinzhao@gcc.gnu.org>
Tue, 2 Jul 2019 20:23:30 +0000 (20:23 +0000)
PR preprocessor/90581
Add a cpp option -fmax-include-depth to set the maximum depth of the nested #include.

From-SVN: r272948

15 files changed:
gcc/ChangeLog
gcc/c-family/ChangeLog
gcc/c-family/c-opts.c
gcc/c-family/c.opt
gcc/doc/cppopts.texi
gcc/doc/invoke.texi
gcc/testsuite/ChangeLog
gcc/testsuite/c-c++-common/cpp/fmax-include-depth-1a.h [new file with mode: 0644]
gcc/testsuite/c-c++-common/cpp/fmax-include-depth-1b.h [new file with mode: 0644]
gcc/testsuite/c-c++-common/cpp/fmax-include-depth.c [new file with mode: 0644]
libcpp/ChangeLog
libcpp/directives.c
libcpp/include/cpplib.h
libcpp/init.c
libcpp/internal.h

index f40d55e0d3f364d8555a342be1d4da29f40482f9..62b126fb5b80ff6be84546f86818e0df930f1c4f 100644 (file)
@@ -1,3 +1,9 @@
+2019-07-02  qing zhao  <qing.zhao@oracle.com>
+
+       PR preprocessor/90581
+       * doc/cppopts.texi: Add document for -fmax-include-depth.
+       * doc/invoke.texi (Preprocessor Options): List -fmax-include-depth.
+
 2019-07-02  Uroš Bizjak  <ubizjak@gmail.com>
 
        * config/i386/mmx.md (mmx_pack<s_trunsuffix>swb):
index 22d7c6ecf7cfeda7a5f4e8329a72c737a4974aef..e56bd7a8e35e9788a3134f792551dca1130a3cb8 100644 (file)
@@ -1,3 +1,9 @@
+2019-07-02  qing zhao  <qing.zhao@oracle.com>
+
+       PR preprocessor/90581
+       * c-opts.c (c_common_handle_option): Handle -fmax-include-depth.
+       * c.opt: Add new option -fmax-include-depth.
+
 2019-06-26  Jason Merrill  <jason@redhat.com>
 
        PR c++/55442 - memory-hog with highly recursive constexpr.
index 954b6a494f85b900a185a458b859cf7bfc441ad7..188da437507f3d607c8f509790a546c865a3adbb 100644 (file)
@@ -460,6 +460,10 @@ c_common_handle_option (size_t scode, const char *arg, HOST_WIDE_INT value,
       cpp_opts->extended_identifiers = value;
       break;
 
+    case OPT_fmax_include_depth_:
+       cpp_opts->max_include_depth = value;
+      break;
+
     case OPT_foperator_names:
       cpp_opts->operator_names = value;
       break;
index 080066fa6087787190eee482ca6d607baa319920..4c8b00260008d26873b3fbb174b437a2ffb03875 100644 (file)
@@ -1575,6 +1575,10 @@ flax-vector-conversions
 C ObjC C++ ObjC++ Var(flag_lax_vector_conversions)
 Allow implicit conversions between vectors with differing numbers of subparts and/or differing element types.
 
+fmax-include-depth=
+C ObjC C++ ObjC++ Joined RejectNegative UInteger
+fmax-include-depth=<number> Set the maximum depth of the nested #include.
+
 fms-extensions
 C ObjC C++ ObjC++ Var(flag_ms_extensions)
 Don't warn about uses of Microsoft extensions.
index 48c574f419faa7b29618ddb3ddb3189301882ec5..61e22cd93aef3184ced1b9c00a204d64a53834b8 100644 (file)
@@ -261,6 +261,10 @@ enabled by default for C99 (and later C standard versions) and C++.
 @opindex fno-canonical-system-headers
 When preprocessing, do not shorten system header paths with canonicalization.
 
+@item -fmax-include-depth=@var{depth}
+@opindex fmax-include-depth
+Set the maximum depth of the nested #include. The default is 200. 
+
 @item -ftabstop=@var{width}
 @opindex ftabstop
 Set the distance between tab stops.  This helps the preprocessor report
index 9c6050b574b473256fbd6781b2fa5dfa6b2b2d12..ae1a7d252779f70c6308c75aa12141ef1a3d8242 100644 (file)
@@ -518,6 +518,7 @@ Objective-C and Objective-C++ Dialects}.
 -fdebug-cpp  -fdirectives-only  -fdollars-in-identifiers  @gol
 -fexec-charset=@var{charset}  -fextended-identifiers  @gol
 -finput-charset=@var{charset}  -fmacro-prefix-map=@var{old}=@var{new}  @gol
+-fmax-include-depth=@var{depth} @gol
 -fno-canonical-system-headers  -fpch-deps  -fpch-preprocess  @gol
 -fpreprocessed  -ftabstop=@var{width}  -ftrack-macro-expansion  @gol
 -fwide-exec-charset=@var{charset}  -fworking-directory @gol
index 423e075a9dcb28d0eebe0b0cc74f29f1307699d4..55deef7255dc20a505c5de63de8ad006339e200a 100644 (file)
@@ -1,3 +1,10 @@
+2019-07-02  qing zhao  <qing.zhao@oracle.com>
+
+       PR preprocessor/90581
+       * c-c++-common/cpp/fmax-include-depth-1a.h: New test.
+       * c-c++-common/cpp/fmax-include-depth-1b.h: New test.
+       * c-c++-common/cpp/fmax-include-depth.c: New test.
+
 2019-07-02  Jan Hubicka  <jh@suse.cz>
 
        * gcc.dg/tree-ssa/alias-access-path-7.c: New testcase.
diff --git a/gcc/testsuite/c-c++-common/cpp/fmax-include-depth-1a.h b/gcc/testsuite/c-c++-common/cpp/fmax-include-depth-1a.h
new file mode 100644 (file)
index 0000000..4e610c0
--- /dev/null
@@ -0,0 +1 @@
+int a;
diff --git a/gcc/testsuite/c-c++-common/cpp/fmax-include-depth-1b.h b/gcc/testsuite/c-c++-common/cpp/fmax-include-depth-1b.h
new file mode 100644 (file)
index 0000000..c167587
--- /dev/null
@@ -0,0 +1 @@
+#include "fmax-include-depth-1a.h"
diff --git a/gcc/testsuite/c-c++-common/cpp/fmax-include-depth.c b/gcc/testsuite/c-c++-common/cpp/fmax-include-depth.c
new file mode 100644 (file)
index 0000000..bd8cc3a
--- /dev/null
@@ -0,0 +1,4 @@
+/* { dg-do preprocess} */
+/* { dg-options "-fmax-include-depth=1" } */
+
+#include "fmax-include-depth-1b.h" /* { dg-error ".include nested depth 1 exceeds maximum of 1 .use -fmax-include-depth=DEPTH to increase the maximum." } */
index 1575d4bcda24199748ac5ad504f29624393e601c..a3185e3109c459a1be2b38ab9f8b5e8e98f4e8dc 100644 (file)
@@ -1,3 +1,13 @@
+2019-07-02  qing zhao  <qing.zhao@oracle.com>
+
+       PR preprocessor/90581
+       * directives.c (do_include_common): Replace CPP_STACK_MAX with
+       CPP_OPTION (pfile, max_include_depth).
+       * include/cpplib.h (struct cpp_options): Add new field
+       max_include_depth.
+       * init.c (cpp_create_reader): Initiate new field max_include_depth.
+       * internal.h: Delete CPP_STACK_MAX.
+
 2019-06-26  Nathan Sidwell  <nathan@acm.org>
 
        PR preprocessor/90927
index 3ee8bc4053b9c11ccf1835c66b6be8f52064d36e..2fdfaf0242c8b8a1b78b87edb2c2d289efbda777 100644 (file)
@@ -831,8 +831,13 @@ do_include_common (cpp_reader *pfile, enum include_type type)
     }
 
   /* Prevent #include recursion.  */
-  if (pfile->line_table->depth >= CPP_STACK_MAX)
-    cpp_error (pfile, CPP_DL_ERROR, "#include nested too deeply");
+  if (pfile->line_table->depth >= CPP_OPTION (pfile, max_include_depth))
+    cpp_error (pfile, 
+              CPP_DL_ERROR, 
+              "#include nested depth %u exceeds maximum of %u"
+              " (use -fmax-include-depth=DEPTH to increase the maximum)",
+              pfile->line_table->depth,
+              CPP_OPTION (pfile, max_include_depth));
   else
     {
       /* Get out of macro context, if we are.  */
index 3edb93dce460d468a51c85c321c93a826feb279e..91d97f9f097f9da542187785cfc259489bcd1428 100644 (file)
@@ -550,6 +550,9 @@ struct cpp_options
 
   /* True enables canonicalization of system header file paths. */
   bool canonical_system_headers;
+
+  /* The maximum depth of the nested #include.  */
+  unsigned int max_include_depth;
 };
 
 /* Diagnostic levels.  To get a diagnostic without associating a
index c0c9020fdb7fda9578409da518e3cdd6ddae5ff7..d06f95e68b0acd936c56ee303c26b30fcaceda2b 100644 (file)
@@ -185,6 +185,7 @@ cpp_create_reader (enum c_lang lang, cpp_hash_table *table,
   CPP_OPTION (pfile, warn_multichar) = 1;
   CPP_OPTION (pfile, discard_comments) = 1;
   CPP_OPTION (pfile, discard_comments_in_macro_exp) = 1;
+  CPP_OPTION (pfile, max_include_depth) = 200;
   CPP_OPTION (pfile, tabstop) = 8;
   CPP_OPTION (pfile, operator_names) = 1;
   CPP_OPTION (pfile, warn_trigraphs) = 2;
index 5b9c389bb493d55d07a765388a2739dcc80ab693..0ab44706864063155e3c13da67f7c437cb2ee850 100644 (file)
@@ -74,10 +74,6 @@ struct cset_converter
     linemap_line_start (PFILE->line_table, line + 1, COLS_HINT); \
   } while (0)
 
-/* Maximum nesting of cpp_buffers.  We use a static limit, partly for
-   efficiency, and partly to limit runaway recursion.  */
-#define CPP_STACK_MAX 200
-
 /* Host alignment handling.  */
 struct dummy
 {