[PATCH] D128801: [yaml] Error on alias nodes that have properties attached

Nathan James via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 29 03:45:38 PDT 2022


njames93 created this revision.
njames93 added reviewers: dblaikie, chandlerc, JDevlieghere, dexonsmith, jhenderson.
Herald added subscribers: jeroen.dobbelaere, hiraditya.
Herald added a project: All.
njames93 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Alias nodes are explicitly forbidden from having properties.
Currently this is handled by just ignoring the properties which is not correct and will lead to confusion if a user tries to attach a property to an alias and not get the expected behaviour.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D128801

Files:
  llvm/lib/Support/YAMLParser.cpp
  llvm/unittests/Support/YAMLParserTest.cpp


Index: llvm/unittests/Support/YAMLParserTest.cpp
===================================================================
--- llvm/unittests/Support/YAMLParserTest.cpp
+++ llvm/unittests/Support/YAMLParserTest.cpp
@@ -183,6 +183,12 @@
   ExpectParseError("KeyValueNode with null value", "test: '");
 }
 
+TEST(YAMLParser, FailsOnAliasProperties) {
+  ExpectParseError("Alias node cannot have any properties", "&A *B");
+  ExpectParseError("Alias node cannot have any properties", "!A *B");
+  ExpectParseError("Alias node cannot have any properties", "!A &A *B");
+}
+
 // Checks that the given string can be parsed into an identical string inside
 // of an array.
 static void ExpectCanParseString(StringRef String) {
Index: llvm/lib/Support/YAMLParser.cpp
===================================================================
--- llvm/lib/Support/YAMLParser.cpp
+++ llvm/lib/Support/YAMLParser.cpp
@@ -2446,6 +2446,11 @@
   return stream.scanner->failed();
 }
 
+static SMRange getRangeFromStringRef(StringRef Str) {
+  return SMRange(SMLoc::getFromPointer(Str.begin()),
+                 SMLoc::getFromPointer(Str.end()));
+}
+
 Node *Document::parseBlockNode() {
   Token T = peekNext();
   // Handle properties.
@@ -2455,6 +2460,24 @@
   switch (T.Kind) {
   case Token::TK_Alias:
     getNext();
+    // YAML1.2 - 7.1 Alias Nodes
+    //
+    // Note that an alias node must not specify any properties or content, as
+    // these were already specified at the first occurrence of the node.
+    if (AnchorInfo.Kind == Token::TK_Anchor || TagInfo.Kind == Token::TK_Tag) {
+      setError("Alias node cannot have any properties", T);
+      if (AnchorInfo.Kind == Token::TK_Anchor) {
+        auto Range = getRangeFromStringRef(AnchorInfo.Range);
+        stream.scanner->printError(Range.Start, SourceMgr::DK_Note,
+                                   "anchor declared here", {Range});
+      }
+      if (TagInfo.Kind == Token::TK_Tag) {
+        auto Range = getRangeFromStringRef(TagInfo.Range);
+        stream.scanner->printError(Range.Start, SourceMgr::DK_Note,
+                                   "tag declared here", {Range});
+      }
+      return nullptr;
+    }
     return new (NodeAllocator) AliasNode(stream.CurrentDoc, T.Range.substr(1));
   case Token::TK_Anchor:
     if (AnchorInfo.Kind == Token::TK_Anchor) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D128801.440925.patch
Type: text/x-patch
Size: 2327 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220629/16ddd586/attachment.bin>


More information about the llvm-commits mailing list