From b1cfa8b6044ddc9d84556e820a4239e9449c9056 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Thu, 24 Sep 2015 13:38:29 -0400
Subject: [PATCH 09/58] FIXME: Undo "Implement token range tracking within
 libcpp and the C FE (v2)" and reimplement it and tree expr tracking with
 location_t

---
 gcc/c-family/c-lex.c    |  9 ++-------
 gcc/c-family/c-pragma.h |  4 ++--
 gcc/c/c-parser.c        | 35 ++++++++++++++++++++++-------------
 gcc/cp/parser.c         |  3 +--
 libcpp/include/cpplib.h |  7 +++----
 libcpp/lex.c            | 18 +++++++-----------
 6 files changed, 37 insertions(+), 39 deletions(-)

diff --git a/gcc/c-family/c-lex.c b/gcc/c-family/c-lex.c
index 9016179..c69f4a6 100644
--- a/gcc/c-family/c-lex.c
+++ b/gcc/c-family/c-lex.c
@@ -380,14 +380,11 @@ c_common_has_attribute (cpp_reader *pfile)
 }
 
 /* Read a token and return its type.  Fill *VALUE with its value, if
-   applicable.  Fill *LOC with the source location of the token.
-   If non-NULL, fill *RANGE with the source range of the token.
-   Fill *CPP_FLAGS with the token's flags, if it is
+   applicable.  Fill *CPP_FLAGS with the token's flags, if it is
    non-NULL.  */
 
 enum cpp_ttype
