Skip to content

Commit bde7474

Browse files
authored
Merge pull request #666 from zeromq/warnings
fix: fix compiler warnings, sign-conversion, clang-tidy issues
2 parents 84b8866 + e908698 commit bde7474

33 files changed

+823
-566
lines changed

.github/workflows/CI.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ jobs:
139139
- name: Build Native Windows 32
140140
if: ${{ matrix.os == 'windows-2019' && matrix.node_arch == 'ia32' }}
141141
run:
142-
node ./node_modules/@aminya/cmake-ts/build/main.js named-configs windows-x86
142+
node ./node_modules/@aminya/cmake-ts/build/main.js named-configs
143+
windows-x86
143144

144145
- name: Use Node 20
145146
if: ${{ !matrix.docker }}

.mocharc.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const config = {
88
"v8-expose-gc": true,
99
exit: true,
1010
parallel: true,
11+
timeout: 10000,
12+
retries: 3,
1113
}
1214

1315
module.exports = config

CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,12 @@ project_options(ENABLE_CACHE ENABLE_COMPILE_COMMANDS_SYMLINK)
102102
file(GLOB_RECURSE SOURCES "./src/*.cc")
103103
add_library(addon SHARED ${SOURCES})
104104

105-
target_link_libraries(addon PRIVATE project_options) # project_warnings
105+
if(CMAKE_CXX_COMPILER_ID STREQUAL GNU
106+
OR CMAKE_CXX_COMPILER_ID STREQUAL Clang
107+
OR CMAKE_CXX_COMPILER_ID STREQUAL AppleClang)
108+
target_compile_options(project_warnings INTERFACE "-Wno-shadow")
109+
endif()
110+
target_link_libraries(addon PRIVATE project_options project_warnings)
106111

107112
if(ZMQ_DRAFT)
108113
target_compile_definitions(addon PRIVATE ZMQ_BUILD_DRAFT_API)

package.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
"build.doc": "typedoc --options ./typedoc.json && minify-all -s docs-unminified -d docs --jsCompressor terser && shx rm -rf docs-unminified",
9696
"deploy.doc": "run-s build.doc && gh-pages --dist \"./docs\"",
9797
"build.native": "cmake-ts nativeonly",
98-
"build.native.debug": "cmake-ts nativeonly",
98+
"build.native.debug": "cmake-ts dev-os-only",
9999
"build": "run-p build.js build.native",
100100
"build.debug": "run-s build.js build.native.debug",
101101
"test": "run-s clean.temp build && mocha",
@@ -107,7 +107,7 @@
107107
"test.electron.renderer": "run-s build && electron-mocha --renderer",
108108
"lint-test.eslint": "eslint ./**/*.{ts,tsx,js,jsx,cjs,mjs,json,yaml} --no-error-on-unmatched-pattern --cache --cache-location ./.cache/eslint/",
109109
"lint.eslint": "pnpm run lint-test.eslint --fix",
110-
"lint.clang-tidy": "git ls-files --exclude-standard | grep -E '\\.(cpp|hpp|c|cc|cxx|hxx|h|ixx)$' | xargs -n 1 -P $(nproc) clang-tidy --fix ",
110+
"lint.clang-tidy": "git ls-files --exclude-standard | grep -E '\\.(cpp|hpp|c|cc|cxx|hxx|h|ixx)$' | xargs -n 1 -P $(nproc) clang-tidy",
111111
"lint": "run-p format lint.eslint format",
112112
"lint-test": "run-s lint-test.eslint",
113113
"bench": "node --expose-gc test/bench",
@@ -124,6 +124,15 @@
124124
"runtime": "node",
125125
"runtimeVersion": "12.22.12"
126126
},
127+
{
128+
"name": "linux-x64-dev",
129+
"dev": true,
130+
"buildType": "Debug",
131+
"os": "linux",
132+
"arch": "x64",
133+
"runtime": "node",
134+
"runtimeVersion": "12.22.12"
135+
},
127136
{
128137
"name": "windows-x64",
129138
"os": "win32",

src/closable.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
up ZMQ resources at agent exit. */
55
namespace zmq {
66
struct Closable {
7+
Closable() = default;
8+
Closable(const Closable&) = default;
9+
Closable(Closable&&) = default;
10+
Closable& operator=(const Closable&) = default;
11+
Closable& operator=(Closable&&) = default;
12+
virtual ~Closable() = default;
13+
714
virtual void Close() = 0;
815
};
9-
}
16+
} // namespace zmq

