Skip to content

Commit 12c8b4d

Browse files
committed
tools: add cpplint rule for NULL usage
This commit is a suggestion for adding a rule for NULL usages in the code base. This will currently report a number of errors which could be ignored using // NOLINT (readability/null_usage) PR-URL: nodejs#17373 Reviewed-By: Jon Moss <me@jonathanmoss.me> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent e32bbbf commit 12c8b4d

File tree

8 files changed

+49
-19
lines changed

8 files changed

+49
-19
lines changed

src/node.h

+8-7
Original file line numberDiff line numberDiff line change
@@ -501,13 +501,13 @@ extern "C" NODE_EXTERN void node_module_register(void* mod);
501501
{ \
502502
NODE_MODULE_VERSION, \
503503
flags, \
504-
NULL, \
504+
NULL, /* NOLINT (readability/null_usage) */ \
505505
__FILE__, \
506506
(node::addon_register_func) (regfunc), \
507-
NULL, \
507+
NULL, /* NOLINT (readability/null_usage) */ \
508508
NODE_STRINGIFY(modname), \
509509
priv, \
510-
NULL \
510+
NULL /* NOLINT (readability/null_usage) */ \
511511
}; \
512512
NODE_C_CTOR(_register_ ## modname) { \
513513
node_module_register(&_module); \
@@ -520,23 +520,24 @@ extern "C" NODE_EXTERN void node_module_register(void* mod);
520520
{ \
521521
NODE_MODULE_VERSION, \
522522
flags, \
523-
NULL, \
523+
NULL, /* NOLINT (readability/null_usage) */ \
524524
__FILE__, \
525-
NULL, \
525+
NULL, /* NOLINT (readability/null_usage) */ \
526526
(node::addon_context_register_func) (regfunc), \
527527
NODE_STRINGIFY(modname), \
528528
priv, \
529-
NULL \
529+
NULL /* NOLINT (readability/null_usage) */ \
530530
}; \
531531
NODE_C_CTOR(_register_ ## modname) { \
532532
node_module_register(&_module); \
533533
} \
534534
}
535535

536536
#define NODE_MODULE(modname, regfunc) \
537-
NODE_MODULE_X(modname, regfunc, NULL, 0)
537+
NODE_MODULE_X(modname, regfunc, NULL, 0) // NOLINT (readability/null_usage)
538538

539539
#define NODE_MODULE_CONTEXT_AWARE(modname, regfunc) \
540+
/* NOLINTNEXTLINE (readability/null_usage) */ \
540541
NODE_MODULE_CONTEXT_AWARE_X(modname, regfunc, NULL, 0)
541542

