re PR c++/24009 (C++ fails to print #include stack)
authorJoseph Myers <joseph@codesourcery.com>
Sat, 26 Aug 2006 21:38:46 +0000 (22:38 +0100)
committerJoseph Myers <jsm28@gcc.gnu.org>
Sat, 26 Aug 2006 21:38:46 +0000 (22:38 +0100)
PR c++/24009
* input.h (restore_input_file_stack): Declare.
(INPUT_FILE_STACK_BITS): Define.
* toplev.c (fs_p, input_file_stack_history,
input_file_stack_restored, restore_input_file_stack): New.
(push_srcloc, pop_srcloc): Check for input_file_stack_tick
overflowing INPUT_FILE_STACK_BITS bits.  Save new state of stack.
(pop_srcloc): Don't free old state of stack.

cp:
* parser.c (struct cp_token): Add input_file_stack_index.
(eof_token): Update.
(cp_lexer_get_preprocessor_token): Save input_file_stack_tick.
(cp_lexer_set_source_position_from_token): Restore input file
stack.

From-SVN: r116479

gcc/ChangeLog
gcc/cp/ChangeLog
gcc/cp/parser.c
gcc/input.h
gcc/toplev.c

index 5014f6aee5e3bbb679eeb0364b516f787b0ed2ed..7e4cd7ce31ac558c03dde1afaa7b996722c68c45 100644 (file)
@@ -1,3 +1,14 @@
+2006-08-26  Joseph S. Myers  <joseph@codesourcery.com>
+
+       PR c++/24009
+       * input.h (restore_input_file_stack): Declare.
+       (INPUT_FILE_STACK_BITS): Define.
+       * toplev.c (fs_p, input_file_stack_history,
+       input_file_stack_restored, restore_input_file_stack): New.
+       (push_srcloc, pop_srcloc): Check for input_file_stack_tick
+       overflowing INPUT_FILE_STACK_BITS bits.  Save new state of stack.
+       (pop_srcloc): Don't free old state of stack.
+
 2006-08-26  David Edelsohn  <edelsohn@gnu.org>
 
        PR target/27544
index e219547c545c0f4c553cbe62d4a7efbc9d61cdd4..5c8b1cb156476a338a113bc80b9d4221a25d5a02 100644 (file)
@@ -1,3 +1,12 @@
+2006-08-26  Joseph S. Myers  <joseph@codesourcery.com>
+
+       PR c++/24009
+       * parser.c (struct cp_token): Add input_file_stack_index.
+       (eof_token): Update.
+       (cp_lexer_get_preprocessor_token): Save input_file_stack_tick.
+       (cp_lexer_set_source_position_from_token): Restore input file
+       stack.
+
 2006-08-26  Lee Millward  <lee.millward@codesourcery.com>
 
         PR c++/28736
index 4e62340a0e9d724e327c65c42e4ae5406940c765..a97518ac9a58d2d57702af2bab86ae0b1847a9b3 100644 (file)
@@ -66,6 +66,8 @@ typedef struct cp_token GTY (())
      KEYWORD is RID_MAX) iff this name was looked up and found to be
      ambiguous.  An error has already been reported.  */
   BOOL_BITFIELD ambiguous_p : 1;
+  /* The input file stack index at which this token was found.  */
+  unsigned input_file_stack_index : INPUT_FILE_STACK_BITS;
   /* The value associated with this token, if any.  */
   tree value;
   /* The location at which this token was found.  */
@@ -79,7 +81,7 @@ DEF_VEC_ALLOC_P (cp_token_position,heap);
 
 static const cp_token eof_token =
 {
-  CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, NULL_TREE,
+  CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, 0, NULL_TREE,
 #if USE_MAPPED_LOCATION
   0
 #else
@@ -393,6 +395,7 @@ cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
    /* Get a new token from the preprocessor.  */
   token->type
     = c_lex_with_flags (&token->value, &token->location, &token->flags);
+  token->input_file_stack_index = input_file_stack_tick;
   token->keyword = RID_MAX;
   token->pragma_kind = PRAGMA_NONE;
   token->in_system_header = in_system_header;
@@ -450,7 +453,8 @@ cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
     }
 }
 
