This patch fixes two issues in locations with offsets that these two
new tests revealed:
1) we should check the return value of location_get_source_line;
2) linemap_position_for_loc_and_offset can be confused if a location
   is in a different map than it expects it to be.  Proper fix is unknown
   yet, so removing two asserts and just giving up is best for now.
   Manuel said he'd take a look at this later.

Bootstrapped/regtested on x86_64-linux, ok for trunk?

2015-06-04  Marek Polacek  <pola...@redhat.com>

        PR c/66415
        * c-format.c (location_from_offset): Return LOC if LINE is null.

        * line-map.c (linemap_position_for_loc_and_offset): Remove
        linemap_assert_fails; reverse conditions.

        * gcc.dg/cpp/pr66415-1.c: New test.
        * gcc.dg/cpp/pr66415-2.c: New test.

diff --git gcc/gcc/c-family/c-format.c gcc/gcc/c-family/c-format.c
index a6c2500..972dc18 100644
--- gcc/gcc/c-family/c-format.c
+++ gcc/gcc/c-family/c-format.c
@@ -142,9 +142,11 @@ location_from_offset (location_t loc, int offset)
   expanded_location s = expand_location_to_spelling_point (loc);
   int line_width;
   const char *line = location_get_source_line (s, &line_width);
+  if (line == NULL)
+    return loc;
   line += s.column - 1 ;
   line_width -= s.column - 1;
-  unsigned int column = 
+  unsigned int column =
     location_column_from_byte_offset (line, line_width, (unsigned) offset);
 
   return linemap_position_for_loc_and_offset (line_table, loc, column);
diff --git gcc/gcc/testsuite/gcc.dg/cpp/pr66415-1.c 
gcc/gcc/testsuite/gcc.dg/cpp/pr66415-1.c
index e69de29..922e326 100644
--- gcc/gcc/testsuite/gcc.dg/cpp/pr66415-1.c
+++ gcc/gcc/testsuite/gcc.dg/cpp/pr66415-1.c
@@ -0,0 +1,9 @@
+/* PR c/66415 */
+/* { dg-do compile } */
+/* { dg-options "-Wformat" } */
+
+void
+fn1 (void)
+{
+  __builtin_printf                                
("xxxxxxxxxxxxxxxxx%dxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); 
/* { dg-warning "format" } */
+}
diff --git gcc/gcc/testsuite/gcc.dg/cpp/pr66415-2.c 
gcc/gcc/testsuite/gcc.dg/cpp/pr66415-2.c
index e69de29..c168ce9 100644
--- gcc/gcc/testsuite/gcc.dg/cpp/pr66415-2.c
+++ gcc/gcc/testsuite/gcc.dg/cpp/pr66415-2.c
@@ -0,0 +1,11 @@
+/* PR c/66415 */
+/* { dg-do compile } */
+/* { dg-options "-Wformat" } */
+
+#24
+void
+fn1 (void)
+{
+  __builtin_printf                                
("xxxxxxxxxxxxxxxxx%dxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
+  /* { dg-warning "format" "" { target *-*-* } 27 } */
+}
diff --git gcc/libcpp/line-map.c gcc/libcpp/line-map.c
index 972f66c..85ad434 100644
--- gcc/libcpp/line-map.c
+++ gcc/libcpp/line-map.c
@@ -688,15 +688,17 @@ linemap_position_for_loc_and_offset (struct line_maps 
*set,
   /* We find the real location and shift it.  */
   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.  */
-  if (linemap_assert_fails (MAP_START_LOCATION (map) < loc + offset))
+     location encoded by MAP.
+     FIXME: We used to linemap_assert_fails here and in the if below,
+     but that led to PR66415.  So give up for now.  */
+  if ((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))
-    if (linemap_assert_fails (loc + offset < MAP_START_LOCATION (&map[1])))
+    if ((loc + offset >= MAP_START_LOCATION (&map[1])))
       return loc;
 
   offset += SOURCE_COLUMN (map, loc);

        Marek

Reply via email to