542543
/*

src/node_api.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ typedef struct {
100100
EXTERN_C_END
101101

102102
#define NAPI_MODULE(modname, regfunc) \
103-
NAPI_MODULE_X(modname, regfunc, NULL, 0)
103+
NAPI_MODULE_X(modname, regfunc, NULL, 0) // NOLINT (readability/null_usage)
104104

105105
#define NAPI_AUTO_LENGTH SIZE_MAX
106106

src/node_win32_etw_provider-inl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ extern int events_enabled;
115115
DWORD status = event_write(node_provider, \
116116
&eventDescriptor, \
117117
0, \
118-
NULL); \
118+
NULL); // NOLINT (readability/null_usage) \
119119
CHECK_EQ(status, ERROR_SUCCESS);
120120

121121

test/addons-napi/7_factory_wrap/binding.cc

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ napi_value Init(napi_env env, napi_value exports) {
1616
NAPI_CALL(env, MyObject::Init(env));
1717

1818
NAPI_CALL(env,
19+
// NOLINTNEXTLINE (readability/null_usage)
1920
napi_create_function(env, "exports", -1, CreateObject, NULL, &exports));
2021
return exports;
2122
}

test/addons-napi/test_make_callback/binding.cc

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ napi_value MakeCallback(napi_env env, napi_callback_info info) {
88
const int kMaxArgs = 10;
99
size_t argc = kMaxArgs;
1010
napi_value args[kMaxArgs];
11+
// NOLINTNEXTLINE (readability/null_usage)
1112
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
1213

1314
NAPI_ASSERT(env, argc > 0, "Wrong number of arguments");
@@ -47,6 +48,7 @@ napi_value MakeCallback(napi_env env, napi_callback_info info) {
4748
napi_value Init(napi_env env, napi_value exports) {
4849
napi_value fn;
4950
NAPI_CALL(env, napi_create_function(
51+
// NOLINTNEXTLINE (readability/null_usage)
5052
env, NULL, NAPI_AUTO_LENGTH, MakeCallback, NULL, &fn));
5153
NAPI_CALL(env, napi_set_named_property(env, exports, "makeCallback", fn));
5254
return exports;

test/addons-napi/test_make_callback_recurse/binding.cc

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace {
77
napi_value MakeCallback(napi_env env, napi_callback_info info) {
88
size_t argc = 2;
99
napi_value args[2];
10+
// NOLINTNEXTLINE (readability/null_usage)
1011
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
1112

1213
napi_value recv = args[0];
@@ -21,6 +22,7 @@ napi_value MakeCallback(napi_env env, napi_callback_info info) {
2122
napi_value Init(napi_env env, napi_value exports) {
2223
napi_value fn;
2324
NAPI_CALL(env, napi_create_function(
25+
// NOLINTNEXTLINE (readability/null_usage)
2426
env, NULL, NAPI_AUTO_LENGTH, MakeCallback, NULL, &fn));
2527
NAPI_CALL(env, napi_set_named_property(env, exports, "makeCallback", fn));
2628
return exports;

tools/cpplint.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,6 @@
499499
_ALT_TOKEN_REPLACEMENT_PATTERN = re.compile(
500500
r'[ =()](' + ('|'.join(_ALT_TOKEN_REPLACEMENT.keys())) + r')(?=[ (]|$)')
501501

502-
503502
# These constants define types of headers for use with
504503
# _IncludeState.CheckNextIncludeOrder().
505504
_C_SYS_HEADER = 1
@@ -526,6 +525,8 @@
526525
# Match string that indicates we're working on a Linux Kernel file.
527526
_SEARCH_KERNEL_FILE = re.compile(r'\b(?:LINT_KERNEL_FILE)')
528527

528+
_NULL_TOKEN_PATTERN = re.compile(r'\bNULL\b')
529+
529530
_regexp_compile_cache = {}
530531

531532
# {str, set(int)}: a map from error categories to sets of linenumbers
@@ -4156,6 +4157,27 @@ def CheckAltTokens(filename, clean_lines, linenum, error):
41564157
'Use operator %s instead of %s' % (
41574158
_ALT_TOKEN_REPLACEMENT[match.group(1)], match.group(1)))
41584159

4160+
def CheckNullTokens(filename, clean_lines, linenum, error):
4161+
"""Check NULL usage.
4162+
4163+
Args:
4164+
filename: The name of the current file.
4165+
clean_lines: A CleansedLines instance containing the file.
4166+
linenum: The number of the line to check.
4167+
error: The function to call with any errors found.
4168+
"""
4169+
line = clean_lines.elided[linenum]
4170+
4171+
# Avoid preprocessor lines
4172+
if Match(r'^\s*#', line):
4173+
return
4174+
4175+
if line.find('/*') >= 0 or line.find('*/') >= 0:
4176+
return
4177+
4178+
for match in _NULL_TOKEN_PATTERN.finditer(line):
4179+
error(filename, linenum, 'readability/null_usage', 2,
4180+
'Use nullptr instead of NULL')
41594181

