Skip to content

Commit

Permalink
Costmetics. (#17)
Browse files Browse the repository at this point in the history
* Costmetics.
Avoid `snprintf` return value related issues.

* Add missing `__clang__` checks.
  • Loading branch information
helly25 authored Mar 13, 2024
1 parent 66f9bda commit 1094eec
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 30 deletions.
2 changes: 0 additions & 2 deletions .clangd
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
CompileFlags:
Compiler: /usr/bin/clang
Diagnostics:
UnusedIncludes: Strict

Expand Down
4 changes: 2 additions & 2 deletions mbo/container/any_scan.h
Original file line number Diff line number Diff line change
Expand Up @@ -418,14 +418,14 @@ class AnyScanImpl {
// That means we bypass any protection an iterator may have, but we can make this function
// `noexcept` assuming the iterator is noexcept for access. On the other hand we expect that
// out of bounds access may actually raise. So we effectively side step such exceptions.
ABSL_CHECK(funcs_.curr != nullptr && funcs_.more());
ABSL_CHECK(funcs_.curr != nullptr && funcs_.more()); // NOLINT(*-missing-default-case)
return funcs_.curr();
}

value_type operator*() const noexcept
requires(!kAccessByRef)
{
ABSL_CHECK(funcs_.curr != nullptr && funcs_.more());
ABSL_CHECK(funcs_.curr != nullptr && funcs_.more()); // NOLINT(*-missing-default-case)
return value_type(funcs_.curr());
}

Expand Down
68 changes: 43 additions & 25 deletions mbo/types/extend_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,13 @@ TEST_F(ExtendTest, Streamable) {
Conditional(kStructNameSupport, R"({.a: 25, .b: 42, .c: "", .ptr: <nullptr>})", R"({25, 42, "", <nullptr>})"));
}

#if defined(__clang__)

namespace debug {

// NOLINTBEGIN(bugprone-easily-swappable-parameters,cert-dcl50-cpp)
int DumpStructVisitor(
std::size_t field_index,
std::size_t /*field_index*/,
std::string_view format,
std::string_view indent = {},
std::string_view type = {},
Expand All @@ -190,71 +193,80 @@ int DumpStructVisitor(
std::cout << ", Indent: '" << indent << "'";
std::cout << ", Type: '" << type << "'";
std::cout << ", Name: '" << name << "'";
std::cout << std::endl;
std::cout << '\n';
return 0;
}

int PrintStructVisitor(
std::size_t field_index,
std::size_t /*field_index*/,
std::vector<std::tuple<std::string, std::string, std::string>>& fields,
std::string_view format,
std::string_view indent = {},
std::string_view type = {},
std::string_view name = {},
...) {
char buffer[1'024];
int l = 0;
if (!format.starts_with('%')) {
l = snprintf(buffer, 1'023, "%s", format.data());
} else if (format == "%s" || format == "%s}\n") {
l = snprintf(buffer, 1'023, format.data(), indent.data());
} else if (format == "%s%s") {
l = snprintf(buffer, 1'023, format.data(), indent.data(), type.data());
} else if (format == "%s%s %s =") {
l = snprintf(buffer, 1'023, "%s%s %s =", indent.data(), type.data(), name.data());
} else if (format.starts_with("%s%s %s =")) {
l = snprintf(buffer, 1'023, "%s%s %s =\n", indent.data(), type.data(), name.data());
}
const std::string line = [&]() -> std::string {
if (!format.starts_with('%')) {
return std::string(format);
} else if (format == "%s" || format == "%s}\n") {
return absl::StrCat(indent, format.substr(2));
} else if (format == "%s%s") {
return absl::StrFormat("%s%s", indent.data(), type.data());
} else if (format == "%s%s %s =") {
return absl::StrFormat("%s%s %s =", indent.data(), type.data(), name.data());
} else if (format.starts_with("%s%s %s =")) {
return absl::StrFormat("%s%s %s =\n", indent.data(), type.data(), name.data());
} else {
return absl::StrFormat("Unknown format: '%s'", format);
}
}();
std::cout << line;
if (format.starts_with("%s%s %s =") && indent == " ") {
fields.emplace_back(format, indent, name);
}
buffer[l] = 0;
std::cout << buffer;
return 0;
}

// NOLINTEND(bugprone-easily-swappable-parameters,cert-dcl50-cpp)

// NOLINTBEGIN(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
template<typename T>
void Print(const T* ptr) {
std::size_t field_index = 0;
std::vector<std::tuple<std::string, std::string, std::string>> fields;
__builtin_dump_struct(ptr, &PrintStructVisitor, field_index, fields);
std::cout << std::endl;
std::cout << '\n';
for (const auto& [format, indent, name] : fields) {
std::cout << format << " -> " << name << std::endl;
std::cout << format << " -> " << name << '\n';
}
field_index = 0;
__builtin_dump_struct(ptr, &DumpStructVisitor, field_index);
}

// NOLINTEND(cppcoreguidelines-pro-type-vararg,hicpp-vararg)

} // namespace debug

#endif // __clang__

struct PersonData : Extend<PersonData> {
int index;
int index = 0;
Person person;
const std::set<std::string>* data = nullptr;
};

TEST_F(ExtendTest, StreamableComplexFields) {
const std::set<std::string> data{"foo", "bar"};
PersonData person{
.index = 25,
.index = 25, // NOLINT(*-magic-numbers)
.person =
{
.name =
{
.first = "Hugo",
.last = "Meyer",
},
.age = 42,
.age = 42, // NOLINT(*-magic-numbers)
},
.data = &data,
};
Expand All @@ -266,8 +278,14 @@ TEST_F(ExtendTest, StreamableComplexFields) {
Conditional(
kStructNameSupport, R"({25, {.name: {.first: "Hugo", .last: "Meyer"}, .age: 42}, *{{"bar", "foo"}}})",
R"({25, {{"Hugo", "Meyer"}, 42}, *{{"bar", "foo"}}})"));
// debug::Print(&person);
// debug::Print(&person.person.name);
#ifdef __clang__
if (HasFailure()) {
std::cout << "Person:\n";
debug::Print(&person);
std::cout << "Person::person.name:\n";
debug::Print(&person.person.name);
}
#endif // __clang__
}

TEST_F(ExtendTest, Comparable) {
Expand Down
2 changes: 1 addition & 1 deletion mbo/types/extender.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
#include "absl/strings/str_format.h"
#include "mbo/types/internal/extender.h" // IWYU pragma: export
#include "mbo/types/internal/struct_names.h" // IWYU pragma: keep
#include "mbo/types/traits.h"
#include "mbo/types/traits.h" // IWYU pragma: keep
#include "mbo/types/tstring.h"

namespace mbo::types::extender {
Expand Down

0 comments on commit 1094eec

Please sign in to comment.