Skip to content

Commit 622ffae

Browse files
mcheshkovGabriel Schulhof
authored andcommitted
test: Tighten up compiler warnings
PR-URL: #315 Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com>
1 parent 7cdd787 commit 622ffae

File tree

7 files changed

+57
-44
lines changed

7 files changed

+57
-44
lines changed

napi-inl.h

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ namespace details {
2727
#define NAPI_THROW_IF_FAILED(env, status, ...) \
2828
if ((status) != napi_ok) throw Error::New(env);
2929

30+
#define NAPI_THROW_IF_FAILED_VOID(env, status) \
31+
if ((status) != napi_ok) throw Error::New(env);
32+
3033
#else // NAPI_CPP_EXCEPTIONS
3134

3235
#define NAPI_THROW(e) (e).ThrowAsJavaScriptException();
@@ -40,6 +43,14 @@ namespace details {
4043
return __VA_ARGS__; \
4144
}
4245

46+
// We need a _VOID version of this macro to avoid warnings resulting from
47+
// leaving the NAPI_THROW_IF_FAILED `...` argument empty.
48+
#define NAPI_THROW_IF_FAILED_VOID(env, status) \
49+
if ((status) != napi_ok) { \
50+
Error::New(env).ThrowAsJavaScriptException(); \
51+
return; \
52+
}
53+
4354
#endif // NAPI_CPP_EXCEPTIONS
4455

4556
#define NAPI_FATAL_IF_FAILED(status, location, message) \
@@ -160,7 +171,7 @@ struct AccessorCallbackData {
160171
napi_value exports) { \
161172
return Napi::RegisterModule(env, exports, regfunc); \
162173
} \
163-
NAPI_MODULE(modname, __napi_ ## regfunc);
174+
NAPI_MODULE(modname, __napi_ ## regfunc)
164175

165176
// Adapt the NAPI_MODULE registration function:
166177
// - Wrap the arguments in NAPI wrappers.
@@ -867,21 +878,21 @@ template <typename ValueType>
867878
inline void Object::Set(napi_value key, const ValueType& value) {
868879
napi_status status =
869880
napi_set_property(_env, _value, key, Value::From(_env, value));
870-
NAPI_THROW_IF_FAILED(_env, status);
881+
NAPI_THROW_IF_FAILED_VOID(_env, status);
871882
}
872883

873884
template <typename ValueType>
874885
inline void Object::Set(Value key, const ValueType& value) {
875886
napi_status status =
876887
napi_set_property(_env, _value, key, Value::From(_env, value));
877-
NAPI_THROW_IF_FAILED(_env, status);
888+
NAPI_THROW_IF_FAILED_VOID(_env, status);
878889
}
879890

880891
template <typename ValueType>
881892
inline void Object::Set(const char* utf8name, const ValueType& value) {
882893
napi_status status =
883894
napi_set_named_property(_env, _value, utf8name, Value::From(_env, value));
884-
NAPI_THROW_IF_FAILED(_env, status);
895+
NAPI_THROW_IF_FAILED_VOID(_env, status);
885896
}
886897

887898
template <typename ValueType>
@@ -929,7 +940,7 @@ template <typename ValueType>
929940
inline void Object::Set(uint32_t index, const ValueType& value) {
930941
napi_status status =
931942
napi_set_element(_env, _value, index, Value::From(_env, value));
932-
NAPI_THROW_IF_FAILED(_env, status);
943+
NAPI_THROW_IF_FAILED_VOID(_env, status);
933944
}
934945

935946
inline bool Object::Delete(uint32_t index) {
@@ -949,19 +960,19 @@ inline Array Object::GetPropertyNames() {
949960
inline void Object::DefineProperty(const PropertyDescriptor& property) {
950961
napi_status status = napi_define_properties(_env, _value, 1,
951962
reinterpret_cast<const napi_property_descriptor*>(&property));
952-
NAPI_THROW_IF_FAILED(_env, status);
963+
NAPI_THROW_IF_FAILED_VOID(_env, status);
953964
}
954965

955966
inline void Object::DefineProperties(const std::initializer_list<PropertyDescriptor>& properties) {
956967
napi_status status = napi_define_properties(_env, _value, properties.size(),
957968
reinterpret_cast<const napi_property_descriptor*>(properties.begin()));
958-
NAPI_THROW_IF_FAILED(_env, status);
969+
NAPI_THROW_IF_FAILED_VOID(_env, status);
959970
}
960971

961972
inline void Object::DefineProperties(const std::vector<PropertyDescriptor>& properties) {
962973
napi_status status = napi_define_properties(_env, _value, properties.size(),
963974
reinterpret_cast<const napi_property_descriptor*>(properties.data()));
964-
NAPI_THROW_IF_FAILED(_env, status);
975+
NAPI_THROW_IF_FAILED_VOID(_env, status);
965976
}
966977

967978
inline bool Object::InstanceOf(const Function& constructor) const {
@@ -1171,7 +1182,7 @@ inline void ArrayBuffer::EnsureInfo() const {
11711182
// since they can never change during the lifetime of the ArrayBuffer.
11721183
if (_data == nullptr) {
11731184
napi_status status = napi_get_arraybuffer_info(_env, _value, &_data, &_length);
1174-
NAPI_THROW_IF_FAILED(_env, status);
1185+
NAPI_THROW_IF_FAILED_VOID(_env, status);
11751186
}
11761187
}
11771188

@@ -1222,7 +1233,7 @@ inline DataView::DataView(napi_env env, napi_value value) : Object(env, value) {
12221233
&_data /* data */,
12231234
nullptr /* arrayBuffer */,
12241235
nullptr /* byteOffset */);
1225-
NAPI_THROW_IF_FAILED(_env, status);
1236+
NAPI_THROW_IF_FAILED_VOID(_env, status);
12261237
}
12271238

12281239
inline Napi::ArrayBuffer DataView::ArrayBuffer() const {
@@ -1466,7 +1477,7 @@ inline TypedArrayOf<T>::TypedArrayOf(napi_env env, napi_value value)
14661477
: TypedArray(env, value), _data(nullptr) {
14671478
napi_status status = napi_get_typedarray_info(
14681479
_env, _value, &_type, &_length, reinterpret_cast<void**>(&_data), nullptr, nullptr);
1469-
NAPI_THROW_IF_FAILED(_env, status);
1480+
NAPI_THROW_IF_FAILED_VOID(_env, status);
14701481
}
14711482

14721483
template <typename T>
@@ -1615,7 +1626,7 @@ inline Promise::Deferred Promise::Deferred::New(napi_env env) {
16151626

16161627
inline Promise::Deferred::Deferred(napi_env env) : _env(env) {
16171628
napi_status status = napi_create_promise(_env, &_deferred, &_promise);
1618-
NAPI_THROW_IF_FAILED(_env, status);
1629+
NAPI_THROW_IF_FAILED_VOID(_env, status);
16191630
}
16201631

16211632
inline Promise Promise::Deferred::Promise() const {
@@ -1628,12 +1639,12 @@ inline Napi::Env Promise::Deferred::Env() const {
16281639

16291640
inline void Promise::Deferred::Resolve(napi_value value) const {
16301641
napi_status status = napi_resolve_deferred(_env, _deferred, value);
1631-
NAPI_THROW_IF_FAILED(_env, status);
1642+
NAPI_THROW_IF_FAILED_VOID(_env, status);
16321643
}
16331644

16341645
inline void Promise::Deferred::Reject(napi_value value) const {
16351646
napi_status status = napi_reject_deferred(_env, _deferred, value);
1636-
NAPI_THROW_IF_FAILED(_env, status);
1647+
NAPI_THROW_IF_FAILED_VOID(_env, status);
16371648
}
16381649

16391650
inline Promise::Promise(napi_env env, napi_value value) : Object(env, value) {
@@ -1752,7 +1763,7 @@ inline void Buffer<T>::EnsureInfo() const {
17521763
size_t byteLength;
17531764
void* voidData;
17541765
napi_status status = napi_get_buffer_info(_env, _value, &voidData, &byteLength);
1755-
NAPI_THROW_IF_FAILED(_env, status);
1766+
NAPI_THROW_IF_FAILED_VOID(_env, status);
17561767
_length = byteLength / sizeof (T);
17571768
_data = static_cast<T*>(voidData);
17581769
}
@@ -1888,7 +1899,7 @@ inline void Error::ThrowAsJavaScriptException() const {
18881899
HandleScope scope(_env);
18891900
if (!IsEmpty()) {
18901901
napi_status status = napi_throw(_env, Value());
1891-
NAPI_THROW_IF_FAILED(_env, status);
1902+
NAPI_THROW_IF_FAILED_VOID(_env, status);
18921903
}
18931904
}
18941905

@@ -2077,7 +2088,7 @@ template <typename T>
20772088
inline void Reference<T>::Reset() {
20782089
if (_ref != nullptr) {
20792090
napi_status status = napi_delete_reference(_env, _ref);
2080-
NAPI_THROW_IF_FAILED(_env, status);
2091+
NAPI_THROW_IF_FAILED_VOID(_env, status);
20812092
_ref = nullptr;
20822093
}
20832094
}
@@ -2090,7 +2101,7 @@ inline void Reference<T>::Reset(const T& value, uint32_t refcount) {
20902101
napi_value val = value;
20912102
if (val != nullptr) {
20922103
napi_status status = napi_create_reference(_env, value, refcount, &_ref);
2093-
NAPI_THROW_IF_FAILED(_env, status);
2104+
NAPI_THROW_IF_FAILED_VOID(_env, status);
20942105
}
20952106
}
20962107

@@ -2364,7 +2375,7 @@ inline CallbackInfo::CallbackInfo(napi_env env, napi_callback_info info)
23642375
_argc = _staticArgCount;
23652376
_argv = _staticArgs;
23662377
napi_status status = napi_get_cb_info(env, info, &_argc, _argv, &_this, &_data);
2367-
NAPI_THROW_IF_FAILED(_env, status);
2378+
NAPI_THROW_IF_FAILED_VOID(_env, status);
23682379

23692380
if (_argc > _staticArgCount) {
23702381
// Use either a fixed-size array (on the stack) or a dynamically-allocated
@@ -2373,7 +2384,7 @@ inline CallbackInfo::CallbackInfo(napi_env env, napi_callback_info info)
23732384
_argv = _dynamicArgs;
23742385

23752386
status = napi_get_cb_info(env, info, &_argc, _argv, nullptr, nullptr);
2376-
NAPI_THROW_IF_FAILED(_env, status);
2387+
NAPI_THROW_IF_FAILED_VOID(_env, status);
23772388
}
23782389
}
23792390

@@ -2430,7 +2441,7 @@ inline PropertyDescriptor
24302441
PropertyDescriptor::Accessor(const char* utf8name,
24312442
Getter getter,
24322443
napi_property_attributes attributes,
2433-
void* data) {
2444+
void* /*data*/) {
24342445
typedef details::CallbackData<Getter, Napi::Value> CbData;
24352446
// TODO: Delete when the function is destroyed
24362447
auto callbackData = new CbData({ getter, nullptr });
@@ -2459,7 +2470,7 @@ template <typename Getter>
24592470
inline PropertyDescriptor PropertyDescriptor::Accessor(napi_value name,
24602471
Getter getter,
24612472
napi_property_attributes attributes,
2462-
void* data) {
2473+
void* /*data*/) {
24632474
typedef details::CallbackData<Getter, Napi::Value> CbData;
24642475
// TODO: Delete when the function is destroyed
24652476
auto callbackData = new CbData({ getter, nullptr });
@@ -2490,7 +2501,7 @@ inline PropertyDescriptor PropertyDescriptor::Accessor(const char* utf8name,
24902501
Getter getter,
24912502
Setter setter,
24922503
napi_property_attributes attributes,
2493-
void* data) {
2504+
void* /*data*/) {
24942505
typedef details::AccessorCallbackData<Getter, Setter> CbData;
24952506
// TODO: Delete when the function is destroyed
24962507
auto callbackData = new CbData({ getter, setter });
@@ -2521,7 +2532,7 @@ inline PropertyDescriptor PropertyDescriptor::Accessor(napi_value name,
25212532
Getter getter,
25222533
Setter setter,
25232534
napi_property_attributes attributes,
2524-
void* data) {
2535+
void* /*data*/) {
25252536
typedef details::AccessorCallbackData<Getter, Setter> CbData;
25262537
// TODO: Delete when the function is destroyed
25272538
auto callbackData = new CbData({ getter, setter });
@@ -2552,7 +2563,7 @@ template <typename Callable>
25522563
inline PropertyDescriptor PropertyDescriptor::Function(const char* utf8name,
25532564
Callable cb,
25542565
napi_property_attributes attributes,
2555-
void* data) {
2566+
void* /*data*/) {
25562567
typedef decltype(cb(CallbackInfo(nullptr, nullptr))) ReturnType;
25572568
typedef details::CallbackData<Callable, ReturnType> CbData;
25582569
// TODO: Delete when the function is destroyed
@@ -2582,7 +2593,7 @@ template <typename Callable>
25822593
inline PropertyDescriptor PropertyDescriptor::Function(napi_value name,
25832594
Callable cb,
25842595
napi_property_attributes attributes,
2585-
void* data) {
2596+
void* /*data*/) {
25862597
typedef decltype(cb(CallbackInfo(nullptr, nullptr))) ReturnType;
25872598
typedef details::CallbackData<Callable, ReturnType> CbData;
25882599
// TODO: Delete when the function is destroyed
@@ -2663,7 +2674,7 @@ inline ObjectWrap<T>::ObjectWrap(const Napi::CallbackInfo& callbackInfo) {
26632674
napi_ref ref;
26642675
T* instance = static_cast<T*>(this);
26652676
status = napi_wrap(env, wrapper, instance, FinalizeCallback, nullptr, &ref);
2666-
NAPI_THROW_IF_FAILED(env, status)
2677+
NAPI_THROW_IF_FAILED_VOID(env, status);
26672678

26682679
Reference<Object>* instanceRef = instance;
26692680
*instanceRef = Reference<Object>(env, ref);
@@ -3031,7 +3042,7 @@ inline HandleScope::HandleScope(napi_env env, napi_handle_scope scope)
30313042

30323043
inline HandleScope::HandleScope(Napi::Env env) : _env(env) {
30333044
napi_status status = napi_open_handle_scope(_env, &_scope);
3034-
NAPI_THROW_IF_FAILED(_env, status);
3045+
NAPI_THROW_IF_FAILED_VOID(_env, status);
30353046
}
30363047

30373048
inline HandleScope::~HandleScope() {
@@ -3056,7 +3067,7 @@ inline EscapableHandleScope::EscapableHandleScope(
30563067

30573068
inline EscapableHandleScope::EscapableHandleScope(Napi::Env env) : _env(env) {
30583069
napi_status status = napi_open_escapable_handle_scope(_env, &_scope);
3059-
NAPI_THROW_IF_FAILED(_env, status);
3070+
NAPI_THROW_IF_FAILED_VOID(_env, status);
30603071
}
30613072

30623073
inline EscapableHandleScope::~EscapableHandleScope() {
@@ -3124,11 +3135,11 @@ inline AsyncWorker::AsyncWorker(const Object& receiver,
31243135
napi_value resource_id;
31253136
napi_status status = napi_create_string_latin1(
31263137
_env, resource_name, NAPI_AUTO_LENGTH, &resource_id);
3127-
NAPI_THROW_IF_FAILED(_env, status);
3138+
NAPI_THROW_IF_FAILED_VOID(_env, status);
31283139

31293140
status = napi_create_async_work(_env, resource, resource_id, OnExecute,
31303141
OnWorkComplete, this, &_work);
3131-
NAPI_THROW_IF_FAILED(_env, status);
3142+
NAPI_THROW_IF_FAILED_VOID(_env, status);
31323143
}
31333144

31343145
inline AsyncWorker::~AsyncWorker() {
@@ -3169,12 +3180,12 @@ inline Napi::Env AsyncWorker::Env() const {
31693180

31703181
inline void AsyncWorker::Queue() {
31713182
napi_status status = napi_queue_async_work(_env, _work);
3172-
NAPI_THROW_IF_FAILED(_env, status);
3183+
NAPI_THROW_IF_FAILED_VOID(_env, status);
31733184
}
31743185

31753186
inline void AsyncWorker::Cancel() {
31763187
napi_status status = napi_cancel_async_work(_env, _work);
3177-
NAPI_THROW_IF_FAILED(_env, status);
3188+
NAPI_THROW_IF_FAILED_VOID(_env, status);
31783189
}
31793190

31803191
inline ObjectReference& AsyncWorker::Receiver() {
@@ -3197,7 +3208,7 @@ inline void AsyncWorker::SetError(const std::string& error) {
31973208
_error = error;
31983209
}
31993210

3200-
inline void AsyncWorker::OnExecute(napi_env env, void* this_pointer) {
3211+
inline void AsyncWorker::OnExecute(napi_env /*env*/, void* this_pointer) {
32013212
AsyncWorker* self = static_cast<AsyncWorker*>(this_pointer);
32023213
#ifdef NAPI_CPP_EXCEPTIONS
32033214
try {
@@ -3211,7 +3222,7 @@ inline void AsyncWorker::OnExecute(napi_env env, void* this_pointer) {
32113222
}
32123223

32133224
inline void AsyncWorker::OnWorkComplete(
3214-
napi_env env, napi_status status, void* this_pointer) {
3225+
napi_env /*env*/, napi_status status, void* this_pointer) {
32153226
AsyncWorker* self = static_cast<AsyncWorker*>(this_pointer);
32163227
if (status != napi_cancelled) {
32173228
HandleScope scope(self->_env);

test/arraybuffer.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Value CreateExternalBufferWithFinalize(const CallbackInfo& info) {
6363
info.Env(),
6464
data,
6565
testLength,
66-
[](Env env, void* finalizeData) {
66+
[](Env /*env*/, void* finalizeData) {
6767
delete[] static_cast<uint8_t*>(finalizeData);
6868
finalizeCount++;
6969
});
@@ -92,7 +92,7 @@ Value CreateExternalBufferWithFinalizeHint(const CallbackInfo& info) {
9292
info.Env(),
9393
data,
9494
testLength,
95-
[](Env env, void* finalizeData, char* finalizeHint) {
95+
[](Env /*env*/, void* finalizeData, char* /*finalizeHint*/) {
9696
delete[] static_cast<uint8_t*>(finalizeData);
9797
finalizeCount++;
9898
},

test/binding.gyp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
],
2929
'include_dirs': ["<!@(node -p \"require('../').include\")"],
3030
'dependencies': ["<!(node -p \"require('../').gyp\")"],
31+
'cflags': [ '-Werror', '-Wall', '-Wextra', '-Wpedantic', '-Wunused-parameter' ],
32+
'cflags_cc': [ '-Werror', '-Wall', '-Wextra', '-Wpedantic', '-Wunused-parameter' ],
3133
},
3234
'targets': [
3335
{

test/buffer.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Value CreateExternalBufferWithFinalize(const CallbackInfo& info) {
6868
info.Env(),
6969
data,
7070
testLength,
71-
[](Env env, uint16_t* finalizeData) {
71+
[](Env /*env*/, uint16_t* finalizeData) {
7272
delete[] finalizeData;
7373
finalizeCount++;
7474
});
@@ -97,7 +97,7 @@ Value CreateExternalBufferWithFinalizeHint(const CallbackInfo& info) {
9797
info.Env(),
9898
data,
9999
testLength,
100-
[](Env env, uint16_t* finalizeData, char* finalizeHint) {
100+
[](Env /*env*/, uint16_t* finalizeData, char* /*finalizeHint*/) {
101101
delete[] finalizeData;
102102
finalizeCount++;
103103
},

test/error.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ void CatchAndRethrowErrorThatEscapesScope(const CallbackInfo& info) {
154154

155155
#endif // NAPI_CPP_EXCEPTIONS
156156

157-
void ThrowFatalError(const CallbackInfo& info) {
157+
void ThrowFatalError(const CallbackInfo& /*info*/) {
158158
Error::Fatal("Error::ThrowFatalError", "This is a fatal error");
159159
}
160160

test/external.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Value CreateExternal(const CallbackInfo& info) {
1515
Value CreateExternalWithFinalize(const CallbackInfo& info) {
1616
finalizeCount = 0;
1717
return External<int>::New(info.Env(), new int(1),
18-
[](Env env, int* data) {
18+
[](Env /*env*/, int* data) {
1919
delete data;
2020
finalizeCount++;
2121
});
@@ -25,7 +25,7 @@ Value CreateExternalWithFinalizeHint(const CallbackInfo& info) {
2525
finalizeCount = 0;
2626
char* hint = nullptr;
2727
return External<int>::New(info.Env(), new int(1),
28-
[](Env env, int* data, char* hint) {
28+
[](Env /*env*/, int* data, char* /*hint*/) {
2929
delete data;
3030
finalizeCount++;
3131
},

0 commit comments

Comments
 (0)