Add ability to force lexed tokens' source_locations.
authorGabriel Charette <gchare@google.com>
Mon, 22 Aug 2011 20:41:07 +0000 (20:41 +0000)
committerGabriel Charette <gchare@gcc.gnu.org>
Mon, 22 Aug 2011 20:41:07 +0000 (20:41 +0000)
Use it to force BUILTINS_LOCATION when declaring builtins instead of creating a <built-in> entry in the line_table which is wrong.

* c-opts.c (c_finish_options): Force BUILTINS_LOCATION for tokens
defined in cpp_init_builtins and c_cpp_builtins.

gcc/fortran/ChangeLog
* cpp.c (gfc_cpp_init): Force BUILTINS_LOCATION for tokens
defined in cpp_define_builtins.

libcpp/ChangeLog
* init.c (cpp_create_reader): Inititalize forced_token_location_p.
* internal.h (struct cpp_reader): Add field forced_token_location_p.
* lex.c (_cpp_lex_direct): Use forced_token_location_p.
(cpp_force_token_locations): New.
(cpp_stop_forcing_token_locations): New.

From-SVN: r177973

gcc/c-family/ChangeLog
gcc/c-family/c-opts.c
gcc/fortran/ChangeLog
gcc/fortran/cpp.c
libcpp/ChangeLog
libcpp/include/cpplib.h
libcpp/init.c
libcpp/internal.h
libcpp/lex.c

index df9cdff40f4825919695ff6f8763c09eaa1e6545..fb00ca98958eed592592c2b0717d7b69c874a969 100644 (file)
@@ -1,3 +1,8 @@
+011-08-22  Gabriel Charette  <gchare@google.com>
+
+       * c-opts.c (c_finish_options): Force BUILTINS_LOCATION for tokens
+       defined in cpp_init_builtins and c_cpp_builtins.
+
 2011-08-19  Joseph Myers  <joseph@codesourcery.com>
 
        * c-common.c (c_common_reswords): Add __builtin_complex.
index 3227f7b986019f29342ca7d417b7b8776926d295..49ff80dda7e00a705a17b11e1c5d9115c85b2134 100644 (file)
@@ -1306,12 +1306,17 @@ c_finish_options (void)
     {
       size_t i;
 
-      cb_file_change (parse_in,
-                     linemap_add (line_table, LC_RENAME, 0,
-                                  _("<built-in>"), 0));
+      {
+       /* Make sure all of the builtins about to be declared have
+         BUILTINS_LOCATION has their source_location.  */
+       source_location builtins_loc = BUILTINS_LOCATION;
+       cpp_force_token_locations (parse_in, &builtins_loc);
 
-      cpp_init_builtins (parse_in, flag_hosted);
-      c_cpp_builtins (parse_in);
+       cpp_init_builtins (parse_in, flag_hosted);
+       c_cpp_builtins (parse_in);
+
+       cpp_stop_forcing_token_locations (parse_in);
+      }
 
       /* We're about to send user input to cpplib, so make it warn for
         things that we previously (when we sent it internal definitions)
index 69d901e0c7be97b168a0bb581784917d13cbd7cf..075c366f0b590d218d2919a9a8229e9d7629adad 100644 (file)
@@ -1,3 +1,8 @@
+2011-08-22  Gabriel Charette  <gchare@google.com>
+
+       * cpp.c (gfc_cpp_init): Force BUILTINS_LOCATION for tokens
+       defined in cpp_define_builtins.
+
 2011-08-22  Mikael Morin  <mikael.morin@gcc.gnu.org>
 
        PR fortran/50050
index a40442ee4d7be92df536b24902f7e0b2d00046ee..9368d890973b6edac181735f5af536decb505fb2 100644 (file)
@@ -565,9 +565,17 @@ gfc_cpp_init (void)
   if (gfc_option.flag_preprocessed)
     return;
 
-  cpp_change_file (cpp_in, LC_RENAME, _("<built-in>"));
   if (!gfc_cpp_option.no_predefined)
-    cpp_define_builtins (cpp_in);
+    {
+      /* Make sure all of the builtins about to be declared have
+       BUILTINS_LOCATION has their source_location.  */
+      source_location builtins_loc = BUILTINS_LOCATION;
+      cpp_force_token_locations (cpp_in, &builtins_loc);
+
+      cpp_define_builtins (cpp_in);
+
+      cpp_stop_forcing_token_locations (cpp_in);
+    }
 
   /* Handle deferred options from command-line.  */
   cpp_change_file (cpp_in, LC_RENAME, _("<command-line>"));
