JSONWriter deals in StringRefs instead of std::strings

Together with liberal use of `_sr` UDL to compile-time convert
string literals into StringRefs, this will reduce the number of
allocation and remove most of the strcpy calls inherent in
converting string lits into `std::string`s.
This commit is contained in:
Martin Hořeňovský 2023-11-16 22:17:21 +01:00
parent de7ba4e889
commit 32d9ae24bc
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
4 changed files with 119 additions and 128 deletions

View File

@ -29,7 +29,7 @@ namespace Catch {
JsonObjectWriter::JsonObjectWriter( std::ostream& os, JsonObjectWriter::JsonObjectWriter( std::ostream& os,
std::uint64_t indent_level ): std::uint64_t indent_level ):
m_os{ os }, m_indent_level{ indent_level } { m_os{ os }, m_indent_level{ indent_level } {
m_os << "{"; m_os << '{';
} }
JsonObjectWriter::JsonObjectWriter( JsonObjectWriter&& source ): JsonObjectWriter::JsonObjectWriter( JsonObjectWriter&& source ):
m_os{ source.m_os }, m_os{ source.m_os },
@ -47,11 +47,11 @@ namespace Catch {
m_os << '}'; m_os << '}';
} }
JsonValueWriter JsonObjectWriter::write( std::string const& key ) { JsonValueWriter JsonObjectWriter::write( StringRef key ) {
JsonUtils::appendCommaNewline( JsonUtils::appendCommaNewline(
m_os, m_should_comma, m_indent_level + 1 ); m_os, m_should_comma, m_indent_level + 1 );
m_os << '"' << key << '"' << ": "; m_os << '"' << key << "\": ";
return JsonValueWriter{ m_os, m_indent_level + 1 }; return JsonValueWriter{ m_os, m_indent_level + 1 };
} }
@ -60,7 +60,7 @@ namespace Catch {
JsonArrayWriter::JsonArrayWriter( std::ostream& os, JsonArrayWriter::JsonArrayWriter( std::ostream& os,
std::uint64_t indent_level ): std::uint64_t indent_level ):
m_os{ os }, m_indent_level{ indent_level } { m_os{ os }, m_indent_level{ indent_level } {
m_os << "["; m_os << '[';
} }
JsonArrayWriter::JsonArrayWriter( JsonArrayWriter&& source ): JsonArrayWriter::JsonArrayWriter( JsonArrayWriter&& source ):
m_os{ source.m_os }, m_os{ source.m_os },
@ -108,8 +108,41 @@ namespace Catch {
return JsonArrayWriter{ m_os, m_indent_level }; return JsonArrayWriter{ m_os, m_indent_level };
} }
void JsonValueWriter::write( Catch::StringRef value ) && {
writeImpl( value, true );
}
void JsonValueWriter::write( bool value ) && { void JsonValueWriter::write( bool value ) && {
writeImpl( value ? "true" : "false", false ); writeImpl( value ? "true"_sr : "false"_sr, false );
}
void JsonValueWriter::writeImpl( Catch::StringRef value, bool quote ) {
if ( quote ) { m_os << '"'; }
for (char c : value) {
// Escape list taken from https://www.json.org/json-en.html,
// string definition.
// Note that while forward slash _can_ be escaped, it does
// not have to be, if JSON is not further embedded somewhere
// where forward slash is meaningful.
if ( c == '"' ) {
m_os << "\\\"";
} else if ( c == '\\' ) {
m_os << "\\\\";
} else if ( c == '\b' ) {
m_os << "\\b";
} else if ( c == '\f' ) {
m_os << "\\f";
} else if ( c == '\n' ) {
m_os << "\\n";
} else if ( c == '\r' ) {
m_os << "\\r";
} else if ( c == '\t' ) {
m_os << "\\t";
} else {
m_os << c;
}
}
if ( quote ) { m_os << '"'; }
} }
} // namespace Catch } // namespace Catch

View File

