Skip to content

Commit 0bbb794

Browse files
authored
[lldb][ClangASTImporter] Don't ASTImport LambdaExpr nodes (#154962)
This patch works around an assertion that we hit in the `LambdaExpr` constructor when we call it from `ASTNodeImporter::VisitLambdaExpr` (see #149477). The lambda that we imported doesn't have the `NumCaptures` field accurately set to the one on the source decl. This is because in `MinimalImport` mode, we skip importing of lambda definitions: https://github.com/llvm/llvm-project/blob/e21b0dd81928a3266df0e3ede008fb7a6676ff95/clang/lib/AST/ASTImporter.cpp#L2499 In practice we have seen this assertion occur in our `import-std-module` test-suite when libc++ headers decide to use lambdas inside inline function bodies (the latest failure being caused by #144602). To avoid running into this whenever libc++ decides to use lambdas in headers, this patch skips `ASTImport` of lambdas alltogether. Ideally this would bubble up to the user or log as an error, but we swallow the `ASTImportError`s currently. The only way this codepath is hit is when lambdas are used inside functions in defined in the expression evaluator, or when importing AST nodes from Clang modules. Both of these are very niche use-cases (for now), so a workaround seemed appropriate.
1 parent de6bd15 commit 0bbb794

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

lldb/source/Plugins/ExpressionParser/Clang/ClangASTImporter.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,16 @@ ClangASTImporter::MapCompleter::~MapCompleter() = default;
10481048

10491049
llvm::Expected<Decl *>
10501050
ClangASTImporter::ASTImporterDelegate::ImportImpl(Decl *From) {
1051+
// FIXME: The Minimal import mode of clang::ASTImporter does not correctly
1052+
// import Lambda definitions. Work around this for now by not importing
1053+
// lambdas at all. This is most likely encountered when importing decls from
1054+
// the `std` module (not from debug-info), where lambdas can be defined in
1055+
// inline function bodies. Those will be imported by LLDB.
1056+
if (const auto *CXX = llvm::dyn_cast<clang::CXXRecordDecl>(From))
1057+
if (CXX->isLambda())
1058+
return llvm::make_error<ASTImportError>(
1059+
ASTImportError::UnsupportedConstruct);
1060+
10511061
if (m_std_handler) {
10521062
std::optional<Decl *> D = m_std_handler->Import(From);
10531063
if (D) {
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
from lldbsuite.test import lldbinline
22
from lldbsuite.test import decorators
33

4-
lldbinline.MakeInlineTest(__file__, globals())
4+
lldbinline.MakeInlineTest(
5+
__file__,
6+
globals(),
7+
[
8+
decorators.expectedFailureAll(
9+
bugnumber="https://github.com/llvm/llvm-project/issues/149477"
10+
)
11+
],
12+
)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Test that we can successfully ASTImport clang::LambdaExpr nodes.
2+
# Currently this is not supported in MinimalImport mode (which LLDB
3+
# uses always).
4+
5+
# RUN: split-file %s %t
6+
# RUN: %clang_host -g -gdwarf %t/main.cpp -o %t.out
7+
# RUN: %lldb -o "settings set interpreter.stop-command-source-on-error false" \
8+
# RUN: -x -b -s %t/commands.input %t.out 2>&1 \
9+
# RUN: | FileCheck %s
10+
11+
#--- main.cpp
12+
13+
int main() {
14+
__builtin_debugtrap();
15+
}
16+
17+
#--- commands.input
18+
19+
run
20+
expression --top-level -- void method(int x) { [x=x] { ; }; }
21+
target dump typesystem
22+
23+
# CHECK: expression
24+
# CHECK: target dump typesystem
25+
# CHECK-NOT: FunctionDecl
26+
# CHECK-NOT: LambdaExpr

0 commit comments

Comments
 (0)