diff options
author | Raphael Isemann <teemperor@gmail.com> | 2019-08-16 14:27:35 +0000 |
---|---|---|
committer | Raphael Isemann <teemperor@gmail.com> | 2019-08-16 14:27:35 +0000 |
commit | 4c78b7882506957febf4318978d31b5aaae7cced (patch) | |
tree | 57b4c6df88edcf2ba5ae517d0a00a993630c3067 | |
parent | 213edc34922afa08f8cf070017fcd398b12f15a5 (diff) | |
download | bcm5719-llvm-4c78b7882506957febf4318978d31b5aaae7cced.tar.gz bcm5719-llvm-4c78b7882506957febf4318978d31b5aaae7cced.zip |
[lldb][NFC] Allow for-ranges on StringList
llvm-svn: 369113
-rw-r--r-- | lldb/include/lldb/Utility/CompletionRequest.h | 4 | ||||
-rw-r--r-- | lldb/include/lldb/Utility/StringList.h | 12 | ||||
-rw-r--r-- | lldb/source/Breakpoint/WatchpointOptions.cpp | 5 | ||||
-rw-r--r-- | lldb/source/Commands/CommandObjectApropos.cpp | 6 | ||||
-rw-r--r-- | lldb/source/Commands/CommandObjectCommands.cpp | 6 | ||||
-rw-r--r-- | lldb/source/Commands/CommandObjectMultiword.cpp | 4 | ||||
-rw-r--r-- | lldb/source/Commands/CommandObjectType.cpp | 8 | ||||
-rw-r--r-- | lldb/source/Utility/Args.cpp | 4 | ||||
-rw-r--r-- | lldb/source/Utility/StringList.cpp | 5 | ||||
-rw-r--r-- | lldb/unittests/Editline/EditlineTest.cpp | 8 | ||||
-rw-r--r-- | lldb/unittests/Utility/StringListTest.cpp | 18 |
11 files changed, 48 insertions, 32 deletions
diff --git a/lldb/include/lldb/Utility/CompletionRequest.h b/lldb/include/lldb/Utility/CompletionRequest.h index 050976cd4bc..d2f08ea1c72 100644 --- a/lldb/include/lldb/Utility/CompletionRequest.h +++ b/lldb/include/lldb/Utility/CompletionRequest.h @@ -116,8 +116,8 @@ public: /// /// \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. diff --git a/lldb/include/lldb/Utility/StringList.h b/lldb/include/lldb/Utility/StringList.h index 22b5b2c3b09..1c70d398550 100644 --- a/lldb/include/lldb/Utility/StringList.h +++ b/lldb/include/lldb/Utility/StringList.h @@ -23,6 +23,8 @@ class Stream; namespace lldb_private { class StringList { + typedef std::vector<std::string> StorageType; + public: StringList(); @@ -52,6 +54,14 @@ public: 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]; @@ -125,7 +135,7 @@ public: } private: - std::vector<std::string> m_strings; + StorageType m_strings; }; } // namespace lldb_private diff --git a/lldb/source/Breakpoint/WatchpointOptions.cpp b/lldb/source/Breakpoint/WatchpointOptions.cpp index 7dd130a3072..cd5ef930e5d 100644 --- a/lldb/source/Breakpoint/WatchpointOptions.cpp +++ b/lldb/source/Breakpoint/WatchpointOptions.cpp @@ -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 { diff --git a/lldb/source/Commands/CommandObjectApropos.cpp b/lldb/source/Commands/CommandObjectApropos.cpp index 957de475569..d336a1652a9 100644 --- a/lldb/source/Commands/CommandObjectApropos.cpp +++ b/lldb/source/Commands/CommandObjectApropos.cpp @@ -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) diff --git a/lldb/source/Commands/CommandObjectCommands.cpp b/lldb/source/Commands/CommandObjectCommands.cpp index e7f99e2316c..65fc8869edf 100644 --- a/lldb/source/Commands/CommandObjectCommands.cpp +++ b/lldb/source/Commands/CommandObjectCommands.cpp @@ -966,11 +966,9 @@ protected: 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(); diff --git a/lldb/source/Commands/CommandObjectMultiword.cpp b/lldb/source/Commands/CommandObjectMultiword.cpp index 4011cceb8a2..eeda1fed25c 100644 --- a/lldb/source/Commands/CommandObjectMultiword.cpp +++ b/lldb/source/Commands/CommandObjectMultiword.cpp @@ -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"); diff --git a/lldb/source/Commands/CommandObjectType.cpp b/lldb/source/Commands/CommandObjectType.cpp index 496f856a253..f5d82a26249 100644 --- a/lldb/source/Commands/CommandObjectType.cpp +++ b/lldb/source/Commands/CommandObjectType.cpp @@ -195,9 +195,7 @@ public: 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 @@ protected: 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( diff --git a/lldb/source/Utility/Args.cpp b/lldb/source/Utility/Args.cpp index 77b0d43254a..5efad66a969 100644 --- a/lldb/source/Utility/Args.cpp +++ b/lldb/source/Utility/Args.cpp @@ -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) { diff --git a/lldb/source/Utility/StringList.cpp b/lldb/source/Utility/StringList.cpp index 03249e00ccc..c242ccafa38 100644 --- a/lldb/source/Utility/StringList.cpp +++ b/lldb/source/Utility/StringList.cpp @@ -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(); } diff --git a/lldb/unittests/Editline/EditlineTest.cpp b/lldb/unittests/Editline/EditlineTest.cpp index eeaa0a3027e..55845aef05c 100644 --- a/lldb/unittests/Editline/EditlineTest.cpp +++ b/lldb/unittests/Editline/EditlineTest.cpp @@ -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; @@ -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)); } diff --git a/lldb/unittests/Utility/StringListTest.cpp b/lldb/unittests/Utility/StringListTest.cpp index a0890800861..264ec92d42e 100644 --- a/lldb/unittests/Utility/StringListTest.cpp +++ b/lldb/unittests/Utility/StringListTest.cpp @@ -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; @@ -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")); +} |