From e800967eb1105177f561732814297bfecfaf63c2 Mon Sep 17 00:00:00 2001 From: Zain Jaffal Date: Mon, 26 Jun 2023 13:07:56 +0100 Subject: [PATCH] [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 --- llvm/lib/Remarks/YAMLRemarkParser.cpp | 24 ++++++++++++++++++----- llvm/unittests/Remarks/YAMLRemarksParsingTest.cpp | 10 ++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/llvm/lib/Remarks/YAMLRemarkParser.cpp b/llvm/lib/Remarks/YAMLRemarkParser.cpp index feb39df..f5123b0 100644 --- a/llvm/lib/Remarks/YAMLRemarkParser.cpp +++ b/llvm/lib/Remarks/YAMLRemarkParser.cpp @@ -293,9 +293,16 @@ Expected YAMLRemarkParser::parseKey(yaml::KeyValueNode &Node) { Expected YAMLRemarkParser::parseStr(yaml::KeyValueNode &Node) { auto *Value = dyn_cast(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(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> YAMLRemarkParser::next() { Expected YAMLStrTabRemarkParser::parseStr(yaml::KeyValueNode &Node) { auto *Value = dyn_cast(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(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 MaybeStrID = parseUnsigned(Node)) diff --git a/llvm/unittests/Remarks/YAMLRemarksParsingTest.cpp b/llvm/unittests/Remarks/YAMLRemarksParsingTest.cpp index 85b6fb4..3c740dd 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. -- 2.7.4