Skip to content

Commit 62262a2

Browse files
committed
[NFC] Improve deserialization for invalid attrs
As well as adding a filename to the message, if one is available.
1 parent 48e8b3e commit 62262a2

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

lib/Serialization/Deserialization.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ const char ExtensionError::ID = '\0';
149149
void ExtensionError::anchor() {}
150150
const char DeclAttributesDidNotMatch::ID = '\0';
151151
void DeclAttributesDidNotMatch::anchor() {}
152+
const char InvalidRecordKindError::ID = '\0';
153+
void InvalidRecordKindError::anchor() {}
152154

153155
/// Skips a single record in the bitstream.
154156
///
@@ -432,7 +434,7 @@ SILLayout *ModuleFile::readSILLayout(llvm::BitstreamCursor &Cursor) {
432434
return SILLayout::get(getContext(), canSig, fields, capturesGenerics);
433435
}
434436
default:
435-
fatal();
437+
fatal(llvm::make_error<InvalidRecordKindError>(kind));
436438
}
437439
}
438440

@@ -503,7 +505,7 @@ ProtocolConformanceDeserializer::read(
503505

504506
// Not a protocol conformance.
505507
default:
506-
MF.fatal();
508+
MF.fatal(llvm::make_error<InvalidRecordKindError>(kind));
507509
}
508510
}
509511

@@ -5062,7 +5064,7 @@ llvm::Error DeclDeserializer::deserializeDeclCommon() {
50625064

50635065
default:
50645066
// We don't know how to deserialize this kind of attribute.
5065-
MF.fatal();
5067+
MF.fatal(llvm::make_error<InvalidRecordKindError>(recordID));
50665068
}
50675069

50685070
if (!skipAttr) {
@@ -5188,7 +5190,7 @@ DeclDeserializer::getDeclCheckedImpl(
51885190

51895191
default:
51905192
// We don't know how to deserialize this kind of decl.
5191-
MF.fatal();
5193+
MF.fatal(llvm::make_error<InvalidRecordKindError>(recordID));
51925194
}
51935195
}
51945196

lib/Serialization/DeserializationErrors.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,32 @@ class DeclAttributesDidNotMatch : public llvm::ErrorInfo<DeclAttributesDidNotMat
445445
}
446446
};
447447

448+
class InvalidRecordKindError :
449+
public llvm::ErrorInfo<InvalidRecordKindError, DeclDeserializationError> {
450+
friend ErrorInfo;
451+
static const char ID;
452+
void anchor() override;
453+
454+
unsigned recordKind;
455+
456+
public:
457+
explicit InvalidRecordKindError(unsigned kind) {
458+
this->recordKind = kind;
459+
}
460+
461+
void log(raw_ostream &OS) const override {
462+
OS << "don't know how to deserialize record with code " << recordKind;
463+
if (recordKind >= decls_block::SILGenName_DECL_ATTR)
464+
OS << " (attribute kind "
465+
<< recordKind - decls_block::SILGenName_DECL_ATTR << ")";
466+
OS << "; this may be a compiler bug";
467+
}
468+
469+
std::error_code convertToErrorCode() const override {
470+
return llvm::inconvertibleErrorCode();
471+
}
472+
};
473+
448474
LLVM_NODISCARD
449475
static inline std::unique_ptr<llvm::ErrorInfoBase>
450476
takeErrorInfo(llvm::Error error) {

lib/Serialization/ModuleFileSharedCore.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,8 @@ void ModuleFileSharedCore::outputDiagnosticInfo(llvm::raw_ostream &os) const {
582582
<< "'";
583583
if (Bits.IsAllowModuleWithCompilerErrorsEnabled)
584584
os << " (built with -experimental-allow-module-with-compiler-errors)";
585+
if (ModuleInputBuffer)
586+
os << " at '" << ModuleInputBuffer->getBufferIdentifier() << "'";
585587
}
586588

587589
ModuleFileSharedCore::~ModuleFileSharedCore() { }

0 commit comments

Comments
 (0)