1// RUN: %check_clang_tidy %s bugprone-reserved-identifier %t -- -- \
2// RUN: -I%S/Inputs/reserved-identifier \
3// RUN: -isystem %S/Inputs/reserved-identifier/system
4
5// no warnings expected without -header-filter=
6#include "user-header.h"
7#include <system-header.h>
8
9#define _MACRO(m) int m = 0
10// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: declaration uses identifier '_MACRO', which is a reserved identifier [bugprone-reserved-identifier]
11// CHECK-FIXES: {{^}}#define MACRO(m) int m = 0{{$}}
12
13namespace _Ns {
14// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: declaration uses identifier '_Ns', which is a reserved identifier [bugprone-reserved-identifier]
15// CHECK-FIXES: {{^}}namespace Ns {{{$}}
16
17class _Object {
18 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: declaration uses identifier '_Object', which is a reserved identifier [bugprone-reserved-identifier]
19 // CHECK-FIXES: {{^}}class Object {{{$}}
20 int _Member;
21 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: declaration uses identifier '_Member', which is a reserved identifier [bugprone-reserved-identifier]
22 // CHECK-FIXES: {{^}} int Member;{{$}}
23};
24
25float _Global;
26// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: declaration uses identifier '_Global', which is a reserved identifier [bugprone-reserved-identifier]
27// CHECK-FIXES: {{^}}float Global;{{$}}
28
29void _Function() {}
30// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: declaration uses identifier '_Function', which is a reserved identifier [bugprone-reserved-identifier]
31// CHECK-FIXES: {{^}}void Function() {}{{$}}
32
33using _Alias = int;
34// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: declaration uses identifier '_Alias', which is a reserved identifier [bugprone-reserved-identifier]
35// CHECK-FIXES: {{^}}using Alias = int;{{$}}
36
37template <typename _TemplateParam>
38// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: declaration uses identifier '_TemplateParam', which is a reserved identifier [bugprone-reserved-identifier]
39// CHECK-FIXES: {{^}}template <typename TemplateParam>{{$}}
40struct S {};
41
42} // namespace _Ns
43
44//
45
46#define __macro(m) int m = 0
47// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: declaration uses identifier '__macro', which is a reserved identifier [bugprone-reserved-identifier]
48// CHECK-FIXES: {{^}}#define _macro(m) int m = 0{{$}}
49
50namespace __ns {
51// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: declaration uses identifier '__ns', which is a reserved identifier [bugprone-reserved-identifier]
52// CHECK-FIXES: {{^}}namespace ns {{{$}}
53class __object {
54 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: declaration uses identifier '__object', which is a reserved identifier [bugprone-reserved-identifier]
55 // CHECK-FIXES: {{^}}class _object {{{$}}
56 int __member;
57 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: declaration uses identifier '__member', which is a reserved identifier [bugprone-reserved-identifier]
58 // CHECK-FIXES: {{^}} int _member;{{$}}
59};
60
61float __global;
62// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: declaration uses identifier '__global', which is a reserved identifier [bugprone-reserved-identifier]
63// CHECK-FIXES: {{^}}float _global;{{$}}
64
65void __function() {}
66// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: declaration uses identifier '__function', which is a reserved identifier [bugprone-reserved-identifier]
67// CHECK-FIXES: {{^}}void _function() {}{{$}}
68
69using __alias = int;
70// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: declaration uses identifier '__alias', which is a reserved identifier [bugprone-reserved-identifier]
71// CHECK-FIXES: {{^}}using _alias = int;{{$}}
72
73template <typename __templateParam>
74// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: declaration uses identifier '__templateParam', which is a reserved identifier [bugprone-reserved-identifier]
75// CHECK-FIXES: {{^}}template <typename _templateParam>{{$}}
76struct S {};
77
78} // namespace __ns
79
80//
81
82#define macro___m(m) int m = 0
83// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: declaration uses identifier 'macro___m', which is a reserved identifier [bugprone-reserved-identifier]
84// CHECK-FIXES: {{^}}#define macro_m(m) int m = 0{{$}}
85
86namespace ns___n {
87// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: declaration uses identifier 'ns___n', which is a reserved identifier [bugprone-reserved-identifier]
88// CHECK-FIXES: {{^}}namespace ns_n {{{$}}
89class object___o {
90 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: declaration uses identifier 'object___o', which is a reserved identifier [bugprone-reserved-identifier]
91 // CHECK-FIXES: {{^}}class object_o {{{$}}
92 int member___m;
93 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: declaration uses identifier 'member___m', which is a reserved identifier [bugprone-reserved-identifier]
94 // CHECK-FIXES: {{^}} int member_m;{{$}}
95};
96
97float global___g;
98// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: declaration uses identifier 'global___g', which is a reserved identifier [bugprone-reserved-identifier]
99// CHECK-FIXES: {{^}}float global_g;{{$}}
100
101void function___f() {}
102// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: declaration uses identifier 'function___f', which is a reserved identifier [bugprone-reserved-identifier]
103// CHECK-FIXES: {{^}}void function_f() {}{{$}}
104
105using alias___a = int;
106// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: declaration uses identifier 'alias___a', which is a reserved identifier [bugprone-reserved-identifier]
107// CHECK-FIXES: {{^}}using alias_a = int;{{$}}
108
109template <typename templateParam___t>
110// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: declaration uses identifier 'templateParam___t', which is a reserved identifier [bugprone-reserved-identifier]
111// CHECK-FIXES: {{^}}template <typename templateParam_t>{{$}}
112struct S {};
113
114} // namespace ns___n
115
116//
117
118// OK, this rule does not apply to macros:
119// https://github.com/llvm/llvm-project/issues/64130#issuecomment-1655751676
120#define _macro(m) int m = 0
121
122namespace _ns {
123// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: declaration uses identifier '_ns', which is reserved in the global namespace [bugprone-reserved-identifier]
124// CHECK-FIXES: {{^}}namespace ns {{{$}}
125int _i;
126// no warning
127} // namespace _ns
128class _object {
129 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: declaration uses identifier '_object', which is reserved in the global namespace [bugprone-reserved-identifier]
130 // CHECK-FIXES: {{^}}class object {{{$}}
131 int _member;
132 // no warning
133};
134float _global;
135// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: declaration uses identifier '_global', which is reserved in the global namespace [bugprone-reserved-identifier]
136// CHECK-FIXES: {{^}}float global;{{$}}
137void _function() {}
138// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: declaration uses identifier '_function', which is reserved in the global namespace [bugprone-reserved-identifier]
139// CHECK-FIXES: {{^}}void function() {}{{$}}
140using _alias = int;
141// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: declaration uses identifier '_alias', which is reserved in the global namespace [bugprone-reserved-identifier]
142// CHECK-FIXES: {{^}}using alias = int;{{$}}
143template <typename _templateParam> // no warning, template params are not in the global namespace
144struct S {};
145
146void _float() {}
147// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: declaration uses identifier '_float', which is reserved in the global namespace; cannot be fixed because 'float' would conflict with a keyword [bugprone-reserved-identifier]
148// CHECK-FIXES: {{^}}void _float() {}{{$}}
149
150#define SOME_MACRO
151int SOME__MACRO;
152// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: declaration uses identifier 'SOME__MACRO', which is a reserved identifier; cannot be fixed because 'SOME_MACRO' would conflict with a macro definition [bugprone-reserved-identifier]
153// CHECK-FIXES: {{^}}int SOME__MACRO;{{$}}
154
155void _TWO__PROBLEMS() {}
156// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: declaration uses identifier '_TWO__PROBLEMS', which is a reserved identifier [bugprone-reserved-identifier]
157// CHECK-FIXES: {{^}}void TWO_PROBLEMS() {}{{$}}
158void _two__problems() {}
159// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: declaration uses identifier '_two__problems', which is a reserved identifier [bugprone-reserved-identifier]
160// CHECK-FIXES: {{^}}void two_problems() {}{{$}}
161
162int __;
163// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: declaration uses identifier '__', which is a reserved identifier; cannot be fixed automatically [bugprone-reserved-identifier]
164// CHECK-FIXES: {{^}}int __;{{$}}
165
166int _________;
167// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: declaration uses identifier '_________', which is a reserved identifier; cannot be fixed automatically [bugprone-reserved-identifier]
168// CHECK-FIXES: {{^}}int _________;{{$}}
169
170int _;
171// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: declaration uses identifier '_', which is reserved in the global namespace; cannot be fixed automatically [bugprone-reserved-identifier]
172// CHECK-FIXES: {{^}}int _;{{$}}
173
174// This should not trigger a warning
175// https://github.com/llvm/llvm-project/issues/52895
176#define _5_kmph_rpm 459
177
178// these should pass
179#define MACRO(m) int m = 0
180
181namespace Ns {
182class Object {
183 int Member;
184};
185float Global;
186
187void Function() {}
188using Alias = int;
189template <typename TemplateParam>
190struct S {};
191} // namespace Ns
192namespace ns_ {
193class object_ {
194 int member_;
195};
196float global_;
197void function_() {}
198using alias_ = int;
199template <typename templateParam_>
200struct S {};
201} // namespace ns_
202
203class object_ {
204 int member_;
205};
206float global_;
207void function_() {}
208using alias_ = int;
209template <typename templateParam_>
210struct S_ {};
211

source code of clang-tools-extra/test/clang-tidy/checkers/bugprone/reserved-identifier.cpp