Skip to content

Commit 8dc824d

Browse files
committed
limiting completion results to max_return_elements, adding api tests
1 parent 04e503a commit 8dc824d

File tree

5 files changed

+57
-14
lines changed

5 files changed

+57
-14
lines changed

lldb/include/lldb/Utility/CompletionRequest.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,10 @@ class CompletionRequest {
164164

165165
size_t GetMaxReturnElements() const { return m_max_return_elements; }
166166

167-
/// Returns true if the maximum number of completions has been reached
168-
/// already.
169-
bool ShouldStopAddingResults() const {
170-
return m_result.GetNumberOfResults() >= m_max_return_elements;
167+
/// Returns true if the maximum number of completions has not been reached
168+
/// yet, hence we should keep adding completions.
169+
bool ShouldAddCompletions() const {
170+
return m_result.GetNumberOfResults() < m_max_return_elements;
171171
}
172172

173173
/// Returns the maximum number of completions that need to be added
@@ -254,7 +254,7 @@ class CompletionRequest {
254254
/// The cursor position in the argument indexed by m_cursor_index.
255255
size_t m_cursor_char_position;
256256
/// The maximum number of completions that should be returned.
257-
size_t m_max_return_elements = SIZE_MAX;
257+
size_t m_max_return_elements = std::numeric_limits<size_t>::max();
258258

259259
/// The result this request is supposed to fill out.
260260
/// We keep this object private to ensure that no backend can in any way

lldb/packages/Python/lldbsuite/test/lldbtest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2257,12 +2257,12 @@ def complete_from_to(self, str_input, patterns):
22572257
substrs=[p],
22582258
)
22592259

2260-
def completions_match(self, command, completions):
2260+
def completions_match(self, command, completions, max_completions=-1):
22612261
"""Checks that the completions for the given command are equal to the
22622262
given list of completions"""
22632263
interp = self.dbg.GetCommandInterpreter()
22642264
match_strings = lldb.SBStringList()
2265-
interp.HandleCompletion(command, len(command), 0, -1, match_strings)
2265+
interp.HandleCompletion(command, len(command), 0, max_completions, match_strings)
22662266
# match_strings is a 1-indexed list, so we have to slice...
22672267
self.assertCountEqual(
22682268
completions, list(match_strings)[1:], "List of returned completion is wrong"

lldb/source/API/SBCommandInterpreter.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,15 +263,26 @@ int SBCommandInterpreter::HandleCompletionWithDescriptions(
263263
if (!IsValid())
264264
return 0;
265265

266+
if (max_return_elements == 0)
267+
return 0;
268+
266269
lldb_private::StringList lldb_matches, lldb_descriptions;
267270
CompletionResult result;
268271
CompletionRequest request(current_line, cursor - current_line, result);
269-
if (max_return_elements >= 0)
272+
if (max_return_elements > 0)
270273
request.SetMaxReturnElements(max_return_elements);
271274
m_opaque_ptr->HandleCompletion(request);
272275
result.GetMatches(lldb_matches);
273276
result.GetDescriptions(lldb_descriptions);
274277

278+
// limit the matches to the max_return_elements if necessary
279+
if (max_return_elements > 0 &&
280+
lldb_matches.GetSize() > static_cast<size_t>(max_return_elements)) {
281+
lldb_matches.SetSize(max_return_elements);
282+
lldb_descriptions.SetSize(max_return_elements);
283+
}
284+
int number_of_matches = lldb_matches.GetSize();
285+
275286
// Make the result array indexed from 1 again by adding the 'common prefix'
276287
// of all completions as element 0. This is done to emulate the old API.
277288
if (request.GetParsedLine().GetArgumentCount() == 0) {
@@ -305,7 +316,7 @@ int SBCommandInterpreter::HandleCompletionWithDescriptions(
305316
matches.AppendList(temp_matches_list);
306317
SBStringList temp_descriptions_list(&lldb_descriptions);
307318
descriptions.AppendList(temp_descriptions_list);
308-
return result.GetNumberOfResults();
319+
return number_of_matches;
309320
}
310321

311322
int SBCommandInterpreter::HandleCompletionWithDescriptions(

lldb/source/Commands/CommandCompletions.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ bool CommandCompletions::InvokeCommonCompletionCallbacks(
9191
nullptr} // This one has to be last in the list.
9292
};
9393

94-
for (int i = 0; !request.ShouldStopAddingResults(); i++) {
94+
for (int i = 0; request.ShouldAddCompletions(); i++) {
9595
if (common_completions[i].type == lldb::eTerminatorCompletion)
9696
break;
9797
else if ((common_completions[i].type & completion_mask) ==
@@ -232,9 +232,8 @@ class SymbolCompleter : public Completer {
232232

233233
// Now add the functions & symbols to the list - only add if unique:
234234
for (const SymbolContext &sc : sc_list) {
235-
if (m_match_set.size() >= m_request.GetMaxNumberOfResultsToAdd()) {
235+
if (m_match_set.size() >= m_request.GetMaxNumberOfResultsToAdd())
236236
break;
237-
}
238237

239238
ConstString func_name = sc.GetFunctionName(Mangled::ePreferDemangled);
240239
// Ensure that the function name matches the regex. This is more than
@@ -313,7 +312,8 @@ class ModuleCompleter : public Completer {
313312
m_request.AddCompletion(cur_file_name);
314313
}
315314
}
316-
return Searcher::eCallbackReturnContinue;
315+
return m_request.ShouldAddCompletions() ? Searcher::eCallbackReturnContinue
316+
: Searcher::eCallbackReturnStop;
317317
}
318318

319319
void DoCompletion(SearchFilter *filter) override { filter->Search(*this); }
@@ -437,7 +437,8 @@ static void DiskFilesOrDirectories(const llvm::Twine &partial_name,
437437
std::error_code EC;
438438
llvm::vfs::directory_iterator Iter = fs.DirBegin(SearchDir, EC);
439439
llvm::vfs::directory_iterator End;
440-
for (; Iter != End && !EC; Iter.increment(EC)) {
440+
for (; Iter != End && !EC && request.ShouldAddCompletions();
441+
Iter.increment(EC)) {
441442
auto &Entry = *Iter;
442443
llvm::ErrorOr<llvm::vfs::Status> Status = fs.GetStatus(Entry.path());
443444

lldb/test/API/commands/expression/completion/TestExprCompletion.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,37 @@ def test_expr_completion_with_descriptions(self):
297297
enforce_order=True,
298298
)
299299

300+
def test_expr_completion_max_results(self):
301+
self.build()
302+
self.main_source = "main.cpp"
303+
self.main_source_spec = lldb.SBFileSpec(self.main_source)
304+
self.createTestTarget()
305+
306+
(target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
307+
self, "// Break here", self.main_source_spec
308+
)
309+
310+
expected_completions = [
311+
"some_expr.~Expr()",
312+
"some_expr.operator=(", # Copy operator
313+
"some_expr.operator=(", # Move operator
314+
"some_expr.MemberVariableBar",
315+
"some_expr.StaticMemberMethodBar()",
316+
"some_expr.Self()",
317+
"some_expr.FooNoArgsBar()",
318+
"some_expr.FooWithArgsBar(",
319+
"some_expr.FooNumbersBar1()",
320+
"some_expr.FooUnderscoreBar_()",
321+
"some_expr.FooWithMultipleArgsBar(",
322+
]
323+
324+
for i in range(1, len(expected_completions)):
325+
self.completions_match(
326+
"expr some_expr.",
327+
expected_completions[:i],
328+
max_completions=i,
329+
)
330+
300331
def assume_no_completions(self, str_input, cursor_pos=None):
301332
interp = self.dbg.GetCommandInterpreter()
302333
match_strings = lldb.SBStringList()

0 commit comments

Comments
 (0)