From 50c30427405142f1b587edee846606184ae6af8e Mon Sep 17 00:00:00 2001 From: Nick Kledzik Date: Fri, 4 Jan 2013 19:32:00 +0000 Subject: [PATCH] Fix how YAML I/O detects flow sequences. Update test case to verify flow sequence is written as a flow sequence. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@171514 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/YAMLTraits.h | 56 +++++++++++++++------------------------ lib/Support/YAMLTraits.cpp | 2 +- unittests/Support/YAMLIOTest.cpp | 12 ++++++--- 3 files changed, 31 insertions(+), 39 deletions(-) diff --git a/include/llvm/Support/YAMLTraits.h b/include/llvm/Support/YAMLTraits.h index 3fb1f09063a..821333bf2f7 100644 --- a/include/llvm/Support/YAMLTraits.h +++ b/include/llvm/Support/YAMLTraits.h @@ -276,20 +276,9 @@ public: // Test if SequenceTraits is defined on type T -// and SequenceTraits::flow is *not* defined. template struct has_SequenceTraits : public llvm::integral_constant::value - && !has_FlowTraits::value > { }; - - -// Test if SequenceTraits is defined on type T -// and SequenceTraits::flow is defined. -template -struct has_FlowSequenceTraits : public llvm::integral_constant::value - && has_FlowTraits::value > { }; - + has_SequenceMethodTraits::value > { }; // Test if DocumentListTraits is defined on type T @@ -318,7 +307,6 @@ struct missingTraits : public llvm::integral_constant::value && !has_MappingTraits::value && !has_SequenceTraits::value - && !has_FlowSequenceTraits::value && !has_DocumentListTraits::value > {}; @@ -510,35 +498,33 @@ yamlize(IO &io, T &Val, bool) { template typename llvm::enable_if_c::value,void>::type yamlize(IO &io, T &Seq, bool) { - unsigned incount = io.beginSequence(); - unsigned count = io.outputting() ? SequenceTraits::size(io, Seq) : incount; - for(unsigned i=0; i < count; ++i) { - void *SaveInfo; - if ( io.preflightElement(i, SaveInfo) ) { - yamlize(io, SequenceTraits::element(io, Seq, i), true); - io.postflightElement(SaveInfo); + if ( has_FlowTraits< SequenceTraits >::value ) { + unsigned incnt = io.beginFlowSequence(); + unsigned count = io.outputting() ? SequenceTraits::size(io, Seq) : incnt; + for(unsigned i=0; i < count; ++i) { + void *SaveInfo; + if ( io.preflightFlowElement(i, SaveInfo) ) { + yamlize(io, SequenceTraits::element(io, Seq, i), true); + io.postflightFlowElement(SaveInfo); + } } + io.endFlowSequence(); } - io.endSequence(); -} - -template -typename llvm::enable_if_c::value,void>::type -yamlize(IO &io, T &Seq, bool) { - unsigned incount = io.beginFlowSequence(); - unsigned count = io.outputting() ? SequenceTraits::size(io, Seq) : incount; - for(unsigned i=0; i < count; ++i) { - void *SaveInfo; - if ( io.preflightFlowElement(i, SaveInfo) ) { - yamlize(io, SequenceTraits::element(io, Seq, i), true); - io.postflightFlowElement(SaveInfo); + else { + unsigned incnt = io.beginSequence(); + unsigned count = io.outputting() ? SequenceTraits::size(io, Seq) : incnt; + for(unsigned i=0; i < count; ++i) { + void *SaveInfo; + if ( io.preflightElement(i, SaveInfo) ) { + yamlize(io, SequenceTraits::element(io, Seq, i), true); + io.postflightElement(SaveInfo); + } } + io.endSequence(); } - io.endFlowSequence(); } - template<> struct ScalarTraits { static void output(const bool &, void*, llvm::raw_ostream &); diff --git a/lib/Support/YAMLTraits.cpp b/lib/Support/YAMLTraits.cpp index 8fcef40d243..ef3948dfd3a 100644 --- a/lib/Support/YAMLTraits.cpp +++ b/lib/Support/YAMLTraits.cpp @@ -411,8 +411,8 @@ void Output::postflightElement(void *) { } unsigned Output::beginFlowSequence() { - this->newLineCheck(); StateStack.push_back(inFlowSeq); + this->newLineCheck(); ColumnAtFlowStart = Column; output("[ "); NeedFlowSequenceComma = false; diff --git a/unittests/Support/YAMLIOTest.cpp b/unittests/Support/YAMLIOTest.cpp index fab2d5b666b..afa71cc25ea 100644 --- a/unittests/Support/YAMLIOTest.cpp +++ b/unittests/Support/YAMLIOTest.cpp @@ -600,8 +600,14 @@ TEST(YAMLIO, TestReadWriteMyFlowSequence) { map.numbers.push_back(1024); llvm::raw_string_ostream ostr(intermediate); - Output yout(ostr); + Output yout(ostr); yout << map; + + // Verify sequences were written in flow style + ostr.flush(); + llvm::StringRef flowOut(intermediate); + EXPECT_NE(llvm::StringRef::npos, flowOut.find("one, two")); + EXPECT_NE(llvm::StringRef::npos, flowOut.find("10, -30, 1024")); } { @@ -632,7 +638,7 @@ LLVM_YAML_STRONG_TYPEDEF(uint32_t, TotalSeconds) typedef std::vector SecondsSequence; -LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(TotalSeconds) +LLVM_YAML_IS_SEQUENCE_VECTOR(TotalSeconds) namespace llvm { @@ -745,7 +751,7 @@ struct KindAndFlags { typedef std::vector KindAndFlagsSequence; -LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(KindAndFlags) +LLVM_YAML_IS_SEQUENCE_VECTOR(KindAndFlags) namespace llvm { namespace yaml { -- 2.11.0