[PATCH] [gdb/build] Fix static cast of virtual base
Tom Tromey
tom@tromey.com
Fri Mar 8 16:18:39 GMT 2024
>>>>> "Simon" == Simon Marchi <simon.marchi@polymtl.ca> writes:
>> - T result = dynamic_cast<T> (v);
>> - gdb_assert (result != nullptr);
>> + gdb_assert (dynamic_cast<T> (v) != nullptr);
>> +
>> + /* If a base class of V is virtual then the dynamic_cast will succeed,
>> + but the production mode static_cast will fail. So having checked with
>> + the dynamic_cast that we didn't get nullptr, now use static_cast to
>> + catch the virtual base case. This has the side effect of guaranteeing
>> + that GDB will compile in production mode. */
>> + T result = static_cast<T> (v);
>> #else
>> T result = static_cast<T> (v);
>> #endif
>>
Simon> This is similar to what I proposed here:
Simon> https://inbox.sourceware.org/gdb-patches/24af4ea8-5426-4ce4-b1c5-12858b38a952@simark.ca/
Simon> The idea is the same, to have a static_cast in the DEVELOPMENT branch.
Simon> I kinda like my version better, as it factors out the static cast
Simon> (notice that both branches have identical static_cast lines after your
Simon> patch) and the ifdef is just around a single assert. Also, I'm pretty
Simon> sure the nullptr check is not necessary, as both dynamic_cast and
Simon> static_cast can handle it.
I think either of these would be fine.
Tom
More information about the Gdb-patches
mailing list