gcc there is a single line_maps instance: "line_table", declared in
gcc/input.h and defined in gcc/input.cc.
- The values of the keys are intended to be internal to libcpp,
- but for ease-of-understanding the implementation, they are currently
- assigned as follows:
+ The values of the keys are intended to be internal to libcpp, but for
+ ease-of-understanding the implementation, they are currently assigned as
+ follows in the case of 32-bit location_t:
Actual | Value | Meaning
-----------+-------------------------------+-------------------------------
To further see how location_t works in practice, see the
worked example in libcpp/location-example.txt. */
typedef unsigned int location_t;
+typedef int64_t location_diff_t;
+
+/* Sometimes we need a type that has the same size as location_t but that does
+ not represent a location. This typedef provides more clarity in those
+ cases. */
+typedef location_t line_map_uint_t;
/* Do not track column numbers higher than this one. As a result, the
range of column_bits is [12, 18] (or 0 if column numbers are
/* Highest possible source location encoded within an ordinary map. */
const location_t LINE_MAP_MAX_LOCATION = 0x70000000;
+/* This is the number of range bits suggested to enable, if range tracking is
+ desired. */
+const int line_map_suggested_range_bits = 5;
+
/* A range of source locations.
Ranges are closed:
line_map_ordinary * GTY ((length ("%h.used"))) maps;
/* The total number of allocated maps. */
- unsigned int allocated;
+ line_map_uint_t allocated;
/* The number of elements used in maps. This number is smaller
or equal to ALLOCATED. */
- unsigned int used;
+ line_map_uint_t used;
/* The index of the last ordinary map that was looked up with
linemap_lookup. */
- mutable unsigned int m_cache;
+ mutable line_map_uint_t m_cache;
};
struct GTY(()) maps_info_macro {
line_map_macro * GTY ((length ("%h.used"))) maps;
/* The total number of allocated maps. */
- unsigned int allocated;
+ line_map_uint_t allocated;
/* The number of elements used in maps. This number is smaller
or equal to ALLOCATED. */
- unsigned int used;
+ line_map_uint_t used;
/* The index of the last macro map that was looked up with
linemap_lookup. */
- mutable unsigned int m_cache;
+ mutable line_map_uint_t m_cache;
};
/* Data structure to associate a source_range together with an arbitrary
struct GTY(()) location_adhoc_data_map {
struct htab * GTY((skip)) htab;
location_t curr_loc;
- unsigned int allocated;
+ line_map_uint_t allocated;
struct location_adhoc_data GTY((length ("%h.allocated"))) *data;
};
/* The default value of range_bits in ordinary line maps. */
unsigned int default_range_bits;
- unsigned int m_num_optimized_ranges;
- unsigned int m_num_unoptimized_ranges;
+ line_map_uint_t m_num_optimized_ranges;
+ line_map_uint_t m_num_unoptimized_ranges;
};
/* Returns the number of allocated maps so far. MAP_KIND shall be TRUE
if we are interested in macro maps, FALSE otherwise. */
-inline unsigned int
+inline line_map_uint_t
LINEMAPS_ALLOCATED (const line_maps *set, bool map_kind)
{
if (map_kind)
/* As above, but by reference (e.g. as an lvalue). */
-inline unsigned int &
+inline line_map_uint_t &
LINEMAPS_ALLOCATED (line_maps *set, bool map_kind)
{
if (map_kind)
/* Returns the number of used maps so far. MAP_KIND shall be TRUE if
we are interested in macro maps, FALSE otherwise.*/
-inline unsigned int
+inline line_map_uint_t
LINEMAPS_USED (const line_maps *set, bool map_kind)
{
if (map_kind)
/* As above, but by reference (e.g. as an lvalue). */
-inline unsigned int &
+inline line_map_uint_t &
LINEMAPS_USED (line_maps *set, bool map_kind)
{
if (map_kind)
/* Return the map at a given index. */
inline line_map *
-LINEMAPS_MAP_AT (const line_maps *set, bool map_kind, int index)
+LINEMAPS_MAP_AT (const line_maps *set, bool map_kind, line_map_uint_t index)
{
if (map_kind)
return &set->info_macro.maps[index];
inline line_map *
LINEMAPS_LAST_MAP (const line_maps *set, bool map_kind)
{
+ linemap_assert (LINEMAPS_USED (set, map_kind));
return LINEMAPS_MAP_AT (set, map_kind,
LINEMAPS_USED (set, map_kind) - 1);
}
/* Returns the INDEXth ordinary map. */
inline line_map_ordinary *
-LINEMAPS_ORDINARY_MAP_AT (const line_maps *set, int index)
+LINEMAPS_ORDINARY_MAP_AT (const line_maps *set, line_map_uint_t index)
{
- linemap_assert (index >= 0
- && (unsigned int)index < LINEMAPS_USED (set, false));
+ linemap_assert (index < LINEMAPS_USED (set, false));
return (line_map_ordinary *)LINEMAPS_MAP_AT (set, false, index);
}
/* Return the number of ordinary maps allocated in the line table
SET. */
-inline unsigned int
+inline line_map_uint_t
LINEMAPS_ORDINARY_ALLOCATED (const line_maps *set)
{
return LINEMAPS_ALLOCATED (set, false);
}
/* Return the number of ordinary maps used in the line table SET. */
-inline unsigned int
+inline line_map_uint_t
LINEMAPS_ORDINARY_USED (const line_maps *set)
{
return LINEMAPS_USED (set, false);
/* Returns the INDEXth macro map. */
inline line_map_macro *
-LINEMAPS_MACRO_MAP_AT (const line_maps *set, int index)
+LINEMAPS_MACRO_MAP_AT (const line_maps *set, line_map_uint_t index)
{
- linemap_assert (index >= 0
- && (unsigned int)index < LINEMAPS_USED (set, true));
+ linemap_assert (index < LINEMAPS_USED (set, true));
return (line_map_macro *)LINEMAPS_MAP_AT (set, true, index);
}
/* Returns the number of macro maps that were allocated in the line
table SET. */
-inline unsigned int
+inline line_map_uint_t
LINEMAPS_MACRO_ALLOCATED (const line_maps *set)
{
return LINEMAPS_ALLOCATED (set, true);
}
/* Returns the number of macro maps used in the line table SET. */
-inline unsigned int
+inline line_map_uint_t
LINEMAPS_MACRO_USED (const line_maps *set)
{
return LINEMAPS_USED (set, true);
(class line_maps *set, linenum_type to_line, unsigned int max_column_hint);
/* Allocate a raw block of line maps, zero initialized. */
-extern line_map *line_map_new_raw (line_maps *, bool, unsigned);
+extern line_map *line_map_new_raw (line_maps *, bool, line_map_uint_t);
/* Add a mapping of logical source line to physical source file and
line number. This function creates an "ordinary map", which is a
/* Restore the linemap state such that the map at LWM-1 continues.
Return start location of the new map. */
-extern unsigned linemap_module_restore
- (line_maps *, unsigned lwm);
+extern location_t linemap_module_restore
+ (line_maps *, line_map_uint_t lwm);
/* Given a logical source location, returns the map which the
corresponding (source file, line, column) triplet can be deduced
extern const line_map *linemap_lookup
(const line_maps *, location_t);
-unsigned linemap_lookup_macro_index (const line_maps *, location_t);
+line_map_uint_t linemap_lookup_macro_index (const line_maps *, location_t);
/* Returns TRUE if the line table set tracks token locations across
macro expansion, FALSE otherwise. */
SOURCE_COLUMN (const line_map_ordinary *ord_map, location_t loc)
{
return ((loc - ord_map->start_location)
- & ((1 << ord_map->m_column_and_range_bits) - 1)) >> ord_map->m_range_bits;
+ & ((location_t (1) << ord_map->m_column_and_range_bits) - 1))
+ >> ord_map->m_range_bits;
}
comes before the token of POST, 0 if PRE denotes the location of
the same token as the token for POST, and a negative value
otherwise. */
-int linemap_compare_locations (const line_maps *set,
- location_t pre,
- location_t post);
+int
+linemap_compare_locations (const line_maps *set,
+ location_t pre,
+ location_t post);
/* Return TRUE if LOC_A denotes the location a token that comes
topogically before the token denoted by location LOC_B, or if they
/* Dump line map at index IX in line table SET to STREAM. If STREAM
is NULL, use stderr. IS_MACRO is true if the caller wants to
dump a macro map, false otherwise. */
-void linemap_dump (FILE *, const line_maps *, unsigned, bool);
+void linemap_dump (FILE *, const line_maps *, line_map_uint_t, bool);
/* Dump line table SET to STREAM. If STREAM is NULL, stderr is used.
NUM_ORDINARY specifies how many ordinary maps to dump. NUM_MACRO
specifies how many macro maps to dump. */
-void line_table_dump (FILE *, const line_maps *, unsigned int, unsigned int);
+void line_table_dump (FILE *, const line_maps *,
+ line_map_uint_t, line_map_uint_t);
/* An enum for distinguishing the various parts within a location_t. */
#include "internal.h"
#include "hashtab.h"
+/* Useful for the bit manipulations in this file. */
+static constexpr location_t loc_one = 1;
+
static void trace_include (const line_maps *, const line_map_ordinary *);
static const line_map_ordinary * linemap_ordinary_map_lookup (const line_maps *,
location_t);
const line_map_ordinary **);
/* Counters defined in macro.cc. */
-extern unsigned num_expanded_macros_counter;
-extern unsigned num_macro_tokens_counter;
+extern line_map_uint_t num_expanded_macros_counter;
+extern line_map_uint_t num_macro_tokens_counter;
/* Destructor for class line_maps.
Ensure non-GC-managed memory is released. */
{
const struct location_adhoc_data *lb =
(const struct location_adhoc_data *) l;
- return ((hashval_t) lb->locus
- + (hashval_t) lb->src_range.m_start
- + (hashval_t) lb->src_range.m_finish
- + (size_t) lb->data
- + lb->discriminator);
+ return lb->locus
+ + lb->src_range.m_start
+ + lb->src_range.m_finish
+ + (size_t) lb->data
+ + lb->discriminator;
}
/* Compare function for location_adhoc_data hashtable. */
linemap_assert (pure_location_p (locus));
const line_map *map = linemap_lookup (this, locus);
const line_map_ordinary *ordmap = linemap_check_ordinary (map);
- unsigned int int_diff = src_range.m_finish - src_range.m_start;
- unsigned int col_diff = (int_diff >> ordmap->m_range_bits);
- if (col_diff < (1U << ordmap->m_range_bits))
+ auto int_diff = src_range.m_finish - src_range.m_start;
+ auto col_diff = (int_diff >> ordmap->m_range_bits);
+ if (col_diff < (loc_one << ordmap->m_range_bits))
{
location_t packed = locus | col_diff;
m_num_optimized_ranges++;
m_location_adhoc_data_map.data[m_location_adhoc_data_map.curr_loc++]
= lb;
}
- return ((*slot) - m_location_adhoc_data_map.data) | 0x80000000;
+ return ((*slot) - m_location_adhoc_data_map.data) | (1 + MAX_LOCATION_T);
}
/* Construct a location with caret at CARET, ranging from START to
const line_map *map = linemap_lookup (this, loc);
const line_map_ordinary *ordmap = linemap_check_ordinary (map);
source_range result;
- int offset = loc & ((1 << ordmap->m_range_bits) - 1);
+ auto offset = loc & ((loc_one << ordmap->m_range_bits) - 1);
result.m_start = loc - offset;
result.m_finish = result.m_start + (offset << ordmap->m_range_bits);
return result;
return true;
const line_map_ordinary *ordmap = linemap_check_ordinary (map);
- if (loc & ((1U << ordmap->m_range_bits) - 1))
+ if (loc & ((loc_one << ordmap->m_range_bits) - 1))
return false;
return true;
const line_map *map = linemap_lookup (this, loc);
const line_map_ordinary *ordmap = linemap_check_ordinary (map);
- return loc & ~((1 << ordmap->m_range_bits) - 1);
+ return loc & ~((loc_one << ordmap->m_range_bits) - 1);
}
location_t
/* Create NUM zero-initialized maps of type MACRO_P. */
line_map *
-line_map_new_raw (line_maps *set, bool macro_p, unsigned num)
+line_map_new_raw (line_maps *set, bool macro_p, line_map_uint_t num)
{
- unsigned num_maps_allocated = LINEMAPS_ALLOCATED (set, macro_p);
- unsigned num_maps_used = LINEMAPS_USED (set, macro_p);
+ auto num_maps_allocated = LINEMAPS_ALLOCATED (set, macro_p);
+ auto num_maps_used = LINEMAPS_USED (set, macro_p);
if (num > num_maps_allocated - num_maps_used)
{
/* Now alloc_size contains the exact memory size we would get if
we have asked for the initial alloc_size amount of memory.
Let's get back to the number of map that amounts to. */
- unsigned num_maps = alloc_size / size_of_a_map;
+ line_map_uint_t num_maps = alloc_size / size_of_a_map;
buffer = set->m_reallocator (buffer, num_maps * size_of_a_map);
memset ((char *)buffer + num_maps_used * size_of_a_map, 0,
(num_maps - num_maps_used) * size_of_a_map);
{
return (((map[1].start_location - 1
- map->start_location)
- & ~((1 << map->m_column_and_range_bits) - 1))
+ & ~((loc_one << map->m_column_and_range_bits) - 1))
+ map->start_location);
}
unsigned range_bits = 0;
if (start_location < LINE_MAP_MAX_LOCATION_WITH_COLS)
range_bits = set->default_range_bits;
- start_location += (1 << range_bits) - 1;
- start_location &= ~((1 << range_bits) - 1);
+ start_location += (loc_one << range_bits) - 1;
+ start_location &= ~((loc_one << range_bits) - 1);
linemap_assert (!LINEMAPS_ORDINARY_USED (set)
|| (start_location
/* The location of the end of the just-closed map. */
map->included_from
= (((map[0].start_location - 1 - map[-1].start_location)
- & ~((1 << map[-1].m_column_and_range_bits) - 1))
+ & ~((loc_one << map[-1].m_column_and_range_bits) - 1))
+ map[-1].start_location);
set->depth++;
if (set->trace_includes)
Append a new map, continuing the interrupted one. Return the start location
of the new map, or 0 if failed (because we ran out of locations. */
-unsigned
-linemap_module_restore (line_maps *set, unsigned lwm)
+location_t
+linemap_module_restore (line_maps *set, line_map_uint_t lwm)
{
linemap_assert (lwm);
location_t r;
linenum_type last_line =
SOURCE_LINE (map, set->highest_line);
- int line_delta = to_line - last_line;
+ auto line_delta = (linenum_arith_t) to_line - (linenum_arith_t) last_line;
bool add_map = false;
linemap_assert (map->m_column_and_range_bits >= map->m_range_bits);
int effective_column_bits = map->m_column_and_range_bits - map->m_range_bits;
single line we can sometimes just increase its column_bits instead. */
if (line_delta < 0
|| last_line != ORDINARY_MAP_STARTING_LINE_NUMBER (map)
- || SOURCE_COLUMN (map, highest) >= (1U << (column_bits - range_bits))
+ || SOURCE_COLUMN (map, highest) >= (loc_one
+ << (column_bits - range_bits))
|| ( /* We can't reuse the map if the line offset is sufficiently
large to cause overflow when computing location_t values. */
(to_line - ORDINARY_MAP_STARTING_LINE_NUMBER (map))
map->m_column_and_range_bits = column_bits;
map->m_range_bits = range_bits;
r = (MAP_START_LOCATION (map)
- + ((to_line - ORDINARY_MAP_STARTING_LINE_NUMBER (map))
+ + (location_t (to_line - ORDINARY_MAP_STARTING_LINE_NUMBER (map))
<< column_bits));
}
else
- r = set->highest_line + (line_delta << map->m_column_and_range_bits);
+ r = set->highest_line + (location_t (line_delta)
+ << map->m_column_and_range_bits);
/* Locations of ordinary tokens are always lower than locations of
macro tokens. */
}
}
line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
- r = r + (to_column << map->m_range_bits);
+ r = r + (location_t (to_column) << map->m_range_bits);
if (r >= set->highest_location)
set->highest_location = r;
return r;
linemap_assert (ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map) <= line);
location_t r = MAP_START_LOCATION (ord_map);
- r += ((line - ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map))
+ r += (location_t (line - ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map))
<< ord_map->m_column_and_range_bits);
if (r <= LINE_MAP_MAX_LOCATION_WITH_COLS)
- r += ((column & ((1 << ord_map->m_column_and_range_bits) - 1))
+ r += ((column & ((loc_one << ord_map->m_column_and_range_bits) - 1))
<< ord_map->m_range_bits);
location_t upper_limit = LINEMAPS_MACRO_LOWEST_LOCATION (set);
if (r >= upper_limit)
/* We find the real location and shift it. */
loc = linemap_resolve_location (set, loc, LRK_SPELLING_LOCATION, &map);
+ auto shifted_offset = location_t (column_offset) << map->m_range_bits;
+
/* The new location (loc + offset) should be higher than the first
location encoded by MAP. This can fail if the line information
is messed up because of line directives (see PR66415). */
- if (MAP_START_LOCATION (map) >= loc + (column_offset << map->m_range_bits))
+ if (MAP_START_LOCATION (map) >= loc + shifted_offset)
return loc;
linenum_type line = SOURCE_LINE (map, loc);
the next line map of the set. Otherwise, we try to encode the
location in the next map. */
for (; map != LINEMAPS_LAST_ORDINARY_MAP (set)
- && (loc + (column_offset << map->m_range_bits)
- >= MAP_START_LOCATION (map + 1)); map++)
+ && loc + shifted_offset >= MAP_START_LOCATION (map + 1); map++)
/* If the next map is a different file, or starts in a higher line, we
cannot encode the location there. */
if ((map + 1)->reason != LC_RENAME
if (set == NULL || line < RESERVED_LOCATION_COUNT)
return NULL;
- unsigned mn = set->info_ordinary.m_cache;
- unsigned mx = LINEMAPS_ORDINARY_USED (set);
+ auto mn = set->info_ordinary.m_cache;
+ auto mx = LINEMAPS_ORDINARY_USED (set);
const line_map_ordinary *cached = LINEMAPS_ORDINARY_MAP_AT (set, mn);
/* We should get a segfault if no line_maps have been added yet. */
while (mx - mn > 1)
{
- unsigned md = (mn + mx) / 2;
+ auto md = (mn + mx) / 2;
if (MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (set, md)) > line)
mx = md;
else
if (set == NULL)
return NULL;
- unsigned ix = linemap_lookup_macro_index (set, line);
+ auto ix = linemap_lookup_macro_index (set, line);
const struct line_map_macro *result = LINEMAPS_MACRO_MAP_AT (set, ix);
linemap_assert (MAP_START_LOCATION (result) <= line);
return result;
}
-unsigned
+line_map_uint_t
linemap_lookup_macro_index (const line_maps *set, location_t line)
{
- unsigned mn = set->info_macro.m_cache;
- unsigned mx = LINEMAPS_MACRO_USED (set);
+ auto mn = set->info_macro.m_cache;
+ auto mx = LINEMAPS_MACRO_USED (set);
const struct line_map_macro *cached = LINEMAPS_MACRO_MAP_AT (set, mn);
if (line >= MAP_START_LOCATION (cached))
while (mn < mx)
{
- unsigned md = (mx + mn) / 2;
+ auto md = (mx + mn) / 2;
if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, md)) > line)
mn = md + 1;
else
if (IS_ADHOC_LOC (l1))
l1 = get_location_from_adhoc_loc (set, l1);
- return l1 - l0;
+ /* This function is intended e.g. for implementing a qsort() comparator, so it
+ needs to return really an "int" and not something larger. */
+ const auto res = (location_diff_t)l1 - (location_diff_t)l0;
+ return res < INT_MIN ? INT_MIN : res > INT_MAX ? INT_MAX : res;
}
/* Print an include trace, for e.g. the -H option of the preprocessor. */
dump a macro map, false otherwise. */
void
-linemap_dump (FILE *stream, const line_maps *set, unsigned ix, bool is_macro)
+linemap_dump (FILE *stream, const line_maps *set, line_map_uint_t ix,
+ bool is_macro)
{
const char *const lc_reasons_v[LC_HWM]
= { "LC_ENTER", "LC_LEAVE", "LC_RENAME", "LC_RENAME_VERBATIM",
reason = LC_ENTER_MACRO;
}
- fprintf (stream, "Map #%u [%p] - LOC: %u - REASON: %s - SYSP: %s\n",
- ix, (void *) map, map->start_location,
+ fprintf (stream, "Map #%llu [%p] - LOC: %llu - REASON: %s - SYSP: %s\n",
+ (unsigned long long) ix,
+ (void *) map,
+ (unsigned long long) map->start_location,
reason < LC_HWM ? lc_reasons_v[reason] : "???",
((!is_macro
&& ORDINARY_MAP_IN_SYSTEM_HEADER_P (linemap_check_ordinary (map)))
const line_map_ordinary *includer_map
= linemap_included_from_linemap (set, ord_map);
- fprintf (stream, "File: %s:%d\n", ORDINARY_MAP_FILE_NAME (ord_map),
+ fprintf (stream, "File: %s:%u\n", ORDINARY_MAP_FILE_NAME (ord_map),
ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map));
- fprintf (stream, "Included from: [%d] %s\n",
- includer_map ? int (includer_map - set->info_ordinary.maps) : -1,
+ const long long from_ind
+ = includer_map ? includer_map - set->info_ordinary.maps : -1;
+ fprintf (stream, "Included from: [%lld] %s\n",
+ from_ind,
includer_map ? ORDINARY_MAP_FILE_NAME (includer_map) : "None");
}
else
/* P: path, L: line, C: column, S: in-system-header, M: map address,
E: macro expansion?, LOC: original location, R: resolved location */
- fprintf (stream, "{P:%s;F:%s;L:%d;C:%d;S:%d;M:%p;E:%d,LOC:%d,R:%d}",
- path, from, l, c, s, (void*)map, e, loc, location);
+ fprintf (stream, "{P:%s;F:%s;L:%d;C:%d;S:%d;M:%p;E:%d,LOC:%llu,R:%llu}",
+ path, from, l, c, s, (void*)map, e,
+ (unsigned long long) loc,
+ (unsigned long long) location);
}
/* Return the highest location emitted for a given file for which
return false;
/* Now look for the last ordinary map created for FILE_NAME. */
- int i;
+ location_diff_t i;
for (i = set->info_ordinary.used - 1; i >= 0; --i)
{
const char *fname = set->info_ordinary.maps[i].to_file;
location of the next map minus one, or -- if the map is the
latest one -- the highest location of the set. */
location_t result;
- if (i == (int) set->info_ordinary.used - 1)
+ if ((location_t) i == set->info_ordinary.used - 1)
result = set->highest_location;
else
result = set->info_ordinary.maps[i + 1].start_location - 1;
specifies how many macro maps to dump. */
void
-line_table_dump (FILE *stream, const line_maps *set, unsigned int num_ordinary,
- unsigned int num_macro)
+line_table_dump (FILE *stream, const line_maps *set,
+ line_map_uint_t num_ordinary, line_map_uint_t num_macro)
{
- unsigned int i;
+ line_map_uint_t i;
if (set == NULL)
return;
if (stream == NULL)
stream = stderr;
- fprintf (stream, "# of ordinary maps: %d\n", LINEMAPS_ORDINARY_USED (set));
- fprintf (stream, "# of macro maps: %d\n", LINEMAPS_MACRO_USED (set));
+ fprintf (stream, "# of ordinary maps: %llu\n",
+ (unsigned long long) LINEMAPS_ORDINARY_USED (set));
+ fprintf (stream, "# of macro maps: %llu\n",
+ (unsigned long long) LINEMAPS_MACRO_USED (set));
fprintf (stream, "Include stack depth: %d\n", set->depth);
- fprintf (stream, "Highest location: %u\n", set->highest_location);
+ fprintf (stream, "Highest location: %llu\n",
+ (unsigned long long) set->highest_location);
if (num_ordinary)
{