[Lldb-commits] [PATCH] D151603: [lldb][NFCI] Refactor Language::GetFormatterPrefixSuffix

Felipe de Azevedo Piovezan via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Sat May 27 04:47:53 PDT 2023


fdeazeve added inline comments.


================
Comment at: lldb/source/Plugins/Language/ObjC/ObjCLanguage.cpp:1001
 
-bool ObjCLanguage::GetFormatterPrefixSuffix(ValueObject &valobj,
-                                            ConstString type_hint,
-                                            std::string &prefix,
-                                            std::string &suffix) {
-  static ConstString g_CFBag("CFBag");
-  static ConstString g_CFBinaryHeap("CFBinaryHeap");
-
-  static ConstString g_NSNumberChar("NSNumber:char");
-  static ConstString g_NSNumberShort("NSNumber:short");
-  static ConstString g_NSNumberInt("NSNumber:int");
-  static ConstString g_NSNumberLong("NSNumber:long");
-  static ConstString g_NSNumberInt128("NSNumber:int128_t");
-  static ConstString g_NSNumberFloat("NSNumber:float");
-  static ConstString g_NSNumberDouble("NSNumber:double");
-
-  static ConstString g_NSData("NSData");
-  static ConstString g_NSArray("NSArray");
-  static ConstString g_NSString("NSString");
-  static ConstString g_NSStringStar("NSString*");
-
-  if (type_hint.IsEmpty())
-    return false;
-
-  prefix.clear();
-  suffix.clear();
-
-  if (type_hint == g_CFBag || type_hint == g_CFBinaryHeap) {
-    prefix = "@";
-    return true;
-  }
-
-  if (type_hint == g_NSNumberChar) {
-    prefix = "(char)";
-    return true;
-  }
-  if (type_hint == g_NSNumberShort) {
-    prefix = "(short)";
-    return true;
-  }
-  if (type_hint == g_NSNumberInt) {
-    prefix = "(int)";
-    return true;
-  }
-  if (type_hint == g_NSNumberLong) {
-    prefix = "(long)";
-    return true;
-  }
-  if (type_hint == g_NSNumberInt128) {
-    prefix = "(int128_t)";
-    return true;
-  }
-  if (type_hint == g_NSNumberFloat) {
-    prefix = "(float)";
-    return true;
-  }
-  if (type_hint == g_NSNumberDouble) {
-    prefix = "(double)";
-    return true;
-  }
-
-  if (type_hint == g_NSData || type_hint == g_NSArray) {
-    prefix = "@\"";
-    suffix = "\"";
-    return true;
-  }
-
-  if (type_hint == g_NSString || type_hint == g_NSStringStar) {
-    prefix = "@";
-    return true;
-  }
+std::pair<llvm::StringRef, llvm::StringRef>
+ObjCLanguage::GetFormatterPrefixSuffix(llvm::StringRef type_hint) {
----------------
We can make this whole map const and remove the explicit call_once by folding the `insert` calls into the ctor:

```
static constexpr llvm::StringRef empty;
static const llvm::StringMap<
    std::pair<const llvm::StringRef, const llvm::StringRef>>
    g_affix_map = {{"CFBag", std::make_pair("@", empty)},
                   {"CFBinaryHeap", std::make_pair("@", empty)},
                   ..., };
```

If you're so inclined, you can even get rid of the final make_pair calls:

```
static const llvm::StringMap<
    std::pair<const llvm::StringRef, const llvm::StringRef>>
    g_affix_map = {{"CFBag", {"@", empty}},
                   {"CFBinaryHeap", {"@", empty}}};
```


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D151603/new/

https://reviews.llvm.org/D151603



More information about the lldb-commits mailing list