[llvm] r238584 - [YAMLIO] Make line-wrapping configurable and test it.
Frederic Riss
friss at apple.com
Fri May 29 10:56:28 PDT 2015
Author: friss
Date: Fri May 29 12:56:28 2015
New Revision: 238584
URL: http://llvm.org/viewvc/llvm-project?rev=238584&view=rev
Log:
[YAMLIO] Make line-wrapping configurable and test it.
Summary:
We would wrap flow mappings and sequences when they go over a hardcoded 70
characters limit. Make the wrapping column configurable (and default to 70
co the change should be NFC for current users). Passing 0 allows to completely
suppress the wrapping which makes it easier to handle in tools like FileCheck.
Reviewers: bogner
Subscribers: llvm-commits
Differential Revision: http://reviews.llvm.org/D10109
Modified:
llvm/trunk/docs/YamlIO.rst
llvm/trunk/include/llvm/Support/YAMLTraits.h
llvm/trunk/lib/Support/YAMLTraits.cpp
llvm/trunk/unittests/Support/YAMLIOTest.cpp
Modified: llvm/trunk/docs/YamlIO.rst
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/docs/YamlIO.rst?rev=238584&r1=238583&r2=238584&view=diff
==============================================================================
--- llvm/trunk/docs/YamlIO.rst (original)
+++ llvm/trunk/docs/YamlIO.rst Fri May 29 12:56:28 2015
@@ -798,6 +798,8 @@ add "static const bool flow = true;". Fo
static const bool flow = true;
}
+Flow mappings are subject to line wrapping according to the Output object
+configuration.
Sequence
========
@@ -845,6 +847,8 @@ With the above, if you used MyList as th
structures, then when converted to YAML, a flow sequence of integers
will be used (e.g. [ 10, -3, 4 ]).
+Flow sequences are subject to line wrapping according to the Output object
+configuration.
Utility Macros
--------------
@@ -908,14 +912,14 @@ Output
The llvm::yaml::Output class is used to generate a YAML document from your
in-memory data structures, using traits defined on your data types.
-To instantiate an Output object you need an llvm::raw_ostream, and optionally
-a context pointer:
+To instantiate an Output object you need an llvm::raw_ostream, an optional
+context pointer and an optional wrapping column:
.. code-block:: c++
class Output : public IO {
public:
- Output(llvm::raw_ostream &, void *context=NULL);
+ Output(llvm::raw_ostream &, void *context = NULL, int WrapColumn = 70);
Once you have an Output object, you can use the C++ stream operator on it
to write your native data as YAML. One thing to recall is that a YAML file
@@ -924,6 +928,10 @@ streaming as YAML is a mapping, scalar,
are generating one document and wraps the mapping output
with "``---``" and trailing "``...``".
+The WrapColumn parameter will cause the flow mappings and sequences to
+line-wrap when they go over the supplied column. Pass 0 to completely
+suppress the wrapping.
+
.. code-block:: c++
using llvm::yaml::Output;
Modified: llvm/trunk/include/llvm/Support/YAMLTraits.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/YAMLTraits.h?rev=238584&r1=238583&r2=238584&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/YAMLTraits.h (original)
+++ llvm/trunk/include/llvm/Support/YAMLTraits.h Fri May 29 12:56:28 2015
@@ -1114,7 +1114,7 @@ private:
///
class Output : public IO {
public:
- Output(llvm::raw_ostream &, void *Ctxt=nullptr);
+ Output(llvm::raw_ostream &, void *Ctxt = nullptr, int WrapColumn = 70);
~Output() override;
bool outputting() override;
@@ -1170,6 +1170,7 @@ private:
};
llvm::raw_ostream &Out;
+ int WrapColumn;
SmallVector<InState, 8> StateStack;
int Column;
int ColumnAtFlowStart;
Modified: llvm/trunk/lib/Support/YAMLTraits.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/YAMLTraits.cpp?rev=238584&r1=238583&r2=238584&view=diff
==============================================================================
--- llvm/trunk/lib/Support/YAMLTraits.cpp (original)
+++ llvm/trunk/lib/Support/YAMLTraits.cpp Fri May 29 12:56:28 2015
@@ -404,9 +404,10 @@ bool Input::canElideEmptySequence() {
// Output
//===----------------------------------------------------------------------===//
-Output::Output(raw_ostream &yout, void *context)
+Output::Output(raw_ostream &yout, void *context, int WrapColumn)
: IO(context),
Out(yout),
+ WrapColumn(WrapColumn),
Column(0),
ColumnAtFlowStart(0),
ColumnAtMapFlowStart(0),
@@ -529,7 +530,7 @@ void Output::endFlowSequence() {
bool Output::preflightFlowElement(unsigned, void *&) {
if (NeedFlowSequenceComma)
output(", ");
- if (Column > 70) {
+ if (WrapColumn && Column > WrapColumn) {
output("\n");
for (int i = 0; i < ColumnAtFlowStart; ++i)
output(" ");
@@ -720,7 +721,7 @@ void Output::paddedKey(StringRef key) {
void Output::flowKey(StringRef Key) {
if (StateStack.back() == inFlowMapOtherKey)
output(", ");
- if (Column > 70) {
+ if (WrapColumn && Column > WrapColumn) {
output("\n");
for (int I = 0; I < ColumnAtMapFlowStart; ++I)
output(" ");
Modified: llvm/trunk/unittests/Support/YAMLIOTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/YAMLIOTest.cpp?rev=238584&r1=238583&r2=238584&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/YAMLIOTest.cpp (original)
+++ llvm/trunk/unittests/Support/YAMLIOTest.cpp Fri May 29 12:56:28 2015
@@ -2074,3 +2074,123 @@ TEST(YAMLIO, TestEmptyStringSucceedsForS
EXPECT_FALSE(yin.error());
EXPECT_TRUE(seq.empty());
}
+
+struct FlowMap {
+ llvm::StringRef str1, str2, str3;
+ FlowMap(llvm::StringRef str1, llvm::StringRef str2, llvm::StringRef str3)
+ : str1(str1), str2(str2), str3(str3) {}
+};
+
+namespace llvm {
+namespace yaml {
+ template <>
+ struct MappingTraits<FlowMap> {
+ static void mapping(IO &io, FlowMap &fm) {
+ io.mapRequired("str1", fm.str1);
+ io.mapRequired("str2", fm.str2);
+ io.mapRequired("str3", fm.str3);
+ }
+
+ static const bool flow = true;
+ };
+}
+}
+
+struct FlowSeq {
+ llvm::StringRef str;
+ FlowSeq(llvm::StringRef S) : str(S) {}
+ FlowSeq() = default;
+};
+
+template <>
+struct ScalarTraits<FlowSeq> {
+ static void output(const FlowSeq &value, void*, llvm::raw_ostream &out) {
+ out << value.str;
+ }
+ static StringRef input(StringRef scalar, void*, FlowSeq &value) {
+ value.str = scalar;
+ return "";
+ }
+
+ static bool mustQuote(StringRef S) { return false; }
+};
+
+LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(FlowSeq)
+
+TEST(YAMLIO, TestWrapFlow) {
+ std::string out;
+ llvm::raw_string_ostream ostr(out);
+ FlowMap Map("This is str1", "This is str2", "This is str3");
+ std::vector<FlowSeq> Seq;
+ Seq.emplace_back("This is str1");
+ Seq.emplace_back("This is str2");
+ Seq.emplace_back("This is str3");
+
+ {
+ // 20 is just bellow the total length of the first mapping field.
+ // We should wreap at every element.
+ Output yout(ostr, nullptr, 15);
+
+ yout << Map;
+ ostr.flush();
+ EXPECT_EQ(out,
+ "---\n"
+ "{ str1: This is str1, \n"
+ " str2: This is str2, \n"
+ " str3: This is str3 }\n"
+ "...\n");
+ out.clear();
+
+ yout << Seq;
+ ostr.flush();
+ EXPECT_EQ(out,
+ "---\n"
+ "[ This is str1, \n"
+ " This is str2, \n"
+ " This is str3 ]\n"
+ "...\n");
+ out.clear();
+ }
+ {
+ // 25 will allow the second field to be output on the first line.
+ Output yout(ostr, nullptr, 25);
+
+ yout << Map;
+ ostr.flush();
+ EXPECT_EQ(out,
+ "---\n"
+ "{ str1: This is str1, str2: This is str2, \n"
+ " str3: This is str3 }\n"
+ "...\n");
+ out.clear();
+
+ yout << Seq;
+ ostr.flush();
+ EXPECT_EQ(out,
+ "---\n"
+ "[ This is str1, This is str2, \n"
+ " This is str3 ]\n"
+ "...\n");
+ out.clear();
+ }
+ {
+ // 0 means no wrapping.
+ Output yout(ostr, nullptr, 0);
+
+ yout << Map;
+ ostr.flush();
+ EXPECT_EQ(out,
+ "---\n"
+ "{ str1: This is str1, str2: This is str2, str3: This is str3 }\n"
+ "...\n");
+ out.clear();
+
+ yout << Seq;
+ ostr.flush();
+ EXPECT_EQ(out,
+ "---\n"
+ "[ This is str1, This is str2, This is str3 ]\n"
+ "...\n");
+ out.clear();
+ }
+}
More information about the llvm-commits
mailing list