This is an archive of the discontinued LLVM Phabricator instance.

[lldb] Use templates to simplify {Get,Set}PropertyAtIndex (NFC)
ClosedPublic

Authored by JDevlieghere on May 3 2023, 10:51 AM.

Details

Summary

Use templates to simplify {Get,Set}PropertyAtIndex. It has always bothered me how cumbersome those calls are when adding new properties. After this patch, SetPropertyAtIndex infers the type from its arguments and GetPropertyAtIndex required a single template argument for the return value. As an added benefit, this enables us to remove a bunch of wrappers from UserSettingsController and OptionValueProperties.

The end goal is to phase out all the methods with option type names (i.e. GetPropertyAtIndexAsSInt64) from those two classes. There's more work to be done to get there but this patch keeps growing and I think it has reached critical mass to be reviewed. The remaining methods can be converted/removed incrementally. Besides that there's a few more things we can do to simplify the call sites even more: we can add overloads for booleans and enums to avoid the static_cast and != 0 comparisons. I didn't include them in this patch because they are more error prone and therefore increase the risk of regressing something.

Diff Detail

Event Timeline

JDevlieghere created this revision.May 3 2023, 10:51 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 3 2023, 10:51 AM
Herald added a subscriber: arphaman. · View Herald Transcript
JDevlieghere requested review of this revision.May 3 2023, 10:51 AM
bulbazord accepted this revision.May 3 2023, 11:01 AM

I quite like the idea. Probably want to wait for others to look over it but I think it's good!

lldb/source/Core/Debugger.cpp
394

remove commented out line.

This revision is now accepted and ready to land.May 3 2023, 11:01 AM

Use C++17 to simplify things

Remove duplicate if-clause

mib added a subscriber: mib.May 4 2023, 3:12 PM

Very cool stuff!

lldb/include/lldb/Interpreter/OptionValue.h
325–344

nit: In the template argument, you use std::is_pointer<T>::value instead of std::is_pointer_v<T> and the in the if statement you do the opposite (std::is_same_v<T> vs std::is_same<T>::value). I personally not a fan of the _v alias but what I'm saying here is it would be good to stay consistent.

lldb/include/lldb/Interpreter/OptionValueProperties.h
162–173

Very interesting!

lldb/source/Interpreter/Property.cpp
229

May be we should print a warning or error to the user ?

JDevlieghere marked 2 inline comments as done.May 4 2023, 4:36 PM
JDevlieghere added inline comments.
lldb/include/lldb/Interpreter/OptionValue.h
325–344

Good point, I've unified everything to use the _v variant.

lldb/source/Interpreter/Property.cpp
229

Properties are all tablegen'd so if this fires we did something wrong. There's nothing actionable here for a user.

This revision was automatically updated to reflect the committed changes.
JDevlieghere marked 2 inline comments as done.
Herald added a project: Restricted Project. · View Herald TranscriptMay 4 2023, 4:43 PM
jgorbe added a subscriber: jgorbe.May 8 2023, 11:28 AM
jgorbe added inline comments.
lldb/source/Target/Target.cpp
4520

I think this is causing settings set target.max-string-summary-length not to work properly.

As far as I can tell, the problem is that GetPropertyAtIndexAs<uint64_t> will end up calling OptionValue::GetAsUInt64() which checks the declared type of the property and returns null if there's a mismatch. target.max-string-summary-length is, for some reason, defined as signed, so this will always return the default value regardless of what it was set to.

You should be able to reproduce it by writing a simple program with a std::string, setting target.max-string-summary-length to some small value and checking that the string is not truncated.

GetMaximumMemReadSize, just below this one, seems to be mismatched too because the diff shows a change from GetPropertyAtIndexAsSInt64 to GetPropertyAtIndexAs<uint64_t> but I haven't actually tested that one. It would probably be a good idea to double check the rest of the patch looking for other mismatches.