Skip to content

Commit d380c38

Browse files
committed
[YAML] Use correct source location for unknown key errors.
Currently unknown keys when inputting mapping traits have the location set to the Value. Example: ``` YAML:1:14: error: unknown key 'UnknownKey' {UnknownKey: SomeValue} ^~~~~~~~~ ``` This is unhelpful for a user as it draws them to fix the wrong item. Reviewed By: silvas Differential Revision: https://reviews.llvm.org/D93037
1 parent cf638f8 commit d380c38

12 files changed

+45
-21
lines changed

llvm/include/llvm/Support/YAMLParser.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ class Stream {
102102

103103
void printError(Node *N, const Twine &Msg,
104104
SourceMgr::DiagKind Kind = SourceMgr::DK_Error);
105+
void printError(const SMRange &Range, const Twine &Msg,
106+
SourceMgr::DiagKind Kind = SourceMgr::DK_Error);
105107

106108
private:
107109
friend class Document;

llvm/include/llvm/Support/YAMLTraits.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "llvm/Support/Allocator.h"
1919
#include "llvm/Support/Endian.h"
2020
#include "llvm/Support/Regex.h"
21+
#include "llvm/Support/SMLoc.h"
2122
#include "llvm/Support/SourceMgr.h"
2223
#include "llvm/Support/VersionTuple.h"
2324
#include "llvm/Support/YAMLParser.h"
@@ -1471,9 +1472,10 @@ class Input : public IO {
14711472

14721473
static bool classof(const MapHNode *) { return true; }
14731474

1474-
using NameToNode = StringMap<std::unique_ptr<HNode>>;
1475+
using NameToNodeAndLoc =
1476+
StringMap<std::pair<std::unique_ptr<HNode>, SMRange>>;
14751477

1476-
NameToNode Mapping;
1478+
NameToNodeAndLoc Mapping;
14771479
SmallVector<std::string, 6> ValidKeys;
14781480
};
14791481

@@ -1495,9 +1497,11 @@ class Input : public IO {
14951497
std::unique_ptr<Input::HNode> createHNodes(Node *node);
14961498
void setError(HNode *hnode, const Twine &message);
14971499
void setError(Node *node, const Twine &message);
1500+
void setError(const SMRange &Range, const Twine &message);
14981501

14991502
void reportWarning(HNode *hnode, const Twine &message);
15001503
void reportWarning(Node *hnode, const Twine &message);
1504+
void reportWarning(const SMRange &Range, const Twine &message);
15011505

15021506
public:
15031507
// These are only used by operator>>. They could be private

llvm/lib/Support/YAMLParser.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1774,7 +1774,11 @@ Stream::~Stream() = default;
17741774
bool Stream::failed() { return scanner->failed(); }
17751775

17761776
void Stream::printError(Node *N, const Twine &Msg, SourceMgr::DiagKind Kind) {
1777-
SMRange Range = N ? N->getSourceRange() : SMRange();
1777+
printError(N ? N->getSourceRange() : SMRange(), Msg, Kind);
1778+
}
1779+
1780+
void Stream::printError(const SMRange &Range, const Twine &Msg,
1781+
SourceMgr::DiagKind Kind) {
17781782
scanner->printError(Range.Start, Kind, Msg, Range);
17791783
}
17801784

llvm/lib/Support/YAMLTraits.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
175175
return false;
176176
}
177177
MN->ValidKeys.push_back(Key);
178-
HNode *Value = MN->Mapping[Key].get();
178+
HNode *Value = MN->Mapping[Key].first.get();
179179
if (!Value) {
180180
if (Required)
181181
setError(CurrentNode, Twine("missing required key '") + Key + "'");
@@ -201,12 +201,12 @@ void Input::endMapping() {
201201
return;
202202
for (const auto &NN : MN->Mapping) {
203203
if (!is_contained(MN->ValidKeys, NN.first())) {
204-
HNode *ReportNode = NN.second.get();
204+
const SMRange &ReportLoc = NN.second.second;
205205
if (!AllowUnknownKeys) {
206-
setError(ReportNode, Twine("unknown key '") + NN.first() + "'");
206+
setError(ReportLoc, Twine("unknown key '") + NN.first() + "'");
207207
break;
208208
} else
209-
reportWarning(ReportNode, Twine("unknown key '") + NN.first() + "'");
209+
reportWarning(ReportLoc, Twine("unknown key '") + NN.first() + "'");
210210
}
211211
}
212212
}
@@ -378,11 +378,24 @@ void Input::setError(Node *node, const Twine &message) {
378378
EC = make_error_code(errc::invalid_argument);
379379
}
380380

381+
void Input::setError(const SMRange &range, const Twine &message) {
382+
Strm->printError(range, message);
383+
EC = make_error_code(errc::invalid_argument);
384+
}
385+
381386
void Input::reportWarning(HNode *hnode, const Twine &message) {
382387
assert(hnode && "HNode must not be NULL");
383388
Strm->printError(hnode->_node, message, SourceMgr::DK_Warning);
384389
}
385390

391+
void Input::reportWarning(Node *node, const Twine &message) {
392+
Strm->printError(node, message, SourceMgr::DK_Warning);
393+
}
394+
395+
void Input::reportWarning(const SMRange &range, const Twine &message) {
396+
Strm->printError(range, message, SourceMgr::DK_Warning);
397+
}
398+
386399
std::unique_ptr<Input::HNode> Input::createHNodes(Node *N) {
387400
SmallString<128> StringStorage;
388401
if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
@@ -426,7 +439,8 @@ std::unique_ptr<Input::HNode> Input::createHNodes(Node *N) {
426439
auto ValueHNode = createHNodes(Value);
427440
if (EC)
428441
break;
429-
mapHNode->Mapping[KeyStr] = std::move(ValueHNode);
442+
mapHNode->Mapping[KeyStr] =
443+
std::make_pair(std::move(ValueHNode), KeyNode->getSourceRange());
430444
}
431445
return std::move(mapHNode);
432446
} else if (isa<NullNode>(N)) {

llvm/test/CodeGen/MIR/X86/spill-slot-fixed-stack-object-aliased.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ name: test
1818
frameInfo:
1919
maxAlignment: 4
2020
fixedStack:
21-
# CHECK: [[@LINE+1]]:63: unknown key 'isAliased'
21+
# CHECK: [[@LINE+1]]:52: unknown key 'isAliased'
2222
- { id: 0, type: spill-slot, offset: 0, size: 4, isAliased: true }
2323
stack:
2424
- { id: 0, offset: -12, size: 4, alignment: 4 }

llvm/test/CodeGen/MIR/X86/spill-slot-fixed-stack-object-immutable.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ name: test
1818
frameInfo:
1919
maxAlignment: 4
2020
fixedStack:
21-
# CHECK: [[@LINE+1]]:65: unknown key 'isImmutable'
21+
# CHECK: [[@LINE+1]]:52: unknown key 'isImmutable'
2222
- { id: 0, type: spill-slot, offset: 0, size: 4, isImmutable: true }
2323
stack:
2424
- { id: 0, offset: -12, size: 4, alignment: 4 }

llvm/test/CodeGen/MIR/X86/variable-sized-stack-object-size-error.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ frameInfo:
2323
stack:
2424
- { id: 0, offset: -20, size: 4, alignment: 4 }
2525
- { id: 1, offset: -32, size: 8, alignment: 8 }
26-
# CHECK: [[@LINE+1]]:55: unknown key 'size'
26+
# CHECK: [[@LINE+1]]:49: unknown key 'size'
2727
- { id: 2, type: variable-sized, offset: -32, size: 42, alignment: 1 }
2828
body: |
2929
bb.0.entry:

llvm/test/Object/nm-tapi-invalids.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ RUN: | FileCheck %s -check-prefix V3
99

1010
# Typo Check
1111
V1: tapi-invalid-v1.tbd malformed file
12-
V1-NEXT: tapi-invalid-v1.tbd:12:2: error: unknown key 'expors'
12+
V1-NEXT: tapi-invalid-v1.tbd:11:1: error: unknown key 'expors'
1313

1414
# Missing required key
1515
V2: tapi-invalid-v2.tbd malformed file
1616
V2-NEXT: tapi-invalid-v2.tbd:2:1: error: missing required key 'archs'
1717

1818
# v2 key in v3 specified file
1919
V3: tapi-invalid-v3.tbd malformed file
20-
V3-NEXT: tapi-invalid-v3.tbd:19:16: error: unknown key 'swift-version'
20+
V3-NEXT: tapi-invalid-v3.tbd:19:1: error: unknown key 'swift-version'

llvm/unittests/TextAPI/TextStubV1Tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,8 @@ TEST(TBDv1, MalformedFile2) {
451451
EXPECT_FALSE(!!Result);
452452
std::string ErrorMessage = toString(Result.takeError());
453453
ASSERT_EQ(
454-
"malformed file\nTest.tbd:5:9: error: unknown key 'foobar'\nfoobar: "
455-
"\"Unsupported key\"\n ^~~~~~~~~~~~~~~~~\n",
454+
"malformed file\nTest.tbd:5:1: error: unknown key 'foobar'\nfoobar: "
455+
"\"Unsupported key\"\n^~~~~~\n",
456456
ErrorMessage);
457457
}
458458

llvm/unittests/TextAPI/TextStubV2Tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,8 @@ TEST(TBDv2, MalformedFile2) {
486486
EXPECT_FALSE(!!Result);
487487
std::string ErrorMessage = toString(Result.takeError());
488488
ASSERT_EQ(
489-
"malformed file\nTest.tbd:5:9: error: unknown key 'foobar'\nfoobar: "
490-
"\"Unsupported key\"\n ^~~~~~~~~~~~~~~~~\n",
489+
"malformed file\nTest.tbd:5:1: error: unknown key 'foobar'\nfoobar: "
490+
"\"Unsupported key\"\n^~~~~~\n",
491491
ErrorMessage);
492492
}
493493

llvm/unittests/TextAPI/TextStubV3Tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -831,8 +831,8 @@ TEST(TBDv3, MalformedFile2) {
831831
EXPECT_FALSE(!!Result);
832832
std::string ErrorMessage = toString(Result.takeError());
833833
ASSERT_EQ(
834-
"malformed file\nTest.tbd:5:9: error: unknown key 'foobar'\nfoobar: "
835-
"\"Unsupported key\"\n ^~~~~~~~~~~~~~~~~\n",
834+
"malformed file\nTest.tbd:5:1: error: unknown key 'foobar'\nfoobar: "
835+
"\"Unsupported key\"\n^~~~~~\n",
836836
ErrorMessage);
837837
}
838838

llvm/unittests/TextAPI/TextStubV4Tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -924,8 +924,8 @@ TEST(TBDv4, MalformedFile2) {
924924
EXPECT_FALSE(!!Result);
925925
std::string ErrorMessage = toString(Result.takeError());
926926
ASSERT_EQ(
927-
"malformed file\nTest.tbd:5:9: error: unknown key 'foobar'\nfoobar: "
928-
"\"unsupported key\"\n ^~~~~~~~~~~~~~~~~\n",
927+
"malformed file\nTest.tbd:5:1: error: unknown key 'foobar'\nfoobar: "
928+
"\"unsupported key\"\n^~~~~~\n",
929929
ErrorMessage);
930930
}
931931

0 commit comments

Comments
 (0)