Skip to content

[lldb] Fix the computation of the hidden/in_user_code flag #10066

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 1 commit into from
Feb 20, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,52 @@ class StoringDiagnosticConsumer : public swift::DiagnosticConsumer {
// FXIME: This is a heuristic.
uint16_t len = info.Ranges.size() ? info.Ranges.front().getByteLength() : 0;
bool in_user_input = false;
// FIXME: improve this!
if (buffer_name == "<REPL>" || buffer_name == "<EXPR>" ||
buffer_name == "repl.swift" ||
buffer_name.starts_with("<user expression"))
in_user_input = true;
bool hidden = false;
[&]() {
if (buffer_name == "repl.swift") {
in_user_input = true;
return;
}
if (buffer_name != "<REPL>" && buffer_name != "<EXPR>" &&
!buffer_name.starts_with("<user expression"))
return;

unsigned buffer_id = source_mgr.findBufferContainingLoc(info.Loc);
llvm::SourceMgr &src_mgr = source_mgr.getLLVMSourceMgr();
auto *buffer = src_mgr.getMemoryBuffer(buffer_id);
if (!buffer)
return;

// Clang uses the much more elegant #line mechanism to find the
// user code, but doing this here could interfere with
// ExprOptions.PoundLineLine mechanism used Swift
// Playgrounds. So instead we find the markers from both ends.
StringRef buffer_data = buffer->getBuffer();
size_t pos = buffer_data.find("__LLDB_USER_START__");
if (pos == StringRef::npos)
return;
auto start_loc =
llvm::SMLoc::getFromPointer(buffer->getBufferStart() + pos);
unsigned start_line = src_mgr.FindLineNumber(start_loc, buffer_id);

pos = buffer_data.rfind("__LLDB_USER_END__");
if (pos == StringRef::npos)
return;
auto end_loc =
llvm::SMLoc::getFromPointer(buffer->getBufferStart() + pos);
unsigned end_line = src_mgr.FindLineNumber(end_loc, buffer_id);

unsigned diag_line =
source_mgr.getLineAndColumnInBuffer(info.Loc, buffer_id).first;

in_user_input = start_line < diag_line && diag_line < end_line;
hidden = !in_user_input;
}();
DiagnosticDetail::SourceLocation loc = {FileSpec{buffer_name.str()},
line_col.first,
(uint16_t)line_col.second,
len,
!in_user_input,
hidden,
in_user_input};
DiagnosticDetail detail = {loc, severity, raw_text, formatted_text};
RawDiagnostic diagnostic(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,35 @@ def test_missing_var(self):
target, process, thread, bkpt = lldbutil.run_to_source_breakpoint(
self, 'break here', lldb.SBFileSpec('main.swift'))

# This produces two errors:
# error: <EXPR>:8:1: initializers may only be declared within a type
# } catch (let __lldb_tmp_error) {
# ^
#
# error: <EXPR>:6:5: expected '(' for initializer parameters
# init
# ^
# The first one is outside of user code, the second one isn't.

options = lldb.SBExpressionOptions()
value = self.frame().EvaluateExpression("init", options)
data = value.GetError().GetErrorData()

version = data.GetValueForKey("version")
self.assertEqual(version.GetIntegerValue(), 1)
diags = data.GetValueForKey("errors").GetItemAtIndex(0)
details = diags.GetValueForKey("details")
err0 = details.GetItemAtIndex(0)
self.assertIn("initializers", str(err0.GetValueForKey("message")))
loc0 = err0.GetValueForKey("source_location")
self.assertTrue(loc0.GetValueForKey("hidden").GetBooleanValue())
self.assertFalse(loc0.GetValueForKey("in_user_input").GetBooleanValue())
err1 = details.GetItemAtIndex(1)
self.assertIn("expected '('", str(err1.GetValueForKey("message")))
loc1 = err1.GetValueForKey("source_location")
self.assertFalse(loc1.GetValueForKey("hidden").GetBooleanValue())
self.assertTrue(loc1.GetValueForKey("in_user_input").GetBooleanValue())

options = lldb.SBExpressionOptions()
value = self.frame().EvaluateExpression(
"ceciNestPasUnVar", options)
Expand Down Expand Up @@ -58,3 +87,4 @@ def check(value):
process.Continue()
self.expect('expression -O -- number', error=True,
substrs=['self', 'not', 'found'])