Index: lib/CodeGen/MIRParser/MIRParser.cpp =================================================================== --- lib/CodeGen/MIRParser/MIRParser.cpp +++ lib/CodeGen/MIRParser/MIRParser.cpp @@ -42,6 +42,9 @@ MIRParserImpl(std::unique_ptr Contents, StringRef Filename, LLVMContext &Context); + /// Return an error at an unknown location with the given message. + SMDiagnostic error(const Twine &Msg); + /// Try to parse the optional LLVM module and the machine functions in the MIR /// file. /// @@ -51,7 +54,7 @@ /// Parse the machine function in the current YAML document. /// /// Return true if an error occurred. - bool parseMachineFunction(yaml::Input &In); + bool parseMachineFunction(yaml::Input &In, SMDiagnostic &Error); /// Initialize the machine function to the state that's described in the MIR /// file. @@ -68,6 +71,10 @@ SM.AddNewSourceBuffer(std::move(Contents), SMLoc()); } +SMDiagnostic MIRParserImpl::error(const Twine &Msg) { + return SM.GetMessage(SMLoc(), SourceMgr::DK_Error, Msg); +} + static void handleYAMLDiag(const SMDiagnostic &Diag, void *Context) { *reinterpret_cast(Context) = Diag; } @@ -102,7 +109,7 @@ // Parse the machine functions. do { - if (parseMachineFunction(In)) + if (parseMachineFunction(In, Error)) return nullptr; In.nextDocument(); } while (In.setCurrentDocument()); @@ -110,13 +117,20 @@ return M; } -bool MIRParserImpl::parseMachineFunction(yaml::Input &In) { +bool MIRParserImpl::parseMachineFunction(yaml::Input &In, SMDiagnostic &Error) { auto MF = llvm::make_unique(); yaml::yamlize(In, *MF, false); if (In.error()) return true; auto FunctionName = MF->Name; - Functions.insert(std::make_pair(FunctionName, std::move(MF))); + if (!FunctionName.empty()) { + if (Functions.find(FunctionName) != Functions.end()) { + Error = error(Twine("redefinition of machine function '") + FunctionName + + "'"); + return true; + } + Functions.insert(std::make_pair(FunctionName, std::move(MF))); + } return false; } Index: test/CodeGen/MIR/machine-function-redefinition-error.mir =================================================================== --- /dev/null +++ test/CodeGen/MIR/machine-function-redefinition-error.mir @@ -0,0 +1,10 @@ +# RUN: not llc -start-after branch-folder -stop-after branch-folder -o /dev/null %s 2>&1 | FileCheck %s +# This test ensures that the machine function errors are reported correctly. + +--- +name: foo +... +--- +# CHECK: error: redefinition of machine function 'foo' +name: foo +...