Skip to content

[lldb] Fix module name tab completion #93458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 36 additions & 21 deletions lldb/source/Commands/CommandCompletions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
//
//===----------------------------------------------------------------------===//

#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSet.h"

#include "lldb/Breakpoint/Watchpoint.h"
Expand Down Expand Up @@ -262,9 +264,25 @@ class ModuleCompleter : public Completer {
public:
ModuleCompleter(CommandInterpreter &interpreter, CompletionRequest &request)
: Completer(interpreter, request) {
FileSpec partial_spec(m_request.GetCursorArgumentPrefix());
m_file_name = partial_spec.GetFilename().GetCString();
m_dir_name = partial_spec.GetDirectory().GetCString();
llvm::StringRef request_str = m_request.GetCursorArgumentPrefix();
// We can match the full path, or the file name only. The full match will be
// attempted always, the file name match only if the request does not
// contain a path separator.

// Preserve both the path as spelled by the user (used for completion) and
// the canonical version (used for matching).
m_spelled_path = request_str;
m_canonical_path = FileSpec(m_spelled_path).GetPath();
if (!m_spelled_path.empty() &&
llvm::sys::path::is_separator(m_spelled_path.back()) &&
!llvm::StringRef(m_canonical_path).ends_with(m_spelled_path.back())) {
m_canonical_path += m_spelled_path.back();
}

if (llvm::find_if(request_str, [](char c) {
return llvm::sys::path::is_separator(c);
}) == request_str.end())
m_file_name = request_str;
}

lldb::SearchDepth GetDepth() override { return lldb::eSearchDepthModule; }
Expand All @@ -273,22 +291,18 @@ class ModuleCompleter : public Completer {
SymbolContext &context,
Address *addr) override {
if (context.module_sp) {
const char *cur_file_name =
context.module_sp->GetFileSpec().GetFilename().GetCString();
const char *cur_dir_name =
context.module_sp->GetFileSpec().GetDirectory().GetCString();

bool match = false;
if (m_file_name && cur_file_name &&
strstr(cur_file_name, m_file_name) == cur_file_name)
match = true;

if (match && m_dir_name && cur_dir_name &&
strstr(cur_dir_name, m_dir_name) != cur_dir_name)
match = false;

if (match) {
m_request.AddCompletion(cur_file_name);
// Attempt a full path match.
std::string cur_path = context.module_sp->GetFileSpec().GetPath();
llvm::StringRef cur_path_view = cur_path;
if (cur_path_view.consume_front(m_canonical_path))
m_request.AddCompletion((m_spelled_path + cur_path_view).str());

// And a file name match.
if (m_file_name) {
llvm::StringRef cur_file_name =
context.module_sp->GetFileSpec().GetFilename().GetStringRef();
if (cur_file_name.starts_with(*m_file_name))
m_request.AddCompletion(cur_file_name);
}
}
return Searcher::eCallbackReturnContinue;
Expand All @@ -297,8 +311,9 @@ class ModuleCompleter : public Completer {
void DoCompletion(SearchFilter *filter) override { filter->Search(*this); }

private:
const char *m_file_name;
const char *m_dir_name;
std::optional<llvm::StringRef> m_file_name;
llvm::StringRef m_spelled_path;
std::string m_canonical_path;

ModuleCompleter(const ModuleCompleter &) = delete;
const ModuleCompleter &operator=(const ModuleCompleter &) = delete;
Expand Down
35 changes: 35 additions & 0 deletions lldb/test/API/functionalities/completion/TestCompletion.py
Original file line number Diff line number Diff line change
Expand Up @@ -906,3 +906,38 @@ def test_ambiguous_command(self):
def test_ambiguous_subcommand(self):
"""Test completing a subcommand of an ambiguous command"""
self.complete_from_to("settings s ta", [])

def test_shlib_name(self):
self.build()
target = self.dbg.CreateTarget(self.getBuildArtifact("a.out"))
self.assertTrue(target, VALID_TARGET)
self.registerSharedLibrariesWithTarget(target, ["shared"])

basenames = []
paths = []
for m in target.modules:
basenames.append(m.file.basename)
paths.append(m.file.fullpath)

# To see all the diffs
self.maxDiff = None

# An empty string completes to everything
self.completions_match("target symbols add -s ", basenames + paths)

# Base name completion
self.completions_match("target symbols add -s a.", ["a.out"])

# Full path completion
prefix = os.path.commonpath(paths)
self.completions_match("target symbols add -s '" + prefix, paths)

# Full path, but ending with a path separator
prefix_sep = prefix + os.path.sep
self.completions_match("target symbols add -s '" + prefix_sep, paths)

# The completed path should match the spelling of the input, so if the
# input contains a double separator, so should the completions.
prefix_sep_sep = prefix_sep + os.path.sep
paths_sep = [prefix_sep_sep + p[len(prefix_sep) :] for p in paths]
self.completions_match("target symbols add -s '" + prefix_sep_sep, paths_sep)
Loading