From ebfb9d81ccc9d54a03e33b73143753a6d0f008bb Mon Sep 17 00:00:00 2001 From: Shivam Gupta Date: Sat, 8 Jun 2024 10:43:01 +0530 Subject: [PATCH] [lldb] Use const reference for range variables to improve performance (NFC) Cppcheck recommends using a const reference for range variables in a for-each loop. This avoids unnecessary copying of elements, improving performance. Caught by cppcheck - lldb/source/API/SBBreakpoint.cpp:717:22: performance: Range variable 'name' should be declared as const reference. [iterateByValue] lldb/source/API/SBTarget.cpp:1150:15: performance: Range variable 'name' should be declared as const reference. [iterateByValue] lldb/source/Breakpoint/Breakpoint.cpp:888:26: performance: Range variable 'name' should be declared as const reference. [iterateByValue] lldb/source/Breakpoint/BreakpointIDList.cpp:262:26: performance: Range variable 'name' should be declared as const reference. [iterateByValue] Fix #91213 Fix #91217 Fix #91219 Fix #91220 --- lldb/source/API/SBBreakpoint.cpp | 2 +- lldb/source/API/SBTarget.cpp | 2 +- lldb/source/Breakpoint/Breakpoint.cpp | 2 +- lldb/source/Breakpoint/BreakpointIDList.cpp | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lldb/source/API/SBBreakpoint.cpp b/lldb/source/API/SBBreakpoint.cpp index f1fb6f904f5f0c..3d908047f9455b 100644 --- a/lldb/source/API/SBBreakpoint.cpp +++ b/lldb/source/API/SBBreakpoint.cpp @@ -714,7 +714,7 @@ void SBBreakpoint::GetNames(SBStringList &names) { bkpt_sp->GetTarget().GetAPIMutex()); std::vector names_vec; bkpt_sp->GetNames(names_vec); - for (std::string name : names_vec) { + for (const std::string &name : names_vec) { names.AppendString(name.c_str()); } } diff --git a/lldb/source/API/SBTarget.cpp b/lldb/source/API/SBTarget.cpp index 962ce9ba83cc77..adb9e645610ba4 100644 --- a/lldb/source/API/SBTarget.cpp +++ b/lldb/source/API/SBTarget.cpp @@ -1147,7 +1147,7 @@ void SBTarget::GetBreakpointNames(SBStringList &names) { std::vector name_vec; target_sp->GetBreakpointNames(name_vec); - for (auto name : name_vec) + for (const auto &name : name_vec) names.AppendString(name.c_str()); } } diff --git a/lldb/source/Breakpoint/Breakpoint.cpp b/lldb/source/Breakpoint/Breakpoint.cpp index ae845e92762b97..dc80d435ad4449 100644 --- a/lldb/source/Breakpoint/Breakpoint.cpp +++ b/lldb/source/Breakpoint/Breakpoint.cpp @@ -885,7 +885,7 @@ void Breakpoint::GetDescription(Stream *s, lldb::DescriptionLevel level, s->Printf("Names:"); s->EOL(); s->IndentMore(); - for (std::string name : m_name_list) { + for (const std::string &name : m_name_list) { s->Indent(); s->Printf("%s\n", name.c_str()); } diff --git a/lldb/source/Breakpoint/BreakpointIDList.cpp b/lldb/source/Breakpoint/BreakpointIDList.cpp index 97af1d40eb7a58..5fc9f95a75db12 100644 --- a/lldb/source/Breakpoint/BreakpointIDList.cpp +++ b/lldb/source/Breakpoint/BreakpointIDList.cpp @@ -259,7 +259,7 @@ llvm::Error BreakpointIDList::FindAndReplaceIDRanges( if (!names_found.empty()) { for (BreakpointSP bkpt_sp : target->GetBreakpointList().Breakpoints()) { - for (std::string name : names_found) { + for (const std::string &name : names_found) { if (bkpt_sp->MatchesName(name.c_str())) { StreamString canonical_id_str; BreakpointID::GetCanonicalReference(