#include "llvm/Support/YAMLTraits.h"
#include <vector>
-LLVM_YAML_IS_SEQUENCE_VECTOR(std::string)
+namespace llvm {
+namespace yaml {
+
+/// A wrapper around std::string which contains a source range that's being
+/// set during parsing.
+struct StringValue {
+ std::string Value;
+ SMRange SourceRange;
+
+ StringValue() {}
+ StringValue(std::string Value) : Value(std::move(Value)) {}
+
+ bool operator==(const StringValue &Other) const {
+ return Value == Other.Value;
+ }
+};
+
+template <> struct ScalarTraits<StringValue> {
+ static void output(const StringValue &S, void *, llvm::raw_ostream &OS) {
+ OS << S.Value;
+ }
+
+ static StringRef input(StringRef Scalar, void *Ctx, StringValue &S) {
+ S.Value = Scalar.str();
+ if (const auto *Node =
+ reinterpret_cast<yaml::Input *>(Ctx)->getCurrentNode())
+ S.SourceRange = Node->getSourceRange();
+ return "";
+ }
+
+ static bool mustQuote(StringRef Scalar) { return needsQuotes(Scalar); }
+};
+
+} // end namespace yaml
+} // end namespace llvm
+
+LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::StringValue)
namespace llvm {
namespace yaml {
bool AddressTaken = false;
// TODO: Serialize the successors and liveins.
- std::vector<std::string> Instructions;
+ std::vector<StringValue> Instructions;
};
template <> struct MappingTraits<MachineBasicBlock> {
const yaml::MachineBasicBlock &YamlMBB);
private:
+ /// Return a MIR diagnostic converted from an MI string diagnostic.
+ SMDiagnostic diagFromMIStringDiag(const SMDiagnostic &Error,
+ SMRange SourceRange);
+
/// Return a MIR diagnostic converted from an LLVM assembly diagnostic.
SMDiagnostic diagFromLLVMAssemblyDiag(const SMDiagnostic &Error,
SMRange SourceRange);
std::unique_ptr<Module> MIRParserImpl::parse() {
yaml::Input In(SM.getMemoryBuffer(SM.getMainFileID())->getBuffer(),
/*Ctxt=*/nullptr, handleYAMLDiag, this);
+ In.setContext(&In);
if (!In.setCurrentDocument()) {
if (In.error())
// Parse the instructions.
for (const auto &MISource : YamlMBB.Instructions) {
SMDiagnostic Error;
- if (auto *MI = parseMachineInstr(SM, MF, MISource, Error)) {
+ if (auto *MI = parseMachineInstr(SM, MF, MISource.Value, Error)) {
MBB.insert(MBB.end(), MI);
continue;
}
- reportDiagnostic(Error);
+ reportDiagnostic(diagFromMIStringDiag(Error, MISource.SourceRange));
return true;
}
return false;
}
+SMDiagnostic MIRParserImpl::diagFromMIStringDiag(const SMDiagnostic &Error,
+ SMRange SourceRange) {
+ assert(SourceRange.isValid() && "Invalid source range");
+ SMLoc Loc = SourceRange.Start;
+ bool HasQuote = Loc.getPointer() < SourceRange.End.getPointer() &&
+ *Loc.getPointer() == '\'';
+ // Translate the location of the error from the location in the MI string to
+ // the corresponding location in the MIR file.
+ Loc = Loc.getFromPointer(Loc.getPointer() + Error.getColumnNo() +
+ (HasQuote ? 1 : 0));
+
+ // TODO: Translate any source ranges as well.
+ return SM.GetMessage(Loc, Error.getKind(), Error.getMessage(), None,
+ Error.getFixIts());
+}
+
SMDiagnostic MIRParserImpl::diagFromLLVMAssemblyDiag(const SMDiagnostic &Error,
SMRange SourceRange) {
assert(SourceRange.isValid());