[Lldb-commits] [lldb] r369113 - [lldb][NFC] Allow for-ranges on StringList

Raphael Isemann via lldb-commits lldb-commits at lists.llvm.org
Fri Aug 16 07:27:35 PDT 2019


Author: teemperor
Date: Fri Aug 16 07:27:35 2019
New Revision: 369113

URL: http://llvm.org/viewvc/llvm-project?rev=369113&view=rev
Log:
[lldb][NFC] Allow for-ranges on StringList

Modified:
    lldb/trunk/include/lldb/Utility/CompletionRequest.h
    lldb/trunk/include/lldb/Utility/StringList.h
    lldb/trunk/source/Breakpoint/WatchpointOptions.cpp
    lldb/trunk/source/Commands/CommandObjectApropos.cpp
    lldb/trunk/source/Commands/CommandObjectCommands.cpp
    lldb/trunk/source/Commands/CommandObjectMultiword.cpp
    lldb/trunk/source/Commands/CommandObjectType.cpp
    lldb/trunk/source/Utility/Args.cpp
    lldb/trunk/source/Utility/StringList.cpp
    lldb/trunk/unittests/Editline/EditlineTest.cpp
    lldb/trunk/unittests/Utility/StringListTest.cpp

Modified: lldb/trunk/include/lldb/Utility/CompletionRequest.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Utility/CompletionRequest.h?rev=369113&r1=369112&r2=369113&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Utility/CompletionRequest.h (original)
+++ lldb/trunk/include/lldb/Utility/CompletionRequest.h Fri Aug 16 07:27:35 2019
@@ -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.

Modified: lldb/trunk/include/lldb/Utility/StringList.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Utility/StringList.h?rev=369113&r1=369112&r2=369113&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Utility/StringList.h (original)
+++ lldb/trunk/include/lldb/Utility/StringList.h Fri Aug 16 07:27:35 2019
@@ -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

Modified: lldb/trunk/source/Breakpoint/WatchpointOptions.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Breakpoint/WatchpointOptions.cpp?rev=369113&r1=369112&r2=369113&view=diff
==============================================================================
--- lldb/trunk/source/Breakpoint/WatchpointOptions.cpp (original)
+++ lldb/trunk/source/Breakpoint/WatchpointOptions.cpp Fri Aug 16 07:27:35 2019
@@ -170,9 +170,8 @@ void WatchpointOptions::CommandBaton::Ge
 
   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 {

Modified: lldb/trunk/source/Commands/CommandObjectApropos.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Commands/CommandObjectApropos.cpp?rev=369113&r1=369112&r2=369113&view=diff
==============================================================================
--- lldb/trunk/source/Commands/CommandObjectApropos.cpp (original)
+++ lldb/trunk/source/Commands/CommandObjectApropos.cpp Fri Aug 16 07:27:35 2019
@@ -65,10 +65,8 @@ bool CommandObjectApropos::DoExecute(Arg
               "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)

Modified: lldb/trunk/source/Commands/CommandObjectCommands.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Commands/CommandObjectCommands.cpp?rev=369113&r1=369112&r2=369113&view=diff
==============================================================================
--- lldb/trunk/source/Commands/CommandObjectCommands.cpp (original)
+++ lldb/trunk/source/Commands/CommandObjectCommands.cpp Fri Aug 16 07:27:35 2019
@@ -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();

Modified: lldb/trunk/source/Commands/CommandObjectMultiword.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Commands/CommandObjectMultiword.cpp?rev=369113&r1=369112&r2=369113&view=diff
==============================================================================
--- lldb/trunk/source/Commands/CommandObjectMultiword.cpp (original)
+++ lldb/trunk/source/Commands/CommandObjectMultiword.cpp Fri Aug 16 07:27:35 2019
@@ -136,9 +136,9 @@ bool CommandObjectMultiword::Execute(con
 
   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");

Modified: lldb/trunk/source/Commands/CommandObjectType.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Commands/CommandObjectType.cpp?rev=369113&r1=369112&r2=369113&view=diff
==============================================================================
--- lldb/trunk/source/Commands/CommandObjectType.cpp (original)
+++ lldb/trunk/source/Commands/CommandObjectType.cpp Fri Aug 16 07:27:35 2019
@@ -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(

Modified: lldb/trunk/source/Utility/Args.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Utility/Args.cpp?rev=369113&r1=369112&r2=369113&view=diff
==============================================================================
--- lldb/trunk/source/Utility/Args.cpp (original)
+++ lldb/trunk/source/Utility/Args.cpp Fri Aug 16 07:27:35 2019
@@ -172,8 +172,8 @@ Args::Args(llvm::StringRef command) { Se
 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) {

Modified: lldb/trunk/source/Utility/StringList.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Utility/StringList.cpp?rev=369113&r1=369112&r2=369113&view=diff
==============================================================================
--- lldb/trunk/source/Utility/StringList.cpp (original)
+++ lldb/trunk/source/Utility/StringList.cpp Fri Aug 16 07:27:35 2019
@@ -61,10 +61,7 @@ void StringList::AppendList(const char *
 }
 
 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(); }

Modified: lldb/trunk/unittests/Editline/EditlineTest.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/Editline/EditlineTest.cpp?rev=369113&r1=369112&r2=369113&view=diff
==============================================================================
--- lldb/trunk/unittests/Editline/EditlineTest.cpp (original)
+++ lldb/trunk/unittests/Editline/EditlineTest.cpp Fri Aug 16 07:27:35 2019
@@ -196,8 +196,8 @@ bool EditlineAdapter::IsInputComplete(ll
   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, EditlineRece
   // 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));
 }

Modified: lldb/trunk/unittests/Utility/StringListTest.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/Utility/StringListTest.cpp?rev=369113&r1=369112&r2=369113&view=diff
==============================================================================
--- lldb/trunk/unittests/Utility/StringListTest.cpp (original)
+++ lldb/trunk/unittests/Utility/StringListTest.cpp Fri Aug 16 07:27:35 2019
@@ -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, GetMaxStringLengthE
   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"));
+}




More information about the lldb-commits mailing list