Skip to content

Commit 38cb6fb

Browse files
committed
Merge commit 'ce97507e8b50' from swift/release/5.9 into stable/20221013
Conflicts: lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp
2 parents fd15f78 + ce97507 commit 38cb6fb

File tree

8 files changed

+680
-526
lines changed

8 files changed

+680
-526
lines changed

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

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,6 @@ SetupASTContext(SwiftASTContextForExpressions &swift_ast_context,
771771
// TODO: Find a way to get contraint-solver output sent to a stream
772772
// so we can log it.
773773
// swift_ast_context.GetLanguageOptions().DebugConstraintSolver = true;
774-
swift_ast_context.ClearDiagnostics();
775774

776775
// No longer part of debugger support, set it separately.
777776
swift_ast_context.GetLanguageOptions().EnableDollarIdentifiers = true;
@@ -1217,15 +1216,15 @@ AddArchetypesTypeAliases(std::unique_ptr<SwiftASTManipulator> &code_manipulator,
12171216
/// Attempt to parse an expression and import all the Swift modules
12181217
/// the expression and its context depend on.
12191218
static llvm::Expected<ParsedExpression> ParseAndImport(
1220-
SwiftASTContextForExpressions &swift_ast_context, Expression &expr,
1219+
SwiftASTContextForExpressions &swift_ast_context,
1220+
SwiftASTContext::ScopedDiagnostics &expr_diagnostics, Expression &expr,
12211221
SwiftExpressionParser::SILVariableMap &variable_map, unsigned &buffer_id,
12221222
DiagnosticManager &diagnostic_manager,
12231223
SwiftExpressionParser &swift_expr_parser,
12241224
lldb::StackFrameWP &stack_frame_wp, SymbolContext &sc,
1225-
ExecutionContextScope &exe_scope,
1225+
ExecutionContextScope &exe_scope,
12261226
llvm::SmallVectorImpl<SwiftASTManipulator::VariableInfo> &local_variables,
1227-
const EvaluateExpressionOptions &options,
1228-
bool repl, bool playground) {
1227+
const EvaluateExpressionOptions &options, bool repl, bool playground) {
12291228
Log *log = GetLog(LLDBLog::Expressions);
12301229
LLDB_SCOPED_TIMER();
12311230

@@ -1373,17 +1372,16 @@ static llvm::Expected<ParsedExpression> ParseAndImport(
13731372
// inserting them in.
13741373
swift_ast_context.AddDebuggerClient(external_lookup);
13751374

1376-
if (swift_ast_context.HasErrors())
1375+
if (expr_diagnostics.HasErrors())
13771376
return make_error<SwiftASTContextError>();
13781377

13791378
// Resolve the file's imports, including the implicit ones returned from
13801379
// GetImplicitImports.
13811380
swift::performImportResolution(*source_file);
13821381

1383-
if (swift_ast_context.HasErrors())
1382+
if (expr_diagnostics.HasErrors())
13841383
return make_error<ModuleImportError>(
1385-
swift_ast_context.GetAllErrors().AsCString(
1386-
"Explicit module import error"));
1384+
llvm::toString(expr_diagnostics.GetAllErrors()));
13871385

13881386
std::unique_ptr<SwiftASTManipulator> code_manipulator;
13891387
if (repl || !playground) {
@@ -1654,18 +1652,24 @@ RedirectCallFromSinkToTrampolineFunction(llvm::Module &module,
16541652
SwiftExpressionParser::ParseResult
16551653
SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
16561654
uint32_t first_line, uint32_t last_line) {
1655+
SwiftExpressionParser::SILVariableMap variable_map;
16571656
using ParseResult = SwiftExpressionParser::ParseResult;
16581657
Log *log = GetLog(LLDBLog::Expressions);
16591658
LLDB_SCOPED_TIMER();
16601659

1661-
SwiftExpressionParser::SILVariableMap variable_map;
1660+
// Get a scoped diagnostics consumer for all diagnostics produced by
1661+
// this expression.
1662+
auto expr_diagnostics = m_swift_ast_ctx.getScopedDiagnosticConsumer();
1663+
m_swift_ast_ctx.GetDiagnosticEngine().resetHadAnyError();
16621664

16631665
// Helper function to diagnose errors in m_swift_scratch_context.
16641666
unsigned buffer_id = UINT32_MAX;
16651667
auto DiagnoseSwiftASTContextError = [&]() {
1666-
assert(m_swift_ast_ctx.HasErrors() && "error expected");
1667-
m_swift_ast_ctx.PrintDiagnostics(diagnostic_manager, buffer_id, first_line,
1668-
last_line);
1668+
assert((expr_diagnostics->HasErrors() ||
1669+
m_swift_ast_ctx.HasClangImporterErrors()) &&
1670+
"error expected");
1671+
expr_diagnostics->PrintDiagnostics(diagnostic_manager, buffer_id,
1672+
first_line, last_line);
16691673
};
16701674

16711675
// In the case of playgrounds, we turn all rewriting functionality off.
@@ -1677,9 +1681,9 @@ SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
16771681

16781682
// Parse the expression and import all nececssary swift modules.
16791683
auto parsed_expr = ParseAndImport(
1680-
m_swift_ast_ctx, m_expr, variable_map, buffer_id, diagnostic_manager,
1681-
*this, m_stack_frame_wp, m_sc, *m_exe_scope, m_local_variables, m_options,
1682-
repl, playground);
1684+
m_swift_ast_ctx, *expr_diagnostics, m_expr, variable_map, buffer_id,
1685+
diagnostic_manager, *this, m_stack_frame_wp, m_sc, *m_exe_scope,
1686+
m_local_variables, m_options, repl, playground);
16831687

16841688
if (!parsed_expr) {
16851689
bool retry = false;
@@ -1738,7 +1742,7 @@ SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
17381742
if (log)
17391743
dumpModule("Module after type checking:");
17401744

1741-
if (m_swift_ast_ctx.HasErrors()) {
1745+
if (expr_diagnostics->HasErrors()) {
17421746
DiagnoseSwiftASTContextError();
17431747
return ParseResult::unrecoverable_error;
17441748
}
@@ -1915,7 +1919,7 @@ SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
19151919
log->PutCString(s.c_str());
19161920
}
19171921

1918-
if (m_swift_ast_ctx.HasErrors()) {
1922+
if (expr_diagnostics->HasErrors()) {
19191923
DiagnoseSwiftASTContextError();
19201924
return ParseResult::unrecoverable_error;
19211925
}
@@ -1943,7 +1947,7 @@ SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
19431947
log->PutCString(s.c_str());
19441948
}
19451949

1946-
if (m_swift_ast_ctx.HasErrors()) {
1950+
if (expr_diagnostics->HasErrors()) {
19471951
DiagnoseSwiftASTContextError();
19481952
return ParseResult::unrecoverable_error;
19491953
}
@@ -1969,30 +1973,22 @@ SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
19691973
m_module.reset(ContextAndModule.second);
19701974
}
19711975

1972-
if (m_swift_ast_ctx.HasErrors()) {
1976+
// If IRGen failed without errors, the root cause may be a fatal
1977+
// Clang diagnostic.
1978+
if (expr_diagnostics->HasErrors() ||
1979+
m_swift_ast_ctx.HasClangImporterErrors()) {
1980+
diagnostic_manager.PutString(eDiagnosticSeverityRemark,
1981+
"couldn't IRGen expression");
19731982
DiagnoseSwiftASTContextError();
19741983
return ParseResult::unrecoverable_error;
19751984
}
19761985

19771986
if (!m_module) {
1978-
auto &warnings = m_swift_ast_ctx.GetModuleImportWarnings();
1979-
for (StringRef message : warnings) {
1980-
// FIXME: Don't store diagnostics as strings.
1981-
auto severity = eDiagnosticSeverityWarning;
1982-
if (message.consume_front("warning: "))
1983-
severity = eDiagnosticSeverityWarning;
1984-
if (message.consume_front("error: "))
1985-
severity = eDiagnosticSeverityError;
1986-
diagnostic_manager.PutString(severity, message);
1987-
}
1988-
std::string error = "couldn't IRGen expression";
19891987
diagnostic_manager.Printf(
1990-
eDiagnosticSeverityError, "couldn't IRGen expression. %s",
1991-
warnings.empty()
1992-
? "Please enable the expression log by running \"log enable lldb "
1993-
"expr\", then run the failing expression again, and file a "
1994-
"bugreport with the log output."
1995-
: "Please check the above error messages for possible root causes.");
1988+
eDiagnosticSeverityError,
1989+
"couldn't IRGen expression. Please enable the expression log by "
1990+
"running \"log enable lldb expr\", then run the failing expression "
1991+
"again, and file a bug report with the log output.");
19961992
return ParseResult::unrecoverable_error;
19971993
}
19981994

@@ -2032,12 +2028,21 @@ SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
20322028
std::lock_guard<std::recursive_mutex> global_context_locker(
20332029
IRExecutionUnit::GetLLVMGlobalContextMutex());
20342030

2035-
LLVMVerifyModule((LLVMOpaqueModule *)m_module.get(), LLVMReturnStatusAction,
2036-
nullptr);
2031+
bool has_errors = LLVMVerifyModule((LLVMOpaqueModule *)m_module.get(),
2032+
LLVMReturnStatusAction, nullptr);
2033+
if (has_errors) {
2034+
diagnostic_manager.PutString(eDiagnosticSeverityRemark,
2035+
"LLVM verification error");
2036+
return ParseResult::unrecoverable_error;
2037+
}
20372038
}
20382039

2039-
if (m_swift_ast_ctx.HasErrors())
2040+
if (expr_diagnostics->HasErrors()) {
2041+
diagnostic_manager.PutString(eDiagnosticSeverityRemark,
2042+
"post-IRGen error");
2043+
DiagnoseSwiftASTContextError();
20402044
return ParseResult::unrecoverable_error;
2045+
}
20412046

20422047
// The Parse succeeded! Now put this module into the context's list
20432048
// of loaded modules, and copy the Decls that were globalized as

0 commit comments

Comments
 (0)