Hello,

I noticed that linemap_macro_map_lookup can yield the wrong map when
at the end of the lookup, we end up with a pair of maps whose indexes
are (mn,mx) with mx-mn == 1.  In that case, if the map we want is mn,
we'd wrongly yield mx.

Fixed thus, bootstrapped and tested on x86_64-unknown-linux-gnu
against trunk.

        * line-map.c (linemap_macro_map_lookup): Make sure to always pick
        the map with the highest MAP_START_LOCATION.
---
 libcpp/line-map.c |    9 +++++++--
 1 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/libcpp/line-map.c b/libcpp/line-map.c
index 352d697..a94bf0c 100644
--- a/libcpp/line-map.c
+++ b/libcpp/line-map.c
@@ -588,14 +588,19 @@ linemap_macro_map_lookup (struct line_maps *set, 
source_location line)
       mn = 0;
     }
 
-  do 
+  while (mx - mn > 1)
     {
       md = (mx + mn) / 2;
       if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, md)) > line)
        mn = md;
       else
        mx = md;
-    } while (mx - mn > 1);
+    }
+
+  /* There are cases where mx - mn = 1 and where the map we want is
+     mn.  Let's not miss it.  */
+  if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, mn)) >= line)
+      mx = mn;
 
   LINEMAPS_MACRO_CACHE (set) = mx;
   result = LINEMAPS_MACRO_MAP_AT (set, LINEMAPS_MACRO_CACHE (set));
-- 
1.7.6.4

Reply via email to