Tested on hppa-unknown-linux-gnu.

Okay.

Dave
---

libstdc++: Work around gdb.error for hh_mm_ss::fractional_width [PR128817]

When GDB fails to materialize the static constexpr member
std::chrono::hh_mm_ss::fractional_width (reporting 'value has been
optimized out' even at -O0), StdChronoCalendarPrinter.to_string raises
a gdb.error.

Catch gdb.error when evaluating val['fractional_width'] and fall back
to computing the fractional width from the Duration template argument's
std::ratio period per C++20 [time.hms].

libstdc++-v3/ChangeLog:

        PR libstdc++/128817
        * python/libstdcxx/v6/printers.py
        (StdChronoCalendarPrinter.to_string): Fall back to deriving
        fractional_width from the Duration template argument if
        val['fractional_width'] raises gdb.error.

diff --git a/libstdc++-v3/python/libstdcxx/v6/printers.py 
b/libstdc++-v3/python/libstdcxx/v6/printers.py
index 1bba377513c..dfe1f48696e 100644
--- a/libstdc++-v3/python/libstdcxx/v6/printers.py
+++ b/libstdc++-v3/python/libstdcxx/v6/printers.py
@@ -2251,9 +2276,20 @@ class StdChronoCalendarPrinter(printer_base):
             return '{}/{}/{}'.format(y, m, val['_M_wdl'])
         if typ.startswith('std::chrono::hh_mm_ss'):
             fract = ''
-            if val['fractional_width'] != 0:
+            try:
+                width = int(val['fractional_width'])
+            except gdb.error:
+                # static constexpr fractional_width may be optimized out
+                dur = val.type.strip_typedefs().template_argument(0)
+                period = (dur.strip_typedefs()
+                          .template_argument(1).strip_typedefs())
+                num = int(period.template_argument(0))
+                den = int(period.template_argument(1))
+                width = next((w for w in range(19)
+                              if (num * (10 ** w)) % den == 0), 6)
+            if width != 0:
                 fract = '.{:0{}d}'.format(int(val['_M_ss']['_M_r']),
-                                          int(val['fractional_width']))
+                                          width)
             h = int(val['_M_h']['__r'])
             m = int(val['_M_m']['__r'])
             s = int(val['_M_s']['__r'])

Attachment: signature.asc
Description: PGP signature

Reply via email to