clang-tools 24.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;
13using namespace clang::ast_matchers::internal;
14
16namespace {
17// FIXME: The matcher 'hasName(Name)' asserts that its argument 'Name' is
18// nonempty. Perhaps remove that assertion and replace 'isUnnamed()' with
19// 'hasName("")'.
20AST_MATCHER(EnumDecl, isUnnamed) { return Node.getName().empty(); }
21} // namespace
22
24 : ClangTidyCheck(Name, Context),
25 IgnoreUnscopedEnumsInClasses(
26 Options.get("IgnoreUnscopedEnumsInClasses", false)),
27 IgnoreMacros(Options.get("IgnoreMacros", false)) {}
28
30 Options.store(Opts, "IgnoreUnscopedEnumsInClasses",
31 IgnoreUnscopedEnumsInClasses);
32 Options.store(Opts, "IgnoreMacros", IgnoreMacros);
33}
34
35void UseEnumClassCheck::registerMatchers(MatchFinder *Finder) {
36 const auto EnumDecl = IgnoreUnscopedEnumsInClasses
37 ? enumDecl(unless(isScoped()), unless(isUnnamed()),
38 unless(hasParent(recordDecl())))
39 : enumDecl(unless(isScoped()), unless(isUnnamed()));
40 Finder->addMatcher(EnumDecl.bind("unscoped_enum"), this);
41}
42
43void UseEnumClassCheck::check(const MatchFinder::MatchResult &Result) {
44 const auto *UnscopedEnum = Result.Nodes.getNodeAs<EnumDecl>("unscoped_enum");
45 const SourceLocation SourceLoc = UnscopedEnum->getLocation();
46
47 if (IgnoreMacros && SourceLoc.isMacroID())
48 return;
49
50 diag(SourceLoc, "enum %0 is unscoped, use 'enum class' instead")
51 << UnscopedEnum;
52}
53
54} // 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
AST_MATCHER(BinaryOperator, isRelationalOperator)
llvm::StringMap< ClangTidyValue > OptionMap