@ -37,45 +37,26 @@ namespace Catch {
void write( T const& value ) && { void write( T const& value ) && {
writeImpl( value, !std::is_arithmetic<T>::value ); writeImpl( value, !std::is_arithmetic<T>::value );
} }
void write( StringRef value ) &&;
void write( bool value ) &&; void write( bool value ) &&;
private: private:
template <typename T> void writeImpl( StringRef value, bool quote );
void writeImpl( T const& value, bool quote_value ) {
if ( quote_value ) { m_os << '"'; }
m_sstream << value;
while ( true ) {
char c = m_sstream.get();
if ( m_sstream.eof() ) { break; }
// see https://www.json.org/json-en.html, string definition for the escape list // Without this SFINAE, this overload is a better match
if ( c == '"' ) { // for `std::string`, `char const*`, `char const[N]` args.
m_os << "\\\""; // While it would still work, it would cause code bloat
} else if ( c == '\\' ) { // and multiple iteration over the strings
m_os << "\\\\"; template <typename T,
// Note that while forward slash _can_ be escaped, it typename = typename std::enable_if_t<
// does not have to be, if JSON is not further embedded !std::is_convertible<T, StringRef>::value>>
// somewhere where forward slash is meaningful. void writeImpl( T const& value, bool quote_value ) {
} else if ( c == '\b' ) { m_sstream << value;
m_os << "\\b"; writeImpl( m_sstream.str(), quote_value );
} else if ( c == '\f' ) {
m_os << "\\f";
} else if ( c == '\n' ) {
m_os << "\\n";
} else if ( c == '\r' ) {
m_os << "\\r";
} else if ( c == '\t') {
m_os << "\\t";
} else {
m_os << c;
}
}
if ( quote_value ) { m_os << '"'; }
} }
std::ostream& m_os; std::ostream& m_os;
std::stringstream m_sstream{}; std::stringstream m_sstream;
std::uint64_t m_indent_level; std::uint64_t m_indent_level;
}; };
@ -89,7 +70,7 @@ namespace Catch {
~JsonObjectWriter(); ~JsonObjectWriter();
JsonValueWriter write( std::string const& key ); JsonValueWriter write( StringRef key );
private: private:
std::ostream& m_os; std::ostream& m_os;

View File

@ -19,9 +19,10 @@ namespace Catch {
void writeSourceInfo( JsonObjectWriter& writer, void writeSourceInfo( JsonObjectWriter& writer,
SourceLineInfo const& sourceInfo ) { SourceLineInfo const& sourceInfo ) {
auto source_location_writer = auto source_location_writer =
writer.write( "source-location" ).writeObject(); writer.write( "source-location"_sr ).writeObject();
source_location_writer.write( "filename" ).write( sourceInfo.file ); source_location_writer.write( "filename"_sr )
source_location_writer.write( "line" ).write( sourceInfo.line ); .write( sourceInfo.file );
source_location_writer.write( "line"_sr ).write( sourceInfo.line );
} }
void writeTags( JsonArrayWriter writer, std::vector<Tag> const& tags ) { void writeTags( JsonArrayWriter writer, std::vector<Tag> const& tags ) {
@ -32,43 +33,14 @@ namespace Catch {
void writeProperties( JsonArrayWriter writer, void writeProperties( JsonArrayWriter writer,
TestCaseInfo const& info ) { TestCaseInfo const& info ) {
if ( info.isHidden() ) { writer.write( "is-hidden" ); } if ( info.isHidden() ) { writer.write( "is-hidden"_sr ); }
if ( info.okToFail() ) { writer.write( "ok-to-fail" ); } if ( info.okToFail() ) { writer.write( "ok-to-fail"_sr ); }
if ( info.expectedToFail() ) { writer.write( "expected-to-fail" ); } if ( info.expectedToFail() ) {
if ( info.throws() ) { writer.write( "throws" ); } writer.write( "expected-to-fail"_sr );
}
if ( info.throws() ) { writer.write( "throws"_sr ); }
} }
//void writeCounts( JsonObjectWriter writer, Counts const& counts ) {
// writer.write( "passed" ).write( counts.passed );
// writer.write( "failed" ).write( counts.failed );
// writer.write( "fail-but-ok" ).write( counts.failedButOk );
// writer.write( "skipped" ).write( counts.skipped );
//}
//void writeTestInfo( JsonObjectWriter writer,
// TestCaseInfo const& info ) {
// writer.write( "name" ).write( info.name );
// writeTags( writer.write( "tags" ).writeArray(), info.tags );
// writeSourceInfo( writer, info.lineInfo );
// writeProperties( writer.write( "properties" ).writeArray(), info );
//}
//void writeSection( JsonObjectWriter& writer,
// CumulativeReporterBase::SectionNode const& section,
// bool selfWrite ) {
// if ( selfWrite ) {
// writer.write( "name" ).write( section.stats.sectionInfo.name );
// writeSourceInfo( writer, section.stats.sectionInfo.lineInfo );
// writeCounts( writer.write( "assertions-stats" ).writeObject(),
// section.stats.assertions );
// }
// if ( section.childSections.empty() ) { return; }
// auto sectionsWriter = writer.write( "sections" ).writeArray();
// for ( auto const& childPtr : section.childSections ) {
// auto childSectionWriter = sectionsWriter.writeObject();
// writeSection( childSectionWriter, *childPtr, true );
// }
//}
} // namespace } // namespace
JsonReporter::JsonReporter( ReporterConfig&& config ): JsonReporter::JsonReporter( ReporterConfig&& config ):
@ -84,15 +56,16 @@ namespace Catch {
m_writers.emplace( Writer::Object ); m_writers.emplace( Writer::Object );
auto& writer = m_objectWriters.top(); auto& writer = m_objectWriters.top();
writer.write( "version" ).write( 1 ); writer.write( "version"_sr ).write( 1 );
{ {
auto metadata_writer = writer.write( "metadata" ).writeObject(); auto metadata_writer = writer.write( "metadata"_sr ).writeObject();
metadata_writer.write( "name" ).write( m_config->name() ); metadata_writer.write( "name"_sr ).write( m_config->name() );
metadata_writer.write( "rng-seed" ).write( m_config->rngSeed() ); metadata_writer.write( "rng-seed"_sr ).write( m_config->rngSeed() );
metadata_writer.write( "catch2-version" ).write( libraryVersion() ); metadata_writer.write( "catch2-version"_sr )
.write( libraryVersion() );
if ( m_config->testSpec().hasFilters() ) { if ( m_config->testSpec().hasFilters() ) {
metadata_writer.write( "filters" ) metadata_writer.write( "filters"_sr )
.write( m_config->testSpec() ); .write( m_config->testSpec() );
} }
} }
@ -113,7 +86,7 @@ namespace Catch {
m_writers.emplace( Writer::Array ); m_writers.emplace( Writer::Array );
return m_arrayWriters.top(); return m_arrayWriters.top();
} }
JsonArrayWriter& JsonReporter::startArray( std::string const& key ) { JsonArrayWriter& JsonReporter::startArray( StringRef key ) {
m_arrayWriters.emplace( m_arrayWriters.emplace(
m_objectWriters.top().write( key ).writeArray() ); m_objectWriters.top().write( key ).writeArray() );
m_writers.emplace( Writer::Array ); m_writers.emplace( Writer::Array );
@ -125,7 +98,7 @@ namespace Catch {
m_writers.emplace( Writer::Object ); m_writers.emplace( Writer::Object );
return m_objectWriters.top(); return m_objectWriters.top();
} }
JsonObjectWriter& JsonReporter::startObject( std::string const& key ) { JsonObjectWriter& JsonReporter::startObject( StringRef key ) {
m_objectWriters.emplace( m_objectWriters.emplace(
m_objectWriters.top().write( key ).writeObject() ); m_objectWriters.top().write( key ).writeObject() );
m_writers.emplace( Writer::Object ); m_writers.emplace( Writer::Object );
@ -148,7 +121,7 @@ namespace Catch {
} }
void JsonReporter::startListing() { void JsonReporter::startListing() {
if ( !m_startedListing ) { startObject( "listings" ); } if ( !m_startedListing ) { startObject( "listings"_sr ); }
m_startedListing = true; m_startedListing = true;
} }
void JsonReporter::endListing() { void JsonReporter::endListing() {
@ -165,15 +138,15 @@ namespace Catch {
endListing(); endListing();
assert( isInside( Writer::Object ) ); assert( isInside( Writer::Object ) );
startObject( "test-run" ); startObject( "test-run"_sr );
startArray( "test-cases" ); startArray( "test-cases"_sr );
} }
static void writeCounts( JsonObjectWriter&& writer, Counts const& counts ) { static void writeCounts( JsonObjectWriter&& writer, Counts const& counts ) {
writer.write( "passed" ).write( counts.passed ); writer.write( "passed"_sr ).write( counts.passed );
writer.write( "failed" ).write( counts.failed ); writer.write( "failed"_sr ).write( counts.failed );
writer.write( "fail-but-ok" ).write( counts.failedButOk ); writer.write( "fail-but-ok"_sr ).write( counts.failedButOk );
writer.write( "skipped" ).write( counts.skipped ); writer.write( "skipped"_sr ).write( counts.skipped );
} }
void JsonReporter::testRunEnded(TestRunStats const& runStats) { void JsonReporter::testRunEnded(TestRunStats const& runStats) {
@ -182,10 +155,11 @@ namespace Catch {
endArray(); endArray();
{ {
auto totals = m_objectWriters.top().write( "totals" ).writeObject(); auto totals =
writeCounts( totals.write( "assertions" ).writeObject(), m_objectWriters.top().write( "totals"_sr ).writeObject();
writeCounts( totals.write( "assertions"_sr ).writeObject(),
runStats.totals.assertions ); runStats.totals.assertions );
writeCounts( totals.write( "test-cases" ).writeObject(), writeCounts( totals.write( "test-cases"_sr ).writeObject(),
runStats.totals.testCases ); runStats.totals.testCases );
} }
@ -202,18 +176,18 @@ namespace Catch {
// "test-info" prelude // "test-info" prelude
{ {
auto testInfo = auto testInfo =
m_objectWriters.top().write( "test-info" ).writeObject(); m_objectWriters.top().write( "test-info"_sr ).writeObject();
// TODO: handle testName vs className!! // TODO: handle testName vs className!!
testInfo.write( "name" ).write( tcInfo.name ); testInfo.write( "name"_sr ).write( tcInfo.name );
writeSourceInfo(testInfo, tcInfo.lineInfo); writeSourceInfo(testInfo, tcInfo.lineInfo);
writeTags( testInfo.write( "tags" ).writeArray(), tcInfo.tags ); writeTags( testInfo.write( "tags"_sr ).writeArray(), tcInfo.tags );
writeProperties( testInfo.write( "properties" ).writeArray(), writeProperties( testInfo.write( "properties"_sr ).writeArray(),
tcInfo ); tcInfo );
} }
// Start the array for individual test runs (testCasePartial pairs) // Start the array for individual test runs (testCasePartial pairs)
startArray( "runs" ); startArray( "runs"_sr );
} }
void JsonReporter::testCaseEnded( TestCaseStats const& tcStats ) { void JsonReporter::testCaseEnded( TestCaseStats const& tcStats ) {
@ -224,8 +198,9 @@ namespace Catch {
endArray(); endArray();
{ {
auto totals = m_objectWriters.top().write( "totals" ).writeObject(); auto totals =
writeCounts( totals.write( "assertions" ).writeObject(), m_objectWriters.top().write( "totals"_sr ).writeObject();
writeCounts( totals.write( "assertions"_sr ).writeObject(),
tcStats.totals.assertions ); tcStats.totals.assertions );
// We do not write the test case totals, because there will always be just one test case here. // We do not write the test case totals, because there will always be just one test case here.
// TODO: overall "result" -> success, skip, fail here? Or in partial result? // TODO: overall "result" -> success, skip, fail here? Or in partial result?
@ -242,9 +217,8 @@ namespace Catch {
void JsonReporter::testCasePartialStarting( TestCaseInfo const& /*tcInfo*/, void JsonReporter::testCasePartialStarting( TestCaseInfo const& /*tcInfo*/,
uint64_t index ) { uint64_t index ) {
startObject(); startObject();
m_objectWriters.top().write( "run-idx" ).write( index ); m_objectWriters.top().write( "run-idx"_sr ).write( index );
startArray( "path" ); startArray( "path"_sr );
//startObject( "path" );
// TODO: we want to delay most of the printing to the 'root' section // TODO: we want to delay most of the printing to the 'root' section
// TODO: childSection key name? // TODO: childSection key name?
} }
@ -256,17 +230,18 @@ namespace Catch {
endArray(); endArray();
if ( !tcStats.stdOut.empty() ) { if ( !tcStats.stdOut.empty() ) {
m_objectWriters.top() m_objectWriters.top()
.write( "captured-stdout" ) .write( "captured-stdout"_sr )
.write( tcStats.stdOut ); .write( tcStats.stdOut );
} }
if ( !tcStats.stdErr.empty() ) { if ( !tcStats.stdErr.empty() ) {
m_objectWriters.top() m_objectWriters.top()
.write( "captured-stderr" ) .write( "captured-stderr"_sr )
.write( tcStats.stdErr ); .write( tcStats.stdErr );
} }
{ {
auto totals = m_objectWriters.top().write( "totals" ).writeObject(); auto totals =
writeCounts( totals.write( "assertions" ).writeObject(), m_objectWriters.top().write( "totals"_sr ).writeObject();
writeCounts( totals.write( "assertions"_sr ).writeObject(),
tcStats.totals.assertions ); tcStats.totals.assertions );
// We do not write the test case totals, because there will // We do not write the test case totals, because there will
// always be just one test case here. // always be just one test case here.
@ -284,8 +259,8 @@ namespace Catch {
// We want to nest top level sections, even though it shares name // We want to nest top level sections, even though it shares name
// and source loc with the TEST_CASE // and source loc with the TEST_CASE
auto& sectionObject = startObject(); auto& sectionObject = startObject();
sectionObject.write( "kind" ).write( "section" ); sectionObject.write( "kind"_sr ).write( "section"_sr );
sectionObject.write( "name" ).write( sectionInfo.name ); sectionObject.write( "name"_sr ).write( sectionInfo.name );
writeSourceInfo( m_objectWriters.top(), sectionInfo.lineInfo ); writeSourceInfo( m_objectWriters.top(), sectionInfo.lineInfo );
@ -293,7 +268,7 @@ namespace Catch {
// rather complex, but we could do it, and it would look // rather complex, but we could do it, and it would look
// better for empty sections. OTOH, empty sections should // better for empty sections. OTOH, empty sections should
// be rare. // be rare.
startArray( "path" ); startArray( "path"_sr );
} }
void JsonReporter::sectionEnded( SectionStats const& /*sectionStats */) { void JsonReporter::sectionEnded( SectionStats const& /*sectionStats */) {
// End the subpath array // End the subpath array
@ -317,10 +292,10 @@ namespace Catch {
assert( isInside( Writer::Array ) ); assert( isInside( Writer::Array ) );
auto assertionObject = m_arrayWriters.top().writeObject(); auto assertionObject = m_arrayWriters.top().writeObject();
assertionObject.write( "kind" ).write( "assertion" ); assertionObject.write( "kind"_sr ).write( "assertion"_sr );
writeSourceInfo( assertionObject, writeSourceInfo( assertionObject,
assertionStats.assertionResult.getSourceInfo() ); assertionStats.assertionResult.getSourceInfo() );
assertionObject.write( "status" ) assertionObject.write( "status"_sr )
.write( assertionStats.assertionResult.isOk() ); .write( assertionStats.assertionResult.isOk() );
// TODO: handling of result. // TODO: handling of result.
// TODO: messages // TODO: messages
@ -337,38 +312,40 @@ namespace Catch {
std::vector<ReporterDescription> const& descriptions ) { std::vector<ReporterDescription> const& descriptions ) {
startListing(); startListing();
auto writer = m_objectWriters.top().write( "reporters" ).writeArray(); auto writer =
m_objectWriters.top().write( "reporters"_sr ).writeArray();
for ( auto const& desc : descriptions ) { for ( auto const& desc : descriptions ) {
auto desc_writer = writer.writeObject(); auto desc_writer = writer.writeObject();
desc_writer.write( "name" ).write( desc.name ); desc_writer.write( "name"_sr ).write( desc.name );
desc_writer.write( "description" ).write( desc.description ); desc_writer.write( "description"_sr ).write( desc.description );
} }
} }
void JsonReporter::listListeners( void JsonReporter::listListeners(
std::vector<ListenerDescription> const& descriptions ) { std::vector<ListenerDescription> const& descriptions ) {
startListing(); startListing();
auto writer = m_objectWriters.top().write( "listeners" ).writeArray(); auto writer =
m_objectWriters.top().write( "listeners"_sr ).writeArray();
for ( auto const& desc : descriptions ) { for ( auto const& desc : descriptions ) {
auto desc_writer = writer.writeObject(); auto desc_writer = writer.writeObject();
desc_writer.write( "name" ).write( desc.name ); desc_writer.write( "name"_sr ).write( desc.name );
desc_writer.write( "description" ).write( desc.description ); desc_writer.write( "description"_sr ).write( desc.description );
} }
} }
void JsonReporter::listTests( std::vector<TestCaseHandle> const& tests ) { void JsonReporter::listTests( std::vector<TestCaseHandle> const& tests ) {
startListing(); startListing();
auto writer = m_objectWriters.top().write( "tests" ).writeArray(); auto writer = m_objectWriters.top().write( "tests"_sr ).writeArray();
for ( auto const& test : tests ) { for ( auto const& test : tests ) {
auto desc_writer = writer.writeObject(); auto desc_writer = writer.writeObject();
auto const& info = test.getTestCaseInfo(); auto const& info = test.getTestCaseInfo();
desc_writer.write( "name" ).write( info.name ); desc_writer.write( "name"_sr ).write( info.name );
desc_writer.write( "class-name" ).write( info.className ); desc_writer.write( "class-name"_sr ).write( info.className );
{ {
auto tag_writer = desc_writer.write( "tags" ).writeArray(); auto tag_writer = desc_writer.write( "tags"_sr ).writeArray();
for ( auto const& tag : info.tags ) { for ( auto const& tag : info.tags ) {
tag_writer.write( tag.original ); tag_writer.write( tag.original );
} }
@ -379,17 +356,17 @@ namespace Catch {
void JsonReporter::listTags( std::vector<TagInfo> const& tags ) { void JsonReporter::listTags( std::vector<TagInfo> const& tags ) {
startListing(); startListing();
auto writer = m_objectWriters.top().write( "tags" ).writeArray(); auto writer = m_objectWriters.top().write( "tags"_sr ).writeArray();
for ( auto const& tag : tags ) { for ( auto const& tag : tags ) {
auto tag_writer = writer.writeObject(); auto tag_writer = writer.writeObject();
{ {
auto aliases_writer = auto aliases_writer =
tag_writer.write( "aliases" ).writeArray(); tag_writer.write( "aliases"_sr ).writeArray();
for ( auto alias : tag.spellings ) { for ( auto alias : tag.spellings ) {
aliases_writer.write( alias ); aliases_writer.write( alias );
} }
} }
tag_writer.write( "count" ).write( tag.count ); tag_writer.write( "count"_sr ).write( tag.count );
} }
} }
} // namespace Catch } // namespace Catch

View File

@ -64,10 +64,10 @@ namespace Catch {
}; };
JsonArrayWriter& startArray(); JsonArrayWriter& startArray();
JsonArrayWriter& startArray( std::string const& key ); JsonArrayWriter& startArray( StringRef key );
JsonObjectWriter& startObject(); JsonObjectWriter& startObject();
JsonObjectWriter& startObject( std::string const& key ); JsonObjectWriter& startObject( StringRef key );
void endObject(); void endObject();
void endArray(); void endArray();