-/* Update the globals input_location and in_system_header from TOKEN.  */
+/* Update the globals input_location and in_system_header and the
+   input file stack from TOKEN.  */
 static inline void
 cp_lexer_set_source_position_from_token (cp_token *token)
 {
@@ -458,6 +462,7 @@ cp_lexer_set_source_position_from_token (cp_token *token)
     {
       input_location = token->location;
       in_system_header = token->in_system_header;
+      restore_input_file_stack (token->input_file_stack_index);
     }
 }
 
index 0ca3ccfd7338d862375fb46a1a11852a1b0315a8..2fea1a65c9b488cb4ed2c019c4a0c31dce72401a 100644 (file)
@@ -84,6 +84,7 @@ extern void push_srcloc (location_t);
 extern void push_srcloc (const char *name, int line);
 #endif /* ! USE_MAPPED_LOCATION */
 extern void pop_srcloc (void);
+extern void restore_input_file_stack (int);
 
 #define LOCATION_FILE(LOC) ((expand_location (LOC)).file)
 #define LOCATION_LINE(LOC) ((expand_location (LOC)).line)
@@ -98,4 +99,7 @@ extern struct file_stack *input_file_stack;
 /* Incremented on each change to input_file_stack.  */
 extern int input_file_stack_tick;
 
+/* The number of bits available for input_file_stack_tick.  */
+#define INPUT_FILE_STACK_BITS  31
+
 #endif
index 55019ed5413e98b3c3a30e36073ec48ea03a5553..53fcdfe580c830bd125b6683dad61a8e173f71e8 100644 (file)
@@ -164,6 +164,16 @@ struct file_stack *input_file_stack;
 /* Incremented on each change to input_file_stack.  */
 int input_file_stack_tick;
 
+/* Record of input_file_stack at each tick.  */
+typedef struct file_stack *fs_p;
+DEF_VEC_P(fs_p);
+DEF_VEC_ALLOC_P(fs_p,heap);
+static VEC(fs_p,heap) *input_file_stack_history;
+
+/* Whether input_file_stack has been restored to a previous state (in
+   which case there should be no more pushing).  */
+static bool input_file_stack_restored;
+
 /* Name to use as base of names for dump output files.  */
 
 const char *dump_base_name;
@@ -951,6 +961,10 @@ push_srcloc (const char *file, int line)
 {
   struct file_stack *fs;
 
+  gcc_assert (!input_file_stack_restored);
+  if (input_file_stack_tick == (int) ((1U << INPUT_FILE_STACK_BITS) - 1))
+    sorry ("GCC supports only %d input file changes", input_file_stack_tick);
+
   fs = XNEW (struct file_stack);
   fs->location = input_location;
   fs->next = input_file_stack;
@@ -962,6 +976,7 @@ push_srcloc (const char *file, int line)
 #endif
   input_file_stack = fs;
   input_file_stack_tick++;
+  VEC_safe_push (fs_p, heap, input_file_stack_history, input_file_stack);
 }
 
 /* Pop the top entry off the stack of presently open source files.
@@ -973,11 +988,30 @@ pop_srcloc (void)
 {
   struct file_stack *fs;
 
+  gcc_assert (!input_file_stack_restored);
+  if (input_file_stack_tick == (int) ((1U << INPUT_FILE_STACK_BITS) - 1))
+    sorry ("GCC supports only %d input file changes", input_file_stack_tick);
+
   fs = input_file_stack;
   input_location = fs->location;
   input_file_stack = fs->next;
-  free (fs);
   input_file_stack_tick++;
+  VEC_safe_push (fs_p, heap, input_file_stack_history, input_file_stack);
+}
+
+/* Restore the input file stack to its state as of TICK, for the sake
+   of diagnostics after processing the whole input.  Once this has
+   been called, push_srcloc and pop_srcloc may no longer be
+   called.  */
+void
+restore_input_file_stack (int tick)
+{
+  if (tick == 0)
+    input_file_stack = NULL;
+  else
+    input_file_stack = VEC_index (fs_p, input_file_stack_history, tick - 1);
+  input_file_stack_tick = tick;
+  input_file_stack_restored = true;
 }
 
 /* Compile an entire translation unit.  Write a file of assembly