I discovered a couple of line map bugs when working on streaming macro locations on the modules branch:

1) LINEMAPS_MACRO_LOWEST_LOCATION has an off-by-one error. It is returning the lowest macro loc handed out. MAX_SOURCE_LOCATION is the highest location that could be handed out. If we've not created any macros, then we've not handed out MAX_SOURCE_LOCATION, and should return one more than that. (The impact here is that the first macro created wouldn't use MAX_SOURCE_LOCATION.)

2) linemap_enter_macro allocates num_tokens * 2 locations, but only clears num_tokens locations. It seems we don't always use all those locations, and so can have trailing garbage.

booted & tested on x86_64-linux.

Fixing thusly.

nathan

--
Nathan Sidwell
2018-10-11  Nathan Sidwell  <nat...@acm.org>

	* include/line-map.h (LINEMAPS_MACRO_LOWEST_LOCATION): Fix
	off-by-one error.
	* line-map.c (linemap_enter_macro): Use RAII.  Clear all of the
	macro_locations.

Index: include/line-map.h
===================================================================
--- include/line-map.h	(revision 265035)
+++ include/line-map.h	(working copy)
@@ -1017,7 +1017,7 @@ LINEMAPS_MACRO_LOWEST_LOCATION (const li
 {
   return LINEMAPS_MACRO_USED (set)
          ? MAP_START_LOCATION (LINEMAPS_LAST_MACRO_MAP (set))
-         : MAX_SOURCE_LOCATION;
+         : MAX_SOURCE_LOCATION + 1;
 }
 
 /* Returns the last macro map allocated in the line table SET.  */
Index: line-map.c
===================================================================
--- line-map.c	(revision 265035)
+++ line-map.c	(working copy)
@@ -612,30 +612,24 @@ const line_map_macro *
 linemap_enter_macro (struct line_maps *set, struct cpp_hashnode *macro_node,
 		     source_location expansion, unsigned int num_tokens)
 {
-  line_map_macro *map;
-  source_location start_location;
-  /* Cast away extern "C" from the type of xrealloc.  */
-  line_map_realloc reallocator = (set->reallocator
-				  ? set->reallocator
-				  : (line_map_realloc) xrealloc);
-
-  start_location = LINEMAPS_MACRO_LOWEST_LOCATION (set) - num_tokens;
+  source_location start_location
+    = LINEMAPS_MACRO_LOWEST_LOCATION (set) - num_tokens;
 
   if (start_location < LINE_MAP_MAX_LOCATION)
     /* We ran out of macro map space.   */
     return NULL;
 
-  map = linemap_check_macro (new_linemap (set, start_location));
+  line_map_macro *map = linemap_check_macro (new_linemap (set, start_location));
 
   map->macro = macro_node;
   map->n_tokens = num_tokens;
   map->macro_locations
-    = (source_location*) reallocator (NULL,
-				      2 * num_tokens
-				      * sizeof (source_location));
+    = (source_location*) set->reallocator (NULL,
+					   2 * num_tokens
+					   * sizeof (source_location));
   map->expansion = expansion;
   memset (MACRO_MAP_LOCATIONS (map), 0,
-	  num_tokens * sizeof (source_location));
+	  2 * num_tokens * sizeof (source_location));
 
   LINEMAPS_MACRO_CACHE (set) = LINEMAPS_MACRO_USED (set) - 1;
 

Reply via email to