This is an archive of the discontinued LLVM Phabricator instance.

Add 'oneOf' utility function for comparing a ConstString against a set of strings
AbandonedPublic

Authored by teemperor on Apr 27 2019, 11:51 AM.

Details

Summary

We have a few checks in LLDB where we compare a single ConstString against a hardcoded list of strings. This patch introduces a
utility function 'oneOf' in ConstString which is makes this check more readable.

For example, before this patch we had to write this:

if (ft == llvm::sys::fs::file_type::directory_file &&
    (file_spec.GetFileNameExtension() == g_sdk_suffix ||
     file_spec.GetFileNameExtension() == g_kdk_suffix)) {

And after this patch we can now write this:

if (ft == llvm::sys::fs::file_type::directory_file &&
    file_spec.GetFileNameExtension().oneOf(g_sdk_suffix, g_kdk_suffix)) {

Diff Detail

Event Timeline

teemperor created this revision.Apr 27 2019, 11:51 AM

I'm on the fence on this one. I think the == pattern attracts more attention to the fact that we are comparing strings. However, I also see the benefit, like in the example, where even a temporary variable wouldn't help much.

Regarding the name, how would you feel about anyOf instead? Although not relevant here, it has a nice duality with allOf. I think this is what the AST matchers use.

Yeah, I'm also just like 60% confident that this more readable/expressive.

anyOf sounds good to me. Will update the patch in case this gets community approval.

labath added a subscriber: labath.Apr 29 2019, 1:48 AM

Yeah, I'm also just like 60% confident that this more readable/expressive.

I'm about 70% against this ( :P ), and the main reason for that opinion is that there is nothing like this in llvm, and if llvm was able to get away without it, then I don't see why shouldn't we be able to do it too. The way these kinds of things are usually done in llvm is via the StringSwitch class, and it seems to me that StringSwitch could be directly used in at least half of the motivating use cases you show here (and in the rest, it could be used after a mild code refactor, which that code needs anyway).

(And yeah, I am aware that going through StringSwitch will mean we lose the fast string comparison that ConstString was meant to provide, but I am sure that this will have no impact on the overall performance here.)

lldb/source/Plugins/Language/CPlusPlus/LibStdcppUniquePointer.cpp
133–139

This is a prime example for a StringSwitch, which is how these kinds of things are usually done in llvm.

return StringSwitch<size_t>(name.GetStringRef()).Cases("ptr, "pointer", 0).Cases("del", "deleter", 1).Cases("obj", "object", "$$dereference$$", 2).Default(UINT32_MAX);

(+ running clang-format over that)

lldb/source/Plugins/Language/ObjC/ObjCLanguage.cpp
1039–1082

Using StringSwitch would make this code around three times shorter.

JDevlieghere resigned from this revision.May 21 2019, 4:01 PM
teemperor abandoned this revision.May 21 2019, 4:03 PM