Skip to content

Commit 669627d

Browse files
authored
Add check 'cppcoreguidelines-use-enum-class' (#138282)
Warn on non-class enum definitions as suggested by the Core Guidelines: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Renum-class
1 parent c16dc63 commit 669627d

File tree

8 files changed

+190
-0
lines changed

8 files changed

+190
-0
lines changed

clang-tools-extra/clang-tidy/cppcoreguidelines/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ add_clang_library(clangTidyCppCoreGuidelinesModule STATIC
3333
RvalueReferenceParamNotMovedCheck.cpp
3434
SlicingCheck.cpp
3535
SpecialMemberFunctionsCheck.cpp
36+
UseEnumClassCheck.cpp
3637
VirtualClassDestructorCheck.cpp
3738

3839
LINK_LIBS

clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include "RvalueReferenceParamNotMovedCheck.h"
4949
#include "SlicingCheck.h"
5050
#include "SpecialMemberFunctionsCheck.h"
51+
#include "UseEnumClassCheck.h"
5152
#include "VirtualClassDestructorCheck.h"
5253

5354
namespace clang::tidy {
@@ -131,6 +132,8 @@ class CppCoreGuidelinesModule : public ClangTidyModule {
131132
CheckFactories.registerCheck<SlicingCheck>("cppcoreguidelines-slicing");
132133
CheckFactories.registerCheck<modernize::UseDefaultMemberInitCheck>(
133134
"cppcoreguidelines-use-default-member-init");
135+
CheckFactories.registerCheck<UseEnumClassCheck>(
136+
"cppcoreguidelines-use-enum-class");
134137
CheckFactories.registerCheck<misc::UnconventionalAssignOperatorCheck>(
135138
"cppcoreguidelines-c-copy-assignment-signature");
136139
CheckFactories.registerCheck<VirtualClassDestructorCheck>(
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//===--- UseEnumClassCheck.cpp - clang-tidy -------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "UseEnumClassCheck.h"
10+
#include "clang/ASTMatchers/ASTMatchFinder.h"
11+
12+
using namespace clang::ast_matchers;
13+
14+
namespace clang::tidy::cppcoreguidelines {
15+
16+
UseEnumClassCheck::UseEnumClassCheck(StringRef Name, ClangTidyContext *Context)
17+
: ClangTidyCheck(Name, Context),
18+
IgnoreUnscopedEnumsInClasses(
19+
Options.get("IgnoreUnscopedEnumsInClasses", false)) {}
20+
21+
void UseEnumClassCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
22+
Options.store(Opts, "IgnoreUnscopedEnumsInClasses",
23+
IgnoreUnscopedEnumsInClasses);
24+
}
25+
26+
void UseEnumClassCheck::registerMatchers(MatchFinder *Finder) {
27+
auto EnumDecl =
28+
IgnoreUnscopedEnumsInClasses
29+
? enumDecl(unless(isScoped()), unless(hasParent(recordDecl())))
30+
: enumDecl(unless(isScoped()));
31+
Finder->addMatcher(EnumDecl.bind("unscoped_enum"), this);
32+
}
33+
34+
void UseEnumClassCheck::check(const MatchFinder::MatchResult &Result) {
35+
const auto *UnscopedEnum = Result.Nodes.getNodeAs<EnumDecl>("unscoped_enum");
36+
37+
diag(UnscopedEnum->getLocation(),
38+
"enum %0 is unscoped, use 'enum class' instead")
39+
<< UnscopedEnum;
40+
}
41+
42+
} // namespace clang::tidy::cppcoreguidelines
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===--- UseEnumClassCheck.h - clang-tidy -----------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_USEENUMCLASSCHECK_H
10+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_USEENUMCLASSCHECK_H
11+
12+
#include "../ClangTidyCheck.h"
13+
14+
namespace clang::tidy::cppcoreguidelines {
15+
16+
/// Finds unscoped (non-class) enum declarations and suggests using enum class
17+
/// instead.
18+
///
19+
/// For the user-facing documentation see:
20+
/// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines/use-enum-class.html
21+
class UseEnumClassCheck : public ClangTidyCheck {
22+
public:
23+
UseEnumClassCheck(StringRef Name, ClangTidyContext *Context);
24+
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
25+
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
26+
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
27+
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
28+
return LangOpts.CPlusPlus11;
29+
}
30+
std::optional<TraversalKind> getCheckTraversalKind() const override {
31+
return TraversalKind::TK_IgnoreUnlessSpelledInSource;
32+
}
33+
34+
private:
35+
const bool IgnoreUnscopedEnumsInClasses;
36+
};
37+
38+
} // namespace clang::tidy::cppcoreguidelines
39+
40+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_USEENUMCLASSCHECK_H

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ New checks
136136
Finds unintended character output from ``unsigned char`` and ``signed char``
137137
to an ``ostream``.
138138

139+
- New :doc:`cppcoreguidelines-use-enum-class
140+
<clang-tidy/checks/cppcoreguidelines/use-enum-class>` check.
141+
142+
Finds unscoped (non-class) ``enum`` declarations and suggests using
143+
``enum class`` instead.
144+
139145
- New :doc:`portability-avoid-pragma-once
140146
<clang-tidy/checks/portability/avoid-pragma-once>` check.
141147

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
.. title:: clang-tidy - cppcoreguidelines-use-enum-class
2+
3+
cppcoreguidelines-use-enum-class
4+
================================
5+
6+
Finds unscoped (non-class) ``enum`` declarations and suggests using
7+
``enum class`` instead.
8+
9+
This check implements `Enum.3
10+
<https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Renum-class>`_
11+
from the C++ Core Guidelines."
12+
13+
Example:
14+
15+
.. code-block:: c++
16+
17+
enum E {}; // use "enum class E {};" instead
18+
enum class E {}; // OK
19+
20+
struct S {
21+
enum E {}; // use "enum class E {};" instead
22+
// OK with option IgnoreUnscopedEnumsInClasses
23+
};
24+
25+
namespace N {
26+
enum E {}; // use "enum class E {};" instead
27+
}
28+
29+
Options
30+
-------
31+
32+
.. option:: IgnoreUnscopedEnumsInClasses
33+
34+
When `true`, ignores unscoped ``enum`` declarations in classes.
35+
Default is `false`.

clang-tools-extra/docs/clang-tidy/checks/list.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ Clang-Tidy Checks
212212
:doc:`cppcoreguidelines-rvalue-reference-param-not-moved <cppcoreguidelines/rvalue-reference-param-not-moved>`,
213213
:doc:`cppcoreguidelines-slicing <cppcoreguidelines/slicing>`,
214214
:doc:`cppcoreguidelines-special-member-functions <cppcoreguidelines/special-member-functions>`,
215+
:doc:`cppcoreguidelines-use-enum-class <cppcoreguidelines/use-enum-class>`,
215216
:doc:`cppcoreguidelines-virtual-class-destructor <cppcoreguidelines/virtual-class-destructor>`, "Yes"
216217
:doc:`darwin-avoid-spinlock <darwin/avoid-spinlock>`,
217218
:doc:`darwin-dispatch-once-nonstatic <darwin/dispatch-once-nonstatic>`, "Yes"
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// RUN: %check_clang_tidy -std=c++11-or-later -check-suffix=ALL,DEFAULT %s \
2+
// RUN: cppcoreguidelines-use-enum-class %t --
3+
4+
// RUN: %check_clang_tidy -std=c++11-or-later -check-suffix=ALL %s \
5+
// RUN: cppcoreguidelines-use-enum-class %t -- \
6+
// RUN: -config="{CheckOptions: { \
7+
// RUN: cppcoreguidelines-use-enum-class.IgnoreUnscopedEnumsInClasses: true \
8+
// RUN: }}" --
9+
10+
enum E {};
11+
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:6: warning: enum 'E' is unscoped, use 'enum class' instead
12+
13+
enum class EC {};
14+
15+
enum struct ES {};
16+
17+
struct S {
18+
enum E {};
19+
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:8: warning: enum 'E' is unscoped, use 'enum class' instead
20+
enum class EC {};
21+
};
22+
23+
class C {
24+
enum E {};
25+
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:8: warning: enum 'E' is unscoped, use 'enum class' instead
26+
enum class EC {};
27+
};
28+
29+
template<class T>
30+
class TC {
31+
enum E {};
32+
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:8: warning: enum 'E' is unscoped, use 'enum class' instead
33+
enum class EC {};
34+
};
35+
36+
union U {
37+
enum E {};
38+
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:8: warning: enum 'E' is unscoped, use 'enum class' instead
39+
enum class EC {};
40+
};
41+
42+
namespace {
43+
enum E {};
44+
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:6: warning: enum 'E' is unscoped, use 'enum class' instead
45+
enum class EC {};
46+
} // namespace
47+
48+
namespace N {
49+
enum E {};
50+
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:6: warning: enum 'E' is unscoped, use 'enum class' instead
51+
enum class EC {};
52+
} // namespace N
53+
54+
template<enum ::EC>
55+
static void foo();
56+
57+
enum ForwardE : int;
58+
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:6: warning: enum 'ForwardE' is unscoped, use 'enum class' instead
59+
60+
enum class ForwardEC : int;
61+
62+
enum struct ForwardES : int;

0 commit comments

Comments
 (0)