[lldb][NFC] Remove several inefficient ConstString -> const char * -> StringRef conve...
authorRaphael Isemann <teemperor@gmail.com>
Tue, 11 Feb 2020 08:05:37 +0000 (09:05 +0100)
committerRaphael Isemann <teemperor@gmail.com>
Tue, 11 Feb 2020 08:14:41 +0000 (09:14 +0100)
StringRef will call strlen on the C string which is inefficient (as ConstString already
knows the string lenght and so does StringRef). This patch replaces all those calls
with GetStringRef() which doesn't recompute the length.

22 files changed:
lldb/include/lldb/DataFormatters/FormattersContainer.h
lldb/include/lldb/Expression/IRExecutionUnit.h
lldb/source/Breakpoint/BreakpointResolverName.cpp
lldb/source/Core/Debugger.cpp
lldb/source/Core/DynamicLoader.cpp
lldb/source/Core/FormatEntity.cpp
lldb/source/Core/Mangled.cpp
lldb/source/Core/ModuleList.cpp
lldb/source/Core/ValueObject.cpp
lldb/source/Core/ValueObjectRegister.cpp
lldb/source/Expression/IRExecutionUnit.cpp
lldb/source/Expression/REPL.cpp
lldb/source/Expression/UserExpression.cpp
lldb/source/Plugins/Disassembler/LLVMC/DisassemblerLLVMC.cpp
lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.cpp
lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp
lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.cpp
lldb/source/Target/UnixSignals.cpp
lldb/source/Utility/Broadcaster.cpp
lldb/source/Utility/ConstString.cpp
lldb/source/Utility/StructuredData.cpp
lldb/tools/lldb-server/lldb-platform.cpp

index 86023dd9bf0bec307a7d87883b7bfa5781957b21..88d4afe7d72cb3d8459ef3d0b4ef72eb7f34cf4c 100644 (file)
@@ -266,7 +266,7 @@ protected:
     ConstString key = m_format_map.GetKeyAtIndex(index);
     if (key)
       return lldb::TypeNameSpecifierImplSP(
-          new TypeNameSpecifierImpl(key.AsCString(), false));
+          new TypeNameSpecifierImpl(key.GetStringRef(), false));
     else
       return lldb::TypeNameSpecifierImplSP();
   }
index 05f2f8471ef83124eb40daab30449622db90ab97..9ce4adfda04ab8f145a646772b8eeb03d3395018 100644 (file)
@@ -71,7 +71,7 @@ public:
   llvm::Module *GetModule() { return m_module; }
 
   llvm::Function *GetFunction() {
-    return ((m_module != nullptr) ? m_module->getFunction(m_name.AsCString())
+    return ((m_module != nullptr) ? m_module->getFunction(m_name.GetStringRef())
                                   : nullptr);
   }
 
index 3e5a22ac1fc52f382a27fbe512b7cb5a392d6fd3..fdf70f1d74aa64da1de845312712a207b03eed09 100644 (file)
@@ -199,7 +199,7 @@ StructuredData::ObjectSP BreakpointResolverName::SerializeToStructuredData() {
     StructuredData::ArraySP name_masks_sp(new StructuredData::Array());
     for (auto lookup : m_lookups) {
       names_sp->AddItem(StructuredData::StringSP(
-          new StructuredData::String(lookup.GetName().AsCString())));
+          new StructuredData::String(lookup.GetName().GetStringRef())));
       name_masks_sp->AddItem(StructuredData::IntegerSP(
           new StructuredData::Integer(lookup.GetNameTypeMask())));
     }
