Skip to content

Commit 9c48cac

Browse files
committed
Unify all mechanisms for delaying diagnostics in SwiftASTContext (NFC)
1 parent 7e362b4 commit 9c48cac

File tree

3 files changed

+53
-72
lines changed

3 files changed

+53
-72
lines changed

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

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1974,31 +1974,18 @@ SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
19741974
// If IRGen failed without errors, the root cause may be a fatal
19751975
// Clang diagnostic.
19761976
if (m_swift_ast_ctx.HasErrors() || m_swift_ast_ctx.HasClangImporterErrors()) {
1977-
diagnostic_manager.Printf(eDiagnosticSeverityRemark,
1978-
"couldn't IRGen expression.");
1977+
diagnostic_manager.PutString(eDiagnosticSeverityRemark,
1978+
"couldn't IRGen expression.");
19791979
DiagnoseSwiftASTContextError();
19801980
return ParseResult::unrecoverable_error;
19811981
}
19821982

19831983
if (!m_module) {
1984-
auto &warnings = m_swift_ast_ctx.GetModuleImportWarnings();
1985-
for (StringRef message : warnings) {
1986-
// FIXME: Don't store diagnostics as strings.
1987-
auto severity = eDiagnosticSeverityWarning;
1988-
if (message.consume_front("warning: "))
1989-
severity = eDiagnosticSeverityWarning;
1990-
if (message.consume_front("error: "))
1991-
severity = eDiagnosticSeverityError;
1992-
diagnostic_manager.PutString(severity, message);
1993-
}
1994-
std::string error = "couldn't IRGen expression";
19951984
diagnostic_manager.Printf(
1996-
eDiagnosticSeverityError, "couldn't IRGen expression: %s",
1997-
warnings.empty()
1998-
? "Please enable the expression log by running \"log enable lldb "
1999-
"expr\", then run the failing expression again, and file a "
2000-
"bugreport with the log output."
2001-
: "Please check the above error messages for possible root causes.");
1985+
eDiagnosticSeverityError,
1986+
"couldn't IRGen expression. Please enable the expression log by "
1987+
"running \"log enable lldb expr\", then run the failing expression "
1988+
"again, and file a bug report with the log output.");
20021989
return ParseResult::unrecoverable_error;
20031990
}
20041991

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

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,8 +1052,8 @@ static void printASTValidationError(
10521052
}
10531053

10541054
void SwiftASTContext::DiagnoseWarnings(Process &process, Module &module) const {
1055-
for (const std::string &message : m_module_import_warnings)
1056-
process.PrintWarningCantLoadSwiftModule(module, message);
1055+
if (HasErrors() || HasClangImporterErrors())
1056+
process.PrintWarningCantLoadSwiftModule(module, GetAllErrors().AsCString());
10571057
}
10581058

10591059
/// Locate the swift-plugin-server for a plugin library,
@@ -1690,7 +1690,7 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
16901690
m_description, errs, got_serialized_options,
16911691
found_swift_modules)) {
16921692
// Validation errors are not fatal for the context.
1693-
swift_ast_sp->m_module_import_warnings.push_back(std::string(error));
1693+
swift_ast_sp->AddDiagnostic(eDiagnosticSeverityWarning, errs.str());
16941694
}
16951695

16961696
llvm::StringRef serialized_triple =
@@ -2973,7 +2973,6 @@ class StoringDiagnosticConsumer : public swift::DiagnosticConsumer {
29732973
return old;
29742974
}
29752975

