43 lines · cpp
1//===----------------------------------------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9#include "UseEnumClassCheck.h"10#include "clang/ASTMatchers/ASTMatchFinder.h"11 12using namespace clang::ast_matchers;13 14namespace clang::tidy::cppcoreguidelines {15 16UseEnumClassCheck::UseEnumClassCheck(StringRef Name, ClangTidyContext *Context)17 : ClangTidyCheck(Name, Context),18 IgnoreUnscopedEnumsInClasses(19 Options.get("IgnoreUnscopedEnumsInClasses", false)) {}20 21void UseEnumClassCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {22 Options.store(Opts, "IgnoreUnscopedEnumsInClasses",23 IgnoreUnscopedEnumsInClasses);24}25 26void UseEnumClassCheck::registerMatchers(MatchFinder *Finder) {27 auto EnumDecl =28 IgnoreUnscopedEnumsInClasses29 ? enumDecl(unless(isScoped()), unless(hasParent(recordDecl())))30 : enumDecl(unless(isScoped()));31 Finder->addMatcher(EnumDecl.bind("unscoped_enum"), this);32}33 34void 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::cppcoreguidelines43