[Lldb-commits] [lldb] 8e0001e - [lldb][NFCI] Refactor Language::GetFormatterPrefixSuffix

Alex Langford via lldb-commits lldb-commits at lists.llvm.org
Tue May 30 13:12:10 PDT 2023


Author: Alex Langford
Date: 2023-05-30T13:11:55-07:00
New Revision: 8e0001eb95ce8654660510ddb06f5a8a3c5c6d68

URL: https://github.com/llvm/llvm-project/commit/8e0001eb95ce8654660510ddb06f5a8a3c5c6d68
DIFF: https://github.com/llvm/llvm-project/commit/8e0001eb95ce8654660510ddb06f5a8a3c5c6d68.diff

LOG: [lldb][NFCI] Refactor Language::GetFormatterPrefixSuffix

- Remove unused parameter `valobj` (I checked downstream, not
  even swift is using it).
- Return a std::pair<StringRef, StringRef> insted of having 2 out
  parameter strings.
- Remove the use of ConstStrings.

This change was primarily mechanical except in
`ObjCLanguage::GetFormatterPrefixSuffix`. To keep this fast, we
construct an llvm::StringMap<std::pair<StringRef, StringRef>> so that we
can look things up quickly. There is some amount of cost to setting up
the map the first time it is called, but subsequent lookups should be
as fast as a hash + string comparison (the cost of looking up something
in an llvm::StringMap).

Differential Revision: https://reviews.llvm.org/D151603

Added: 
    

Modified: 
    lldb/include/lldb/Target/Language.h
    lldb/source/Plugins/Language/ObjC/CF.cpp
    lldb/source/Plugins/Language/ObjC/Cocoa.cpp
    lldb/source/Plugins/Language/ObjC/NSArray.cpp
    lldb/source/Plugins/Language/ObjC/NSDictionary.cpp
    lldb/source/Plugins/Language/ObjC/NSSet.cpp
    lldb/source/Plugins/Language/ObjC/NSString.cpp
    lldb/source/Plugins/Language/ObjC/ObjCLanguage.cpp
    lldb/source/Plugins/Language/ObjC/ObjCLanguage.h
    lldb/source/Target/Language.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Target/Language.h b/lldb/include/lldb/Target/Language.h
