[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 86023dd..88d4afe 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 05f2f84..9ce4adf 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 3e5a22a..fdf70f1 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 6e883d1..4f988d3 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 449ea5c..ceccbe4 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 442d3d1..aaec847 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 b1b3c85..b44cbeb 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 51d4d5a..33f74dd 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 486d175..9dfa909 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 f05f772..2523417 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 a2ebf07..dd38e3a 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 8a1eb5d..6c9792c 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 0cbcbe8..3bfb5f2 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 edef757..139bda5 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 6acc231..a2f90d7 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 83aca6a..018b753 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 0bdad8b..6f6fab8 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 cb68df4..dce32ad 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 de59a5c..90f91b4 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 356d211..8911a06 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 c003412..df1b356 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 a6fb563..d045660 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",