41604182
def GetLineWidth(line):
41614183
"""Determines the width of the line in column positions.
@@ -4294,6 +4316,7 @@ def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state,
42944316
CheckSpacingForFunctionCall(filename, clean_lines, linenum, error)
42954317
CheckCheck(filename, clean_lines, linenum, error)
42964318
CheckAltTokens(filename, clean_lines, linenum, error)
4319+
CheckNullTokens(filename, clean_lines, linenum, error)
42974320
classinfo = nesting_state.InnermostClass()
42984321
if classinfo:
42994322
CheckSectionSpacing(filename, clean_lines, classinfo, linenum, error)

tools/icu/iculslocs.cc

+10-9
Original file line numberDiff line numberDiff line change
@@ -231,14 +231,14 @@ int dumpAllButInstalledLocales(int lev,
231231
int list(const char* toBundle) {
232232
UErrorCode status = U_ZERO_ERROR;
233233

234-
FILE* bf = NULL;
234+
FILE* bf = NULL; // NOLINT (readability/null_usage)
235235

236-
if (toBundle != NULL) {
236+
if (toBundle != NULL) { // NOLINT (readability/null_usage)
237237
if (VERBOSE) {
238238
printf("writing to bundle %s\n", toBundle);
239239
}
240240
bf = fopen(toBundle, "wb");
241-
if (bf == NULL) {
241+
if (bf == NULL) { // NOLINT (readability/null_usage)
242242
printf("ERROR: Could not open '%s' for writing.\n", toBundle);
243243
return 1;
244244
}
@@ -258,6 +258,7 @@ int list(const char* toBundle) {
258258
ures_openDirect(packageName.data(), locale, &status));
259259
ASSERT_SUCCESS(&status, "while opening the bundle");
260260
LocalUResourceBundlePointer installedLocales(
261+
// NOLINTNEXTLINE (readability/null_usage)
261262
ures_getByKey(bund.getAlias(), INSTALLEDLOCALES, NULL, &status));
262263
ASSERT_SUCCESS(&status, "while fetching installed locales");
263264

@@ -266,7 +267,7 @@ int list(const char* toBundle) {
266267
printf("Locales: %d\n", count);
267268
}
268269

269-
if (bf != NULL) {
270+
if (bf != NULL) { // NOLINT (readability/null_usage)
270271
// write the HEADER
271272
fprintf(bf,
272273
"// Warning this file is automatically generated\n"
@@ -310,17 +311,17 @@ int list(const char* toBundle) {
310311

311312
UBool exists;
312313
if (localeExists(key, &exists)) {
313-
if (bf != NULL) fclose(bf);
314+
if (bf != NULL) fclose(bf); // NOLINT (readability/null_usage)
314315
return 1; // get out.
315316
}
316317
if (exists) {
317318
validCount++;
318319
printf("%s\n", key);
319-
if (bf != NULL) {
320+
if (bf != NULL) { // NOLINT (readability/null_usage)
320321
fprintf(bf, " %s {\"\"}\n", key);
321322
}
322323
} else {
323-
if (bf != NULL) {
324+
if (bf != NULL) { // NOLINT (readability/null_usage)
324325
fprintf(bf, "// %s {\"\"}\n", key);
325326
}
326327
if (VERBOSE) {
@@ -329,7 +330,7 @@ int list(const char* toBundle) {
329330
}
330331
}
331332

332-
if (bf != NULL) {
333+
if (bf != NULL) { // NOLINT (readability/null_usage)
333334
fprintf(bf, " } // %d/%d valid\n", validCount, count);
334335
// write the HEADER
335336
fprintf(bf, "}\n");
@@ -371,7 +372,7 @@ int main(int argc, const char* argv[]) {
371372
usage();
372373
return 0;
373374
} else if (!strcmp(arg, "-l")) {
374-
if (list(NULL)) {
375+
if (list(NULL)) { // NOLINT (readability/null_usage)
375376
return 1;
376377
}
377378
} else if (!strcmp(arg, "-b") && (argsLeft >= 1)) {

0 commit comments

Comments
 (0)