https://gcc.gnu.org/g:0cc80d1e82c399a64f24ebc2cddf162980d0d286

commit r17-6848-g0cc80d1e82c399a64f24ebc2cddf162980d0d286
Author: John David Anglin <[email protected]>
Date:   Mon Nov 30 10:09:36 2026 -0500

    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:
---
 libstdc++-v3/python/libstdcxx/v6/printers.py | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/libstdc++-v3/python/libstdcxx/v6/printers.py 
b/libstdc++-v3/python/libstdcxx/v6/printers.py
index 1bba377513ca..8927b7c4fd86 100644
--- a/libstdc++-v3/python/libstdcxx/v6/printers.py
+++ b/libstdc++-v3/python/libstdcxx/v6/printers.py
@@ -2251,9 +2251,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'])

Reply via email to