10#include "../utils/Matchers.h"
11#include "../utils/OptionsUtils.h"
12#include "clang/AST/ASTContext.h"
13#include "clang/ASTMatchers/ASTMatchFinder.h"
27 [](
const EnumConstantDecl *Value) {
28 return Value->getInitVal().isZero();
38 utils::options::parseStringList(Options.get(
"EnumIgnoreList",
""))) {}
47 const LangOptions &LangOpts)
const {
48 return LangOpts.CPlusPlus;
54 auto ExcludedOperators = binaryOperation(hasAnyOperatorName(
55 "|",
"&",
"^",
"<<",
">>",
"~",
"|=",
"&=",
"^=",
"<<=",
">>="));
58 castExpr(hasCastKind(CK_IntegralToBoolean),
59 unless(isExpansionInSystemHeader()), hasType(booleanType()),
61 expr(hasType(qualType(hasCanonicalType(hasDeclaration(
62 enumDecl(isCompleteAndHasNoZeroValue(),
66 unless(declRefExpr(to(enumConstantDecl()))),
67 unless(ignoringParenImpCasts(ExcludedOperators)))),
68 unless(hasAncestor(staticAssertDecl())))
74 const MatchFinder::MatchResult &Result) {
75 const auto *Cast = Result.Nodes.getNodeAs<CastExpr>(
"cast");
76 const auto *Enum = Result.Nodes.getNodeAs<EnumDecl>(
"enum");
78 diag(Cast->getExprLoc(),
"conversion of %0 into 'bool' will always return "
79 "'true', enum doesn't have a zero-value enumerator")
81 diag(Enum->getLocation(),
"enum is defined here", DiagnosticIDs::Note);
llvm::SmallString< 256U > Name
::clang::DynTypedNode Node
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override
Override this to disable registering matchers and PP callbacks if an invalid language version is bein...
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
NonZeroEnumToBoolConversionCheck(StringRef Name, ClangTidyContext *Context)
AST_MATCHER(clang::VarDecl, hasConstantDeclaration)
inline ::clang::ast_matchers::internal::Matcher< NamedDecl > matchesAnyListedName(llvm::ArrayRef< StringRef > NameList)
std::string serializeStringList(ArrayRef< StringRef > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
llvm::StringMap< ClangTidyValue > OptionMap