Skip to content

Commit

Permalink
src: use stricter compile-time guidance
Browse files Browse the repository at this point in the history
SnapshotSerializerDeserializer::GetName() appears to confuse static
analysis such as Coverity.

This changes the function structure to a sequence of if-else blocks and
marks all branch conditions as constexpr. (Unfortunately, this results
in a dangling 'else' keyword in the V macro.)

As per a request in the PR discussion, this change does _not_ ensure
that GetName<T>() can only be called for known types T and instead still
returns an empty string in that case.

Also use std::is_unsigned_v instead of !std::is_signed_v.

PR-URL: #46509
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michael Dawson <midawson@redhat.com>
  • Loading branch information
tniessen authored and RafaelGSS committed Apr 8, 2023
1 parent db95ed0 commit 4405fc8
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/node_snapshotable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -166,22 +166,19 @@ class SnapshotSerializerDeserializer {
V(std::string)

#define V(TypeName) \
if (std::is_same_v<T, TypeName>) { \
if constexpr (std::is_same_v<T, TypeName>) { \
return #TypeName; \
}
} else // NOLINT(readability/braces)
TYPE_LIST(V)
#undef V

std::string name;
if (std::is_arithmetic_v<T>) {
if (!std::is_signed_v<T>) {
name += "u";
}
name += std::is_integral_v<T> ? "int" : "float";
name += std::to_string(sizeof(T) * 8);
name += "_t";
if constexpr (std::is_arithmetic_v<T>) {
return (std::is_unsigned_v<T> ? "uint"
: std::is_integral_v<T> ? "int"
: "float") +
std::to_string(sizeof(T) * 8) + "_t";
}
return name;
return "";
}

bool is_debug = false;
Expand Down

0 comments on commit 4405fc8

Please sign in to comment.