index 81b3a94bb50e533f87b4e9019238fdd9b96e2358..8a5e89921a0034487ef00a7ec21c8c4a85f9e966 100644 (file)
@@ -1,3 +1,11 @@
+2011-08-22  Gabriel Charette  <gchare@google.com>
+
+       * init.c (cpp_create_reader): Inititalize forced_token_location_p.
+       * internal.h (struct cpp_reader): Add field forced_token_location_p.
+       * lex.c (_cpp_lex_direct): Use forced_token_location_p.
+       (cpp_force_token_locations): New.
+       (cpp_stop_forcing_token_locations): New.
+
 2011-08-18  Rainer Orth  <ro@CeBiTec.Uni-Bielefeld.DE>
 
        PR libstdc++/1773
index 4d68fa76b093625772d01616a67d155b6981551d..0e90821072fdee5393b3b4b929ee38f48ac86265 100644 (file)
@@ -989,4 +989,8 @@ extern void cpp_prepare_state (cpp_reader *, struct save_macro_data **);
 extern int cpp_read_state (cpp_reader *, const char *, FILE *,
                           struct save_macro_data *);
 
+/* In lex.c */
+extern void cpp_force_token_locations (cpp_reader *, source_location *);
+extern void cpp_stop_forcing_token_locations (cpp_reader *);
+
 #endif /* ! LIBCPP_CPPLIB_H */
index 93f12d0d71f8f18be505cff2f80e4008a015a38a..c5c53256d35643b18890f3a81e5b2dd6213af32d 100644 (file)
@@ -223,6 +223,9 @@ cpp_create_reader (enum c_lang lang, hash_table *table,
   /* Initialize table for push_macro/pop_macro.  */
   pfile->pushed_macros = 0;
 
+  /* Do not force token locations by default.  */
+  pfile->forced_token_location_p = NULL;
+
   /* The expression parser stack.  */
   _cpp_expand_op_stack (pfile);
 
index d2872c4a11fbf7cb1156506a6f8e5467e5cb8892..6c423f056bcddf345ff3182b61384486c36a07e1 100644 (file)
@@ -499,6 +499,10 @@ struct cpp_reader
 
   /* List of saved macros by push_macro.  */
   struct def_pragma_macro *pushed_macros;
+
+  /* If non-null, the lexer will use this location for the next token
+     instead of getting a location from the linemap.  */
+  source_location *forced_token_location_p;
 };
 
 /* Character classes.  Based on the more primitive macros in safe-ctype.h.
index 463b5c80eee4102dc559c391dd15842e9c334077..75b2b1dc7ff7c111703e60947f7b46d741a45054 100644 (file)
@@ -1975,8 +1975,11 @@ _cpp_lex_direct (cpp_reader *pfile)
     }
   c = *buffer->cur++;
 
-  result->src_loc = linemap_position_for_column (pfile->line_table,
-                                                           CPP_BUF_COLUMN (buffer, buffer->cur));
+  if (pfile->forced_token_location_p)
+    result->src_loc = *pfile->forced_token_location_p;
+  else
+    result->src_loc = linemap_position_for_column (pfile->line_table,
+                                         CPP_BUF_COLUMN (buffer, buffer->cur));
 
   switch (c)
     {
@@ -2839,3 +2842,21 @@ cpp_token_val_index (cpp_token *tok)
       return CPP_TOKEN_FLD_NONE;
     }
 }
+
+/* All tokens lexed in R after calling this function will be forced to have
+   their source_location the same as the location referenced by P, until
+   cpp_stop_forcing_token_locations is called for R.  */
+
+void
+cpp_force_token_locations (cpp_reader *r, source_location *p)
+{
+  r->forced_token_location_p = p;
+}
+
+/* Go back to assigning locations naturally for lexed tokens.  */
+
+void
+cpp_stop_forcing_token_locations (cpp_reader *r)
+{
+  r->forced_token_location_p = NULL;
+}