Skip to content

Commit 201b190

Browse files
committed
[clang-tidy] Add more constexpr options to readability-identifier-naming
1 parent 5401210 commit 201b190

File tree

4 files changed

+232
-6
lines changed

4 files changed

+232
-6
lines changed

clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,18 @@ namespace readability {
8383
m(Member) \
8484
m(ClassConstant) \
8585
m(ClassMember) \
86+
m(ClassConstexpr) \
87+
m(GlobalConstexprVariable) \
8688
m(GlobalConstant) \
8789
m(GlobalConstantPointer) \
8890
m(GlobalPointer) \
8991
m(GlobalVariable) \
92+
m(LocalConstexprVariable) \
9093
m(LocalConstant) \
9194
m(LocalConstantPointer) \
9295
m(LocalPointer) \
9396
m(LocalVariable) \
97+
m(StaticConstexprVariable) \
9498
m(StaticConstant) \
9599
m(StaticVariable) \
96100
m(Constant) \
@@ -1497,8 +1501,22 @@ StyleKind IdentifierNamingCheck::findStyleKindForField(
14971501
StyleKind IdentifierNamingCheck::findStyleKindForVar(
14981502
const VarDecl *Var, QualType Type,
14991503
ArrayRef<std::optional<NamingStyle>> NamingStyles) const {
1500-
if (Var->isConstexpr() && NamingStyles[SK_ConstexprVariable])
1501-
return SK_ConstexprVariable;
1504+
if (Var->isConstexpr()) {
1505+
if (Var->isStaticDataMember() && NamingStyles[SK_ClassConstexpr])
1506+
return SK_ClassConstexpr;
1507+
1508+
if (Var->isFileVarDecl() && NamingStyles[SK_GlobalConstexprVariable])
1509+
return SK_GlobalConstexprVariable;
1510+
1511+
if (Var->isStaticLocal() && NamingStyles[SK_StaticConstexprVariable])
1512+
return SK_StaticConstexprVariable;
1513+
1514+
if (Var->isLocalVarDecl() && NamingStyles[SK_LocalConstexprVariable])
1515+
return SK_LocalConstexprVariable;
1516+
1517+
if (NamingStyles[SK_ConstexprVariable])
1518+
return SK_ConstexprVariable;
1519+
}
15021520

15031521
if (!Type.isNull() && Type.isConstQualified()) {
15041522
if (Var->isStaticDataMember() && NamingStyles[SK_ClassConstant])

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,8 @@ Changes in existing checks
379379
- Improved :doc:`readability-identifier-naming
380380
<clang-tidy/checks/readability/identifier-naming>` check by ignoring
381381
declarations and macros in system headers. The documentation is also improved
382-
to differentiate the general options from the specific ones.
382+
to differentiate the general options from the specific ones. Options for
383+
fine-grained control over ``constexpr`` variables were added.
383384

384385
- Improved :doc:`readability-qualified-auto
385386
<clang-tidy/checks/readability/qualified-auto>` check by adding the option

clang-tools-extra/docs/clang-tidy/checks/readability/identifier-naming.rst

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ The available options are summarized below:
5959

6060
- :option:`AbstractClassCase`, :option:`AbstractClassPrefix`, :option:`AbstractClassSuffix`, :option:`AbstractClassIgnoredRegexp`, :option:`AbstractClassHungarianPrefix`
6161
- :option:`ClassCase`, :option:`ClassPrefix`, :option:`ClassSuffix`, :option:`ClassIgnoredRegexp`, :option:`ClassHungarianPrefix`
62+
- :option:`ClassConstexprCase`, :option:`ClassConstexprPrefix`, :option:`ClassConstexprSuffix`, :option:`ClassConstexprIgnoredRegexp`, :option:`ClassConstexprHungarianPrefix`
6263
- :option:`ClassConstantCase`, :option:`ClassConstantPrefix`, :option:`ClassConstantSuffix`, :option:`ClassConstantIgnoredRegexp`, :option:`ClassConstantHungarianPrefix`
6364
- :option:`ClassMemberCase`, :option:`ClassMemberPrefix`, :option:`ClassMemberSuffix`, :option:`ClassMemberIgnoredRegexp`, :option:`ClassMemberHungarianPrefix`
6465
- :option:`ClassMethodCase`, :option:`ClassMethodPrefix`, :option:`ClassMethodSuffix`, :option:`ClassMethodIgnoredRegexp`
@@ -73,12 +74,14 @@ The available options are summarized below:
7374
- :option:`EnumCase`, :option:`EnumPrefix`, :option:`EnumSuffix`, :option:`EnumIgnoredRegexp`
7475
- :option:`EnumConstantCase`, :option:`EnumConstantPrefix`, :option:`EnumConstantSuffix`, :option:`EnumConstantIgnoredRegexp`, :option:`EnumConstantHungarianPrefix`
7576
- :option:`FunctionCase`, :option:`FunctionPrefix`, :option:`FunctionSuffix`, :option:`FunctionIgnoredRegexp`
77+
- :option:`GlobalConstexprVariableCase`, :option:`GlobalConstexprVariablePrefix`, :option:`GlobalConstexprVariableSuffix`, :option:`GlobalConstexprVariableIgnoredRegexp`, :option:`GlobalConstexprVariableHungarianPrefix`
7678
- :option:`GlobalConstantCase`, :option:`GlobalConstantPrefix`, :option:`GlobalConstantSuffix`, :option:`GlobalConstantIgnoredRegexp`, :option:`GlobalConstantHungarianPrefix`
7779
- :option:`GlobalConstantPointerCase`, :option:`GlobalConstantPointerPrefix`, :option:`GlobalConstantPointerSuffix`, :option:`GlobalConstantPointerIgnoredRegexp`, :option:`GlobalConstantPointerHungarianPrefix`
7880
- :option:`GlobalFunctionCase`, :option:`GlobalFunctionPrefix`, :option:`GlobalFunctionSuffix`, :option:`GlobalFunctionIgnoredRegexp`
7981
- :option:`GlobalPointerCase`, :option:`GlobalPointerPrefix`, :option:`GlobalPointerSuffix`, :option:`GlobalPointerIgnoredRegexp`, :option:`GlobalPointerHungarianPrefix`
8082
- :option:`GlobalVariableCase`, :option:`GlobalVariablePrefix`, :option:`GlobalVariableSuffix`, :option:`GlobalVariableIgnoredRegexp`, :option:`GlobalVariableHungarianPrefix`
8183
- :option:`InlineNamespaceCase`, :option:`InlineNamespacePrefix`, :option:`InlineNamespaceSuffix`, :option:`InlineNamespaceIgnoredRegexp`
84+
- :option:`LocalConstexprVariableCase`, :option:`LocalConstexprVariablePrefix`, :option:`LocalConstexprVariableSuffix`, :option:`LocalConstexprVariableIgnoredRegexp`, :option:`LocalConstexprVariableHungarianPrefix`
8285
- :option:`LocalConstantCase`, :option:`LocalConstantPrefix`, :option:`LocalConstantSuffix`, :option:`LocalConstantIgnoredRegexp`, :option:`LocalConstantHungarianPrefix`
8386
- :option:`LocalConstantPointerCase`, :option:`LocalConstantPointerPrefix`, :option:`LocalConstantPointerSuffix`, :option:`LocalConstantPointerIgnoredRegexp`, :option:`LocalConstantPointerHungarianPrefix`
8487
- :option:`LocalPointerCase`, :option:`LocalPointerPrefix`, :option:`LocalPointerSuffix`, :option:`LocalPointerIgnoredRegexp`, :option:`LocalPointerHungarianPrefix`
@@ -97,6 +100,7 @@ The available options are summarized below:
97100
- :option:`PublicMemberCase`, :option:`PublicMemberPrefix`, :option:`PublicMemberSuffix`, :option:`PublicMemberIgnoredRegexp`, :option:`PublicMemberHungarianPrefix`
98101
- :option:`PublicMethodCase`, :option:`PublicMethodPrefix`, :option:`PublicMethodSuffix`, :option:`PublicMethodIgnoredRegexp`
99102
- :option:`ScopedEnumConstantCase`, :option:`ScopedEnumConstantPrefix`, :option:`ScopedEnumConstantSuffix`, :option:`ScopedEnumConstantIgnoredRegexp`
103+
- :option:`StaticConstexprVariableCase`, :option:`StaticConstexprVariablePrefix`, :option:`StaticConstexprVariableSuffix`, :option:`StaticConstexprVariableIgnoredRegexp`, :option:`StaticConstexprVariableHungarianPrefix`
100104
- :option:`StaticConstantCase`, :option:`StaticConstantPrefix`, :option:`StaticConstantSuffix`, :option:`StaticConstantIgnoredRegexp`, :option:`StaticConstantHungarianPrefix`
101105
- :option:`StaticVariableCase`, :option:`StaticVariablePrefix`, :option:`StaticVariableSuffix`, :option:`StaticVariableIgnoredRegexp`, :option:`StaticVariableHungarianPrefix`
102106
- :option:`StructCase`, :option:`StructPrefix`, :option:`StructSuffix`, :option:`StructIgnoredRegexp`
@@ -307,6 +311,58 @@ After:
307311
~pre_foo_post();
308312
};
309313

314+
.. option:: ClassConstexprCase
315+
316+
When defined, the check will ensure class ``constexpr`` names conform to
317+
the selected casing.
318+
319+
.. option:: ClassConstexprPrefix
320+
321+
When defined, the check will ensure class ``constexpr`` names will add the
322+
prefixed with the given value (regardless of casing).
323+
324+
.. option:: ClassConstexprIgnoredRegexp
325+
326+
Identifier naming checks won't be enforced for class ``constexpr`` names
327+
matching this regular expression.
328+
329+
.. option:: ClassConstexprSuffix
330+
331+
When defined, the check will ensure class ``constexpr`` names will add the
332+
suffix with the given value (regardless of casing).
333+
334+
.. option:: ClassConstexprHungarianPrefix
335+
336+
When enabled, the check ensures that the declared identifier will have a
337+
Hungarian notation prefix based on the declared type.
338+
339+
For example using values of:
340+
341+
- ClassConstexprCase of ``lower_case``
342+
- ClassConstexprPrefix of ``pre_``
343+
- ClassConstexprSuffix of ``_post``
344+
- ClassConstexprHungarianPrefix of ``On``
345+
346+
Identifies and/or transforms class ``constexpr`` variable names as follows:
347+
348+
Before:
349+
350+
.. code-block:: c++
351+
352+
class FOO {
353+
public:
354+
static constexpr int CLASS_CONSTEXPR;
355+
};
356+
357+
After:
358+
359+
.. code-block:: c++
360+
361+
class FOO {
362+
public:
363+
static const int pre_class_constexpr_post;
364+
};
365+
310366
.. option:: ClassConstantCase
311367

312368
When defined, the check will ensure class constant names conform to the
@@ -950,6 +1006,52 @@ After:
9501006
different style.
9511007
Default value is `true`.
9521008

1009+
.. option:: GlobalConstexprVariableCase
1010+
1011+
When defined, the check will ensure global ``constexpr`` variable names
1012+
conform to the selected casing.
1013+
1014+
.. option:: GlobalConstexprVariablePrefix
1015+
1016+
When defined, the check will ensure global ``constexpr`` variable names
1017+
will add the prefixed with the given value (regardless of casing).
1018+
1019+
.. option:: GlobalConstexprVariableIgnoredRegexp
1020+
1021+
Identifier naming checks won't be enforced for global ``constexpr``
1022+
variable names matching this regular expression.
1023+
1024+
.. option:: GlobalConstexprVariableSuffix
1025+
1026+
When defined, the check will ensure global ``constexpr`` variable names
1027+
will add the suffix with the given value (regardless of casing).
1028+
1029+
.. option:: GlobalConstexprVariableHungarianPrefix
1030+
1031+
When enabled, the check ensures that the declared identifier will have a
1032+
Hungarian notation prefix based on the declared type.
1033+
1034+
For example using values of:
1035+
1036+
- GlobalConstexprVariableCase of ``lower_case``
1037+
- GlobalConstexprVariablePrefix of ``pre_``
1038+
- GlobalConstexprVariableSuffix of ``_post``
1039+
- GlobalConstexprVariableHungarianPrefix of ``On``
1040+
1041+
Identifies and/or transforms global ``constexpr`` variable names as follows:
1042+
1043+
Before:
1044+
1045+
.. code-block:: c++
1046+
1047+
constexpr unsigned ImportantValue = 69;
1048+
1049+
After:
1050+
1051+
.. code-block:: c++
1052+
1053+
constexpr unsigned pre_important_value_post = 69;
1054+
9531055
.. option:: GlobalConstantCase
9541056

9551057
When defined, the check will ensure global constant names conform to the
@@ -1228,6 +1330,52 @@ After:
12281330
}
12291331
} // namespace FOO_NS
12301332

1333+
.. option:: LocalConstexprVariableCase
1334+
1335+
When defined, the check will ensure local ``constexpr`` variable names
1336+
conform to the selected casing.
1337+
1338+
.. option:: LocalConstexprVariablePrefix
1339+
1340+
When defined, the check will ensure local ``constexpr`` variable names will
1341+
add the prefixed with the given value (regardless of casing).
1342+
1343+
.. option:: LocalConstexprVariableIgnoredRegexp
1344+
1345+
Identifier naming checks won't be enforced for local ``constexpr`` variable
1346+
names matching this regular expression.
1347+
1348+
.. option:: LocalConstexprVariableSuffix
1349+
1350+
When defined, the check will ensure local ``constexpr`` variable names will
1351+
add the suffix with the given value (regardless of casing).
1352+
1353+
.. option:: LocalConstexprVariableHungarianPrefix
1354+
1355+
When enabled, the check ensures that the declared identifier will have a
1356+
Hungarian notation prefix based on the declared type.
1357+
1358+
For example using values of:
1359+
1360+
- LocalConstexprVariableCase of ``lower_case``
1361+
- LocalConstexprVariablePrefix of ``pre_``
1362+
- LocalConstexprVariableSuffix of ``_post``
1363+
- LocalConstexprVariableHungarianPrefix of ``On``
1364+
1365+
Identifies and/or transforms local ``constexpr`` variable names as follows:
1366+
1367+
Before:
1368+
1369+
.. code-block:: c++
1370+
1371+
void foo() { int const local_Constexpr = 420; }
1372+
1373+
After:
1374+
1375+
.. code-block:: c++
1376+
1377+
void foo() { int const pre_local_constexpr_post = 420; }
1378+
12311379
.. option:: LocalConstantCase
12321380

12331381
When defined, the check will ensure local constant names conform to the
@@ -2077,6 +2225,52 @@ After:
20772225

20782226
enum class FOO { pre_One_post, pre_Two_post, pre_Three_post };
20792227

2228+
.. option:: StaticConstexprVariableCase
2229+
2230+
When defined, the check will ensure static ``constexpr`` variable names
2231+
conform to the selected casing.
2232+
2233+
.. option:: StaticConstexprVariablePrefix
2234+
2235+
When defined, the check will ensure static ``constexpr`` variable names
2236+
will add the prefixed with the given value (regardless of casing).
2237+
2238+
.. option:: StaticConstexprVariableIgnoredRegexp
2239+
2240+
Identifier naming checks won't be enforced for static ``constexpr``
2241+
variable names matching this regular expression.
2242+
2243+
.. option:: StaticConstexprVariableSuffix
2244+
2245+
When defined, the check will ensure static ``constexpr`` variable names
2246+
will add the suffix with the given value (regardless of casing).
2247+
2248+
.. option:: StaticConstexprVariableHungarianPrefix
2249+
2250+
When enabled, the check ensures that the declared identifier will have a
2251+
Hungarian notation prefix based on the declared type.
2252+
2253+
For example using values of:
2254+
2255+
- StaticConstexprVariableCase of ``lower_case``
2256+
- StaticConstexprVariablePrefix of ``pre_``
2257+
- StaticConstexprVariableSuffix of ``_post``
2258+
- StaticConstexprVariableHungarianPrefix of ``On``
2259+
2260+
Identifies and/or transforms static ``constexpr`` variable names as follows:
2261+
2262+
Before:
2263+
2264+
.. code-block:: c++
2265+
2266+
static unsigned constexpr MyConstexprStatic_array[] = {1, 2, 3};
2267+
2268+
After:
2269+
2270+
.. code-block:: c++
2271+
2272+
static unsigned constexpr pre_my_constexpr_static_array_post[] = {1, 2, 3};
2273+
20802274
.. option:: StaticConstantCase
20812275

20822276
When defined, the check will ensure static constant names conform to the

clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
// RUN: readability-identifier-naming.ClassConstantCase: CamelCase, \
1111
// RUN: readability-identifier-naming.ClassConstantPrefix: 'k', \
1212
// RUN: readability-identifier-naming.ClassMemberCase: CamelCase, \
13+
// RUN: readability-identifier-naming.ClassConstexprCase: CamelCase, \
14+
// RUN: readability-identifier-naming.GlobalConstexprVariableCase: UPPER_CASE, \
1315
// RUN: readability-identifier-naming.ClassMethodCase: camelBack, \
1416
// RUN: readability-identifier-naming.ConceptCase: CamelCase, \
1517
// RUN: readability-identifier-naming.ConstantCase: UPPER_CASE, \
@@ -27,6 +29,7 @@
2729
// RUN: readability-identifier-naming.GlobalVariableCase: lower_case, \
2830
// RUN: readability-identifier-naming.GlobalVariablePrefix: 'g_', \
2931
// RUN: readability-identifier-naming.InlineNamespaceCase: lower_case, \
32+
// RUN: readability-identifier-naming.LocalConstexprVariableCase: CamelCase, \
3033
// RUN: readability-identifier-naming.LocalConstantCase: CamelCase, \
3134
// RUN: readability-identifier-naming.LocalConstantPrefix: 'k', \
3235
// RUN: readability-identifier-naming.LocalVariableCase: lower_case, \
@@ -47,6 +50,7 @@
4750
// RUN: readability-identifier-naming.ParameterPackCase: camelBack, \
4851
// RUN: readability-identifier-naming.PureFunctionCase: lower_case, \
4952
// RUN: readability-identifier-naming.PureMethodCase: camelBack, \
53+
// RUN: readability-identifier-naming.StaticConstexprVariableCase: UPPER_CASE, \
5054
// RUN: readability-identifier-naming.StaticConstantCase: UPPER_CASE, \
5155
// RUN: readability-identifier-naming.StaticVariableCase: camelBack, \
5256
// RUN: readability-identifier-naming.StaticVariablePrefix: 's_', \
@@ -186,8 +190,8 @@ enum class EMyEnumeration {
186190
};
187191

188192
constexpr int ConstExpr_variable = MyConstant;
189-
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: invalid case style for constexpr variable 'ConstExpr_variable'
190-
// CHECK-FIXES: {{^}}constexpr int const_expr_variable = MY_CONSTANT;{{$}}
193+
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: invalid case style for global constexpr variable 'ConstExpr_variable'
194+
// CHECK-FIXES: {{^}}constexpr int CONST_EXPR_VARIABLE = MY_CONSTANT;{{$}}
191195

192196
class my_class {
193197
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for class 'my_class'
@@ -208,7 +212,7 @@ class my_class {
208212
private:
209213
const int MEMBER_one_1 = ConstExpr_variable;
210214
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: invalid case style for constant member 'MEMBER_one_1'
211-
// CHECK-FIXES: {{^}} const int member_one_1 = const_expr_variable;{{$}}
215+
// CHECK-FIXES: {{^}} const int member_one_1 = CONST_EXPR_VARIABLE;{{$}}
212216
int member2 = 2;
213217
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for private member 'member2'
214218
// CHECK-FIXES: {{^}} int __member2 = 2;{{$}}
@@ -276,6 +280,9 @@ class CMyWellNamedClass2 : public my_class {
276280
int my_Other_Bad_Member = 42;
277281
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for private member 'my_Other_Bad_Member'
278282
// CHECK-FIXES: {{^}} int __my_Other_Bad_Member = 42;{{$}}
283+
static constexpr int my_Other_Other_Bad_Member = 69;
284+
// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: invalid case style for class constexpr 'my_Other_Other_Bad_Member'
285+
// CHECK-FIXES: {{^}} static constexpr int MyOtherOtherBadMember = 69;{{$}}
279286
public:
280287
CMyWellNamedClass2() = default;
281288
CMyWellNamedClass2(CMyWellNamedClass2 const&) = default;
@@ -447,12 +454,18 @@ void global_function(int PARAMETER_1, int const CONST_parameter) {
447454
static const int THIS_static_ConsTant = 4;
448455
// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: invalid case style for static constant 'THIS_static_ConsTant'
449456
// CHECK-FIXES: {{^}} static const int THIS_STATIC_CONS_TANT = 4;{{$}}
457+
static constexpr int THIS_static_ConstExpr = 4;
458+
// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: invalid case style for static constexpr variable 'THIS_static_ConstExpr'
459+
// CHECK-FIXES: {{^}} static constexpr int THIS_STATIC_CONST_EXPR = 4;{{$}}
450460
static int THIS_static_variable;
451461
// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: invalid case style for static variable 'THIS_static_variable'
452462
// CHECK-FIXES: {{^}} static int s_thisStaticVariable;{{$}}
453463
int const local_Constant = 3;
454464
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: invalid case style for local constant 'local_Constant'
455465
// CHECK-FIXES: {{^}} int const kLocalConstant = 3;{{$}}
466+
int constexpr local_Constexpr = 3;
467+
// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: invalid case style for local constexpr variable 'local_Constexpr'
468+
// CHECK-FIXES: {{^}} int constexpr LocalConstexpr = 3;{{$}}
456469
int LOCAL_VARIABLE;
457470
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: invalid case style for local variable 'LOCAL_VARIABLE'
458471
// CHECK-FIXES: {{^}} int local_variable;{{$}}

0 commit comments

Comments
 (0)