@@ -771,7 +771,6 @@ SetupASTContext(SwiftASTContextForExpressions &swift_ast_context,
771
771
// TODO: Find a way to get contraint-solver output sent to a stream
772
772
// so we can log it.
773
773
// swift_ast_context.GetLanguageOptions().DebugConstraintSolver = true;
774
- swift_ast_context.ClearDiagnostics ();
775
774
776
775
// No longer part of debugger support, set it separately.
777
776
swift_ast_context.GetLanguageOptions ().EnableDollarIdentifiers = true ;
@@ -1217,15 +1216,15 @@ AddArchetypesTypeAliases(std::unique_ptr<SwiftASTManipulator> &code_manipulator,
1217
1216
// / Attempt to parse an expression and import all the Swift modules
1218
1217
// / the expression and its context depend on.
1219
1218
static llvm::Expected<ParsedExpression> ParseAndImport (
1220
- SwiftASTContextForExpressions &swift_ast_context, Expression &expr,
1219
+ SwiftASTContextForExpressions &swift_ast_context,
1220
+ SwiftASTContext::ScopedDiagnostics &expr_diagnostics, Expression &expr,
1221
1221
SwiftExpressionParser::SILVariableMap &variable_map, unsigned &buffer_id,
1222
1222
DiagnosticManager &diagnostic_manager,
1223
1223
SwiftExpressionParser &swift_expr_parser,
1224
1224
lldb::StackFrameWP &stack_frame_wp, SymbolContext &sc,
1225
- ExecutionContextScope &exe_scope,
1225
+ ExecutionContextScope &exe_scope,
1226
1226
llvm::SmallVectorImpl<SwiftASTManipulator::VariableInfo> &local_variables,
1227
- const EvaluateExpressionOptions &options,
1228
- bool repl, bool playground) {
1227
+ const EvaluateExpressionOptions &options, bool repl, bool playground) {
1229
1228
Log *log = GetLog (LLDBLog::Expressions);
1230
1229
LLDB_SCOPED_TIMER ();
1231
1230
@@ -1373,17 +1372,16 @@ static llvm::Expected<ParsedExpression> ParseAndImport(
1373
1372
// inserting them in.
1374
1373
swift_ast_context.AddDebuggerClient (external_lookup);
1375
1374
1376
- if (swift_ast_context .HasErrors ())
1375
+ if (expr_diagnostics .HasErrors ())
1377
1376
return make_error<SwiftASTContextError>();
1378
1377
1379
1378
// Resolve the file's imports, including the implicit ones returned from
1380
1379
// GetImplicitImports.
1381
1380
swift::performImportResolution (*source_file);
1382
1381
1383
- if (swift_ast_context .HasErrors ())
1382
+ if (expr_diagnostics .HasErrors ())
1384
1383
return make_error<ModuleImportError>(
1385
- swift_ast_context.GetAllErrors ().AsCString (
1386
- " Explicit module import error" ));
1384
+ llvm::toString (expr_diagnostics.GetAllErrors ()));
1387
1385
1388
1386
std::unique_ptr<SwiftASTManipulator> code_manipulator;
1389
1387
if (repl || !playground) {
@@ -1654,18 +1652,24 @@ RedirectCallFromSinkToTrampolineFunction(llvm::Module &module,
1654
1652
SwiftExpressionParser::ParseResult
1655
1653
SwiftExpressionParser::Parse (DiagnosticManager &diagnostic_manager,
1656
1654
uint32_t first_line, uint32_t last_line) {
1655
+ SwiftExpressionParser::SILVariableMap variable_map;
1657
1656
using ParseResult = SwiftExpressionParser::ParseResult;
1658
1657
Log *log = GetLog (LLDBLog::Expressions);
1659
1658
LLDB_SCOPED_TIMER ();
1660
1659
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 ();
1662
1664
1663
1665
// Helper function to diagnose errors in m_swift_scratch_context.
1664
1666
unsigned buffer_id = UINT32_MAX;
1665
1667
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);
1669
1673
};
1670
1674
1671
1675
// In the case of playgrounds, we turn all rewriting functionality off.
@@ -1677,9 +1681,9 @@ SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
1677
1681
1678
1682
// Parse the expression and import all nececssary swift modules.
1679
1683
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);
1683
1687
1684
1688
if (!parsed_expr) {
1685
1689
bool retry = false ;
@@ -1738,7 +1742,7 @@ SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
1738
1742
if (log)
1739
1743
dumpModule (" Module after type checking:" );
1740
1744
1741
- if (m_swift_ast_ctx. HasErrors ()) {
1745
+ if (expr_diagnostics-> HasErrors ()) {
1742
1746
DiagnoseSwiftASTContextError ();
1743
1747
return ParseResult::unrecoverable_error;
1744
1748
}
@@ -1915,7 +1919,7 @@ SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
1915
1919
log->PutCString (s.c_str ());
1916
1920
}
1917
1921
1918
- if (m_swift_ast_ctx. HasErrors ()) {
1922
+ if (expr_diagnostics-> HasErrors ()) {
1919
1923
DiagnoseSwiftASTContextError ();
1920
1924
return ParseResult::unrecoverable_error;
1921
1925
}
@@ -1943,7 +1947,7 @@ SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
1943
1947
log->PutCString (s.c_str ());
1944
1948
}
1945
1949
1946
- if (m_swift_ast_ctx. HasErrors ()) {
1950
+ if (expr_diagnostics-> HasErrors ()) {
1947
1951
DiagnoseSwiftASTContextError ();
1948
1952
return ParseResult::unrecoverable_error;
1949
1953
}
@@ -1969,30 +1973,22 @@ SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
1969
1973
m_module.reset (ContextAndModule.second );
1970
1974
}
1971
1975
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" );
1973
1982
DiagnoseSwiftASTContextError ();
1974
1983
return ParseResult::unrecoverable_error;
1975
1984
}
1976
1985
1977
1986
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" ;
1989
1987
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." );
1996
1992
return ParseResult::unrecoverable_error;
1997
1993
}
1998
1994
@@ -2032,12 +2028,21 @@ SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
2032
2028
std::lock_guard<std::recursive_mutex> global_context_locker (
2033
2029
IRExecutionUnit::GetLLVMGlobalContextMutex ());
2034
2030
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
+ }
2037
2038
}
2038
2039
2039
- if (m_swift_ast_ctx.HasErrors ())
2040
+ if (expr_diagnostics->HasErrors ()) {
2041
+ diagnostic_manager.PutString (eDiagnosticSeverityRemark,
2042
+ " post-IRGen error" );
2043
+ DiagnoseSwiftASTContextError ();
2040
2044
return ParseResult::unrecoverable_error;
2045
+ }
2041
2046
2042
2047
// The Parse succeeded! Now put this module into the context's list
2043
2048
// of loaded modules, and copy the Decls that were globalized as
0 commit comments