clang-tools 23.0.0git
UseEnumClassCheck.cpp
Go to the documentation of this file.
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-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 IgnoreMacros(Options.get("IgnoreMacros", false)) {}
21
23 Options.store(Opts, "IgnoreUnscopedEnumsInClasses",
24 IgnoreUnscopedEnumsInClasses);
25 Options.store(Opts, "IgnoreMacros", IgnoreMacros);
26}
27
28void UseEnumClassCheck::registerMatchers(MatchFinder *Finder) {
29 auto EnumDecl =
30 IgnoreUnscopedEnumsInClasses
31 ? enumDecl(unless(isScoped()), unless(hasParent(recordDecl())))
32 : enumDecl(unless(isScoped()));
33 Finder->addMatcher(EnumDecl.bind("unscoped_enum"), this);
34}
35
36void UseEnumClassCheck::check(const MatchFinder::MatchResult &Result) {
37 const auto *UnscopedEnum = Result.Nodes.getNodeAs<EnumDecl>("unscoped_enum");
38 const SourceLocation SourceLoc = UnscopedEnum->getLocation();
39
40 if (IgnoreMacros && SourceLoc.isMacroID())
41 return;
42
43 diag(SourceLoc, "enum %0 is unscoped, use 'enum class' instead")
44 << UnscopedEnum;
45}
46
47} // 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