Skip to content

Commit

Permalink
[lldb][NFC] Allow for-ranges on StringList
Browse files Browse the repository at this point in the history
llvm-svn: 369113
  • Loading branch information
Teemperor committed Aug 16, 2019
1 parent 213edc3 commit 4c78b78
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 32 deletions.
4 changes: 2 additions & 2 deletions lldb/include/lldb/Utility/CompletionRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ class CompletionRequest {
///
/// \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.
Expand Down
12 changes: 11 additions & 1 deletion lldb/include/lldb/Utility/StringList.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class Stream;
namespace lldb_private {

class StringList {
typedef std::vector<std::string> StorageType;

public:
StringList();

Expand Down Expand Up @@ -52,6 +54,14 @@ class StringList {

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];
Expand Down Expand Up @@ -125,7 +135,7 @@ class StringList {
}

private:
std::vector<std::string> m_strings;
StorageType m_strings;
};

} // namespace lldb_private
Expand Down
5 changes: 2 additions & 3 deletions lldb/source/Breakpoint/WatchpointOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,8 @@ void WatchpointOptions::CommandBaton::GetDescription(

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 {
Expand Down
6 changes: 2 additions & 4 deletions lldb/source/Commands/CommandObjectApropos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,8 @@ bool CommandObjectApropos::DoExecute(Args &args, CommandReturnObject &result) {
"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)
Expand Down
6 changes: 2 additions & 4 deletions lldb/source/Commands/CommandObjectCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -966,11 +966,9 @@ a number follows 'f':"
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();
Expand Down
4 changes: 2 additions & 2 deletions lldb/source/Commands/CommandObjectMultiword.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ bool CommandObjectMultiword::Execute(const char *args_string,

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");
Expand Down
8 changes: 2 additions & 6 deletions lldb/source/Commands/CommandObjectType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,7 @@ class CommandObjectTypeSummaryAdd : public CommandObjectParsed,

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
Expand Down Expand Up @@ -437,9 +435,7 @@ class CommandObjectTypeSynthAdd : public CommandObjectParsed,

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(
Expand Down
4 changes: 2 additions & 2 deletions lldb/source/Utility/Args.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ Args::Args(llvm::StringRef command) { SetCommandString(command); }
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) {
Expand Down
5 changes: 1 addition & 4 deletions lldb/source/Utility/StringList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,7 @@ void StringList::AppendList(const char **strv, int strc) {
}

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(); }
Expand Down
8 changes: 4 additions & 4 deletions lldb/unittests/Editline/EditlineTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ bool EditlineAdapter::IsInputComplete(lldb_private::Editline *editline,
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;
Expand Down Expand Up @@ -312,8 +312,8 @@ TEST_F(EditlineTestFixture, EditlineReceivesMultiLineText) {
// 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));
}
Expand Down
18 changes: 18 additions & 0 deletions lldb/unittests/Utility/StringListTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "lldb/Utility/StringList.h"
#include "lldb/Utility/StreamString.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

using namespace lldb_private;
Expand Down Expand Up @@ -504,3 +505,20 @@ TEST(StringListTest, GetMaxStringLengthEmpty) {
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" << e;
}

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_THAT(recorded, testing::ElementsAre("a", "b", "c"));
}

0 comments on commit 4c78b78

Please sign in to comment.