diff --git a/llvm/include/llvm/Support/YAMLParser.h b/llvm/include/llvm/Support/YAMLParser.h --- a/llvm/include/llvm/Support/YAMLParser.h +++ b/llvm/include/llvm/Support/YAMLParser.h @@ -102,6 +102,8 @@ void printError(Node *N, const Twine &Msg, SourceMgr::DiagKind Kind = SourceMgr::DK_Error); + void printError(const SMRange &Range, const Twine &Msg, + SourceMgr::DiagKind Kind = SourceMgr::DK_Error); private: friend class Document; diff --git a/llvm/include/llvm/Support/YAMLTraits.h b/llvm/include/llvm/Support/YAMLTraits.h --- a/llvm/include/llvm/Support/YAMLTraits.h +++ b/llvm/include/llvm/Support/YAMLTraits.h @@ -18,6 +18,7 @@ #include "llvm/Support/Allocator.h" #include "llvm/Support/Endian.h" #include "llvm/Support/Regex.h" +#include "llvm/Support/SMLoc.h" #include "llvm/Support/SourceMgr.h" #include "llvm/Support/VersionTuple.h" #include "llvm/Support/YAMLParser.h" @@ -1471,9 +1472,10 @@ static bool classof(const MapHNode *) { return true; } - using NameToNode = StringMap>; + using NameToNodeAndLoc = + StringMap, SMRange>>; - NameToNode Mapping; + NameToNodeAndLoc Mapping; SmallVector ValidKeys; }; @@ -1495,9 +1497,11 @@ std::unique_ptr createHNodes(Node *node); void setError(HNode *hnode, const Twine &message); void setError(Node *node, const Twine &message); + void setError(const SMRange &Range, const Twine &message); void reportWarning(HNode *hnode, const Twine &message); void reportWarning(Node *hnode, const Twine &message); + void reportWarning(const SMRange &Range, const Twine &message); public: // These are only used by operator>>. They could be private diff --git a/llvm/lib/Support/YAMLParser.cpp b/llvm/lib/Support/YAMLParser.cpp --- a/llvm/lib/Support/YAMLParser.cpp +++ b/llvm/lib/Support/YAMLParser.cpp @@ -1774,7 +1774,11 @@ bool Stream::failed() { return scanner->failed(); } void Stream::printError(Node *N, const Twine &Msg, SourceMgr::DiagKind Kind) { - SMRange Range = N ? N->getSourceRange() : SMRange(); + printError(N ? N->getSourceRange() : SMRange(), Msg, Kind); +} + +void Stream::printError(const SMRange &Range, const Twine &Msg, + SourceMgr::DiagKind Kind) { scanner->printError(Range.Start, Kind, Msg, Range); } diff --git a/llvm/lib/Support/YAMLTraits.cpp b/llvm/lib/Support/YAMLTraits.cpp --- a/llvm/lib/Support/YAMLTraits.cpp +++ b/llvm/lib/Support/YAMLTraits.cpp @@ -175,7 +175,7 @@ return false; } MN->ValidKeys.push_back(Key); - HNode *Value = MN->Mapping[Key].get(); + HNode *Value = MN->Mapping[Key].first.get(); if (!Value) { if (Required) setError(CurrentNode, Twine("missing required key '") + Key + "'"); @@ -201,12 +201,12 @@ return; for (const auto &NN : MN->Mapping) { if (!is_contained(MN->ValidKeys, NN.first())) { - HNode *ReportNode = NN.second.get(); + const SMRange &ReportLoc = NN.second.second; if (!AllowUnknownKeys) { - setError(ReportNode, Twine("unknown key '") + NN.first() + "'"); + setError(ReportLoc, Twine("unknown key '") + NN.first() + "'"); break; } else - reportWarning(ReportNode, Twine("unknown key '") + NN.first() + "'"); + reportWarning(ReportLoc, Twine("unknown key '") + NN.first() + "'"); } } } @@ -378,11 +378,24 @@ EC = make_error_code(errc::invalid_argument); } +void Input::setError(const SMRange &range, const Twine &message) { + Strm->printError(range, message); + EC = make_error_code(errc::invalid_argument); +} + void Input::reportWarning(HNode *hnode, const Twine &message) { assert(hnode && "HNode must not be NULL"); Strm->printError(hnode->_node, message, SourceMgr::DK_Warning); } +void Input::reportWarning(Node *node, const Twine &message) { + Strm->printError(node, message, SourceMgr::DK_Warning); +} + +void Input::reportWarning(const SMRange &range, const Twine &message) { + Strm->printError(range, message, SourceMgr::DK_Warning); +} + std::unique_ptr Input::createHNodes(Node *N) { SmallString<128> StringStorage; if (ScalarNode *SN = dyn_cast(N)) { @@ -426,7 +439,8 @@ auto ValueHNode = createHNodes(Value); if (EC) break; - mapHNode->Mapping[KeyStr] = std::move(ValueHNode); + mapHNode->Mapping[KeyStr] = + std::make_pair(std::move(ValueHNode), KeyNode->getSourceRange()); } return std::move(mapHNode); } else if (isa(N)) {