diff --git a/llvm/lib/CodeGen/MIRParser/MIRParser.cpp b/llvm/lib/CodeGen/MIRParser/MIRParser.cpp index 2d8613b413111..7dccd5da9c9a9 100644 --- a/llvm/lib/CodeGen/MIRParser/MIRParser.cpp +++ b/llvm/lib/CodeGen/MIRParser/MIRParser.cpp @@ -51,9 +51,9 @@ namespace llvm { /// file. class MIRParserImpl { SourceMgr SM; + LLVMContext &Context; yaml::Input In; StringRef Filename; - LLVMContext &Context; SlotMapping IRSlots; std::unique_ptr Target; @@ -176,10 +176,11 @@ MIRParserImpl::MIRParserImpl(std::unique_ptr Contents, StringRef Filename, LLVMContext &Context, std::function Callback) : SM(), + Context(Context), In(SM.getMemoryBuffer(SM.AddNewSourceBuffer(std::move(Contents), SMLoc())) ->getBuffer(), nullptr, handleYAMLDiag, this), - Filename(Filename), Context(Context), ProcessIRFunction(Callback) { + Filename(Filename), ProcessIRFunction(Callback) { In.setContext(&In); } diff --git a/llvm/test/CodeGen/MIR/Generic/first-character-parse-error.mir b/llvm/test/CodeGen/MIR/Generic/first-character-parse-error.mir new file mode 100644 index 0000000000000..00a01058dc8cb --- /dev/null +++ b/llvm/test/CodeGen/MIR/Generic/first-character-parse-error.mir @@ -0,0 +1,14 @@ +:# RUN: not llc -run-pass=none %s -o - 2>&1 | FileCheck %s + +# The : before the run comment is syntactically invalid. This used to +# crash in the SourceMgr diagnostic printer because it was called +# before the LLVMContext was initialized. + +# CHECK: error: YAML:1:1: Unrecognized character while tokenizing. + +--- +name: foo +body: | + bb.0: + +...