index 6e883d150e12d74c490162f1fadeef8df8c12147..4f988d3a29413aaed2e2d4f1cac96bb5501c20b5 100644 (file)
@@ -1529,9 +1529,9 @@ bool Debugger::StartEventHandlerThread() {
     listener_sp->StartListeningForEvents(&m_sync_broadcaster,
                                          eBroadcastBitEventThreadIsListening);
 
-    auto thread_name =
+    llvm::StringRef thread_name =
         full_name.GetLength() < llvm::get_max_thread_name_length()
-            ? full_name.AsCString()
+            ? full_name.GetStringRef()
             : "dbg.evt-handler";
 
     // Use larger 8MB stack for this thread
index 449ea5c7ddca462c93520d215772061eb88047b0..ceccbe437e1d6b0433857ae7768cedba3799a6a2 100644 (file)
@@ -193,7 +193,7 @@ ModuleSP DynamicLoader::LoadModuleAtAddress(const FileSpec &file,
     if (error.Success() && memory_info.GetMapped() &&
         memory_info.GetRange().GetRangeBase() == base_addr && 
         !(memory_info.GetName().IsEmpty())) {
-      ModuleSpec new_module_spec(FileSpec(memory_info.GetName().AsCString()),
+      ModuleSpec new_module_spec(FileSpec(memory_info.GetName().GetStringRef()),
                                  target.GetArchitecture());
 
       if ((module_sp = modules.FindFirstModule(new_module_spec))) {
index 442d3d12a1a7f1df779bafae1b4e27a18f204a27..aaec84758ac6597747a144e95410ea1b1f0ad51c 100644 (file)
@@ -2348,10 +2348,10 @@ bool FormatEntity::FormatFileSpec(const FileSpec &file_spec, Stream &s,
     file_spec.Dump(s.AsRawOstream());
     return true;
   } else if (variable_name.equals(".basename")) {
-    s.PutCString(file_spec.GetFilename().AsCString(""));
+    s.PutCString(file_spec.GetFilename().GetStringRef());
     return true;
   } else if (variable_name.equals(".dirname")) {
-    s.PutCString(file_spec.GetFilename().AsCString(""));
+    s.PutCString(file_spec.GetFilename().GetStringRef());
     return true;
   }
   return false;
index b1b3c85066919e500b494339f98566d2254956c8..b44cbeb4fec91262ecdf67464934ce09be571d8e 100644 (file)
@@ -344,11 +344,11 @@ Mangled::GetDisplayDemangledName() const {
 }
 
 bool Mangled::NameMatches(const RegularExpression &regex) const {
-  if (m_mangled && regex.Execute(m_mangled.AsCString()))
+  if (m_mangled && regex.Execute(m_mangled.GetStringRef()))
     return true;
 
   ConstString demangled = GetDemangledName();
-  return demangled && regex.Execute(demangled.AsCString());
+  return demangled && regex.Execute(demangled.GetStringRef());
 }
 
 // Get the demangled name if there is one, else return the mangled name.
index 51d4d5a463a6003b80bf927fd751c0af597db23c..33f74dd713084e90dfe1dc07513852518662a7fc 100644 (file)
@@ -828,7 +828,7 @@ Status ModuleList::GetSharedModule(const ModuleSpec &module_spec,
       if (!FileSystem::Instance().IsDirectory(search_path_spec))
         continue;
       search_path_spec.AppendPathComponent(
-          module_spec.GetFileSpec().GetFilename().AsCString());
+          module_spec.GetFileSpec().GetFilename().GetStringRef());
       if (!FileSystem::Instance().Exists(search_path_spec))
         continue;
 
index 486d175f645aba225857bc3f98bee778e00dc9ff..9dfa909e4686000f25993f4f496209fedb6b5d10 100644 (file)
@@ -2093,7 +2093,7 @@ void ValueObject::GetExpressionPath(Stream &s,
   // name ([%d]) to the expression path
   if (m_is_array_item_for_pointer &&
       epformat == eGetExpressionPathFormatHonorPointers)
-    s.PutCString(m_name.AsCString());
+    s.PutCString(m_name.GetStringRef());
 
   if (!IsBaseClass()) {
     if (!is_deref_of_parent) {
index f05f77266faec5e1acbcb29675f15b90979d7aca..2523417f073fa8e87096c0ad7ca03f3d3f4e3ea9 100644 (file)
@@ -126,7 +126,7 @@ ValueObjectRegisterSet::GetChildMemberWithName(ConstString name,
   ValueObject *valobj = nullptr;
   if (m_reg_ctx_sp && m_reg_set) {
     const RegisterInfo *reg_info =
-        m_reg_ctx_sp->GetRegisterInfoByName(name.AsCString());
+        m_reg_ctx_sp->GetRegisterInfoByName(name.GetStringRef());
     if (reg_info != nullptr)
       valobj = new ValueObjectRegister(*this, m_reg_ctx_sp,
                                        reg_info->kinds[eRegisterKindLLDB]);
@@ -141,7 +141,7 @@ size_t
 ValueObjectRegisterSet::GetIndexOfChildWithName(ConstString name) {
   if (m_reg_ctx_sp && m_reg_set) {
     const RegisterInfo *reg_info =
-        m_reg_ctx_sp->GetRegisterInfoByName(name.AsCString());
+        m_reg_ctx_sp->GetRegisterInfoByName(name.GetStringRef());
     if (reg_info != nullptr)
       return reg_info->kinds[eRegisterKindLLDB];
   }
index a2ebf07a358ac2f61305d04f94528c242edfb851..dd38e3acdb9412f1bfaa1db94c71e2a5c96ca545 100644 (file)
@@ -404,7 +404,7 @@ void IRExecutionUnit::GetRunnableInfo(Status &error, lldb::addr_t &func_addr,
         ss.PutCString("\n");
       emitNewLine = true;
       ss.PutCString("  ");
-      ss.PutCString(Mangled(failed_lookup).GetDemangledName().AsCString());
+      ss.PutCString(Mangled(failed_lookup).GetDemangledName().GetStringRef());
     }
 
     m_failed_lookups.clear();
index 8a1eb5d4e5c979dd30e9d946394aac483b2b3191..6c9792c6e837a5a5ee21194726f6141ce8c80c4e 100644 (file)
@@ -53,11 +53,11 @@ std::string REPL::GetSourcePath() {
   ConstString file_basename = GetSourceFileBasename();
   FileSpec tmpdir_file_spec = HostInfo::GetProcessTempDir();
   if (tmpdir_file_spec) {
-    tmpdir_file_spec.GetFilename().SetCString(file_basename.AsCString());
+    tmpdir_file_spec.GetFilename() = file_basename;
     m_repl_source_path = tmpdir_file_spec.GetPath();
   } else {
     tmpdir_file_spec = FileSpec("/tmp");
-    tmpdir_file_spec.AppendPathComponent(file_basename.AsCString());
+    tmpdir_file_spec.AppendPathComponent(file_basename.GetStringRef());
   }
 
   return tmpdir_file_spec.GetPath();
index 0cbcbe8e510710d74808ddb917881761bc16d6ad..3bfb5f27e6c652b49deac7b60913562e753779b4 100644 (file)
@@ -117,7 +117,7 @@ lldb::addr_t UserExpression::GetObjectPointer(lldb::StackFrameSP frame_sp,
   lldb::ValueObjectSP valobj_sp;
 
   valobj_sp = frame_sp->GetValueForVariableExpressionPath(
-      object_name.AsCString(), lldb::eNoDynamicValues,
+      object_name.GetStringRef(), lldb::eNoDynamicValues,
       StackFrame::eExpressionPathOptionCheckPtrVsMember |
           StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
           StackFrame::eExpressionPathOptionsNoSyntheticChildren |
index edef7571a31497a5d84039b325879abc4a4266d6..139bda59a60c0232bbe953c9191f5ef698a9df7f 100644 (file)
@@ -710,7 +710,7 @@ public:
       s.PutCString(")");
       break;
     case Operand::Type::Register:
-      s.PutCString(op.m_register.AsCString());
+      s.PutCString(op.m_register.GetStringRef());
       break;
     case Operand::Type::Sum:
       s.PutCString("(");
index 6acc23176248e09950ebeedd67edd76a2bcc3f6e..a2f90d7fbbd6c286ee3e9df0df0122fd9452f239 100644 (file)
@@ -895,12 +895,12 @@ size_t AppleObjCRuntimeV2::GetByteOffsetForIvar(CompilerType &parent_ast_type,
                                                 const char *ivar_name) {
   uint32_t ivar_offset = LLDB_INVALID_IVAR_OFFSET;
 
-  const char *class_name = parent_ast_type.GetConstTypeName().AsCString();
-  if (class_name && class_name[0] && ivar_name && ivar_name[0]) {
+  ConstString class_name = parent_ast_type.GetConstTypeName();
+  if (!class_name.IsEmpty() && ivar_name && ivar_name[0]) {
     // Make the objective C V2 mangled name for the ivar offset from the class
     // name and ivar name
     std::string buffer("OBJC_IVAR_$_");
-    buffer.append(class_name);
+    buffer.append(class_name.AsCString());
     buffer.push_back('.');
     buffer.append(ivar_name);
     ConstString ivar_const_str(buffer.c_str());
index 83aca6a380d041e411a7d842f40f9cfb451aaf08..018b753bebc69b156a0affd4808e4413d226003e 100644 (file)
@@ -4003,7 +4003,7 @@ Status GDBRemoteCommunicationClient::ConfigureRemoteStructuredData(
   // Build command: Configure{type_name}: serialized config data.
   StreamGDBRemote stream;
   stream.PutCString("QConfigure");
-  stream.PutCString(type_name.AsCString());
+  stream.PutCString(type_name.GetStringRef());
   stream.PutChar(':');
   if (config_sp) {
     // Gather the plain-text version of the configuration data.
index 0bdad8b7b482a64f9f730d9f67d998bbed1c16a9..6f6fab85fe3afcaafcefa83ad97c5973398e8314 100644 (file)
@@ -2510,7 +2510,7 @@ GDBRemoteCommunicationServerLLGS::Handle_qMemoryRegionInfo(
     ConstString name = region_info.GetName();
     if (name) {
       response.PutCString("name:");
-      response.PutStringAsRawHex8(name.AsCString());
+      response.PutStringAsRawHex8(name.GetStringRef());
       response.PutChar(';');
     }
   }
index cb68df492f96beacc43d870682b3bc5472da6346..dce32adbf0a366d591d58540058a442b94409495 100644 (file)
@@ -145,10 +145,8 @@ bool UnixSignals::SignalIsValid(int32_t signo) const {
 }
 
 ConstString UnixSignals::GetShortName(ConstString name) const {
-  if (name) {
-    const char *signame = name.AsCString();
-    return ConstString(signame + 3); // Remove "SIG" from name
-  }
+  if (name)
+    return ConstString(name.GetStringRef().substr(3)); // Remove "SIG" from name
   return name;
 }
 
index de59a5c4362e34840c56f96fd52d8fa05bc85da7..90f91b4f89cfc85ef6541f0dac44e52b1f19a5bb 100644 (file)
@@ -30,7 +30,7 @@ Broadcaster::Broadcaster(BroadcasterManagerSP manager_sp, const char *name)
       m_manager_sp(std::move(manager_sp)), m_broadcaster_name(name) {
   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
   LLDB_LOG(log, "{0} Broadcaster::Broadcaster(\"{1}\")",
-           static_cast<void *>(this), GetBroadcasterName().AsCString());
+           static_cast<void *>(this), GetBroadcasterName());
 }
 
 Broadcaster::BroadcasterImpl::BroadcasterImpl(Broadcaster &broadcaster)
@@ -40,7 +40,7 @@ Broadcaster::BroadcasterImpl::BroadcasterImpl(Broadcaster &broadcaster)
 Broadcaster::~Broadcaster() {
   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
   LLDB_LOG(log, "{0} Broadcaster::~Broadcaster(\"{1}\")",
-           static_cast<void *>(this), GetBroadcasterName().AsCString());
+           static_cast<void *>(this), GetBroadcasterName());
 
   Clear();
 }
index 356d2119cfc516442cd59b261d156adb6d644373..8911a06218bcce0ce631d41eaf8807dee8deba3a 100644 (file)
@@ -335,5 +335,5 @@ size_t ConstString::StaticMemorySize() {
 void llvm::format_provider<ConstString>::format(const ConstString &CS,
                                                 llvm::raw_ostream &OS,
                                                 llvm::StringRef Options) {
-  format_provider<StringRef>::format(CS.AsCString(), OS, Options);
+  format_provider<StringRef>::format(CS.GetStringRef(), OS, Options);
 }
index c003412b5f4f868689c5bcff8641f3a986dd8d37..df1b35618e4a7ed2700e5bca5d1a3ea1f6cdbbd3 100644 (file)
@@ -156,7 +156,7 @@ void StructuredData::String::Serialize(json::OStream &s) const {
 void StructuredData::Dictionary::Serialize(json::OStream &s) const {
   s.objectBegin();
   for (const auto &pair : m_dict) {
-    s.attributeBegin(pair.first.AsCString());
+    s.attributeBegin(pair.first.GetStringRef());
     pair.second->Serialize(s);
     s.attributeEnd();
   }
index a6fb5639d6422eeecee361b74efda0b33716e9cd..d045660ce7af89cca1df035e197981528d0fcb34 100644 (file)
@@ -96,7 +96,7 @@ static void display_usage(const char *progname, const char *subcommand) {
 
 static Status save_socket_id_to_file(const std::string &socket_id,
                                      const FileSpec &file_spec) {
-  FileSpec temp_file_spec(file_spec.GetDirectory().AsCString());
+  FileSpec temp_file_spec(file_spec.GetDirectory().GetStringRef());
   Status error(llvm::sys::fs::create_directory(temp_file_spec.GetPath()));
   if (error.Fail())
     return Status("Failed to create directory %s: %s",