Skip to content

Commit 5e32561

Browse files
clang-tidy-fixes
1 parent 4a37547 commit 5e32561

File tree

7 files changed

+66
-48
lines changed

7 files changed

+66
-48
lines changed

Diff for: include/hpp_proto/field_types.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,11 @@ class optional { // NOLINT(cppcoreguidelines-special-member-functions)
218218
[[nodiscard]] constexpr const T &&value() const && { return static_cast<const T &&>(_value); }
219219

220220
template <class U>
221-
constexpr T value_or(U &&default_value) const & {
221+
[[nodiscard]] constexpr T value_or(U &&default_value) const & {
222222
return has_value() ? _value : static_cast<T>(std::forward<U>(default_value));
223223
}
224224
template <class U>
225-
constexpr T value_or(U &&default_value) && {
225+
[[nodiscard]] constexpr T value_or(U &&default_value) && {
226226
return has_value() ? std::move(_value) : static_cast<T>(std::forward<U>(default_value));
227227
}
228228

Diff for: include/hpp_proto/grpc_support.hpp

+32-29
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,21 @@ namespace grpc::internal {
1212
template <>
1313
class CallOpRecvMessage<hpp::proto::grpc_support::byte_buffer_access> {
1414
public:
15-
grpc_byte_buffer *operator()(const ::grpc::ByteBuffer &buffer) const {
16-
return const_cast<::grpc::ByteBuffer &>(buffer).c_buffer();
15+
static void convert_slices(std::vector<std::span<const uint8_t>>& dest, const ::grpc::ByteBuffer& buffer) {
16+
grpc_byte_buffer_reader reader;
17+
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
18+
auto* c_buffer = const_cast<::grpc::ByteBuffer &>(buffer).c_buffer();
19+
grpc_byte_buffer_reader_init(&reader, c_buffer);
20+
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access)
21+
dest.reserve(c_buffer->data.raw.slice_buffer.count);
22+
23+
grpc_slice *slice = nullptr;
24+
while (grpc_byte_buffer_reader_peek(&reader, &slice) != 0) {
25+
auto* start_ptr = GRPC_SLICE_START_PTR(*slice);
26+
auto len = GRPC_SLICE_LENGTH(*slice);
27+
dest.emplace_back(start_ptr, len);
28+
}
29+
grpc_byte_buffer_reader_destroy(&reader);
1730
}
1831
};
1932
} // namespace grpc::internal
@@ -36,35 +49,25 @@ class byte_buffer_adaptor {
3649
using iterator = storage_t::iterator;
3750
using const_iterator = storage_t::const_iterator;
3851

39-
byte_buffer_adaptor(grpc_byte_buffer *buffer) {
40-
grpc_byte_buffer_reader reader;
41-
grpc_byte_buffer_reader_init(&reader, buffer);
42-
slices_.reserve(buffer->data.raw.slice_buffer.count);
43-
44-
grpc_slice *slice;
45-
while (grpc_byte_buffer_reader_peek(&reader, &slice)) {
46-
auto start_ptr = GRPC_SLICE_START_PTR(*slice);
47-
auto len = GRPC_SLICE_LENGTH(*slice);
48-
slices_.emplace_back(start_ptr, len);
49-
}
50-
grpc_byte_buffer_reader_destroy(&reader);
52+
explicit byte_buffer_adaptor(const ::grpc::ByteBuffer& buffer) {
53+
grpc::internal::CallOpRecvMessage<byte_buffer_access>::convert_slices(slices_, buffer);
5154
}
5255

53-
size_type size() const { return slices_.size(); }
56+
[[nodiscard]] size_type size() const { return slices_.size(); }
5457

55-
const_pointer data() const { return slices_.data(); }
58+
[[nodiscard]] const_pointer data() const { return slices_.data(); }
5659

57-
const_iterator cbegin() const { return slices_.cbegin(); }
58-
const_iterator cend() const { return slices_.cend(); }
59-
const_iterator begin() const { return slices_.begin(); }
60-
const_iterator end() const { return slices_.end(); }
60+
[[nodiscard]] const_iterator cbegin() const { return slices_.cbegin(); }
61+
[[nodiscard]] const_iterator cend() const { return slices_.cend(); }
62+
[[nodiscard]] const_iterator begin() const { return slices_.begin(); }
63+
[[nodiscard]] const_iterator end() const { return slices_.end(); }
6164
};
6265

6366
struct single_shot_slice_memory_resource {
6467
grpc_slice slice_{};
65-
single_shot_slice_memory_resource() {}
68+
single_shot_slice_memory_resource() = default;
6669
~single_shot_slice_memory_resource() {
67-
if (slice_.refcount) {
70+
if (slice_.refcount != nullptr) {
6871
grpc_slice_unref(slice_);
6972
}
7073
}
@@ -74,14 +77,14 @@ struct single_shot_slice_memory_resource {
7477
single_shot_slice_memory_resource &operator=(single_shot_slice_memory_resource &&) = delete;
7578

7679
void *allocate(std::size_t size, std::size_t) {
77-
assert(slice_.refcount == 0);
80+
assert(slice_.refcount == nullptr);
7881
slice_ = grpc_slice_malloc(size);
7982
return GRPC_SLICE_START_PTR(slice_);
8083
}
8184

82-
::grpc::ByteBuffer finalize() {
85+
[[nodiscard]] ::grpc::ByteBuffer finalize() const {
8386
::grpc::Slice slice(slice_, ::grpc::Slice::STEAL_REF);
84-
return ::grpc::ByteBuffer(&slice, 1);
87+
return {&slice, 1};
8588
}
8689
};
8790
} // namespace detail
@@ -94,17 +97,17 @@ ::grpc::Status write_proto(hpp::proto::concepts::has_meta auto const &message, :
9497
return ::grpc::Status::OK;
9598
}
9699

97-
return ::grpc::Status(::grpc::StatusCode::INTERNAL, "Failed to serialize message");
100+
return {::grpc::StatusCode::INTERNAL, "Failed to serialize message"};
98101
}
99102

100103
::grpc::Status read_proto(hpp::proto::concepts::has_meta auto &message, const ::grpc::ByteBuffer &buffer,
101104
hpp::proto::concepts::is_option_type auto &&...option) {
102-
auto c_buffer = grpc::internal::CallOpRecvMessage<byte_buffer_access>()(buffer);
103-
detail::byte_buffer_adaptor buffers(c_buffer);
105+
106+
detail::byte_buffer_adaptor buffers(buffer);
104107
if (hpp::proto::read_proto(message, buffers, option...).ok()) [[likely]] {
105108
return ::grpc::Status::OK;
106109
}
107-
return ::grpc::Status(::grpc::StatusCode::INTERNAL, "Failed to deserialize message");
110+
return {::grpc::StatusCode::INTERNAL, "Failed to deserialize message"};
108111
}
109112

110113
} // namespace hpp::proto::grpc_support

Diff for: protoc-plugin/hpp_gen.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -1008,10 +1008,11 @@ struct msg_code_generator : code_generator {
10081008
static std::string get_map_container_name(const field_descriptor_t &descriptor) {
10091009
auto opts = descriptor.options.get_extension(hpp::proto::hpp_field_opts()).value_or(hpp::proto::FieldOptions{});
10101010
using enum gpb::FieldDescriptorProto::Type;
1011+
using namespace std::string_literals;
10111012
if (descriptor.map_fields[0]->proto.type == TYPE_STRING) {
1012-
return opts.string_keyed_map.value_or("hpp::proto::flat_map");
1013+
return opts.string_keyed_map.value_or("hpp::proto::flat_map"s);
10131014
} else {
1014-
return opts.numeric_keyed_map.value_or("hpp::proto::flat_map");
1015+
return opts.numeric_keyed_map.value_or("hpp::proto::flat_map"s);
10151016
}
10161017
}
10171018

Diff for: tests/dynamic_serializer_tests.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ const boost::ut::suite dynamic_serializer_skip_test = [] {
4949
expect(fatal(ser.has_value()));
5050
std::string data = read_file("data/proto3_unittest.TestAllTypes.binpb");
5151

52-
std::array<char, 2> sbytes, ebytes;
52+
std::array<char, 2> sbytes{};
53+
std::array<char, 2> ebytes{};
5354
hpp::proto::unchecked_pack_varint(hpp::proto::make_tag(200, hpp::proto::wire_type::sgroup), sbytes.data());
5455
hpp::proto::unchecked_pack_varint(hpp::proto::make_tag(200, hpp::proto::wire_type::egroup), ebytes.data());
5556
std::ranges::copy(sbytes, std::inserter(data, data.begin()));

Diff for: tests/well_known_types_tests.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ using namespace std::string_view_literals;
3030
using namespace std::string_literals;
3131

3232
template <typename T>
33+
// NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
3334
void verify(const hpp::proto::dynamic_serializer &ser, const T &msg, std::string_view json,
34-
std::string_view pretty_json = "") {
35+
std::string_view pretty_json = ""sv) {
3536
expect(eq(json, hpp::proto::write_json(msg).value()));
3637

3738
if (!pretty_json.empty()) {

Diff for: tutorial/grpc/greeter_client.cpp

+8-3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ ::grpc::Status unary_call(::grpc::GenericStub *stub, typename Method::request &r
3434
cv.notify_one();
3535
});
3636
std::unique_lock<std::mutex> lock(mu);
37+
// NOLINTNEXTLINE(bugprone-infinite-loop)
3738
while (!done) {
3839
cv.wait(lock);
3940
}
@@ -50,7 +51,7 @@ class ServerStreamingClientReactor : public ::grpc::ClientBidiReactor<grpc::Byte
5051
public:
5152
ServerStreamingClientReactor(Method, ::grpc::GenericStub *stub, ::grpc::ClientContext &context,
5253
const typename Method::request &req, OnResponseCallback &&on_response)
53-
: context_(context), on_response_(on_response) {
54+
: context_(context), on_response_(std::move(on_response)) {
5455
if (auto status = hpp::proto::grpc_support::write_proto(req, req_buf_); !status.ok()) {
5556
result_ = status;
5657
return;
@@ -72,6 +73,7 @@ class ServerStreamingClientReactor : public ::grpc::ClientBidiReactor<grpc::Byte
7273
std::unique_lock lock(mu_);
7374

7475
cv_.wait(lock, [&] { return result_.has_value(); });
76+
// NOLINTNEXTLINE(bugprone-unchecked-optional-access)
7577
return *result_;
7678
}
7779

@@ -100,6 +102,7 @@ class ServerStreamingClientReactor : public ::grpc::ClientBidiReactor<grpc::Byte
100102
}
101103

102104
private:
105+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
103106
::grpc::ClientContext &context_;
104107
std::mutex mu_;
105108
std::condition_variable cv_;
@@ -111,7 +114,7 @@ class ServerStreamingClientReactor : public ::grpc::ClientBidiReactor<grpc::Byte
111114

112115
class GreeterClient {
113116
public:
114-
GreeterClient(std::shared_ptr<::grpc::Channel> channel) : stub_(new ::grpc::GenericStub(channel)) {}
117+
explicit GreeterClient(const std::shared_ptr<::grpc::Channel>& channel) : stub_(new ::grpc::GenericStub(channel)) {}
115118

116119
// Assembles the client's payload, sends it and prints the response back
117120
// from the server.
@@ -138,7 +141,7 @@ class GreeterClient {
138141
::grpc::ClientContext context;
139142
ServerStreamingClientReactor reactor(helloworld::Greeter::SayHelloStreamReply{}, stub_.get(), context, req,
140143
[](helloworld::HelloReply &reply) {
141-
std::cout << "Received reply: " << reply.message << std::endl;
144+
std::cout << "Received reply: " << reply.message << "\n";
142145
sleep(1);
143146
});
144147
reactor.Start();
@@ -167,11 +170,13 @@ class GreeterClient {
167170
};
168171

169172
int main(int argc, char **argv) {
173+
// NOLINTBEGIN(cppcoreguidelines-pro-bounds-pointer-arithmetic)
170174
const char *endpoint = (argc == 2) ? argv[1] : "localhost:50051";
171175
if (argc > 2) {
172176
std::cerr << "Usage: " << argv[0] << " <hostname:port>\n";
173177
return 1;
174178
}
179+
// NOLINTEND(cppcoreguidelines-pro-bounds-pointer-arithmetic)
175180

176181
GreeterClient greeter(grpc::CreateChannel(endpoint, grpc::InsecureChannelCredentials()));
177182
greeter.SayHello("World");

Diff for: tutorial/grpc/greeter_server.cpp

+17-10
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,11 @@ class GreeterServiceImpl {
137137
int n = count--;
138138
if (n == 0) {
139139
reactor.Finish(grpc::Status::OK);
140-
std::cout << "SayHelloStreamReply Finished" << std::endl;
140+
std::cout << "SayHelloStreamReply Finished\n";
141141
} else {
142142
helloworld::HelloReply reply;
143143
reply.message = "Hello " + name + " " + std::to_string(n);
144-
std::cout << "SayHelloStreamReply Sending reply: " << reply.message << std::endl;
144+
std::cout << "SayHelloStreamReply Sending reply: " << reply.message << "\n";
145145
reactor.Write(reply);
146146
}
147147
}
@@ -152,40 +152,45 @@ class GreeterServiceImpl {
152152
bool done_ = false;
153153

154154
public:
155-
GreeterServiceImpl(GenericService &service) {
155+
explicit GreeterServiceImpl(GenericService &service) {
156156
service.add_reactor_factory([&] {
157+
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
157158
return new ServerUnaryCallReactor<helloworld::Greeter::SayHello, GreeterServiceImpl>{
158159
this, &GreeterServiceImpl::SayHello};
159160
});
160161
service.add_reactor_factory([&] {
162+
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
161163
return new ServerStreamReactor<helloworld::Greeter::SayHelloStreamReply, GreeterServiceImpl,
162164
SayHelloStreamReplyWriter>(this, &GreeterServiceImpl::SayHelloStreamReply);
163165
});
164166

165167
service.add_reactor_factory([&] {
168+
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
166169
return new ServerUnaryCallReactor<helloworld::Greeter::Shutdown, GreeterServiceImpl>(
167170
this, &GreeterServiceImpl::Shutdown);
168171
});
169172
}
173+
174+
// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
170175
grpc::Status SayHello(const helloworld::HelloRequest &request, helloworld::HelloReply &reply) {
171-
if (request.name == "") {
172-
return grpc::Status(::grpc::StatusCode::INVALID_ARGUMENT, "name is not specified");
176+
if (request.name.empty()) {
177+
return {::grpc::StatusCode::INVALID_ARGUMENT, "name is not specified"};
173178
}
174179
reply.message = "Hello " + request.name;
175180
return grpc::Status::OK;
176181
}
177182

178183
void SayHelloStreamReply(const helloworld::HelloRequest &request, SayHelloStreamReplyWriter &writer, auto &reactor) {
179-
if (request.name == "") {
184+
if (request.name.empty()) {
180185
reactor.Finish(grpc::Status(::grpc::StatusCode::INVALID_ARGUMENT, "name is not specified"));
181186
} else {
182-
std::cout << "SayHelloStreamReply Started" << std::endl;
187+
std::cout << "SayHelloStreamReply Started\n";
183188
writer.name = request.name;
184189
writer(reactor);
185190
}
186191
}
187192

188-
grpc::Status Shutdown(const google::protobuf::Empty &request, google::protobuf::Empty &reply) {
193+
grpc::Status Shutdown(const google::protobuf::Empty &/*request*/, google::protobuf::Empty &/*reply*/) {
189194
std::unique_lock<std::mutex> lock(mu_);
190195
done_ = true;
191196
shutdown_cv_.notify_all();
@@ -210,21 +215,23 @@ void RunServer(const char *server_address) {
210215
builder.RegisterCallbackGenericService(&service);
211216
// Finally assemble the server.
212217
std::unique_ptr<::grpc::Server> server(builder.BuildAndStart());
213-
std::cout << "Server listening on " << server_address << std::endl;
218+
std::cout << "Server listening on " << server_address << "\n";
214219

215220
greeter_service.wait();
216221
// Wait for the server to shutdown. Note that some other thread must be
217222
// responsible for shutting down the server for this call to ever return.
218-
std::cout << "Server shutting down" << std::endl;
223+
std::cout << "Server shutting down\n";
219224
server->Shutdown();
220225
}
221226

222227
int main(int argc, char **argv) {
228+
// NOLINTBEGIN(cppcoreguidelines-pro-bounds-pointer-arithmetic)
223229
const char *endpoint = (argc == 2) ? argv[1] : "localhost:50051";
224230
if (argc > 2) {
225231
std::cerr << "Usage: " << argv[0] << " <hostname:port>\n";
226232
return 1;
227233
}
234+
// NOLINTEND(cppcoreguidelines-pro-bounds-pointer-arithmetic)
228235
RunServer(endpoint);
229236
return 0;
230237
}

0 commit comments

Comments
 (0)