teemperor created this revision.
teemperor added a reviewer: LLDB.
Herald added subscribers: lldb-commits, abidh.
Herald added a project: LLDB.
Added support for for-range iterating over StringList. There is no
reverse-iterator support because it seems
no one is doing that in LLDB. Also added some tests and migrated LLDB code over
to for-range loops
where possible.
Repository:
rLLDB LLDB
https://reviews.llvm.org/D66345
Files:
lldb/include/lldb/Utility/CompletionRequest.h
lldb/include/lldb/Utility/StringList.h
lldb/source/Breakpoint/WatchpointOptions.cpp
lldb/source/Commands/CommandObjectApropos.cpp
lldb/source/Commands/CommandObjectCommands.cpp
lldb/source/Commands/CommandObjectMultiword.cpp
lldb/source/Commands/CommandObjectType.cpp
lldb/source/Utility/Args.cpp
lldb/source/Utility/StringList.cpp
lldb/unittests/Editline/EditlineTest.cpp
lldb/unittests/Utility/StringListTest.cpp
Index: lldb/unittests/Utility/StringListTest.cpp
===================================================================
--- lldb/unittests/Utility/StringListTest.cpp
+++ lldb/unittests/Utility/StringListTest.cpp
@@ -504,3 +504,23 @@
StringList s;
EXPECT_EQ(0U, s.GetMaxStringLength());
}
+
+TEST(StringListTest, ForRangeEmpty) {
+ StringList s;
+ for (const std::string &e : s)
+ FAIL() << "Shouldn't have hit an element in for range";
+}
+
+TEST(StringListTest, ForRangeSingle) {
+ StringList s;
+ s.AppendString("a");
+ s.AppendString("b");
+ s.AppendString("c");
+ std::vector<std::string> recorded;
+ for (const std::string &e : s)
+ recorded.push_back(e);
+ EXPECT_EQ(3, recorded.size());
+ EXPECT_EQ("a", recorded.at(0));
+ EXPECT_EQ("b", recorded.at(1));
+ EXPECT_EQ("c", recorded.at(2));
+}
Index: lldb/unittests/Editline/EditlineTest.cpp
===================================================================
--- lldb/unittests/Editline/EditlineTest.cpp
+++ lldb/unittests/Editline/EditlineTest.cpp
@@ -196,8 +196,8 @@
int start_block_count = 0;
int brace_balance = 0;
- for (size_t i = 0; i < lines.GetSize(); ++i) {
- for (auto ch : lines[i]) {
+ for (const std::string &line : lines) {
+ for (auto ch : line) {
if (ch == '{') {
++start_block_count;
++brace_balance;
@@ -312,8 +312,8 @@
// Without any auto indentation support, our output should directly match our
// input.
std::vector<std::string> reported_lines;
- for (size_t i = 0; i < el_reported_lines.GetSize(); ++i)
- reported_lines.push_back(el_reported_lines[i]);
+ for (const std::string &line : el_reported_lines)
+ reported_lines.push_back(line);
EXPECT_THAT(reported_lines, testing::ContainerEq(input_lines));
}
Index: lldb/source/Utility/StringList.cpp
===================================================================
--- lldb/source/Utility/StringList.cpp
+++ lldb/source/Utility/StringList.cpp
@@ -61,10 +61,7 @@
}
void StringList::AppendList(StringList strings) {
- size_t len = strings.GetSize();
-
- for (size_t i = 0; i < len; ++i)
- m_strings.push_back(strings.GetStringAtIndex(i));
+ m_strings.insert(m_strings.end(), strings.begin(), strings.end());
}
size_t StringList::GetSize() const { return m_strings.size(); }
Index: lldb/source/Utility/Args.cpp
===================================================================
--- lldb/source/Utility/Args.cpp
+++ lldb/source/Utility/Args.cpp
@@ -172,8 +172,8 @@
Args::Args(const Args &rhs) { *this = rhs; }
Args::Args(const StringList &list) : Args() {
- for (size_t i = 0; i < list.GetSize(); ++i)
- AppendArgument(list[i]);
+ for (const std::string &arg : list)
+ AppendArgument(arg);
}
Args &Args::operator=(const Args &rhs) {
Index: lldb/source/Commands/CommandObjectType.cpp
===================================================================
--- lldb/source/Commands/CommandObjectType.cpp
+++ lldb/source/Commands/CommandObjectType.cpp
@@ -195,9 +195,7 @@
Status error;
- for (size_t i = 0; i < options->m_target_types.GetSize(); i++) {
- const char *type_name =
- options->m_target_types.GetStringAtIndex(i);
+ for (const std::string &type_name : options->m_target_types) {
CommandObjectTypeSummaryAdd::AddSummary(
ConstString(type_name), script_format,
(options->m_regex
@@ -437,9 +435,7 @@
Status error;
- for (size_t i = 0; i < options->m_target_types.GetSize(); i++) {
- const char *type_name =
- options->m_target_types.GetStringAtIndex(i);
+ for (const std::string &type_name : options->m_target_types) {
ConstString const_type_name(type_name);
if (const_type_name) {
if (!CommandObjectTypeSynthAdd::AddSynth(
Index: lldb/source/Commands/CommandObjectMultiword.cpp
===================================================================
--- lldb/source/Commands/CommandObjectMultiword.cpp
+++ lldb/source/Commands/CommandObjectMultiword.cpp
@@ -136,9 +136,9 @@
if (num_subcmd_matches > 0) {
error_msg.append(" Possible completions:");
- for (size_t i = 0; i < matches.GetSize(); i++) {
+ for (const std::string &match : matches) {
error_msg.append("\n\t");
- error_msg.append(matches.GetStringAtIndex(i));
+ error_msg.append(match);
}
}
error_msg.append("\n");
Index: lldb/source/Commands/CommandObjectCommands.cpp
===================================================================
--- lldb/source/Commands/CommandObjectCommands.cpp
+++ lldb/source/Commands/CommandObjectCommands.cpp
@@ -966,11 +966,9 @@
if (m_regex_cmd_up) {
StringList lines;
if (lines.SplitIntoLines(data)) {
- const size_t num_lines = lines.GetSize();
bool check_only = false;
- for (size_t i = 0; i < num_lines; ++i) {
- llvm::StringRef bytes_strref(lines[i]);
- Status error = AppendRegexSubstitution(bytes_strref, check_only);
+ for (const std::string &line : lines) {
+ Status error = AppendRegexSubstitution(line, check_only);
if (error.Fail()) {
if (!GetDebugger().GetCommandInterpreter().GetBatchCommandMode()) {
StreamSP out_stream = GetDebugger().GetAsyncOutputStream();
Index: lldb/source/Commands/CommandObjectApropos.cpp
===================================================================
--- lldb/source/Commands/CommandObjectApropos.cpp
+++ lldb/source/Commands/CommandObjectApropos.cpp
@@ -65,10 +65,8 @@
"The following commands may relate to '%s':\n", args[0].c_str());
size_t max_len = 0;
- for (size_t i = 0; i < commands_found.GetSize(); ++i) {
- size_t len = strlen(commands_found.GetStringAtIndex(i));
- if (len > max_len)
- max_len = len;
+ for (const std::string &command : commands_found) {
+ max_len = std::max(max_len, command.size());
}
for (size_t i = 0; i < commands_found.GetSize(); ++i)
Index: lldb/source/Breakpoint/WatchpointOptions.cpp
===================================================================
--- lldb/source/Breakpoint/WatchpointOptions.cpp
+++ lldb/source/Breakpoint/WatchpointOptions.cpp
@@ -170,9 +170,8 @@
s->IndentMore();
if (data && data->user_source.GetSize() > 0) {
- const size_t num_strings = data->user_source.GetSize();
- for (size_t i = 0; i < num_strings; ++i) {
- s->Indent(data->user_source.GetStringAtIndex(i));
+ for (const std::string &line : data->user_source) {
+ s->Indent(line);
s->EOL();
}
} else {
Index: lldb/include/lldb/Utility/StringList.h
===================================================================
--- lldb/include/lldb/Utility/StringList.h
+++ lldb/include/lldb/Utility/StringList.h
@@ -23,6 +23,8 @@
namespace lldb_private {
class StringList {
+ typedef std::vector<std::string> StorageType;
+
public:
StringList();
@@ -52,6 +54,14 @@
size_t GetMaxStringLength() const;
+ typedef StorageType::iterator iterator;
+ typedef StorageType::const_iterator const_iterator;
+
+ iterator begin() { return m_strings.begin(); }
+ iterator end() { return m_strings.end(); }
+ const_iterator begin() const { return m_strings.begin(); }
+ const_iterator end() const { return m_strings.end(); }
+
std::string &operator[](size_t idx) {
// No bounds checking, verify "idx" is good prior to calling this function
return m_strings[idx];
Index: lldb/include/lldb/Utility/CompletionRequest.h
===================================================================
--- lldb/include/lldb/Utility/CompletionRequest.h
+++ lldb/include/lldb/Utility/CompletionRequest.h
@@ -116,8 +116,8 @@
///
/// \see AddCompletion
void AddCompletions(const StringList &completions) {
- for (std::size_t i = 0; i < completions.GetSize(); ++i)
- AddCompletion(completions.GetStringAtIndex(i));
+ for (const std::string &completion : completions)
+ AddCompletion(completion);
}
/// Adds multiple possible completion strings alongside their descriptions.
_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits