Skip to content

Commit a062717

Browse files
authored
Merge pull request #7000 from augusto2112/distinguish-error-kind
[lldb] Distinguish between Clang and Swift errors in SwiftASTContext
2 parents 5b190dd + bad6f89 commit a062717

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1976,10 +1976,24 @@ SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
19761976

19771977
// If IRGen failed without errors, the root cause may be a fatal
19781978
// Clang diagnostic.
1979-
if (expr_diagnostics->HasErrors() ||
1979+
using ErrorKind = SwiftASTContext::ScopedDiagnostics::ErrorKind;
1980+
// GetOptionalErrorKind() returns all diagnostics that occurred to during the
1981+
// lifetime of expr_diagnostics, but there could be earlier ClangImporter
1982+
// errors that still caused the expression to fail.
1983+
std::optional<ErrorKind> error_kind =
1984+
expr_diagnostics->GetOptionalErrorKind();
1985+
if (error_kind == ErrorKind::clang ||
19801986
m_swift_ast_ctx.HasClangImporterErrors()) {
1987+
diagnostic_manager.PutString(
1988+
eDiagnosticSeverityRemark,
1989+
"couldn't IRGen expression: Clang importer error");
1990+
DiagnoseSwiftASTContextError();
1991+
return ParseResult::unrecoverable_error;
1992+
}
1993+
1994+
if (error_kind == ErrorKind::swift) {
19811995
diagnostic_manager.PutString(eDiagnosticSeverityRemark,
1982-
"couldn't IRGen expression");
1996+
"couldn't IRGen expression: Swift error");
19831997
DiagnoseSwiftASTContextError();
19841998
return ParseResult::unrecoverable_error;
19851999
}

lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -922,19 +922,25 @@ void SwiftASTContext::ScopedDiagnostics::PrintDiagnostics(
922922
last_line);
923923
}
924924

925-
bool SwiftASTContext::ScopedDiagnostics::HasErrors() const {
925+
std::optional<SwiftASTContext::ScopedDiagnostics::ErrorKind>
926+
SwiftASTContext::ScopedDiagnostics::GetOptionalErrorKind() const {
927+
using ErrorKind = SwiftASTContext::ScopedDiagnostics::ErrorKind;
926928
auto &consumer = *static_cast<StoringDiagnosticConsumer *>(&m_consumer);
927929

928-
if (consumer.m_num_swift_errors > m_cursor.m_num_swift_errors)
929-
return true;
930930
if (consumer.m_raw_clang_diagnostics.size() > m_cursor.clang)
931-
return true;
931+
return ErrorKind::clang;
932+
if (consumer.m_num_swift_errors > m_cursor.m_num_swift_errors)
933+
return ErrorKind::swift;
932934

933935
for (size_t i = m_cursor.lldb; i < consumer.m_diagnostics.size(); ++i)
934936
if (consumer.m_diagnostics[i]->GetSeverity() == eDiagnosticSeverityError)
935-
return true;
937+
return ErrorKind::swift;
936938

937-
return false;
939+
return {};
940+
}
941+
942+
bool SwiftASTContext::ScopedDiagnostics::HasErrors() const {
943+
return GetOptionalErrorKind() != std::nullopt;
938944
}
939945

940946
llvm::Error SwiftASTContext::ScopedDiagnostics::GetAllErrors() const {

lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ class SwiftASTContext : public TypeSystemSwift {
472472
const DiagnosticCursor m_cursor;
473473

474474
public:
475+
enum class ErrorKind { swift, clang };
475476
ScopedDiagnostics(swift::DiagnosticConsumer &consumer);
476477
~ScopedDiagnostics();
477478
/// Print all diagnostics that happened during the lifetime of
@@ -481,6 +482,7 @@ class SwiftASTContext : public TypeSystemSwift {
481482
uint32_t bufferID = UINT32_MAX,
482483
uint32_t first_line = 0,
483484
uint32_t last_line = UINT32_MAX) const;
485+
std::optional<ErrorKind> GetOptionalErrorKind() const;
484486
bool HasErrors() const;
485487
/// Return all errors and warnings that happened during the lifetime of this
486488
/// object.

0 commit comments

Comments
 (0)