Skip to content

Commit d0bb139

Browse files
Merge pull request #6849 from adrian-prantl/setup-error
Swift expression parser: surface ClangImporter setup errors.
2 parents 7cd2f19 + 8b8337b commit d0bb139

File tree

5 files changed

+83
-46
lines changed

5 files changed

+83
-46
lines changed

lldb/source/Plugins/ExpressionParser/Swift/SwiftUserExpression.cpp

Lines changed: 40 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -656,64 +656,61 @@ bool SwiftUserExpression::Parse(DiagnosticManager &diagnostic_manager,
656656

657657
Status err;
658658

659+
auto error = [&](const char *error_msg, const char *detail = nullptr) {
660+
if (detail)
661+
LLDB_LOG(log, "%s: %s", error_msg, detail);
662+
else
663+
LLDB_LOG(log, error_msg);
664+
665+
diagnostic_manager.PutString(eDiagnosticSeverityError, error_msg);
666+
if (detail)
667+
diagnostic_manager.AppendMessageToDiagnostic(detail);
668+
return false;
669+
};
670+
659671
InstallContext(exe_ctx);
660672
Target *target = exe_ctx.GetTargetPtr();
661-
if (!target) {
662-
diagnostic_manager.PutString(eDiagnosticSeverityError,
663-
"couldn't start parsing (no target)");
664-
return false;
665-
}
673+
if (!target)
674+
return error("couldn't start parsing (no target)");
666675

667676
StackFrame *frame = exe_ctx.GetFramePtr();
668-
if (!frame) {
669-
diagnostic_manager.PutString(eDiagnosticSeverityError,
670-
"couldn't start parsing - no stack frame");
671-
LLDB_LOG(log, "no stack frame");
672-
return false;
673-
}
677+
if (!frame)
678+
return error("couldn't start parsing - no stack frame");
674679

675680
auto *exe_scope = exe_ctx.GetBestExecutionContextScope();
676-
if (!exe_scope) {
677-
LLDB_LOG(log, "no execution context scope");
678-
return false;
679-
}
681+
if (!exe_scope)
682+
return error( "no execution context scope");
680683

681684
m_swift_scratch_ctx = target->GetSwiftScratchContext(m_err, *exe_scope);
682-
if (!m_swift_scratch_ctx) {
683-
LLDB_LOG(log, "no scratch context", m_err.AsCString());
684-
return false;
685-
}
685+
if (!m_swift_scratch_ctx)
686+
return error("could not create a Swift scratch context: ",
687+
m_err.AsCString());
688+
686689
m_swift_ast_ctx = llvm::dyn_cast_or_null<SwiftASTContextForExpressions>(
687690
m_swift_scratch_ctx->get()->GetSwiftASTContext());
688691

689-
if (!m_swift_ast_ctx) {
690-
LLDB_LOG(log, "no Swift AST context");
691-
return false;
692-
}
692+
if (!m_swift_ast_ctx)
693+
return error("could not create a Swift AST context");
693694

694695
if (m_swift_ast_ctx->HasFatalErrors()) {
696+
m_swift_ast_ctx->PrintDiagnostics(diagnostic_manager);
695697
LLDB_LOG(log, "Swift AST context is in a fatal error state");
696698
return false;
697699
}
698700

699701
// This may destroy the scratch context.
700702
auto *persistent_state = GetPersistentState(target, exe_ctx);
701-
if (!persistent_state) {
702-
diagnostic_manager.PutString(eDiagnosticSeverityError,
703-
"couldn't start parsing (no persistent data)");
704-
return false;
705-
}
703+
if (!persistent_state)
704+
return error("could not start parsing (no persistent data)");
706705

707-
Status error;
706+
Status status;
708707
SourceModule module_info;
709708
module_info.path.emplace_back("Swift");
710709
swift::ModuleDecl *module_decl =
711-
m_swift_ast_ctx->GetModule(module_info, error);
710+
m_swift_ast_ctx->GetModule(module_info, status);
712711

713-
if (error.Fail() || !module_decl) {
714-
LLDB_LOG(log, "couldn't load Swift Standard Library\n");
715-
return false;
716-
}
712+
if (status.Fail() || !module_decl)
713+
return error("could not load Swift Standard Library", status.AsCString());
717714

718715
persistent_state->AddHandLoadedModule(ConstString("Swift"),
719716
swift::ImportedModule(module_decl));
@@ -722,10 +719,9 @@ bool SwiftUserExpression::Parse(DiagnosticManager &diagnostic_manager,
722719

723720
ScanContext(exe_ctx, err);
724721

725-
if (!err.Success()) {
726-
diagnostic_manager.Printf(eDiagnosticSeverityError, "warning: %s\n",
722+
if (!err.Success())
723+
diagnostic_manager.Printf(eDiagnosticSeverityWarning, "warning: %s\n",
727724
err.AsCString());
728-
}
729725

730726
StreamString m_transformed_stream;
731727

@@ -748,7 +744,8 @@ bool SwiftUserExpression::Parse(DiagnosticManager &diagnostic_manager,
748744

749745
while (true) {
750746
SwiftExpressionParser::ParseResult parse_result =
751-
GetTextAndSetExpressionParser(diagnostic_manager, source_code, exe_ctx, exe_scope);
747+
GetTextAndSetExpressionParser(diagnostic_manager, source_code, exe_ctx,
748+
exe_scope);
752749

753750
if (parse_result == ParseResult::success)
754751
break;
@@ -844,15 +841,12 @@ bool SwiftUserExpression::Parse(DiagnosticManager &diagnostic_manager,
844841
if (process && m_jit_start_addr != LLDB_INVALID_ADDRESS)
845842
m_jit_process_wp = lldb::ProcessWP(process->shared_from_this());
846843
return true;
847-
} else {
848-
const char *error_cstr = jit_error.AsCString();
849-
if (error_cstr && error_cstr[0])
850-
diagnostic_manager.PutString(eDiagnosticSeverityError, error_cstr);
851-
else
852-
diagnostic_manager.PutString(eDiagnosticSeverityError,
853-
"expression can't be interpreted or run\n");
854-
return false;
855844
}
845+
846+
const char *error_cstr = jit_error.AsCString();
847+
if (!error_cstr || !error_cstr[0])
848+
error_cstr = "expression can't be interpreted or run";
849+
return error(error_cstr);
856850
}
857851

858852
bool SwiftUserExpression::AddArguments(ExecutionContext &exe_ctx,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
SWIFT_SOURCES := main.swift
2+
SWIFT_BRIDGING_HEADER := bridging-header.h
3+
SWIFT_PRECOMPILE_BRIDGING_HEADER := NO
4+
5+
SWIFTFLAGS_EXTRAS = -I$(SRCDIR)
6+
7+
include Makefile.rules
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import lldb
2+
from lldbsuite.test.lldbtest import *
3+
from lldbsuite.test.decorators import *
4+
import lldbsuite.test.lldbutil as lldbutil
5+
import unittest2
6+
7+
class TestSwiftExtraClangFlags(TestBase):
8+
9+
mydir = TestBase.compute_mydir(__file__)
10+
11+
NO_DEBUG_INFO_TESTCASE = True
12+
13+
def setUp(self):
14+
TestBase.setUp(self)
15+
16+
# Don't run ClangImporter tests if Clangimporter is disabled.
17+
@skipIf(setting=('symbols.use-swift-clangimporter', 'false'))
18+
@skipIf(oslist=['windows'])
19+
@swiftTest
20+
def test_extra_clang_flags(self):
21+
"""
22+
Test error handling when ClangImporter emits diagnostics.
23+
"""
24+
self.build()
25+
self.addTearDownHook(
26+
lambda: self.runCmd("settings clear target.swift-extra-clang-flags"))
27+
self.expect('settings set -- target.swift-extra-clang-flags '+
28+
'"-DBREAK_STUFF"')
29+
lldbutil.run_to_source_breakpoint(self, "break here",
30+
lldb.SBFileSpec('main.swift'))
31+
self.expect("expr 0", error=True,
32+
substrs=['failed to import bridging header'])
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#ifdef BREAK_STUFF
2+
i am a syntax error
3+
#endif
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print("break here")

0 commit comments

Comments
 (0)