2976-
/// This is only used by ReconstructTypes.
29772976
void AddDiagnostic(std::unique_ptr<Diagnostic> diagnostic) {
29782977
if (diagnostic)
29792978
m_diagnostics.push_back(std::move(diagnostic));
@@ -3062,19 +3061,16 @@ swift::ASTContext *SwiftASTContext::GetASTContext() {
30623061

30633062
// Handle any errors.
30643063
if (!clang_importer_ap || HasErrors()) {
3065-
std::string message;
3066-
if (!HasErrors()) {
3067-
message = "failed to create ClangImporter.";
3068-
m_module_import_warnings.push_back(message);
3069-
} else {
3064+
AddDiagnostic(eDiagnosticSeverityWarning,
3065+
"failed to create ClangImporter");
3066+
if (GetLog(LLDBLog::Types)) {
30703067
DiagnosticManager diagnostic_manager;
30713068
PrintDiagnostics(diagnostic_manager, true);
30723069
std::string underlying_error = diagnostic_manager.GetString();
3073-
message = "failed to initialize ClangImporter: ";
3074-
message += underlying_error;
3075-
m_module_import_warnings.push_back(underlying_error);
3070+
LOG_PRINTF(GetLog(LLDBLog::Types),
3071+
"failed to initialize ClangImporter: %s",
3072+
underlying_error.c_str());
30763073
}
3077-
LOG_PRINTF(GetLog(LLDBLog::Types), "%s", message.c_str());
30783074
}
30793075
if (clang_importer_ap)
30803076
moduleCachePath = swift::getModuleCachePathFromClang(
@@ -4127,10 +4123,9 @@ swift::TypeBase *
41274123
SwiftASTContext::ReconstructType(ConstString mangled_typename) {
41284124
Status error;
41294125

4130-
auto reconstructed_type = this->ReconstructType(mangled_typename, error);
4131-
if (!error.Success()) {
4132-
this->AddErrorStatusAsGenericDiagnostic(error);
4133-
}
4126+
auto reconstructed_type = ReconstructType(mangled_typename, error);
4127+
if (!error.Success())
4128+
AddDiagnostic(eDiagnosticSeverityWarning, error.AsCString());
41344129
return reconstructed_type;
41354130
}
41364131

@@ -4761,19 +4756,32 @@ uint32_t SwiftASTContext::GetPointerByteSize() {
47614756
return m_pointer_byte_size;
47624757
}
47634758

4764-
bool SwiftASTContext::HasErrors() {
4765-
if (m_diagnostic_consumer_ap.get())
4766-
return (
4767-
static_cast<StoringDiagnosticConsumer *>(m_diagnostic_consumer_ap.get())
4768-
->NumErrors() != 0);
4769-
return false;
4759+
bool SwiftASTContext::HasErrors() const {
4760+
if (!m_diagnostic_consumer_ap)
4761+
return false;
4762+
return (
4763+
static_cast<StoringDiagnosticConsumer *>(m_diagnostic_consumer_ap.get())
4764+
->NumErrors() != 0);
47704765
}
4771-
bool SwiftASTContext::HasClangImporterErrors() {
4772-
if (m_diagnostic_consumer_ap.get())
4773-
return (
4774-
static_cast<StoringDiagnosticConsumer *>(m_diagnostic_consumer_ap.get())
4775-
->NumClangErrors() != 0);
4776-
return false;
4766+
bool SwiftASTContext::HasClangImporterErrors() const {
4767+
if (!m_diagnostic_consumer_ap)
4768+
return false;
4769+
return (
4770+
static_cast<StoringDiagnosticConsumer *>(m_diagnostic_consumer_ap.get())
4771+
->NumClangErrors() != 0);
4772+
}
4773+
4774+
void SwiftASTContext::AddDiagnostic(DiagnosticSeverity severity,
4775+
llvm::StringRef message) {
4776+
assert(m_diagnostic_consumer_ap.get());
4777+
HEALTH_LOG_PRINTF("%s", message.str().c_str());
4778+
if (!m_diagnostic_consumer_ap.get())
4779+
return;
4780+
4781+
auto diagnostic = std::make_unique<Diagnostic>(
4782+
message, severity, eDiagnosticOriginLLDB, LLDB_INVALID_COMPILER_ID);
4783+
static_cast<StoringDiagnosticConsumer *>(m_diagnostic_consumer_ap.get())
4784+
->AddDiagnostic(std::move(diagnostic));
47774785
}
47784786

47794787
bool SwiftASTContext::HasFatalErrors(swift::ASTContext *ast_context) {
@@ -4795,17 +4803,6 @@ bool SwiftASTContext::SetColorizeDiagnostics(bool b) {
47954803
return false;
47964804
}
47974805

4798-
void SwiftASTContext::AddErrorStatusAsGenericDiagnostic(Status error) {
4799-
assert(!error.Success() && "status should be in an error state");
4800-
4801-
auto diagnostic = std::make_unique<Diagnostic>(
4802-
error.AsCString(), eDiagnosticSeverityError, eDiagnosticOriginLLDB,
4803-
LLDB_INVALID_COMPILER_ID);
4804-
if (m_diagnostic_consumer_ap.get())
4805-
static_cast<StoringDiagnosticConsumer *>(m_diagnostic_consumer_ap.get())
4806-
->AddDiagnostic(std::move(diagnostic));
4807-
}
4808-
48094806
void SwiftASTContext::PrintDiagnostics(DiagnosticManager &diagnostic_manager,
48104807
uint32_t bufferID, uint32_t first_line,
48114808
uint32_t last_line) const {

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

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,19 @@
1616
#include "Plugins/ExpressionParser/Swift/SwiftPersistentExpressionState.h"
1717
#include "Plugins/TypeSystem/Swift/TypeSystemSwift.h"
1818
#include "Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h"
19-
#include "swift/SymbolGraphGen/SymbolGraphOptions.h"
20-
#include "swift/Parse/ParseVersion.h"
19+
2120
#include "lldb/Core/SwiftForward.h"
2221
#include "lldb/Core/ThreadSafeDenseSet.h"
22+
#include "lldb/Expression/DiagnosticManager.h"
2323
#include "lldb/Utility/Either.h"
24+
25+
#include "swift/Parse/ParseVersion.h"
26+
#include "swift/SymbolGraphGen/SymbolGraphOptions.h"
27+
2428
#include "llvm/ADT/SmallVector.h"
2529
#include "llvm/ADT/StringRef.h"
2630
#include "llvm/Target/TargetOptions.h"
31+
2732
#include <memory>
2833

2934
namespace swift {
@@ -425,9 +430,10 @@ class SwiftASTContext : public TypeSystemSwift {
425430

426431
/// Error handling
427432
/// \{
428-
bool HasErrors();
429-
bool HasClangImporterErrors();
433+
bool HasErrors() const;
434+
bool HasClangImporterErrors() const;
430435

436+
void AddDiagnostic(DiagnosticSeverity severity, llvm::StringRef message);
431437
void RaiseFatalError(std::string msg) { m_fatal_errors.SetErrorString(msg); }
432438
static bool HasFatalErrors(swift::ASTContext *ast_context);
433439
bool HasFatalErrors() const {
@@ -445,7 +451,6 @@ class SwiftASTContext : public TypeSystemSwift {
445451
void ClearDiagnostics();
446452

447453
bool SetColorizeDiagnostics(bool b);
448-
void AddErrorStatusAsGenericDiagnostic(Status error);
449454

450455
void PrintDiagnostics(DiagnosticManager &diagnostic_manager,
451456
uint32_t bufferID = UINT32_MAX, uint32_t first_line = 0,
@@ -470,11 +475,6 @@ class SwiftASTContext : public TypeSystemSwift {
470475

471476
const SwiftModuleMap &GetModuleCache() { return m_swift_module_cache; }
472477

473-
/// Return a list of warnings collected from ClangImporter.
474-
const std::vector<std::string> &GetModuleImportWarnings() const {
475-
return m_module_import_warnings;
476-
}
477-
478478
const swift::irgen::TypeInfo *
479479
GetSwiftTypeInfo(lldb::opaque_compiler_type_t type);
480480

@@ -866,9 +866,6 @@ class SwiftASTContext : public TypeSystemSwift {
866866
llvm::once_flag m_ir_gen_module_once;
867867
std::unique_ptr<swift::DiagnosticConsumer> m_diagnostic_consumer_ap;
868868
std::unique_ptr<swift::DependencyTracker> m_dependency_tracker;
869-
/// A collection of (not necessarily fatal) error messages that
870-
/// should be printed by Process::PrintWarningCantLoadSwift().
871-
std::vector<std::string> m_module_import_warnings;
872869
swift::ModuleDecl *m_scratch_module = nullptr;
873870
std::unique_ptr<swift::Lowering::TypeConverter> m_sil_types_ap;
874871
std::unique_ptr<swift::SILModule> m_sil_module_ap;

0 commit comments

Comments
 (0)