From 79b44a4d470041acf202027054ba86e935d86aa1 Mon Sep 17 00:00:00 2001 From: Xing GUO Date: Tue, 4 Aug 2020 16:47:38 +0800 Subject: [PATCH] [YAMLTraits] Fix mapping value that followed by comments. When mapping an optional value, if the value is and followed by comments, there will be a parsing error. This patch helps fix this issue. e.g., When mapping the following YAML, ``` Sections: - Name: blah Type: SHT_foo Flags: [[FLAGS=]] ## some comments. ``` the raw value of `ScalarNode` is " " rather than "". We need to remove the spaces. Differential Revision: https://reviews.llvm.org/D85180 --- llvm/include/llvm/Support/YAMLTraits.h | 4 +++- llvm/test/tools/yaml2obj/ELF/none-value.yaml | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/llvm/include/llvm/Support/YAMLTraits.h b/llvm/include/llvm/Support/YAMLTraits.h index e52bf78..acb1d61 100644 --- a/llvm/include/llvm/Support/YAMLTraits.h +++ b/llvm/include/llvm/Support/YAMLTraits.h @@ -1629,7 +1629,9 @@ void IO::processKeyWithDefault(const char *Key, Optional &Val, bool IsNone = false; if (!outputting()) if (auto *Node = dyn_cast(((Input *)this)->getCurrentNode())) - IsNone = Node->getRawValue() == ""; + // We use rtrim to ignore possible white spaces that might exist when a + // comment is present on the same line. + IsNone = Node->getRawValue().rtrim(' ') == ""; if (IsNone) Val = DefaultValue; diff --git a/llvm/test/tools/yaml2obj/ELF/none-value.yaml b/llvm/test/tools/yaml2obj/ELF/none-value.yaml index 786a9b5..7993e54 100644 --- a/llvm/test/tools/yaml2obj/ELF/none-value.yaml +++ b/llvm/test/tools/yaml2obj/ELF/none-value.yaml @@ -21,6 +21,7 @@ FileHeader: Sections: - Name: .bar Type: SHT_PROGBITS + Flags: [[TEST=]] ## Comment Offset: [[TEST=]] Address: [[TEST=]] Content: [[TEST=]] -- 2.7.4