diff options
author | manu <manu@138bc75d-0d04-0410-961f-82ee72b054a4> | 2014-12-02 16:03:31 +0000 |
---|---|---|
committer | manu <manu@138bc75d-0d04-0410-961f-82ee72b054a4> | 2014-12-02 16:03:31 +0000 |
commit | 324da92d45b1b704e39ff8802924f16c2ef6cb3f (patch) | |
tree | 5d0f39e1459edaf29cc238629070efeb1fc76e01 /libcpp | |
parent | 351855a5cb5432c9ed4827ec0fe3587dbc0b76a7 (diff) | |
download | gcc-324da92d45b1b704e39ff8802924f16c2ef6cb3f.tar.gz |
libcpp/ChangeLog:
2014-12-02 Manuel López-Ibáñez <manu@gcc.gnu.org>
* include/line-map.h (linemap_assert_fails): Declare.
* line-map.c (linemap_position_for_loc_and_offset): Use it.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@218277 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libcpp')
-rw-r--r-- | libcpp/ChangeLog | 5 | ||||
-rw-r--r-- | libcpp/include/line-map.h | 11 | ||||
-rw-r--r-- | libcpp/line-map.c | 17 |
3 files changed, 28 insertions, 5 deletions
diff --git a/libcpp/ChangeLog b/libcpp/ChangeLog index 4bfd329a2ad..afb0f791f2c 100644 --- a/libcpp/ChangeLog +++ b/libcpp/ChangeLog @@ -1,5 +1,10 @@ 2014-12-02 Manuel López-Ibáñez <manu@gcc.gnu.org> + * include/line-map.h (linemap_assert_fails): Declare. + * line-map.c (linemap_position_for_loc_and_offset): Use it. + +2014-12-02 Manuel López-Ibáñez <manu@gcc.gnu.org> + * line-map.c (linemap_add): Fix typo. (linemap_line_start): Fix whitespace. diff --git a/libcpp/include/line-map.h b/libcpp/include/line-map.h index edcbdc9bd66..48a5dec31e0 100644 --- a/libcpp/include/line-map.h +++ b/libcpp/include/line-map.h @@ -579,6 +579,16 @@ bool linemap_location_from_macro_expansion_p (const struct line_maps *, if (! (EXPR)) \ abort (); \ } while (0) + +/* Assert that becomes a conditional expression when checking is disabled at + compilation time. Use this for conditions that should not happen but if + they happen, it is better to handle them gracefully rather than crash + randomly later. + Usage: + + if (linemap_assert_fails(EXPR)) handle_error(); */ +#define linemap_assert_fails(EXPR) __extension__ \ + ({linemap_assert (EXPR); false;}) /* Assert that MAP encodes locations of tokens that are not part of the replacement-list of a macro expansion. */ @@ -588,6 +598,7 @@ bool linemap_location_from_macro_expansion_p (const struct line_maps *, #else /* Include EXPR, so that unused variable warnings do not occur. */ #define linemap_assert(EXPR) ((void)(0 && (EXPR))) +#define linemap_assert_fails(EXPR) (! (EXPR)) #define linemap_check_ordinary(LINE_MAP) (LINE_MAP) #endif diff --git a/libcpp/line-map.c b/libcpp/line-map.c index c9838a81721..6a695ab0f1b 100644 --- a/libcpp/line-map.c +++ b/libcpp/line-map.c @@ -645,7 +645,9 @@ linemap_position_for_loc_and_offset (struct line_maps *set, const struct line_map * map = NULL; /* This function does not support virtual locations yet. */ - linemap_assert (!linemap_location_from_macro_expansion_p (set, loc)); + if (linemap_assert_fails + (!linemap_location_from_macro_expansion_p (set, loc))) + return loc; if (offset == 0 /* Adding an offset to a reserved location (like @@ -658,22 +660,27 @@ linemap_position_for_loc_and_offset (struct line_maps *set, loc = linemap_resolve_location (set, loc, LRK_SPELLING_LOCATION, &map); /* The new location (loc + offset) should be higher than the first location encoded by MAP. */ - linemap_assert (MAP_START_LOCATION (map) < loc + offset); + if (linemap_assert_fails (MAP_START_LOCATION (map) < loc + offset)) + return loc; /* If MAP is not the last line map of its set, then the new location (loc + offset) should be less than the first location encoded by the next line map of the set. */ if (map != LINEMAPS_LAST_ORDINARY_MAP (set)) - linemap_assert (loc + offset < MAP_START_LOCATION (&map[1])); + if (linemap_assert_fails (loc + offset < MAP_START_LOCATION (&map[1]))) + return loc; offset += SOURCE_COLUMN (map, loc); - linemap_assert (offset < (1u << map->d.ordinary.column_bits)); + if (linemap_assert_fails (offset < (1u << map->d.ordinary.column_bits))) + return loc; source_location r = linemap_position_for_line_and_column (map, SOURCE_LINE (map, loc), offset); - linemap_assert (map == linemap_lookup (set, r)); + if (linemap_assert_fails (map == linemap_lookup (set, r))) + return loc; + return r; } |