-c_lex_with_flags (tree *value, location_t *loc, source_range *range,
-		  unsigned char *cpp_flags,
+c_lex_with_flags (tree *value, location_t *loc, unsigned char *cpp_flags,
 		  int lex_flags)
 {
   static bool no_more_pch;
@@ -400,8 +397,6 @@ c_lex_with_flags (tree *value, location_t *loc, source_range *range,
  retry:
   tok = cpp_get_token_with_location (parse_in, loc);
   type = tok->type;
-  if (range)
-    *range = tok->src_range;
 
  retry_after_at:
   switch (type)
diff --git a/gcc/c-family/c-pragma.h b/gcc/c-family/c-pragma.h
index 3b94e44..f6e1090 100644
--- a/gcc/c-family/c-pragma.h
+++ b/gcc/c-family/c-pragma.h
@@ -225,8 +225,8 @@ extern enum cpp_ttype pragma_lex (tree *, location_t *loc = NULL);
 /* This is not actually available to pragma parsers.  It's merely a
    convenient location to declare this function for c-lex, after
    having enum cpp_ttype declared.  */
-extern enum cpp_ttype c_lex_with_flags (tree *, location_t *, source_range *,
-					unsigned char *, int);
+extern enum cpp_ttype c_lex_with_flags (tree *, location_t *, unsigned char *,
+					int);
 
 extern void c_pp_lookup_pragma (unsigned int, const char **, const char **);
 
diff --git a/gcc/c/c-parser.c b/gcc/c/c-parser.c
index 427b444..8eda04b 100644
--- a/gcc/c/c-parser.c
+++ b/gcc/c/c-parser.c
@@ -188,10 +188,21 @@ struct GTY (()) c_token {
   ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
   /* The location at which this token was found.  */
   location_t location;
-  /* The source range at which this token was found.  */
-  source_range range;
   /* The value associated with this token, if any.  */
   tree value;
+
+  source_range get_range () const
+  {
+    if (IS_ADHOC_LOC (location))
+      return get_range_from_adhoc_loc (line_table, location);
+    else
+      return source_range::from_location (location);
+  }
+
+  location_t get_finish () const
+  {
+    return get_range ().m_finish;
+  }
 };
 
 /* A parser structure recording information about the state and
@@ -259,9 +270,7 @@ c_lex_one_token (c_parser *parser, c_token *token)
 {
   timevar_push (TV_LEX);
 
-  token->type = c_lex_with_flags (&token->value, &token->location,
-				  &token->range,
-				  NULL,
+  token->type = c_lex_with_flags (&token->value, &token->location, NULL,
 				  (parser->lex_untranslated_string
 				   ? C_LEX_STRING_NO_TRANSLATE : 0));
   token->id_kind = C_ID_NONE;
@@ -6765,7 +6774,7 @@ c_parser_sizeof_expression (c_parser *parser)
       expr_loc = c_parser_peek_token (parser)->location;
       type_name = c_parser_type_name (parser);
       c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
-      finish = parser->tokens_buf[0].range.m_finish; // FIXME: better access API to last token
+      finish = parser->tokens_buf[0].location; // FIXME: better access API to last token
       if (type_name == NULL)
 	{
 	  struct c_expr ret;
@@ -7225,7 +7234,7 @@ c_parser_postfix_expression (c_parser *parser)
   struct c_expr expr, e1;
   struct c_type_name *t1, *t2;
   location_t loc = c_parser_peek_token (parser)->location;;
-  source_range tok_range = c_parser_peek_token (parser)->range;
+  source_range tok_range = c_parser_peek_token (parser)->get_range ();
   expr.original_code = ERROR_MARK;
   expr.original_type = NULL;
   switch (c_parser_peek_token (parser)->type)
@@ -8006,7 +8015,7 @@ c_parser_postfix_expression_after_primary (c_parser *parser,
 		  c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
 					     "expected %<]%>");
 		  start = expr.src_range.m_start;
-		  finish = parser->tokens_buf[0].range.m_finish; // FIXME: better access API to last token
+		  finish = parser->tokens_buf[0].location; // FIXME: better access API to last token
 		  expr.value = build_array_ref (op_loc, expr.value, idx);
 		  set_c_expr_source_range (&expr, start, finish);
 		}
@@ -8052,7 +8061,7 @@ c_parser_postfix_expression_after_primary (c_parser *parser,
 			"this could be due to transposed parameters");
 
 	  start = expr.src_range.m_start;
-	  finish = parser->tokens_buf[0].range.m_finish; // FIXME: better access API to last token
+	  finish = parser->tokens_buf[0].get_finish (); // FIXME: better access API to last token
 	  expr.value
 	    = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
 					 exprlist, origtypes);
@@ -8087,7 +8096,7 @@ c_parser_postfix_expression_after_primary (c_parser *parser,
 	      return expr;
 	    }
 	  start = expr.src_range.m_start;
-	  finish = c_parser_peek_token (parser)->range.m_finish;
+	  finish = c_parser_peek_token (parser)->get_finish ();
 	  c_parser_consume_token (parser);
 	  expr.value = build_component_ref (op_loc, expr.value, ident);
 	  set_c_expr_source_range (&expr, start, finish);
@@ -8119,7 +8128,7 @@ c_parser_postfix_expression_after_primary (c_parser *parser,
 	      return expr;
 	    }
 	  start = expr.src_range.m_start;
-	  finish = c_parser_peek_token (parser)->range.m_finish;
+	  finish = c_parser_peek_token (parser)->get_finish ();
 	  c_parser_consume_token (parser);
 	  expr.value = build_component_ref (op_loc,
 					    build_indirect_ref (op_loc,
@@ -8143,7 +8152,7 @@ c_parser_postfix_expression_after_primary (c_parser *parser,
 	case CPP_PLUS_PLUS:
 	  /* Postincrement.  */
 	  start = expr.src_range.m_start;
-	  finish = c_parser_peek_token (parser)->range.m_finish;
+	  finish = c_parser_peek_token (parser)->get_finish ();
 	  c_parser_consume_token (parser);
 	  /* If the expressions have array notations, we expand them.  */
 	  if (flag_cilkplus
@@ -8162,7 +8171,7 @@ c_parser_postfix_expression_after_primary (c_parser *parser,
 	case CPP_MINUS_MINUS:
 	  /* Postdecrement.  */
 	  start = expr.src_range.m_start;
-	  finish = c_parser_peek_token (parser)->range.m_finish;
+	  finish = c_parser_peek_token (parser)->get_finish ();
 	  c_parser_consume_token (parser);
 	  /* If the expressions have array notations, we expand them.  */
 	  if (flag_cilkplus
diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index 4cc0de2..d4ef7f9 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -764,8 +764,7 @@ cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
 
    /* Get a new token from the preprocessor.  */
   token->type
-    = c_lex_with_flags (&token->u.value, &token->location,
-                        NULL, &token->flags,
+    = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
 			lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
   token->keyword = RID_MAX;
   token->pragma_kind = PRAGMA_NONE;
diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h
index 0b1a403..f5c2a21 100644
--- a/libcpp/include/cpplib.h
+++ b/libcpp/include/cpplib.h
@@ -235,11 +235,10 @@ struct GTY(()) cpp_identifier {
 };
 
 /* A preprocessing token.  This has been carefully packed and should
-   occupy 16 bytes on 32-bit hosts and 24 bytes on 64-bit hosts.
-   FIXME: the above comment is no longer true with this patch.  */
+   occupy 16 bytes on 32-bit hosts and 24 bytes on 64-bit hosts.  */
 struct GTY(()) cpp_token {
-  source_location src_loc;	/* Location of first char of token.  */
-  source_range src_range;	/* Source range covered by the token.  */
+  source_location src_loc;	/* Location of first char of token,
+				   together with range of full token.  */
   ENUM_BITFIELD(cpp_ttype) type : CHAR_BIT;  /* token type */
   unsigned short flags;		/* flags - see above */
 
diff --git a/libcpp/lex.c b/libcpp/lex.c
index a6f16b2..f4c964f 100644
--- a/libcpp/lex.c
+++ b/libcpp/lex.c
@@ -2169,8 +2169,6 @@ _cpp_temp_token (cpp_reader *pfile)
 
   result = pfile->cur_token++;
   result->src_loc = old->src_loc;
-  result->src_range.m_start = old->src_loc;
-  result->src_range.m_finish = old->src_loc;
   return result;
 }
 
@@ -2367,13 +2365,6 @@ _cpp_lex_direct (cpp_reader *pfile)
     result->src_loc = linemap_position_for_column (pfile->line_table,
 					  CPP_BUF_COLUMN (buffer, buffer->cur));
 
-  /* The token's src_range begins here.  */
-  result->src_range.m_start = result->src_loc;
-
-  /* Ensure m_finish is also initialized, in case we bail out above
-     via a "goto fresh_line;" below.  */
-  result->src_range.m_finish = result->src_loc;
-
   switch (c)
     {
     case ' ': case '\t': case '\f': case '\v': case '\0':
@@ -2732,11 +2723,16 @@ _cpp_lex_direct (cpp_reader *pfile)
       break;
     }
 
-  /* The token's src_range ends here.  */
-  result->src_range.m_finish =
+  source_range tok_range;
+  tok_range.m_start = result->src_loc;
+  tok_range.m_finish =
     linemap_position_for_column (pfile->line_table,
 				 CPP_BUF_COLUMN (buffer, buffer->cur));
 
+  result->src_loc = COMBINE_LOCATION_DATA (pfile->line_table,
+					   result->src_loc,
+					   tok_range, NULL);
+
   return result;
 }
 
-- 
1.8.5.3

