forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 341
[lldb] Implement diagnostics detecting when a variable is not captured #10772
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
adrian-prantl
merged 5 commits into
swiftlang:swift/release/6.2
from
felipepiovezan:felipe/enhance_not_found_diagnostic
Jun 12, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
60f522c
[lldb][nfc] Fix missing move operations and constness of methods (#14…
felipepiovezan 94fa0a9
[lldb][nfc] Factor out code checking if Variable is in scope (#143572)
felipepiovezan 043a2de
[lldb][NFCI] Implement language-agnostic diagnostic for non-captured …
felipepiovezan 8ab9cf4
[lldb] Implement swift diagnostic for non-captured vars in "frame var"
felipepiovezan 13a1eb2
[lldb] Implement swift diagnostic for non-captured vars in "expr"
felipepiovezan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,7 @@ | |
#include "lldb/Symbol/Type.h" | ||
#include "lldb/Symbol/Variable.h" | ||
#include "lldb/Symbol/VariableList.h" | ||
#include "lldb/Target/Language.h" | ||
#include "lldb/Utility/LLDBAssert.h" | ||
#include "lldb/Utility/LLDBLog.h" | ||
#include "lldb/Utility/Log.h" | ||
|
@@ -693,6 +694,68 @@ SwiftUserExpression::GetTextAndSetExpressionParser( | |
return parse_result; | ||
} | ||
|
||
/// If `sc` represents a "closure-like" function according to `lang`, and | ||
/// `var_name` can be found in a parent context, create a diagnostic | ||
/// explaining that this variable is available but not captured by the closure. | ||
static std::string | ||
CreateVarInParentScopeDiagnostic(StringRef var_name, | ||
StringRef parent_func_name) { | ||
return llvm::formatv("A variable named '{0}' existed in function '{1}', but " | ||
"it was not captured in the closure.\nHint: the " | ||
"variable may be available in a parent frame.", | ||
var_name, parent_func_name); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is this a separate function? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The thinking is that:
|
||
|
||
/// If `diagnostic_manager` contains a "cannot find <var_name> in scope" | ||
/// diagnostic, attempt to enhance it by showing if `var_name` is used inside a | ||
/// closure, not captured, but defined in a parent scope. | ||
static void EnhanceNotInScopeDiagnostics(DiagnosticManager &diagnostic_manager, | ||
ExecutionContextScope *exe_scope) { | ||
if (!exe_scope) | ||
return; | ||
lldb::StackFrameSP stack_frame = exe_scope->CalculateStackFrame(); | ||
if (!stack_frame) | ||
return; | ||
SymbolContext sc = | ||
stack_frame->GetSymbolContext(lldb::eSymbolContextEverything); | ||
Language *swift_lang = | ||
Language::FindPlugin(lldb::LanguageType::eLanguageTypeSwift); | ||
if (!swift_lang) | ||
return; | ||
|
||
static const RegularExpression not_in_scope_regex = | ||
RegularExpression("(.*): cannot find '([^']+)' in scope\n(.*)"); | ||
for (auto &diag : diagnostic_manager.Diagnostics()) { | ||
if (!diag) | ||
continue; | ||
|
||
llvm::SmallVector<StringRef, 4> match_groups; | ||
|
||
if (StringRef old_rendered_msg = diag->GetDetail().rendered; | ||
!not_in_scope_regex.Execute(old_rendered_msg, &match_groups)) | ||
continue; | ||
|
||
StringRef prefix = match_groups[1]; | ||
StringRef var_name = match_groups[2]; | ||
StringRef suffix = match_groups[3]; | ||
|
||
Function *parent_func = | ||
swift_lang->FindParentOfClosureWithVariable(var_name, sc); | ||
if (!parent_func) | ||
continue; | ||
std::string new_message = CreateVarInParentScopeDiagnostic( | ||
var_name, parent_func->GetDisplayName()); | ||
|
||
std::string new_rendered = | ||
llvm::formatv("{0}: {1}\n{2}", prefix, new_message, suffix); | ||
const DiagnosticDetail &old_detail = diag->GetDetail(); | ||
diag = std::make_unique<Diagnostic>( | ||
diag->getKind(), diag->GetCompilerID(), | ||
DiagnosticDetail{old_detail.source_location, old_detail.severity, | ||
std::move(new_message), std::move(new_rendered)}); | ||
} | ||
} | ||
|
||
bool SwiftUserExpression::Parse(DiagnosticManager &diagnostic_manager, | ||
ExecutionContext &exe_ctx, | ||
lldb_private::ExecutionPolicy execution_policy, | ||
|
@@ -888,6 +951,7 @@ bool SwiftUserExpression::Parse(DiagnosticManager &diagnostic_manager, | |
fixed_expression.substr(fixed_start, fixed_end - fixed_start); | ||
} | ||
} | ||
EnhanceNotInScopeDiagnostics(diagnostic_manager, exe_scope); | ||
return false; | ||
case ParseResult::success: | ||
llvm_unreachable("Success case is checked separately before switch!"); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Upstreaming this here llvm#143572