index bbb3e7c0cc8d1..a6b9ccaf31b3c 100644
--- a/lldb/include/lldb/Target/Language.h
+++ b/lldb/include/lldb/Target/Language.h
@@ -208,14 +208,21 @@ class Language : public PluginInterface {
   /// that the name actually belongs to this language.
   virtual bool SymbolNameFitsToLanguage(Mangled name) const { return false; }
 
-  // if an individual data formatter can apply to several types and cross a
-  // language boundary it makes sense for individual languages to want to
-  // customize the printing of values of that type by appending proper
-  // prefix/suffix information in language-specific ways
-  virtual bool GetFormatterPrefixSuffix(ValueObject &valobj,
-                                        ConstString type_hint,
-                                        std::string &prefix,
-                                        std::string &suffix);
+  /// An individual data formatter may apply to several types and cross language
+  /// boundaries. Each of those languages may want to customize the display of
+  /// values of said types by appending proper prefix/suffix information in
+  /// language-specific ways. This function returns that prefix and suffix.
+  ///
+  /// \param[in] type_hint
+  ///   A StringRef used to determine what the prefix and suffix should be. It
+  ///   is called a hint because some types may have multiple variants for which
+  ///   the prefix and/or suffix may vary.
+  ///
+  /// \return
+  ///   A std::pair<StringRef, StringRef>, the first being the prefix and the
+  ///   second being the suffix. They may be empty.
+  virtual std::pair<llvm::StringRef, llvm::StringRef>
+  GetFormatterPrefixSuffix(llvm::StringRef type_hint);
 
   // When looking up functions, we take a user provided string which may be a
   // partial match to the full demangled name and compare it to the actual

diff  --git a/lldb/source/Plugins/Language/ObjC/CF.cpp b/lldb/source/Plugins/Language/ObjC/CF.cpp
index fa2130e4b01e3..0926192a4f384 100644
--- a/lldb/source/Plugins/Language/ObjC/CF.cpp
+++ b/lldb/source/Plugins/Language/ObjC/CF.cpp
@@ -44,7 +44,7 @@ bool lldb_private::formatters::CFAbsoluteTimeSummaryProvider(
 
 bool lldb_private::formatters::CFBagSummaryProvider(
     ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
-  static ConstString g_TypeHint("CFBag");
+  static constexpr llvm::StringLiteral g_TypeHint("CFBag");
 
   ProcessSP process_sp = valobj.GetProcessSP();
   if (!process_sp)
@@ -92,17 +92,13 @@ bool lldb_private::formatters::CFBagSummaryProvider(
   } else
     return false;
 
-  std::string prefix, suffix;
-  if (Language *language = Language::FindPlugin(options.GetLanguage())) {
-    if (!language->GetFormatterPrefixSuffix(valobj, g_TypeHint, prefix,
-                                            suffix)) {
-      prefix.clear();
-      suffix.clear();
-    }
-  }
+  llvm::StringRef prefix, suffix;
+  if (Language *language = Language::FindPlugin(options.GetLanguage()))
+    std::tie(prefix, suffix) = language->GetFormatterPrefixSuffix(g_TypeHint);
 
-  stream.Printf("%s\"%u value%s\"%s", prefix.c_str(), count,
-                (count == 1 ? "" : "s"), suffix.c_str());
+  stream << prefix;
+  stream.Printf("\"%u value%s\"", count, (count == 1 ? "" : "s"));
+  stream << suffix;
   return true;
 }
 
@@ -226,7 +222,7 @@ bool lldb_private::formatters::CFBitVectorSummaryProvider(
 
 bool lldb_private::formatters::CFBinaryHeapSummaryProvider(
     ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
-  static ConstString g_TypeHint("CFBinaryHeap");
+  static constexpr llvm::StringLiteral g_TypeHint("CFBinaryHeap");
 
   ProcessSP process_sp = valobj.GetProcessSP();
   if (!process_sp)
@@ -279,16 +275,12 @@ bool lldb_private::formatters::CFBinaryHeapSummaryProvider(
   } else
     return false;
 
-  std::string prefix, suffix;
-  if (Language *language = Language::FindPlugin(options.GetLanguage())) {
-    if (!language->GetFormatterPrefixSuffix(valobj, g_TypeHint, prefix,
-                                            suffix)) {
-      prefix.clear();
-      suffix.clear();
-    }
-  }
+  llvm::StringRef prefix, suffix;
+  if (Language *language = Language::FindPlugin(options.GetLanguage()))
+    std::tie(prefix, suffix) = language->GetFormatterPrefixSuffix(g_TypeHint);
 
-  stream.Printf("%s\"%u item%s\"%s", prefix.c_str(), count,
-                (count == 1 ? "" : "s"), suffix.c_str());
+  stream << prefix;
+  stream.Printf("\"%u item%s\"", count, (count == 1 ? "" : "s"));
+  stream << suffix;
   return true;
 }

diff  --git a/lldb/source/Plugins/Language/ObjC/Cocoa.cpp b/lldb/source/Plugins/Language/ObjC/Cocoa.cpp
index 46f82daaff8d5..243bec90f70c8 100644
--- a/lldb/source/Plugins/Language/ObjC/Cocoa.cpp
+++ b/lldb/source/Plugins/Language/ObjC/Cocoa.cpp
@@ -305,120 +305,97 @@ bool lldb_private::formatters::NSIndexSetSummaryProvider(
 
 static void NSNumber_FormatChar(ValueObject &valobj, Stream &stream, char value,
                                 lldb::LanguageType lang) {
-  static ConstString g_TypeHint("NSNumber:char");
-
-  std::string prefix, suffix;
-  if (Language *language = Language::FindPlugin(lang)) {
-    if (!language->GetFormatterPrefixSuffix(valobj, g_TypeHint, prefix,
-                                            suffix)) {
-      prefix.clear();
-      suffix.clear();
-    }
-  }
+  static constexpr llvm::StringLiteral g_TypeHint("NSNumber:char");
+
+  llvm::StringRef prefix, suffix;
+  if (Language *language = Language::FindPlugin(lang))
+    std::tie(prefix, suffix) = language->GetFormatterPrefixSuffix(g_TypeHint);
 
-  stream.Printf("%s%hhd%s", prefix.c_str(), value, suffix.c_str());
+  stream << prefix;
+  stream.Printf("%hhd", value);
+  stream << suffix;
 }
 
 static void NSNumber_FormatShort(ValueObject &valobj, Stream &stream,
                                  short value, lldb::LanguageType lang) {
-  static ConstString g_TypeHint("NSNumber:short");
-
-  std::string prefix, suffix;
-  if (Language *language = Language::FindPlugin(lang)) {
-    if (!language->GetFormatterPrefixSuffix(valobj, g_TypeHint, prefix,
-                                            suffix)) {
-      prefix.clear();
-      suffix.clear();
-    }
-  }
+  static constexpr llvm::StringLiteral g_TypeHint("NSNumber:short");
+
+  llvm::StringRef prefix, suffix;
+  if (Language *language = Language::FindPlugin(lang))
+    std::tie(prefix, suffix) = language->GetFormatterPrefixSuffix(g_TypeHint);
 
-  stream.Printf("%s%hd%s", prefix.c_str(), value, suffix.c_str());
+  stream << prefix;
+  stream.Printf("%hd", value);
+  stream << suffix;
 }
 
 static void NSNumber_FormatInt(ValueObject &valobj, Stream &stream, int value,
                                lldb::LanguageType lang) {
-  static ConstString g_TypeHint("NSNumber:int");
-
-  std::string prefix, suffix;
-  if (Language *language = Language::FindPlugin(lang)) {
-    if (!language->GetFormatterPrefixSuffix(valobj, g_TypeHint, prefix,
-                                            suffix)) {
-      prefix.clear();
-      suffix.clear();
-    }
-  }
+  static constexpr llvm::StringLiteral g_TypeHint("NSNumber:int");
+
+  llvm::StringRef prefix, suffix;
+  if (Language *language = Language::FindPlugin(lang))
+    std::tie(prefix, suffix) = language->GetFormatterPrefixSuffix(g_TypeHint);
 
-  stream.Printf("%s%d%s", prefix.c_str(), value, suffix.c_str());
+  stream << prefix;
+  stream.Printf("%d", value);
+  stream << suffix;
 }
 
 static void NSNumber_FormatLong(ValueObject &valobj, Stream &stream,
                                 int64_t value, lldb::LanguageType lang) {
-  static ConstString g_TypeHint("NSNumber:long");
-
-  std::string prefix, suffix;
-  if (Language *language = Language::FindPlugin(lang)) {
-    if (!language->GetFormatterPrefixSuffix(valobj, g_TypeHint, prefix,
-                                            suffix)) {
-      prefix.clear();
-      suffix.clear();
-    }
-  }
+  static constexpr llvm::StringLiteral g_TypeHint("NSNumber:long");
+
+  llvm::StringRef prefix, suffix;
+  if (Language *language = Language::FindPlugin(lang))
+    std::tie(prefix, suffix) = language->GetFormatterPrefixSuffix(g_TypeHint);
 
-  stream.Printf("%s%" PRId64 "%s", prefix.c_str(), value, suffix.c_str());
+  stream << prefix;
+  stream.Printf("%" PRId64 "", value);
+  stream << suffix;
 }
 
 static void NSNumber_FormatInt128(ValueObject &valobj, Stream &stream,
                                   const llvm::APInt &value,
                                   lldb::LanguageType lang) {
-  static ConstString g_TypeHint("NSNumber:int128_t");
-
-  std::string prefix, suffix;
-  if (Language *language = Language::FindPlugin(lang)) {
-    if (!language->GetFormatterPrefixSuffix(valobj, g_TypeHint, prefix,
-                                            suffix)) {
-      prefix.clear();
-      suffix.clear();
-    }
-  }
+  static constexpr llvm::StringLiteral g_TypeHint("NSNumber:int128_t");
+
+  llvm::StringRef prefix, suffix;
+  if (Language *language = Language::FindPlugin(lang))
+    std::tie(prefix, suffix) = language->GetFormatterPrefixSuffix(g_TypeHint);
 
-  stream.PutCString(prefix.c_str());
+  stream << prefix;
   const int radix = 10;
   const bool isSigned = true;
   std::string str = llvm::toString(value, radix, isSigned);
   stream.PutCString(str.c_str());
-  stream.PutCString(suffix.c_str());
+  stream << suffix;
 }
 
 static void NSNumber_FormatFloat(ValueObject &valobj, Stream &stream,
                                  float value, lldb::LanguageType lang) {
-  static ConstString g_TypeHint("NSNumber:float");
-
-  std::string prefix, suffix;
-  if (Language *language = Language::FindPlugin(lang)) {
-    if (!language->GetFormatterPrefixSuffix(valobj, g_TypeHint, prefix,
-                                            suffix)) {
-      prefix.clear();
-      suffix.clear();
-    }
-  }
+  static constexpr llvm::StringLiteral g_TypeHint("NSNumber:float");
+
+  llvm::StringRef prefix, suffix;
+  if (Language *language = Language::FindPlugin(lang))
+    std::tie(prefix, suffix) = language->GetFormatterPrefixSuffix(g_TypeHint);
 
-  stream.Printf("%s%f%s", prefix.c_str(), value, suffix.c_str());
+  stream << prefix;
+  stream.Printf("%f", value);
+  stream << suffix;
 }
 
 static void NSNumber_FormatDouble(ValueObject &valobj, Stream &stream,
                                   double value, lldb::LanguageType lang) {
-  static ConstString g_TypeHint("NSNumber:double");
-
-  std::string prefix, suffix;
-  if (Language *language = Language::FindPlugin(lang)) {
-    if (!language->GetFormatterPrefixSuffix(valobj, g_TypeHint, prefix,
-                                            suffix)) {
-      prefix.clear();
-      suffix.clear();
-    }
-  }
+  static constexpr llvm::StringLiteral g_TypeHint("NSNumber:double");
+
+  llvm::StringRef prefix, suffix;
+  if (Language *language = Language::FindPlugin(lang))
+    std::tie(prefix, suffix) = language->GetFormatterPrefixSuffix(g_TypeHint);
 
-  stream.Printf("%s%g%s", prefix.c_str(), value, suffix.c_str());
+  stream << prefix;
+  stream.Printf("%g", value);
+  stream << suffix;
 }
 
 bool lldb_private::formatters::NSNumberSummaryProvider(
@@ -813,29 +790,27 @@ bool lldb_private::formatters::NSURLSummaryProvider(
   if (!NSStringSummaryProvider(*text, summary, options) || summary.Empty())
     return false;
 
-  const char quote_char = '"';
-  std::string prefix, suffix;
-  if (Language *language = Language::FindPlugin(options.GetLanguage())) {
-    if (!language->GetFormatterPrefixSuffix(*text, ConstString("NSString"),
-                                            prefix, suffix)) {
-      prefix.clear();
-      suffix.clear();
-    }
-  }
+  static constexpr llvm::StringLiteral quote_char("\"");
+  static constexpr llvm::StringLiteral g_TypeHint("NSString");
+  llvm::StringRef prefix, suffix;
+  if (Language *language = Language::FindPlugin(options.GetLanguage()))
+    std::tie(prefix, suffix) = language->GetFormatterPrefixSuffix(g_TypeHint);
+
   // @"A" -> @"A
   llvm::StringRef summary_str = summary.GetString();
-  bool back_consumed = summary_str.consume_back(quote_char + suffix);
+  bool back_consumed =
+      summary_str.consume_back(suffix) && summary_str.consume_back(quote_char);
   assert(back_consumed);
   UNUSED_IF_ASSERT_DISABLED(back_consumed);
   // @"B" -> B"
   llvm::StringRef base_summary_str = base_summary.GetString();
-  bool front_consumed = base_summary_str.consume_front(prefix + quote_char);
+  bool front_consumed = base_summary_str.consume_front(prefix) &&
+                        base_summary_str.consume_front(quote_char);
   assert(front_consumed);
   UNUSED_IF_ASSERT_DISABLED(front_consumed);
   // @"A -- B"
   if (!summary_str.empty() && !base_summary_str.empty()) {
-    stream.Printf("%s -- %s", summary_str.str().c_str(),
-                  base_summary_str.str().c_str());
+    stream << summary_str << " -- " << base_summary_str;
     return true;
   }
 

diff  --git a/lldb/source/Plugins/Language/ObjC/NSArray.cpp b/lldb/source/Plugins/Language/ObjC/NSArray.cpp
index 18bb2b8c4fdcf..bd356a61161a5 100644
--- a/lldb/source/Plugins/Language/ObjC/NSArray.cpp
+++ b/lldb/source/Plugins/Language/ObjC/NSArray.cpp
@@ -334,7 +334,7 @@ class NSArray1SyntheticFrontEnd : public SyntheticChildrenFrontEnd {
 
 bool lldb_private::formatters::NSArraySummaryProvider(
     ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
-  static ConstString g_TypeHint("NSArray");
+  static constexpr llvm::StringLiteral g_TypeHint("NSArray");
 
   ProcessSP process_sp = valobj.GetProcessSP();
   if (!process_sp)
@@ -445,17 +445,13 @@ bool lldb_private::formatters::NSArraySummaryProvider(
       return false;
   }
 
-  std::string prefix, suffix;
-  if (Language *language = Language::FindPlugin(options.GetLanguage())) {
-    if (!language->GetFormatterPrefixSuffix(valobj, g_TypeHint, prefix,
-                                            suffix)) {
-      prefix.clear();
-      suffix.clear();
-    }
-  }
+  llvm::StringRef prefix, suffix;
+  if (Language *language = Language::FindPlugin(options.GetLanguage()))
+    std::tie(prefix, suffix) = language->GetFormatterPrefixSuffix(g_TypeHint);
 
-  stream.Printf("%s%" PRIu64 " %s%s%s", prefix.c_str(), value, "element",
-                value == 1 ? "" : "s", suffix.c_str());
+  stream << prefix;
+  stream.Printf("%" PRIu64 " %s%s", value, "element", value == 1 ? "" : "s");
+  stream << suffix;
   return true;
 }
 

diff  --git a/lldb/source/Plugins/Language/ObjC/NSDictionary.cpp b/lldb/source/Plugins/Language/ObjC/NSDictionary.cpp
index 4bab8924f3a5e..702d196a7dda9 100644
--- a/lldb/source/Plugins/Language/ObjC/NSDictionary.cpp
+++ b/lldb/source/Plugins/Language/ObjC/NSDictionary.cpp
@@ -409,7 +409,7 @@ namespace Foundation1437 {
 template <bool name_entries>
 bool lldb_private::formatters::NSDictionarySummaryProvider(
     ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
-  static ConstString g_TypeHint("NSDictionary");
+  static constexpr llvm::StringLiteral g_TypeHint("NSDictionary");
   ProcessSP process_sp = valobj.GetProcessSP();
   if (!process_sp)
     return false;
@@ -501,17 +501,14 @@ bool lldb_private::formatters::NSDictionarySummaryProvider(
     return false;
   }
 
-  std::string prefix, suffix;
-  if (Language *language = Language::FindPlugin(options.GetLanguage())) {
-    if (!language->GetFormatterPrefixSuffix(valobj, g_TypeHint, prefix,
-                                            suffix)) {
-      prefix.clear();
-      suffix.clear();
-    }
-  }
+  llvm::StringRef prefix, suffix;
+  if (Language *language = Language::FindPlugin(options.GetLanguage()))
+    std::tie(prefix, suffix) = language->GetFormatterPrefixSuffix(g_TypeHint);
 
-  stream.Printf("%s%" PRIu64 " %s%s%s", prefix.c_str(), value, "key/value pair",
-                value == 1 ? "" : "s", suffix.c_str());
+  stream << prefix;
+  stream.Printf("%" PRIu64 " %s%s", value, "key/value pair",
+                value == 1 ? "" : "s");
+  stream << suffix;
   return true;
 }
 

diff  --git a/lldb/source/Plugins/Language/ObjC/NSSet.cpp b/lldb/source/Plugins/Language/ObjC/NSSet.cpp
index fac8594d0c7d9..44097ee0c42b8 100644
--- a/lldb/source/Plugins/Language/ObjC/NSSet.cpp
+++ b/lldb/source/Plugins/Language/ObjC/NSSet.cpp
@@ -249,7 +249,7 @@ class NSSetCodeRunningSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
 template <bool cf_style>
 bool lldb_private::formatters::NSSetSummaryProvider(
     ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
-  static ConstString g_TypeHint("NSSet");
+  static constexpr llvm::StringLiteral g_TypeHint("NSSet");
 
   ProcessSP process_sp = valobj.GetProcessSP();
   if (!process_sp)
@@ -322,17 +322,13 @@ bool lldb_private::formatters::NSSetSummaryProvider(
       return false;
   }
 
-  std::string prefix, suffix;
-  if (Language *language = Language::FindPlugin(options.GetLanguage())) {
-    if (!language->GetFormatterPrefixSuffix(valobj, g_TypeHint, prefix,
-                                            suffix)) {
-      prefix.clear();
-      suffix.clear();
-    }
-  }
+  llvm::StringRef prefix, suffix;
+  if (Language *language = Language::FindPlugin(options.GetLanguage()))
+    std::tie(prefix, suffix) = language->GetFormatterPrefixSuffix(g_TypeHint);
 
-  stream.Printf("%s%" PRIu64 " %s%s%s", prefix.c_str(), value, "element",
-                value == 1 ? "" : "s", suffix.c_str());
+  stream << prefix;
+  stream.Printf("%" PRIu64 " %s%s", value, "element", value == 1 ? "" : "s");
+  stream << suffix;
   return true;
 }
 

diff  --git a/lldb/source/Plugins/Language/ObjC/NSString.cpp b/lldb/source/Plugins/Language/ObjC/NSString.cpp
index 61705c866778c..0a30737d9723b 100644
--- a/lldb/source/Plugins/Language/ObjC/NSString.cpp
+++ b/lldb/source/Plugins/Language/ObjC/NSString.cpp
@@ -33,7 +33,7 @@ NSString_Additionals::GetAdditionalSummaries() {
 bool lldb_private::formatters::NSStringSummaryProvider(
     ValueObject &valobj, Stream &stream,
     const TypeSummaryOptions &summary_options) {
-  static ConstString g_TypeHint("NSString");
+  static constexpr llvm::StringLiteral g_TypeHint("NSString");
 
   ProcessSP process_sp = valobj.GetProcessSP();
   if (!process_sp)
@@ -126,19 +126,13 @@ bool lldb_private::formatters::NSStringSummaryProvider(
     return true;
   }
 
-  std::string prefix, suffix;
-  if (Language *language =
-          Language::FindPlugin(summary_options.GetLanguage())) {
-    if (!language->GetFormatterPrefixSuffix(valobj, g_TypeHint, prefix,
-                                            suffix)) {
-      prefix.clear();
-      suffix.clear();
-    }
-  }
+  llvm::StringRef prefix, suffix;
+  if (Language *language = Language::FindPlugin(summary_options.GetLanguage()))
+    std::tie(prefix, suffix) = language->GetFormatterPrefixSuffix(g_TypeHint);
 
   StringPrinter::ReadStringAndDumpToStreamOptions options(valobj);
-  options.SetPrefixToken(prefix);
-  options.SetSuffixToken(suffix);
+  options.SetPrefixToken(prefix.str());
+  options.SetSuffixToken(suffix.str());
 
   if (is_mutable) {
     uint64_t location = 2 * ptr_size + valobj_addr;
@@ -318,7 +312,7 @@ bool lldb_private::formatters::NSMutableAttributedStringSummaryProvider(
 bool lldb_private::formatters::NSTaggedString_SummaryProvider(
     ValueObject &valobj, ObjCLanguageRuntime::ClassDescriptorSP descriptor,
     Stream &stream, const TypeSummaryOptions &summary_options) {
-  static ConstString g_TypeHint("NSString");
+  static constexpr llvm::StringLiteral g_TypeHint("NSString");
 
   if (!descriptor)
     return false;
@@ -336,23 +330,17 @@ bool lldb_private::formatters::NSTaggedString_SummaryProvider(
   if (len_bits > g_fiveBitMaxLen)
     return false;
 
-  std::string prefix, suffix;
-  if (Language *language =
-          Language::FindPlugin(summary_options.GetLanguage())) {
-    if (!language->GetFormatterPrefixSuffix(valobj, g_TypeHint, prefix,
-                                            suffix)) {
-      prefix.clear();
-      suffix.clear();
-    }
-  }
+  llvm::StringRef prefix, suffix;
+  if (Language *language = Language::FindPlugin(summary_options.GetLanguage()))
+    std::tie(prefix, suffix) = language->GetFormatterPrefixSuffix(g_TypeHint);
 
   // this is a fairly ugly trick - pretend that the numeric value is actually a
   // char* this works under a few assumptions: little endian architecture
   // sizeof(uint64_t) > g_MaxNonBitmaskedLen
   if (len_bits <= g_MaxNonBitmaskedLen) {
-    stream.Printf("%s", prefix.c_str());
+    stream << prefix;
     stream.Printf("\"%s\"", (const char *)&data_bits);
-    stream.Printf("%s", suffix.c_str());
+    stream << suffix;
     return true;
   }
 
@@ -375,8 +363,8 @@ bool lldb_private::formatters::NSTaggedString_SummaryProvider(
     bytes.insert(bytes.begin(), sixBitToCharLookup[packed]);
   }
 
-  stream.Printf("%s", prefix.c_str());
+  stream << prefix;
   stream.Printf("\"%s\"", &bytes[0]);
-  stream.Printf("%s", suffix.c_str());
+  stream << suffix;
   return true;
 }

diff  --git a/lldb/source/Plugins/Language/ObjC/ObjCLanguage.cpp b/lldb/source/Plugins/Language/ObjC/ObjCLanguage.cpp
index 3a9e287158329..762f662d5773b 100644
--- a/lldb/source/Plugins/Language/ObjC/ObjCLanguage.cpp
+++ b/lldb/source/Plugins/Language/ObjC/ObjCLanguage.cpp
@@ -998,78 +998,27 @@ std::unique_ptr<Language::TypeScavenger> ObjCLanguage::GetTypeScavenger() {
                                         ObjCDebugInfoScavenger>());
 }
 
-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;
-  }
-
-  return false;
+std::pair<llvm::StringRef, llvm::StringRef>
+ObjCLanguage::GetFormatterPrefixSuffix(llvm::StringRef type_hint) {
+  static constexpr llvm::StringRef empty;
+  static const llvm::StringMap<
+      std::pair<const llvm::StringRef, const llvm::StringRef>>
+      g_affix_map = {
+          {"CFBag", {"@", empty}},
+          {"CFBinaryHeap", {"@", empty}},
+          {"NSString", {"@", empty}},
+          {"NSString*", {"@", empty}},
+          {"NSNumber:char", {"(char)", empty}},
+          {"NSNumber:short", {"(short)", empty}},
+          {"NSNumber:int", {"(int)", empty}},
+          {"NSNumber:long", {"(long)", empty}},
+          {"NSNumber:int128_t", {"(int128_t)", empty}},
+          {"NSNumber:float", {"(float)", empty}},
+          {"NSNumber:double", {"(double)", empty}},
+          {"NSData", {"@\"", "\""}},
+          {"NSArray", {"@\"", "\""}},
+      };
+  return g_affix_map.lookup(type_hint);
 }
 
 bool ObjCLanguage::IsNilReference(ValueObject &valobj) {

diff  --git a/lldb/source/Plugins/Language/ObjC/ObjCLanguage.h b/lldb/source/Plugins/Language/ObjC/ObjCLanguage.h
index 3b74e79254936..bb8057846bb7c 100644
--- a/lldb/source/Plugins/Language/ObjC/ObjCLanguage.h
+++ b/lldb/source/Plugins/Language/ObjC/ObjCLanguage.h
@@ -150,9 +150,8 @@ class ObjCLanguage : public Language {
 
   std::unique_ptr<TypeScavenger> GetTypeScavenger() override;
 
-  bool GetFormatterPrefixSuffix(ValueObject &valobj, ConstString type_hint,
-                                std::string &prefix,
-                                std::string &suffix) override;
+  std::pair<llvm::StringRef, llvm::StringRef>
+  GetFormatterPrefixSuffix(llvm::StringRef type_hint) override;
 
   bool IsNilReference(ValueObject &valobj) override;
 

diff  --git a/lldb/source/Target/Language.cpp b/lldb/source/Target/Language.cpp
index 4299c402c613a..a307cb345c005 100644
--- a/lldb/source/Target/Language.cpp
+++ b/lldb/source/Target/Language.cpp
@@ -452,11 +452,9 @@ bool Language::ImageListTypeScavenger::Find_Impl(
   return result;
 }
 
-bool Language::GetFormatterPrefixSuffix(ValueObject &valobj,
-                                        ConstString type_hint,
-                                        std::string &prefix,
-                                        std::string &suffix) {
-  return false;
+std::pair<llvm::StringRef, llvm::StringRef>
+Language::GetFormatterPrefixSuffix(llvm::StringRef type_hint) {
+  return std::pair<llvm::StringRef, llvm::StringRef>();
 }
 
 bool Language::DemangledNameContainsPath(llvm::StringRef path, 


        


More information about the lldb-commits mailing list