Skip to content

Commit

Permalink
fix: address clang-tidy warnings (#5915)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nerixyz authored Feb 7, 2025
1 parent f37676f commit 9092f24
Show file tree
Hide file tree
Showing 19 changed files with 333 additions and 146 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
- Bugfix: Fixed the reply button showing for inline whispers and announcements. (#5863)
- Bugfix: Fixed suspicious user treatment update messages not being searchable. (#5865)
- Bugfix: Ensure miniaudio backend exits even if it doesn't exit cleanly. (#5896)
- Dev: Add initial experimental EventSub support. (#5837, #5895, #5897, #5904, #5910, #5903)
- Dev: Add initial experimental EventSub support. (#5837, #5895, #5897, #5904, #5910, #5903, #5915)
- Dev: Highlight checks now use non-capturing groups for the boundaries. (#5784)
- Dev: Removed unused PubSub whisper code. (#5898)
- Dev: Updated Conan dependencies. (#5776)
Expand Down
30 changes: 29 additions & 1 deletion lib/twitch-eventsub-ws/ast/lib/member.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,46 @@ def get_type_name(type: clang.cindex.Type, namespace: tuple[str, ...]) -> str:
return type_name


def _get_template_name(type: clang.cindex.Type) -> str:
type = type.get_canonical()
if type.get_num_template_arguments() < 1:
return type.spelling
name: str = type.spelling
if type.is_const_qualified():
name.removeprefix("const ")
return name[: name.index("<")]


def _is_chrono_like_type(type: clang.cindex.Type) -> bool:
return _get_template_name(type) in ("std::chrono::time_point", "std::chrono::duration")


# clang's C API doesn't expose this, so we emulate it
def _is_trivially_copyable(type: clang.cindex.Type) -> bool:
# remove optional wrapper(s)
type = type.get_canonical()
while type.get_num_template_arguments() and _get_template_name(type) == "std::optional":
type = type.get_template_argument_type(0).get_canonical()

if type.is_pod():
return True
return _is_chrono_like_type(type)


class Member:
def __init__(
self,
name: str,
member_type: MemberType = MemberType.BASIC,
type_name: str = "?",
trivial: bool = False,
) -> None:
self.name = name
self.json_name = name
self.member_type = member_type
self.type_name = type_name
self.tag: Optional[str] = None
self.trivial = trivial

self.dont_fail_on_deserialization: bool = False

Expand Down Expand Up @@ -124,7 +152,7 @@ def from_field(node: clang.cindex.Cursor, namespace: tuple[str, ...]) -> Member:
if overwrite_member_type is not None:
member_type = overwrite_member_type

member = Member(name, member_type, type_name)
member = Member(name, member_type, type_name, _is_trivially_copyable(node.type))

if node.raw_comment is not None:
comment_commands = parse_comment_commands(node.raw_comment)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
boost::json::result_for<{{enum.full_name}}, boost::json::value>::type tag_invoke(
boost::json::try_value_to_tag<{{enum.full_name}}>, const boost::json::value &jvRoot)
boost::json::try_value_to_tag<{{enum.full_name}}> /* tag */, const boost::json::value &jvRoot)
{
if (!jvRoot.is_string())
{
Expand Down
4 changes: 4 additions & 0 deletions lib/twitch-eventsub-ws/ast/lib/templates/field-optional.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ if (jv{{field.name}} != nullptr && !jv{{field.name}}->is_null())
{
return t{{field.name}}.error();
}
{% if field.trivial -%}
{{field.name}} = t{{field.name}}.value();
{%- else -%}
{{field.name}} = std::move(t{{field.name}}.value());
{%- endif %}
{% endif %}
}

2 changes: 1 addition & 1 deletion lib/twitch-eventsub-ws/ast/lib/templates/field-vector.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ if (jv{{field.name}} == nullptr)
{
{% include 'error-missing-field.tmpl' indent content %}
}
const auto {{field.name}} = boost::json::try_value_to<std::vector<{{field.type_name}}>>(*jv{{field.name}});
auto {{field.name}} = boost::json::try_value_to<std::vector<{{field.type_name}}>>(*jv{{field.name}});
if ({{field.name}}.has_error())
{
{% include 'error-failed-to-deserialize.tmpl' indent content %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
{%- if field.trivial -%}
.{{field.name}} = {{field.name}}.value(),
{%- else -%}
.{{field.name}} = std::move({{field.name}}.value()),
{%- endif -%}
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
{%- if field.trivial -%}
.{{field.name}} = {{field.name}},
{%- else -%}
.{{field.name}} = std::move({{field.name}}),
{%- endif -%}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
.{{field.name}} = {{field.name}}.value(),
.{{field.name}} = std::move({{field.name}}.value()),
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
boost::json::result_for<{{struct.full_name}}, boost::json::value>::type tag_invoke(
boost::json::try_value_to_tag<{{struct.full_name}}>, const boost::json::value &jvRoot)
boost::json::try_value_to_tag<{{struct.full_name}}> /* tag */, const boost::json::value &jvRoot)
{
{% if struct.inner_root %}
if (!jvRoot.is_object())
Expand All @@ -18,7 +18,7 @@ boost::json::result_for<{{struct.full_name}}, boost::json::value>::type tag_invo
EVENTSUB_BAIL_HERE(error::Kind::ExpectedObject);
}
const auto &root = jvInnerRoot->get_object();
{% else %}
{% elif struct.members|length %}
if (!jvRoot.is_object())
{
EVENTSUB_BAIL_HERE(error::Kind::ExpectedObject);
Expand All @@ -27,6 +27,9 @@ boost::json::result_for<{{struct.full_name}}, boost::json::value>::type tag_invo
{% endif %}

{% for field in struct.members %}
{% if field.trivial -%}
static_assert(std::is_trivially_copyable_v<std::remove_reference_t<decltype(std::declval<{{struct.full_name}}>().{{field.name}})>>);
{%- endif -%}
{% if field.member_type == MemberType.BASIC -%}
{% include 'field-basic.tmpl' indent content %}
{%- elif field.member_type == MemberType.VECTOR -%}
Expand Down
3 changes: 2 additions & 1 deletion lib/twitch-eventsub-ws/src/generated/messages/metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
namespace chatterino::eventsub::lib::messages {

boost::json::result_for<Metadata, boost::json::value>::type tag_invoke(
boost::json::try_value_to_tag<Metadata>, const boost::json::value &jvRoot)
boost::json::try_value_to_tag<Metadata> /* tag */,
const boost::json::value &jvRoot)
{
if (!jvRoot.is_object())
{
Expand Down
22 changes: 16 additions & 6 deletions lib/twitch-eventsub-ws/src/generated/payloads/channel-ban-v1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
namespace chatterino::eventsub::lib::payload::channel_ban::v1 {

boost::json::result_for<Event, boost::json::value>::type tag_invoke(
boost::json::try_value_to_tag<Event>, const boost::json::value &jvRoot)
boost::json::try_value_to_tag<Event> /* tag */,
const boost::json::value &jvRoot)
{
if (!jvRoot.is_object())
{
Expand Down Expand Up @@ -154,6 +155,8 @@ boost::json::result_for<Event, boost::json::value>::type tag_invoke(
return reason.error();
}

static_assert(std::is_trivially_copyable_v<std::remove_reference_t<
decltype(std::declval<Event>().isPermanent)>>);
const auto *jvisPermanent = root.if_contains("is_permanent");
if (jvisPermanent == nullptr)
{
Expand All @@ -167,6 +170,9 @@ boost::json::result_for<Event, boost::json::value>::type tag_invoke(
return isPermanent.error();
}

static_assert(
std::is_trivially_copyable_v<
std::remove_reference_t<decltype(std::declval<Event>().bannedAt)>>);
const auto *jvbannedAt = root.if_contains("banned_at");
if (jvbannedAt == nullptr)
{
Expand All @@ -182,6 +188,9 @@ boost::json::result_for<Event, boost::json::value>::type tag_invoke(
return bannedAt.error();
}

static_assert(
std::is_trivially_copyable_v<
std::remove_reference_t<decltype(std::declval<Event>().endsAt)>>);
std::optional<std::chrono::system_clock::time_point> endsAt = std::nullopt;
const auto *jvendsAt = root.if_contains("ends_at");
if (jvendsAt != nullptr && !jvendsAt->is_null())
Expand All @@ -194,7 +203,7 @@ boost::json::result_for<Event, boost::json::value>::type tag_invoke(
{
return tendsAt.error();
}
endsAt = std::move(tendsAt.value());
endsAt = tendsAt.value();
}

return Event{
Expand All @@ -208,14 +217,15 @@ boost::json::result_for<Event, boost::json::value>::type tag_invoke(
.userLogin = std::move(userLogin.value()),
.userName = std::move(userName.value()),
.reason = std::move(reason.value()),
.isPermanent = std::move(isPermanent.value()),
.bannedAt = std::move(bannedAt.value()),
.endsAt = std::move(endsAt),
.isPermanent = isPermanent.value(),
.bannedAt = bannedAt.value(),
.endsAt = endsAt,
};
}

boost::json::result_for<Payload, boost::json::value>::type tag_invoke(
boost::json::try_value_to_tag<Payload>, const boost::json::value &jvRoot)
boost::json::try_value_to_tag<Payload> /* tag */,
const boost::json::value &jvRoot)
{
if (!jvRoot.is_object())
{
Expand Down
Loading

0 comments on commit 9092f24

Please sign in to comment.