summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaphael Isemann <teemperor@gmail.com>2019-09-23 08:16:19 +0000
committerRaphael Isemann <teemperor@gmail.com>2019-09-23 08:16:19 +0000
commitf8e733f14994cc739f11978d8d901426a3771cbc (patch)
tree692e7418ec97cfcb7e2fd7d1878c6602e5ab89f8
parentc063b0b0d3346d2a50e2ef28ec0147d6d53f399d (diff)
downloadbcm5719-llvm-f8e733f14994cc739f11978d8d901426a3771cbc.tar.gz
bcm5719-llvm-f8e733f14994cc739f11978d8d901426a3771cbc.zip
[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. llvm-svn: 372556
-rw-r--r--lldb/include/lldb/Utility/CompletionRequest.h7
-rw-r--r--lldb/source/Commands/CommandObjectHelp.cpp3
-rw-r--r--lldb/source/Commands/CommandObjectMultiword.cpp3
-rw-r--r--lldb/source/Interpreter/CommandInterpreter.cpp3
-rw-r--r--lldb/unittests/Utility/CompletionRequestTest.cpp35
5 files changed, 45 insertions, 6 deletions
diff --git a/lldb/include/lldb/Utility/CompletionRequest.h b/lldb/include/lldb/Utility/CompletionRequest.h
index 28ac78a8756..e10c1f706f5 100644
--- a/lldb/include/lldb/Utility/CompletionRequest.h
+++ b/lldb/include/lldb/Utility/CompletionRequest.h
@@ -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; }
diff --git a/lldb/source/Commands/CommandObjectHelp.cpp b/lldb/source/Commands/CommandObjectHelp.cpp
index 34f6e5d544d..c02a583bf9d 100644
--- a/lldb/source/Commands/CommandObjectHelp.cpp
+++ b/lldb/source/Commands/CommandObjectHelp.cpp
@@ -215,8 +215,7 @@ void CommandObjectHelp::HandleCompletion(CompletionRequest &request) {
// user is getting help on...
if (cmd_obj) {
- request.GetParsedLine().Shift();
- request.SetCursorIndex(request.GetCursorIndex() - 1);
+ request.ShiftArguments();
cmd_obj->HandleCompletion(request);
return;
}
diff --git a/lldb/source/Commands/CommandObjectMultiword.cpp b/lldb/source/Commands/CommandObjectMultiword.cpp
index e33a5e04969..e06c7335f89 100644
--- a/lldb/source/Commands/CommandObjectMultiword.cpp
+++ b/lldb/source/Commands/CommandObjectMultiword.cpp
@@ -214,8 +214,7 @@ void CommandObjectMultiword::HandleCompletion(CompletionRequest &request) {
// 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);
}
diff --git a/lldb/source/Interpreter/CommandInterpreter.cpp b/lldb/source/Interpreter/CommandInterpreter.cpp
index e9f22b5ebd2..c8a64b09526 100644
--- a/lldb/source/Interpreter/CommandInterpreter.cpp
+++ b/lldb/source/Interpreter/CommandInterpreter.cpp
@@ -1794,8 +1794,7 @@ void CommandInterpreter::HandleCompletionMatches(CompletionRequest &request) {
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);
}
}
diff --git a/lldb/unittests/Utility/CompletionRequestTest.cpp b/lldb/unittests/Utility/CompletionRequestTest.cpp
index 54bd3429921..469aad22a7e 100644
--- a/lldb/unittests/Utility/CompletionRequestTest.cpp
+++ b/lldb/unittests/Utility/CompletionRequestTest.cpp
@@ -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;
OpenPOWER on IntegriCloud