clang-tools 22.0.0git
UseEnumClassCheck.cpp
Go to the documentation of this file.
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
12using namespace clang::ast_matchers;
13
15
17 : ClangTidyCheck(Name, Context),
18 IgnoreUnscopedEnumsInClasses(
19 Options.get("IgnoreUnscopedEnumsInClasses", false)) {}
20
22 Options.store(Opts, "IgnoreUnscopedEnumsInClasses",
23 IgnoreUnscopedEnumsInClasses);
24}
25
26void 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
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::cppcoreguidelines
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
UseEnumClassCheck(StringRef Name, ClangTidyContext *Context)
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
llvm::StringMap< ClangTidyValue > OptionMap