src/context.cc

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,21 @@ Context::Context(const Napi::CallbackInfo& info)
1313
/* If this module has no global context, then create one with a process
1414
wide context pointer that is shared between threads/agents. */
1515
if (module.GlobalContext.IsEmpty()) {
16-
if (Arg::Validator{}.ThrowIfInvalid(info)) return;
16+
if (Arg::Validator{}.ThrowIfInvalid(info)) {
17+
return;
18+
}
1719

1820
/* Just use the same shared global context pointer. Contexts are
1921
threadsafe anyway. */
2022
context = module.Global().SharedContext;
2123
} else {
22-
Arg::Validator args{
24+
Arg::Validator const args{
2325
Arg::Optional<Arg::Object>("Options must be an object"),
2426
};
2527

26-
if (args.ThrowIfInvalid(info)) return;
28+
if (args.ThrowIfInvalid(info)) {
29+
return;
30+
}
2731

2832
context = zmq_ctx_new();
2933
}
@@ -62,7 +66,7 @@ void Context::Close() {
6266
termination may block depending on ZMQ_BLOCKY/ZMQ_LINGER. This
6367
should definitely be avoided during GC and may only be acceptable
6468
at process exit. */
65-
auto err = zmq_ctx_shutdown(context);
69+
[[maybe_unused]] auto err = zmq_ctx_shutdown(context);
6670
assert(err == 0);
6771

6872
/* Pass the ZMQ context on to terminator for cleanup at exit. */
@@ -76,35 +80,39 @@ void Context::Close() {
7680

7781
template <>
7882
Napi::Value Context::GetCtxOpt<bool>(const Napi::CallbackInfo& info) {
79-
Arg::Validator args{
83+
Arg::Validator const args{
8084
Arg::Required<Arg::Number>("Identifier must be a number"),
8185
};
8286

83-
if (args.ThrowIfInvalid(info)) return Env().Undefined();
87+
if (args.ThrowIfInvalid(info)) {
88+
return Env().Undefined();
89+
}
8490

85-
uint32_t option = info[0].As<Napi::Number>();
91+
const auto option = info[0].As<Napi::Number>();
8692

87-
int32_t value = zmq_ctx_get(context, option);
93+
int32_t const value = zmq_ctx_get(context, option);
8894
if (value < 0) {
8995
ErrnoException(Env(), zmq_errno()).ThrowAsJavaScriptException();
9096
return Env().Undefined();
9197
}
9298

93-
return Napi::Boolean::New(Env(), value);
99+
return Napi::Boolean::New(Env(), value != 0);
94100
}
95101

96102
template <>
97103
void Context::SetCtxOpt<bool>(const Napi::CallbackInfo& info) {
98-
Arg::Validator args{
104+
Arg::Validator const args{
99105
Arg::Required<Arg::Number>("Identifier must be a number"),
100106
Arg::Required<Arg::Boolean>("Option value must be a boolean"),
101107
};
102108

103-
if (args.ThrowIfInvalid(info)) return;
109+
if (args.ThrowIfInvalid(info)) {
110+
return;
111+
}
104112

105-
uint32_t option = info[0].As<Napi::Number>();
113+
const auto option = info[0].As<Napi::Number>();
106114

107-
int32_t value = info[1].As<Napi::Boolean>();
115+
int32_t const value = static_cast<int32_t>(info[1].As<Napi::Boolean>());
108116
if (zmq_ctx_set(context, option, value) < 0) {
109117
ErrnoException(Env(), zmq_errno()).ThrowAsJavaScriptException();
110118
return;
@@ -113,13 +121,15 @@ void Context::SetCtxOpt<bool>(const Napi::CallbackInfo& info) {
113121

114122
template <typename T>
115123
Napi::Value Context::GetCtxOpt(const Napi::CallbackInfo& info) {
116-
Arg::Validator args{
124+
Arg::Validator const args{
117125
Arg::Required<Arg::Number>("Identifier must be a number"),
118126
};
119127

120-
if (args.ThrowIfInvalid(info)) return Env().Undefined();
128+
if (args.ThrowIfInvalid(info)) {
129+
return Env().Undefined();
130+
}
121131

122-
uint32_t option = info[0].As<Napi::Number>();
132+
const auto option = info[0].As<Napi::Number>();
123133

124134
T value = zmq_ctx_get(context, option);
125135
if (value < 0) {
@@ -132,14 +142,16 @@ Napi::Value Context::GetCtxOpt(const Napi::CallbackInfo& info) {
132142

133143
template <typename T>
134144
void Context::SetCtxOpt(const Napi::CallbackInfo& info) {
135-
Arg::Validator args{
145+
Arg::Validator const args{
136146
Arg::Required<Arg::Number>("Identifier must be a number"),
137147
Arg::Required<Arg::Number>("Option value must be a number"),
138148
};
139149

140-
if (args.ThrowIfInvalid(info)) return;
150+
if (args.ThrowIfInvalid(info)) {
151+
return;
152+
}
141153

142-
uint32_t option = info[0].As<Napi::Number>();
154+
const auto option = info[0].As<Napi::Number>();
143155

144156
T value = info[1].As<Napi::Number>();
145157
if (zmq_ctx_set(context, option, value) < 0) {
@@ -166,4 +178,4 @@ void Context::Initialize(Module& module, Napi::Object& exports) {
166178
module.Context = Napi::Persistent(constructor);
167179
exports.Set("Context", constructor);
168180
}
169-
}
181+
} // namespace zmq

src/context.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ class Context : public Napi::ObjectWrap<Context>, public Closable {
1212
static void Initialize(Module& module, Napi::Object& exports);
1313

1414
explicit Context(const Napi::CallbackInfo& info);
15-
virtual ~Context();
1615

16+
Context(const Context&) = delete;
17+
Context& operator=(const Context&) = delete;
1718
Context(Context&&) = delete;
1819
Context& operator=(Context&&) = delete;
20+
~Context() override;
1921

2022
void Close() override;
2123

@@ -34,7 +36,7 @@ class Context : public Napi::ObjectWrap<Context>, public Closable {
3436
friend class Observer;
3537
friend class Proxy;
3638
};
37-
}
39+
} // namespace zmq
3840

39-
static_assert(!std::is_copy_constructible<zmq::Context>::value, "not copyable");
40-
static_assert(!std::is_move_constructible<zmq::Context>::value, "not movable");
41+
static_assert(!std::is_copy_constructible_v<zmq::Context>, "not copyable");
42+
static_assert(!std::is_move_constructible_v<zmq::Context>, "not movable");

src/incoming_msg.cc

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "./incoming_msg.h"
33

44
#include <cassert>
5+
#include <cstdint>
56

67
#include "util/electron_helper.h"
78
#include "util/error.h"
@@ -26,44 +27,46 @@ Napi::Value IncomingMsg::IntoBuffer(const Napi::Env& env) {
2627
return env.Undefined();
2728
}
2829
}
29-
auto data = reinterpret_cast<uint8_t*>(zmq_msg_data(*ref));
30-
auto length = zmq_msg_size(*ref);
30+
auto* data = reinterpret_cast<uint8_t*>(zmq_msg_data(ref->get()));
31+
auto length = zmq_msg_size(ref->get());
3132

3233
if (noElectronMemoryCage) {
33-
static auto constexpr zero_copy_threshold = 1 << 7;
34+
static auto constexpr zero_copy_threshold = 1U << 7U;
3435
if (length > zero_copy_threshold) {
3536
/* Reuse existing buffer for external storage. This avoids copying but
3637
does include an overhead in having to call a finalizer when the
3738
buffer is GC'ed. For very small messages it is faster to copy. */
3839
moved = true;
3940

4041
/* Put appropriate GC pressure according to the size of the buffer. */
41-
Napi::MemoryManagement::AdjustExternalMemory(env, length);
42+
Napi::MemoryManagement::AdjustExternalMemory(
43+
env, static_cast<int64_t>(length));
4244

43-
auto release = [](const Napi::Env& env, uint8_t*, Reference* ref) {
44-
ptrdiff_t length = zmq_msg_size(*ref);
45+
const auto release = [](const Napi::Env& env, uint8_t*, Reference* ref) {
46+
const auto length = static_cast<int64_t>(zmq_msg_size(ref->get()));
4547
Napi::MemoryManagement::AdjustExternalMemory(env, -length);
4648
delete ref;
4749
};
4850

49-
return Napi::Buffer<uint8_t>::New(env, data, length, release, ref);
51+
return Napi::Buffer<uint8_t>::New(env, data, length, release, ref)
52+
.As<Napi::Value>();
5053
}
5154
}
5255

5356
if (length > 0) {
54-
return Napi::Buffer<uint8_t>::Copy(env, data, length);
57+
return Napi::Buffer<uint8_t>::Copy(env, data, length).As<Napi::Value>();
5558
}
5659

57-
return Napi::Buffer<uint8_t>::New(env, 0);
60+
return Napi::Buffer<uint8_t>::New(env, 0).As<Napi::Value>();
5861
}
5962

6063
IncomingMsg::Reference::Reference() {
61-
auto err = zmq_msg_init(&msg);
64+
[[maybe_unused]] auto err = zmq_msg_init(&msg);
6265
assert(err == 0);
6366
}
6467

6568
IncomingMsg::Reference::~Reference() {
66-
auto err = zmq_msg_close(&msg);
69+
[[maybe_unused]] auto err = zmq_msg_close(&msg);
6770
assert(err == 0);
6871
}
69-
}
72+
} // namespace zmq

src/incoming_msg.h

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,36 @@ class IncomingMsg {
1212

1313
IncomingMsg(const IncomingMsg&) = delete;
1414
IncomingMsg& operator=(const IncomingMsg&) = delete;
15+
IncomingMsg(IncomingMsg&&) = delete;
16+
IncomingMsg& operator=(IncomingMsg&&) = delete;
1517

1618
Napi::Value IntoBuffer(const Napi::Env& env);
1719

18-
inline operator zmq_msg_t*() {
19-
return *ref;
20+
zmq_msg_t* get() {
21+
return ref->get();
2022
}
2123

2224
private:
2325
class Reference {
24-
zmq_msg_t msg;
26+
zmq_msg_t msg{};
2527

2628
public:
2729
Reference();
30+
Reference(const Reference&) = delete;
31+
Reference(Reference&&) = delete;
32+
Reference& operator=(const Reference&) = delete;
33+
Reference& operator=(Reference&&) = delete;
2834
~Reference();
2935

30-
inline operator zmq_msg_t*() {
36+
zmq_msg_t* get() {
3137
return &msg;
3238
}
3339
};
3440

3541
Reference* ref = nullptr;
3642
bool moved = false;
3743
};
38-
}
44+
} // namespace zmq
3945

40-
static_assert(!std::is_copy_constructible<zmq::IncomingMsg>::value, "not copyable");
41-
static_assert(!std::is_move_constructible<zmq::IncomingMsg>::value, "not movable");
46+
static_assert(!std::is_copy_constructible_v<zmq::IncomingMsg>, "not copyable");
47+
static_assert(!std::is_move_constructible_v<zmq::IncomingMsg>, "not movable");

src/inline.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22

3-
#ifdef _MSC_VER
4-
#define force_inline inline __forceinline
3+
#if defined(_MSC_VER) && !defined(__clang__)
4+
#define force_inline __forceinline
55
#else
66
#define force_inline inline __attribute__((always_inline))
77
#endif

0 commit comments

Comments
 (0)