| 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(LocalName: "IgnoreUnscopedEnumsInClasses" , Default: false)) {} |
| 20 | |
| 21 | void UseEnumClassCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { |
| 22 | Options.store(Options&: Opts, LocalName: "IgnoreUnscopedEnumsInClasses" , |
| 23 | Value: 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(NodeMatch: EnumDecl.bind(ID: "unscoped_enum" ), Action: this); |
| 32 | } |
| 33 | |
| 34 | void UseEnumClassCheck::check(const MatchFinder::MatchResult &Result) { |
| 35 | const auto *UnscopedEnum = Result.Nodes.getNodeAs<EnumDecl>(ID: "unscoped_enum" ); |
| 36 | |
| 37 | diag(Loc: UnscopedEnum->getLocation(), |
| 38 | Description: "enum %0 is unscoped, use 'enum class' instead" ) |
| 39 | << UnscopedEnum; |
| 40 | } |
| 41 | |
| 42 | } // namespace clang::tidy::cppcoreguidelines |
| 43 | |