Skip to content

Commit 5e4df73

Browse files
committed
[clang-tidy] Enable C23 support in modernize-use-nullptr
C23 introduces the nullptr constant similar to C++11 which means that the checker modernize-use-nullptr can be used on C23 code as well. See N3042: https://open-std.org/JTC1/SC22/WG14/www/docs/n3042.htm
1 parent 418e4b0 commit 5e4df73

File tree

5 files changed

+146
-3
lines changed

5 files changed

+146
-3
lines changed

clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class UseNullptrCheck : public ClangTidyCheck {
1919
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
2020
// FIXME this should be CPlusPlus11 but that causes test cases to
2121
// erroneously fail.
22-
return LangOpts.CPlusPlus;
22+
return LangOpts.CPlusPlus || LangOpts.C23;
2323
}
2424
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
2525
void registerMatchers(ast_matchers::MatchFinder *Finder) override;

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,10 @@ Changes in existing checks
269269
don't remove parentheses used in ``sizeof`` calls when they have array index
270270
accesses as arguments.
271271

272+
- Improved :doc:`modernize-use-nullptr
273+
<clang-tidy/checks/modernize/use-nullptr>` check to include support for
274+
``C23``, which also has introduced the ``nullptr`` keyword.
275+
272276
- Improved :doc:`modernize-use-override
273277
<clang-tidy/checks/modernize/use-override>` check to also remove any trailing
274278
whitespace when deleting the ``virtual`` keyword.

clang-tools-extra/docs/clang-tidy/checks/modernize/use-nullptr.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ modernize-use-nullptr
44
=====================
55

66
The check converts the usage of null pointer constants (e.g. ``NULL``, ``0``)
7-
to use the new C++11 ``nullptr`` keyword.
7+
to use the new C++11 and C23 ``nullptr`` keyword.
88

99
Example
1010
-------
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
// RUN: %check_clang_tidy %s modernize-use-nullptr %t -- -- -std=c23
2+
3+
#define NULL 0
4+
5+
void test_assignment() {
6+
int *p1 = 0;
7+
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use nullptr [modernize-use-nullptr]
8+
// CHECK-FIXES: int *p1 = nullptr;
9+
p1 = 0;
10+
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use nullptr
11+
// CHECK-FIXES: p1 = nullptr;
12+
13+
int *p2 = NULL;
14+
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use nullptr
15+
// CHECK-FIXES: int *p2 = nullptr;
16+
17+
p2 = p1;
18+
// CHECK-FIXES: p2 = p1;
19+
20+
const int null = 0;
21+
int *p3 = &null;
22+
23+
p3 = NULL;
24+
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use nullptr
25+
// CHECK-FIXES: p3 = nullptr;
26+
27+
int *p4 = p3;
28+
29+
int i1 = 0;
30+
31+
int i2 = NULL;
32+
33+
int i3 = null;
34+
35+
int *p5, *p6, *p7;
36+
p5 = p6 = p7 = NULL;
37+
// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: use nullptr
38+
// CHECK-FIXES: p5 = p6 = p7 = nullptr;
39+
}
40+
41+
void test_function(int *p) {}
42+
43+
void test_function_no_ptr_param(int i) {}
44+
45+
void test_function_call() {
46+
test_function(0);
47+
// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use nullptr
48+
// CHECK-FIXES: test_function(nullptr);
49+
50+
test_function(NULL);
51+
// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use nullptr
52+
// CHECK-FIXES: test_function(nullptr);
53+
54+
test_function_no_ptr_param(0);
55+
}
56+
57+
char *test_function_return1() {
58+
return 0;
59+
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use nullptr
60+
// CHECK-FIXES: return nullptr;
61+
}
62+
63+
void *test_function_return2() {
64+
return NULL;
65+
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use nullptr
66+
// CHECK-FIXES: return nullptr;
67+
}
68+
69+
int test_function_return4() {
70+
return 0;
71+
}
72+
73+
int test_function_return5() {
74+
return NULL;
75+
}
76+
77+
int *test_function_return_cast1() {
78+
return(int)0;
79+
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use nullptr
80+
// CHECK-FIXES: return nullptr;
81+
}
82+
83+
int *test_function_return_cast2() {
84+
#define RET return
85+
RET(int)0;
86+
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use nullptr
87+
// CHECK-FIXES: RET nullptr;
88+
#undef RET
89+
}
90+
91+
// Test parentheses expressions resulting in a nullptr.
92+
int *test_parentheses_expression1() {
93+
return(0);
94+
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use nullptr
95+
// CHECK-FIXES: return(nullptr);
96+
}
97+
98+
int *test_parentheses_expression2() {
99+
return((int)(0.0f));
100+
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use nullptr
101+
// CHECK-FIXES: return(nullptr);
102+
}
103+
104+
int *test_nested_parentheses_expression() {
105+
return((((0))));
106+
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use nullptr
107+
// CHECK-FIXES: return((((nullptr))));
108+
}
109+
110+
void test_const_pointers() {
111+
const int *const_p1 = 0;
112+
// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use nullptr
113+
// CHECK-FIXES: const int *const_p1 = nullptr;
114+
const int *const_p2 = NULL;
115+
// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use nullptr
116+
// CHECK-FIXES: const int *const_p2 = nullptr;
117+
const int *const_p3 = (int)0;
118+
// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use nullptr
119+
// CHECK-FIXES: const int *const_p3 = nullptr;
120+
const int *const_p4 = (int)0.0f;
121+
// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use nullptr
122+
// CHECK-FIXES: const int *const_p4 = nullptr;
123+
}
124+
125+
void test_nested_implicit_cast_expr() {
126+
int func0(void*, void*);
127+
int func1(int, void*, void*);
128+
129+
(double)func1(0, 0, 0);
130+
// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: use nullptr
131+
// CHECK-MESSAGES: :[[@LINE-2]]:23: warning: use nullptr
132+
// CHECK-FIXES: (double)func1(0, nullptr, nullptr);
133+
(double)func1(func0(0, 0), 0, 0);
134+
// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: use nullptr
135+
// CHECK-MESSAGES: :[[@LINE-2]]:26: warning: use nullptr
136+
// CHECK-MESSAGES: :[[@LINE-3]]:30: warning: use nullptr
137+
// CHECK-MESSAGES: :[[@LINE-4]]:33: warning: use nullptr
138+
// CHECK-FIXES: (double)func1(func0(nullptr, nullptr), nullptr, nullptr);
139+
}

clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: clang-tidy %s -checks=-*,modernize-use-nullptr -- | count 0
1+
// RUN: clang-tidy %s -checks=-*,modernize-use-nullptr -- -std=c17 | count 0
22

33
// Note: this test expects no diagnostics, but FileCheck cannot handle that,
44
// hence the use of | count 0.

0 commit comments

Comments
 (0)