[llvm] e800967 - [YAMLParser] Support block nodes when parsing YAML strings.

Zain Jaffal via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 26 06:31:07 PDT 2023


Author: Zain Jaffal
Date: 2023-06-26T14:30:28+01:00
New Revision: e800967eb1105177f561732814297bfecfaf63c2

URL: https://github.com/llvm/llvm-project/commit/e800967eb1105177f561732814297bfecfaf63c2
DIFF: https://github.com/llvm/llvm-project/commit/e800967eb1105177f561732814297bfecfaf63c2.diff

LOG: [YAMLParser] Support block nodes when parsing YAML strings.

Previously if a string is in the format
```
|
      val
      val2
      val3
```
Yaml parser will error out without parsing the string. The mentioned pattern is a valid yaml str and should be parsed.

Differential Revision: https://reviews.llvm.org/D153760

Added: 
    

Modified: 
    llvm/lib/Remarks/YAMLRemarkParser.cpp
    llvm/unittests/Remarks/YAMLRemarksParsingTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Remarks/YAMLRemarkParser.cpp b/llvm/lib/Remarks/YAMLRemarkParser.cpp
index feb39dfc7bbca..f5123b0f64ce1 100644
--- a/llvm/lib/Remarks/YAMLRemarkParser.cpp
+++ b/llvm/lib/Remarks/YAMLRemarkParser.cpp
@@ -293,9 +293,16 @@ Expected<StringRef> YAMLRemarkParser::parseKey(yaml::KeyValueNode &Node) {
 
 Expected<StringRef> YAMLRemarkParser::parseStr(yaml::KeyValueNode &Node) {
   auto *Value = dyn_cast<yaml::ScalarNode>(Node.getValue());
-  if (!Value)
-    return error("expected a value of scalar type.", Node);
-  StringRef Result = Value->getRawValue();
+  yaml::BlockScalarNode *ValueBlock;
+  StringRef Result;
+  if (!Value) {
+    // Try to parse the value as a block node.
+    ValueBlock = dyn_cast<yaml::BlockScalarNode>(Node.getValue());
+    if (!ValueBlock)
+      return error("expected a value of scalar type.", Node);
+    Result = ValueBlock->getValue();
+  } else
+    Result = Value->getRawValue();
 
   if (Result.front() == '\'')
     Result = Result.drop_front();
@@ -429,9 +436,16 @@ Expected<std::unique_ptr<Remark>> YAMLRemarkParser::next() {
 
 Expected<StringRef> YAMLStrTabRemarkParser::parseStr(yaml::KeyValueNode &Node) {
   auto *Value = dyn_cast<yaml::ScalarNode>(Node.getValue());
-  if (!Value)
-    return error("expected a value of scalar type.", Node);
+  yaml::BlockScalarNode *ValueBlock;
   StringRef Result;
+  if (!Value) {
+    // Try to parse the value as a block node.
+    ValueBlock = dyn_cast<yaml::BlockScalarNode>(Node.getValue());
+    if (!ValueBlock)
+      return error("expected a value of scalar type.", Node);
+    Result = ValueBlock->getValue();
+  } else
+    Result = Value->getRawValue();
   // If we have a string table, parse it as an unsigned.
   unsigned StrID = 0;
   if (Expected<unsigned> MaybeStrID = parseUnsigned(Node))

diff  --git a/llvm/unittests/Remarks/YAMLRemarksParsingTest.cpp b/llvm/unittests/Remarks/YAMLRemarksParsingTest.cpp
index 85b6fb435528a..3c740ddc8a555 100644
--- a/llvm/unittests/Remarks/YAMLRemarksParsingTest.cpp
+++ b/llvm/unittests/Remarks/YAMLRemarksParsingTest.cpp
@@ -154,6 +154,16 @@ TEST(YAMLRemarks, ParsingGood) {
             "  - String: ' because its definition is unavailable'\n"
             "Pass: inline\n"
             "");
+
+  // Block Remark.
+  parseGood("\n"
+            "--- !Missed\n"
+            "Function: foo\n"
+            "Name: NoDefinition\n"
+            "Args:\n"
+            "  - String:           |\n      \n      \n      blocks\n"
+            "Pass: inline\n"
+            "");
 }
 
 // Mandatory common part of a remark.


        


More information about the llvm-commits mailing list