[llvm] 4bf10f3 - [YAMLTraits] Fix std::optional input on empty documents (#68947)

via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 16 13:14:21 PDT 2023


Author: akirchhoff-modular
Date: 2023-10-16T15:14:17-05:00
New Revision: 4bf10f3da7ab32d70d5c7c43b7705c06c108d326

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

LOG: [YAMLTraits] Fix std::optional input on empty documents (#68947)

When the input document is non-empty, `mapOptional` works as expected,
setting `std::optional` to `std::nullopt` when the field is not present.
When the input document is empty, we hit a special case inside of
`Input::preflightKey` that results in `UseDefault = false`, which
results in the `std::optional` erroneously being set to a non-nullopt
value. `preflightKey` is changed to set `UseDefault = true` in this case
to make the behavior consistent between empty and non-empty documents.

Added: 
    

Modified: 
    llvm/lib/Support/YAMLTraits.cpp
    llvm/unittests/Support/YAMLIOTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Support/YAMLTraits.cpp b/llvm/lib/Support/YAMLTraits.cpp
index 9325a09faaea022..4aaf59be2ce502f 100644
--- a/llvm/lib/Support/YAMLTraits.cpp
+++ b/llvm/lib/Support/YAMLTraits.cpp
@@ -156,6 +156,8 @@ bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
   if (!CurrentNode) {
     if (Required)
       EC = make_error_code(errc::invalid_argument);
+    else
+      UseDefault = true;
     return false;
   }
 

diff  --git a/llvm/unittests/Support/YAMLIOTest.cpp b/llvm/unittests/Support/YAMLIOTest.cpp
index 90c09ed7f79ee34..745d743b2b24498 100644
--- a/llvm/unittests/Support/YAMLIOTest.cpp
+++ b/llvm/unittests/Support/YAMLIOTest.cpp
@@ -2392,6 +2392,7 @@ TEST(YAMLIO, TestMalformedMapFailsGracefully) {
 
 struct OptionalTest {
   std::vector<int> Numbers;
+  std::optional<int> MaybeNumber;
 };
 
 struct OptionalTestSeq {
@@ -2405,6 +2406,7 @@ namespace yaml {
   struct MappingTraits<OptionalTest> {
     static void mapping(IO& IO, OptionalTest &OT) {
       IO.mapOptional("Numbers", OT.Numbers);
+      IO.mapOptional("MaybeNumber", OT.MaybeNumber);
     }
   };
 
@@ -2466,6 +2468,7 @@ TEST(YAMLIO, TestEmptyStringSucceedsForMapWithOptionalFields) {
   Input yin("");
   yin >> doc;
   EXPECT_FALSE(yin.error());
+  EXPECT_FALSE(doc.MaybeNumber.has_value());
 }
 
 TEST(YAMLIO, TestEmptyStringSucceedsForSequence) {


        


More information about the llvm-commits mailing list