tools: fix nfdc::text::ItemAttributes evaluation order bug
When multiple ItemAttributes::operator() invocations appear on the same
expression, they are not always evaluated left-to-right, causing incorrect
text output.
refs #3864
Change-Id: I4c99c5bc4a76b4becf83f368c9f253c0b254fc05
diff --git a/tools/nfdc/format-helpers.cpp b/tools/nfdc/format-helpers.cpp
index b6b8258..3d851dd 100644
--- a/tools/nfdc/format-helpers.cpp
+++ b/tools/nfdc/format-helpers.cpp
@@ -128,17 +128,7 @@
ItemAttributes::Attribute
ItemAttributes::operator()(const std::string& attribute)
{
- ++m_count;
- if (m_wantMultiLine) {
- return {m_count > 1,
- {m_maxAttributeWidth - static_cast<int>(attribute.size())},
- attribute};
- }
- else {
- return {false,
- {m_count > 1 ? 1 : 0},
- attribute};
- }
+ return {*this, attribute};
}
std::string
@@ -150,13 +140,21 @@
std::ostream&
operator<<(std::ostream& os, const ItemAttributes::Attribute& attr)
{
- if (attr.wantNewline) {
- os << '\n';
+ ++attr.ia.m_count;
+ if (attr.ia.m_wantMultiLine) {
+ if (attr.ia.m_count > 1) {
+ os << '\n';
+ }
+ os << Spaces{attr.ia.m_maxAttributeWidth - static_cast<int>(attr.attribute.size())};
}
- return os << attr.spaces << attr.attribute << '=';
+ else {
+ if (attr.ia.m_count > 1) {
+ os << ' ';
+ }
+ }
+ return os << attr.attribute << '=';
}
-
std::string
formatSeconds(time::seconds d, bool isLong)
{
diff --git a/tools/nfdc/format-helpers.hpp b/tools/nfdc/format-helpers.hpp
index 4fd2c38..9283855 100644
--- a/tools/nfdc/format-helpers.hpp
+++ b/tools/nfdc/format-helpers.hpp
@@ -140,11 +140,13 @@
struct Attribute
{
- bool wantNewline;
- Spaces spaces;
+ ItemAttributes& ia;
std::string attribute;
};
+ /** \note Caller must ensure ItemAttributes object is alive until after all Attribute objects are
+ * destructed.
+ */
Attribute
operator()(const std::string& attribute);
@@ -155,6 +157,8 @@
bool m_wantMultiLine;
int m_maxAttributeWidth;
int m_count;
+
+ friend std::ostream& operator<<(std::ostream& os, const ItemAttributes::Attribute& attr);
};
std::ostream&