[Lldb-commits] [lldb] r372556 - [lldb] Reduce some dangerous boilerplate with CompletionRequest::ShiftArguments

Raphael Isemann via lldb-commits lldb-commits at lists.llvm.org
Mon Sep 23 01:16:19 PDT 2019


Author: teemperor
Date: Mon Sep 23 01:16:19 2019
New Revision: 372556

URL: http://llvm.org/viewvc/llvm-project?rev=372556&view=rev
Log:
[lldb] Reduce some dangerous boilerplate with CompletionRequest::ShiftArguments

We should in general not allow external code to fiddle with the internals of
CompletionRequest, but until this is gone let's at least provide a utility
function that makes this less dangerous.

This also now correct updates the partially parsed argument list,
but it doesn't seem to be used by anything that is behind one of
the current shift/SetCursorIndex calls, so this doesn't seeem to
fix any currently used completion.

Modified:
    lldb/trunk/include/lldb/Utility/CompletionRequest.h
    lldb/trunk/source/Commands/CommandObjectHelp.cpp
    lldb/trunk/source/Commands/CommandObjectMultiword.cpp
    lldb/trunk/source/Interpreter/CommandInterpreter.cpp
    lldb/trunk/unittests/Utility/CompletionRequestTest.cpp

Modified: lldb/trunk/include/lldb/Utility/CompletionRequest.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Utility/CompletionRequest.h?rev=372556&r1=372555&r2=372556&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Utility/CompletionRequest.h (original)
+++ lldb/trunk/include/lldb/Utility/CompletionRequest.h Mon Sep 23 01:16:19 2019
@@ -119,6 +119,13 @@ public:
     return GetParsedLine()[GetCursorIndex()];
   }
 
+  /// Drops the first argument from the argument list.
+  void ShiftArguments() {
+    m_cursor_index--;
+    m_parsed_line.Shift();
+    m_partial_parsed_line.Shift();
+  }
+
   void SetCursorIndex(int i) { m_cursor_index = i; }
   int GetCursorIndex() const { return m_cursor_index; }
 

Modified: lldb/trunk/source/Commands/CommandObjectHelp.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Commands/CommandObjectHelp.cpp?rev=372556&r1=372555&r2=372556&view=diff
==============================================================================
--- lldb/trunk/source/Commands/CommandObjectHelp.cpp (original)
+++ lldb/trunk/source/Commands/CommandObjectHelp.cpp Mon Sep 23 01:16:19 2019
@@ -215,8 +215,7 @@ void CommandObjectHelp::HandleCompletion
   // user is getting help on...
 
   if (cmd_obj) {
-    request.GetParsedLine().Shift();
-    request.SetCursorIndex(request.GetCursorIndex() - 1);
+    request.ShiftArguments();
     cmd_obj->HandleCompletion(request);
     return;
   }

Modified: lldb/trunk/source/Commands/CommandObjectMultiword.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Commands/CommandObjectMultiword.cpp?rev=372556&r1=372555&r2=372556&view=diff
==============================================================================
--- lldb/trunk/source/Commands/CommandObjectMultiword.cpp (original)
+++ lldb/trunk/source/Commands/CommandObjectMultiword.cpp Mon Sep 23 01:16:19 2019
@@ -214,8 +214,7 @@ void CommandObjectMultiword::HandleCompl
   // Remove the one match that we got from calling GetSubcommandObject.
   new_matches.DeleteStringAtIndex(0);
   request.AddCompletions(new_matches);
-  request.GetParsedLine().Shift();
-  request.SetCursorIndex(request.GetCursorIndex() - 1);
+  request.ShiftArguments();
   sub_command_object->HandleCompletion(request);
 }
 

Modified: lldb/trunk/source/Interpreter/CommandInterpreter.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/CommandInterpreter.cpp?rev=372556&r1=372555&r2=372556&view=diff
==============================================================================
--- lldb/trunk/source/Interpreter/CommandInterpreter.cpp (original)
+++ lldb/trunk/source/Interpreter/CommandInterpreter.cpp Mon Sep 23 01:16:19 2019
@@ -1794,8 +1794,7 @@ void CommandInterpreter::HandleCompletio
     CommandObject *command_object =
         GetCommandObject(request.GetParsedLine().GetArgumentAtIndex(0));
     if (command_object) {
-      request.GetParsedLine().Shift();
-      request.SetCursorIndex(request.GetCursorIndex() - 1);
+      request.ShiftArguments();
       command_object->HandleCompletion(request);
     }
   }

Modified: lldb/trunk/unittests/Utility/CompletionRequestTest.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/Utility/CompletionRequestTest.cpp?rev=372556&r1=372555&r2=372556&view=diff
==============================================================================
--- lldb/trunk/unittests/Utility/CompletionRequestTest.cpp (original)
+++ lldb/trunk/unittests/Utility/CompletionRequestTest.cpp Mon Sep 23 01:16:19 2019
@@ -31,6 +31,41 @@ TEST(CompletionRequest, Constructor) {
   EXPECT_STREQ(request.GetPartialParsedLine().GetArgumentAtIndex(1), "b");
 }
 
+TEST(CompletionRequest, ShiftArguments) {
+  std::string command = "a bad c";
+  const unsigned cursor_pos = 3;
+  const int arg_index = 1;
+  const int arg_cursor_pos = 1;
+  StringList matches;
+  CompletionResult result;
+
+  CompletionRequest request(command, cursor_pos, result);
+  result.GetMatches(matches);
+
+  EXPECT_STREQ(request.GetRawLine().str().c_str(), command.c_str());
+  EXPECT_EQ(request.GetRawCursorPos(), cursor_pos);
+  EXPECT_EQ(request.GetCursorIndex(), arg_index);
+  EXPECT_EQ(request.GetCursorCharPosition(), arg_cursor_pos);
+
+  EXPECT_EQ(request.GetPartialParsedLine().GetArgumentCount(), 2u);
+  EXPECT_STREQ(request.GetPartialParsedLine().GetArgumentAtIndex(1), "b");
+
+  // Shift away the 'a' argument.
+  request.ShiftArguments();
+
+  // The raw line/cursor stays identical.
+  EXPECT_STREQ(request.GetRawLine().str().c_str(), command.c_str());
+  EXPECT_EQ(request.GetRawCursorPos(), cursor_pos);
+
+  // Relative cursor position in arg is identical.
+  EXPECT_EQ(request.GetCursorCharPosition(), arg_cursor_pos);
+
+  // Partially parsed line and cursor should be updated.
+  EXPECT_EQ(request.GetCursorIndex(), arg_index - 1U);
+  EXPECT_EQ(request.GetPartialParsedLine().GetArgumentCount(), 1u);
+  EXPECT_STREQ(request.GetPartialParsedLine().GetArgumentAtIndex(0), "b");
+}
+
 TEST(CompletionRequest, DuplicateFiltering) {
   std::string command = "a bad c";
   const unsigned cursor_pos = 3;




More information about the lldb-commits mailing list