-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Add check 'cppcoreguidelines-use-enum-class' #138282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JungPhilipp
wants to merge
9
commits into
llvm:main
Choose a base branch
from
JungPhilipp:clang-tidy_modernize_enum_class
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+196
−0
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0567bc8
Add check 'modernize-use-enum-class'
JungPhilipp a4afe4a
Move check to cppcoreguidelines- section
JungPhilipp 129b95d
Remove traverse call
JungPhilipp 723c913
Add option to ignore unscoped enums in classes
JungPhilipp f7463f6
Improve documentation
JungPhilipp a5c1437
Address review comments
JungPhilipp 3f93c6f
Fix order of checks in docu
JungPhilipp 880b73f
Revert remove whitespace
JungPhilipp c371e64
Make release notes match docu
JungPhilipp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
clang-tools-extra/clang-tidy/cppcoreguidelines/UseEnumClassCheck.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//===--- UseEnumClassCheck.cpp - clang-tidy -------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "UseEnumClassCheck.h" | ||
#include "clang/ASTMatchers/ASTMatchFinder.h" | ||
|
||
using namespace clang::ast_matchers; | ||
|
||
namespace clang::tidy::cppcoreguidelines { | ||
|
||
UseEnumClassCheck::UseEnumClassCheck(StringRef Name, ClangTidyContext *Context) | ||
: ClangTidyCheck(Name, Context), | ||
IgnoreUnscopedEnumsInClasses( | ||
Options.get("IgnoreUnscopedEnumsInClasses", false)) {} | ||
|
||
void UseEnumClassCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { | ||
Options.store(Opts, "IgnoreUnscopedEnumsInClasses", | ||
IgnoreUnscopedEnumsInClasses); | ||
} | ||
|
||
void UseEnumClassCheck::registerMatchers(MatchFinder *Finder) { | ||
auto EnumDecl = | ||
IgnoreUnscopedEnumsInClasses | ||
? enumDecl(unless(isScoped()), unless(hasParent(recordDecl()))) | ||
: enumDecl(unless(isScoped())); | ||
Finder->addMatcher(EnumDecl.bind("unscoped_enum"), this); | ||
} | ||
|
||
void UseEnumClassCheck::check(const MatchFinder::MatchResult &Result) { | ||
const auto *UnscopedEnum = Result.Nodes.getNodeAs<EnumDecl>("unscoped_enum"); | ||
|
||
diag(UnscopedEnum->getLocation(), | ||
"enum %0 is unscoped, use 'enum class' instead") | ||
<< UnscopedEnum; | ||
} | ||
|
||
} // namespace clang::tidy::cppcoreguidelines |
40 changes: 40 additions & 0 deletions
40
clang-tools-extra/clang-tidy/cppcoreguidelines/UseEnumClassCheck.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
//===--- UseEnumClassCheck.h - clang-tidy -----------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_USEENUMCLASSCHECK_H | ||
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_USEENUMCLASSCHECK_H | ||
|
||
#include "../ClangTidyCheck.h" | ||
|
||
namespace clang::tidy::cppcoreguidelines { | ||
|
||
/// Check for unscoped enums and suggest to use scoped enums (enum class). | ||
/// Optionally, ignore unscoped enums in classes via IgnoreUnscopedEnumsInClasses | ||
/// | ||
/// For the user-facing documentation see: | ||
/// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines/use-enum-class.html | ||
class UseEnumClassCheck : public ClangTidyCheck { | ||
public: | ||
UseEnumClassCheck(StringRef Name, ClangTidyContext *Context); | ||
void storeOptions(ClangTidyOptions::OptionMap &Opts) override; | ||
void registerMatchers(ast_matchers::MatchFinder *Finder) override; | ||
void check(const ast_matchers::MatchFinder::MatchResult &Result) override; | ||
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { | ||
return LangOpts.CPlusPlus11; | ||
} | ||
std::optional<TraversalKind> getCheckTraversalKind() const override { | ||
return TraversalKind::TK_IgnoreUnlessSpelledInSource; | ||
} | ||
|
||
private: | ||
const bool IgnoreUnscopedEnumsInClasses; | ||
}; | ||
|
||
} // namespace clang::tidy::cppcoreguidelines | ||
|
||
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_USEENUMCLASSCHECK_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/use-enum-class.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
.. title:: clang-tidy - cppcoreguidelines-use-enum-class | ||
|
||
cppcoreguidelines-use-enum-class | ||
================================ | ||
|
||
Finds unscoped (non-class) ``enum`` declarations and suggests using | ||
``enum class`` instead. | ||
|
||
This check implements `Enum.3 | ||
<https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Renum-class>`_ | ||
from the C++ Core Guidelines." | ||
|
||
Example: | ||
|
||
.. code-block:: c++ | ||
|
||
enum E {}; // use "enum class E {};" instead | ||
enum class E {}; // OK | ||
|
||
struct S { | ||
enum E {}; // use "enum class E {};" instead | ||
// OK with option IgnoreUnscopedEnumsInClasses | ||
}; | ||
|
||
namespace N { | ||
enum E {}; // use "enum class E {};" instead | ||
} | ||
|
||
Options | ||
------- | ||
|
||
.. option:: IgnoreUnscopedEnumsInClasses | ||
|
||
When `true`, ignores unscoped ``enum`` declarations in classes. | ||
Default is `false`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...clang-tidy/checkers/cppcoreguidelines/use-enum-class-ignore-unscoped-enums-in-classes.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// RUN: %check_clang_tidy -std=c++11-or-later %s cppcoreguidelines-use-enum-class %t -- \ | ||
// RUN: -config="{CheckOptions: {cppcoreguidelines-use-enum-class.IgnoreUnscopedEnumsInClasses: true}}" -- | ||
|
||
enum E {}; | ||
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: enum 'E' is unscoped, use 'enum class' instead | ||
|
||
enum class EC {}; | ||
|
||
struct S { | ||
enum E {}; | ||
JungPhilipp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// CHECK-MESSAGES-NOT: :[[@LINE-1]]:8: warning: enum 'E' is unscoped, use 'enum class' instead | ||
// Ignore unscoped enums in recordDecl | ||
enum class EC {}; | ||
}; | ||
|
||
enum ForwardE : int; | ||
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: enum 'ForwardE' is unscoped, use 'enum class' instead | ||
enum class ForwardEC : int; |
50 changes: 50 additions & 0 deletions
50
clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/use-enum-class.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// RUN: %check_clang_tidy -std=c++11-or-later %s cppcoreguidelines-use-enum-class %t | ||
|
||
enum E {}; | ||
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: enum 'E' is unscoped, use 'enum class' instead | ||
|
||
enum class EC {}; | ||
|
||
struct S { | ||
enum E {}; | ||
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: enum 'E' is unscoped, use 'enum class' instead | ||
enum class EC {}; | ||
}; | ||
|
||
class C { | ||
enum E {}; | ||
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: enum 'E' is unscoped, use 'enum class' instead | ||
enum class EC {}; | ||
}; | ||
|
||
template<class T> | ||
class TC { | ||
enum E {}; | ||
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: enum 'E' is unscoped, use 'enum class' instead | ||
enum class EC {}; | ||
}; | ||
|
||
union U { | ||
enum E {}; | ||
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: enum 'E' is unscoped, use 'enum class' instead | ||
enum class EC {}; | ||
}; | ||
|
||
namespace { | ||
enum E {}; | ||
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: enum 'E' is unscoped, use 'enum class' instead | ||
enum class EC {}; | ||
} // namespace | ||
|
||
namespace N { | ||
enum E {}; | ||
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: enum 'E' is unscoped, use 'enum class' instead | ||
enum class EC {}; | ||
} // namespace N | ||
|
||
template<enum ::EC> | ||
static void foo(); | ||
|
||
enum ForwardE : int; | ||
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: enum 'ForwardE' is unscoped, use 'enum class' instead | ||
enum class ForwardEC : int; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.