[clang-tools-extra] 86ef379 - [clang-tidy] Add scoped enum constants to identifier naming check
Nathan James via cfe-commits
cfe-commits at lists.llvm.org
Mon Oct 19 07:57:55 PDT 2020
Author: Nathan James
Date: 2020-10-19T15:57:47+01:00
New Revision: 86ef379800162e5d6bb0d478c5bfb4b56498a272
URL: https://github.com/llvm/llvm-project/commit/86ef379800162e5d6bb0d478c5bfb4b56498a272
DIFF: https://github.com/llvm/llvm-project/commit/86ef379800162e5d6bb0d478c5bfb4b56498a272.diff
LOG: [clang-tidy] Add scoped enum constants to identifier naming check
Added option `ScopedEnumConstant(Prefix|Case|Suffix)` to readability-identitied-naming.
This controls the style for constants in scoped enums, declared as enum (class|struct).
If this option is unspecified the EnumConstant style will be used instead.
Reviewed By: aaron.ballman
Differential Revision: https://reviews.llvm.org/D89407
Added:
Modified:
clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/docs/clang-tidy/checks/readability-identifier-naming.rst
clang-tools-extra/test/clang-tidy/checkers/readability-identifier-naming.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
index c2a32474b2a8..f0444049f5ac 100644
--- a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
@@ -55,6 +55,7 @@ namespace readability {
m(Namespace) \
m(InlineNamespace) \
m(EnumConstant) \
+ m(ScopedEnumConstant) \
m(ConstexprVariable) \
m(ConstantMember) \
m(PrivateMember) \
@@ -405,7 +406,11 @@ static StyleKind findStyleKind(
if (isa<EnumDecl>(D) && NamingStyles[SK_Enum])
return SK_Enum;
- if (isa<EnumConstantDecl>(D)) {
+ if (const auto *EnumConst = dyn_cast<EnumConstantDecl>(D)) {
+ if (cast<EnumDecl>(EnumConst->getDeclContext())->isScoped() &&
+ NamingStyles[SK_ScopedEnumConstant])
+ return SK_ScopedEnumConstant;
+
if (NamingStyles[SK_EnumConstant])
return SK_EnumConstant;
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 595b1c465d5f..9a182d534615 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -127,6 +127,9 @@ Changes in existing checks
Now renames overridden virtual methods if the method they override has a
style violation.
+
+ Added support for specifying the style of scoped ``enum`` constants. If
+ unspecified, will fall back to the style for regular ``enum`` constants.
- Removed `google-runtime-references` check because the rule it checks does
not exist in the Google Style Guide anymore.
diff --git a/clang-tools-extra/docs/clang-tidy/checks/readability-identifier-naming.rst b/clang-tools-extra/docs/clang-tidy/checks/readability-identifier-naming.rst
index 9eec3c03f7d7..036103e7e80e 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/readability-identifier-naming.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/readability-identifier-naming.rst
@@ -76,6 +76,7 @@ The following options are describe below:
- :option:`ProtectedMethodCase`, :option:`ProtectedMethodPrefix`, :option:`ProtectedMethodSuffix`
- :option:`PublicMemberCase`, :option:`PublicMemberPrefix`, :option:`PublicMemberSuffix`
- :option:`PublicMethodCase`, :option:`PublicMethodPrefix`, :option:`PublicMethodSuffix`
+ - :option:`ScopedEnumConstantCase`, :option:`ScopedEnumConstantPrefix`, :option:`ScopedEnumConstantSuffix`
- :option:`StaticConstantCase`, :option:`StaticConstantPrefix`, :option:`StaticConstantSuffix`
- :option:`StaticVariableCase`, :option:`StaticVariablePrefix`, :option:`StaticVariableSuffix`
- :option:`StructCase`, :option:`StructPrefix`, :option:`StructSuffix`
@@ -1595,6 +1596,41 @@ After:
int pre_member_method_post();
}
+.. option:: ScopedEnumConstantCase
+
+ When defined, the check will ensure scoped enum constant names conform to
+ the selected casing.
+
+.. option:: ScopedEnumConstantPrefix
+
+ When defined, the check will ensure scoped enum constant names will add the
+ prefixed with the given value (regardless of casing).
+
+.. option:: ScopedEnumConstantSuffix
+
+ When defined, the check will ensure scoped enum constant names will add the
+ suffix with the given value (regardless of casing).
+
+For example using values of:
+
+ - ScopedEnumConstantCase of ``lower_case``
+ - ScopedEnumConstantPrefix of ``pre_``
+ - ScopedEnumConstantSuffix of ``_post``
+
+Identifies and/or transforms enumeration constant names as follows:
+
+Before:
+
+.. code-block:: c++
+
+ enum class FOO { One, Two, Three };
+
+After:
+
+.. code-block:: c++
+
+ enum class FOO { pre_One_post, pre_Two_post, pre_Three_post };
+
.. option:: StaticConstantCase
When defined, the check will ensure static constant names conform to the
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability-identifier-naming.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability-identifier-naming.cpp
index de53dddc0f92..66b6009d62ae 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability-identifier-naming.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability-identifier-naming.cpp
@@ -18,6 +18,7 @@
// RUN: {key: readability-identifier-naming.ConstexprVariableCase, value: lower_case}, \
// RUN: {key: readability-identifier-naming.EnumCase, value: CamelCase}, \
// RUN: {key: readability-identifier-naming.EnumPrefix, value: 'E'}, \
+// RUN: {key: readability-identifier-naming.ScopedEnumConstantCase, value: CamelCase}, \
// RUN: {key: readability-identifier-naming.EnumConstantCase, value: UPPER_CASE}, \
// RUN: {key: readability-identifier-naming.FunctionCase, value: camelBack}, \
// RUN: {key: readability-identifier-naming.GlobalConstantCase, value: UPPER_CASE}, \
@@ -160,6 +161,18 @@ enum my_enumeration {
// CHECK-FIXES: {{^}} THIS_CONST_VALUE = 1,{{$}}
};
+enum class EMyEnumeration {
+ myConstant = 1,
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: invalid case style for scoped enum constant 'myConstant'
+// CHECK-FIXES: {{^}} MyConstant = 1,{{$}}
+ your_CONST = 1,
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: invalid case style for scoped enum constant 'your_CONST'
+// CHECK-FIXES: {{^}} YourConst = 1,{{$}}
+ THIS_ConstValue = 1,
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: invalid case style for scoped enum constant 'THIS_ConstValue'
+// CHECK-FIXES: {{^}} ThisConstValue = 1,{{$}}
+};
+
constexpr int ConstExpr_variable = MyConstant;
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: invalid case style for constexpr variable 'ConstExpr_variable'
// CHECK-FIXES: {{^}}constexpr int const_expr_variable = MY_CONSTANT;{{$}}
More information about the cfe-commits
mailing list