Skip to content

Commit 0718eea

Browse files
committed
Improve reporting when type reconstruction fails
1 parent ab1ec7c commit 0718eea

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed

lldb/include/lldb/Symbol/SwiftASTContext.h

+2
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,7 @@ class SwiftASTContext : public TypeSystemSwift {
612612

613613
/// Reconstruct a Swift AST type from a mangled name by looking its
614614
/// components up in Swift modules.
615+
swift::TypeBase *ReconstructType(ConstString mangled_typename);
615616
swift::TypeBase *ReconstructType(ConstString mangled_typename, Status &error);
616617
CompilerType GetTypeFromMangledTypename(ConstString mangled_typename);
617618

@@ -667,6 +668,7 @@ class SwiftASTContext : public TypeSystemSwift {
667668
void ClearDiagnostics();
668669

669670
bool SetColorizeDiagnostics(bool b);
671+
void AddErrorStatusAsGenericDiagnostic(Status error);
670672

671673
void PrintDiagnostics(DiagnosticManager &diagnostic_manager,
672674
uint32_t bufferID = UINT32_MAX, uint32_t first_line = 0,

lldb/source/Symbol/SwiftASTContext.cpp

+46-13
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,10 @@ swift::Type TypeSystemSwiftTypeRef::GetSwiftType(CompilerType compiler_type) {
191191
if (!ts)
192192
return {};
193193

194-
Status error;
195194
// FIXME: Suboptimal performance, because the ConstString is looked up again.
196195
ConstString mangled_name(
197196
reinterpret_cast<const char *>(compiler_type.GetOpaqueQualType()));
198-
return ts->m_swift_ast_context->ReconstructType(mangled_name, error);
197+
return ts->m_swift_ast_context->ReconstructType(mangled_name);
199198
}
200199

201200
swift::Type SwiftASTContext::GetSwiftType(CompilerType compiler_type) {
@@ -2853,8 +2852,8 @@ class ANSIColorStringStream : public llvm::raw_string_ostream {
28532852
class StoringDiagnosticConsumer : public swift::DiagnosticConsumer {
28542853
public:
28552854
StoringDiagnosticConsumer(SwiftASTContext &ast_context)
2856-
: m_ast_context(ast_context), m_diagnostics(), m_num_errors(0),
2857-
m_colorize(false) {
2855+
: m_ast_context(ast_context), m_raw_diagnostics(), m_diagnostics(),
2856+
m_num_errors(0), m_colorize(false) {
28582857
m_ast_context.GetDiagnosticEngine().resetHadAnyError();
28592858
m_ast_context.GetDiagnosticEngine().addConsumer(*this);
28602859
}
@@ -2926,19 +2925,19 @@ class StoringDiagnosticConsumer : public swift::DiagnosticConsumer {
29262925
std::string &message_ref = os.str();
29272926

29282927
if (message_ref.empty())
2929-
m_diagnostics.push_back(RawDiagnostic(
2928+
m_raw_diagnostics.push_back(RawDiagnostic(
29302929
text.str(), info.Kind, bufferName, bufferID, line_col.first,
29312930
line_col.second,
29322931
use_fixits ? info.FixIts
29332932
: llvm::ArrayRef<swift::Diagnostic::FixIt>()));
29342933
else
2935-
m_diagnostics.push_back(RawDiagnostic(
2934+
m_raw_diagnostics.push_back(RawDiagnostic(
29362935
message_ref, info.Kind, bufferName, bufferID, line_col.first,
29372936
line_col.second,
29382937
use_fixits ? info.FixIts
29392938
: llvm::ArrayRef<swift::Diagnostic::FixIt>()));
29402939
} else {
2941-
m_diagnostics.push_back(RawDiagnostic(
2940+
m_raw_diagnostics.push_back(RawDiagnostic(
29422941
text.str(), info.Kind, bufferName, bufferID, line_col.first,
29432942
line_col.second, llvm::ArrayRef<swift::Diagnostic::FixIt>()));
29442943
}
@@ -2949,6 +2948,7 @@ class StoringDiagnosticConsumer : public swift::DiagnosticConsumer {
29492948

29502949
void Clear() {
29512950
m_ast_context.GetDiagnosticEngine().resetHadAnyError();
2951+
m_raw_diagnostics.clear();
29522952
m_diagnostics.clear();
29532953
m_num_errors = 0;
29542954
}
@@ -2980,8 +2980,13 @@ class StoringDiagnosticConsumer : public swift::DiagnosticConsumer {
29802980
void PrintDiagnostics(DiagnosticManager &diagnostic_manager,
29812981
uint32_t bufferID = UINT32_MAX, uint32_t first_line = 0,
29822982
uint32_t last_line = UINT32_MAX) {
2983-
bool added_one_diagnostic = false;
2984-
for (const RawDiagnostic &diagnostic : m_diagnostics) {
2983+
bool added_one_diagnostic = !m_diagnostics.empty();
2984+
2985+
for (std::unique_ptr<Diagnostic> &diagnostic : m_diagnostics) {
2986+
diagnostic_manager.AddDiagnostic(std::move(diagnostic));
2987+
}
2988+
2989+
for (const RawDiagnostic &diagnostic : m_raw_diagnostics) {
29852990
// We often make expressions and wrap them in some code. When
29862991
// we see errors we want the line numbers to be correct so we
29872992
// correct them below. LLVM stores in SourceLoc objects as
@@ -3053,7 +3058,7 @@ class StoringDiagnosticConsumer : public swift::DiagnosticConsumer {
30533058
// This will report diagnostic errors from outside the
30543059
// expression's source range. Those are not interesting to
30553060
// users, so we only emit them in debug builds.
3056-
for (const RawDiagnostic &diagnostic : m_diagnostics) {
3061+
for (const RawDiagnostic &diagnostic : m_raw_diagnostics) {
30573062
const DiagnosticSeverity severity = SeverityForKind(diagnostic.kind);
30583063
const DiagnosticOrigin origin = eDiagnosticOriginSwift;
30593064
diagnostic_manager.AddDiagnostic(diagnostic.description.c_str(),
@@ -3070,6 +3075,10 @@ class StoringDiagnosticConsumer : public swift::DiagnosticConsumer {
30703075
return old;
30713076
}
30723077

3078+
void AddDiagnostic(std::unique_ptr<Diagnostic> diagnostic) {
3079+
m_diagnostics.push_back(std::move(diagnostic));
3080+
}
3081+
30733082
private:
30743083
// We don't currently use lldb_private::Diagostic or any of the lldb
30753084
// DiagnosticManager machinery to store diagnostics as they
@@ -3095,9 +3104,12 @@ class StoringDiagnosticConsumer : public swift::DiagnosticConsumer {
30953104
std::vector<swift::DiagnosticInfo::FixIt> fixits;
30963105
};
30973106
typedef std::vector<RawDiagnostic> RawDiagnosticBuffer;
3107+
typedef std::vector<std::unique_ptr<Diagnostic>> DiagnosticList;
30983108

30993109
SwiftASTContext &m_ast_context;
3100-
RawDiagnosticBuffer m_diagnostics;
3110+
RawDiagnosticBuffer m_raw_diagnostics;
3111+
DiagnosticList m_diagnostics;
3112+
31013113
unsigned m_num_errors = 0;
31023114
bool m_colorize;
31033115
};
@@ -4360,9 +4372,8 @@ swift::Type convertSILFunctionTypesToASTFunctionTypes(swift::Type t) {
43604372

43614373
CompilerType
43624374
SwiftASTContext::GetTypeFromMangledTypename(ConstString mangled_typename) {
4363-
Status error;
43644375
if (llvm::isa<SwiftASTContextForExpressions>(this))
4365-
return GetCompilerType(ReconstructType(mangled_typename, error));
4376+
return GetCompilerType(ReconstructType(mangled_typename));
43664377
return GetCompilerType(mangled_typename);
43674378
}
43684379

@@ -4410,6 +4421,17 @@ CompilerType SwiftASTContext::GetAsClangType(ConstString mangled_name) {
44104421
return clang_type;
44114422
}
44124423

4424+
swift::TypeBase *SwiftASTContext::ReconstructType(ConstString mangled_typename) {
4425+
Status error;
4426+
4427+
auto reconstructed_type =
4428+
this->ReconstructType(mangled_typename, error);
4429+
if (!error.Success()) {
4430+
this->AddErrorStatusAsGenericDiagnostic(error);
4431+
}
4432+
return reconstructed_type;
4433+
}
4434+
44134435
swift::TypeBase *SwiftASTContext::ReconstructType(ConstString mangled_typename,
44144436
Status &error) {
44154437
VALID_OR_RETURN(nullptr);
@@ -5001,6 +5023,17 @@ bool SwiftASTContext::SetColorizeDiagnostics(bool b) {
50015023
return false;
50025024
}
50035025

5026+
void SwiftASTContext::AddErrorStatusAsGenericDiagnostic(Status error) {
5027+
assert(!error.Success() && "status should be in an error state");
5028+
5029+
auto diagnostic = std::make_unique<Diagnostic>(
5030+
error.AsCString(), eDiagnosticSeverityError, eDiagnosticOriginLLDB,
5031+
LLDB_INVALID_COMPILER_ID);
5032+
if (m_diagnostic_consumer_ap.get())
5033+
static_cast<StoringDiagnosticConsumer *>(m_diagnostic_consumer_ap.get())
5034+
->AddDiagnostic(std::move(diagnostic));
5035+
}
5036+
50045037
void SwiftASTContext::PrintDiagnostics(DiagnosticManager &diagnostic_manager,
50055038
uint32_t bufferID, uint32_t first_line,
50065039
uint32_t last_line) {

0 commit comments

Comments
 (0)