#151035 centralized the code to skip matching nodes in system headers, so individual checks no longer need the `unless(isExpansionInSystemHeader())` boilerplate to achieve that. You can verify that the logic introduced in that PR is equivalent to `unless(isExpansionInSystemHeader())`; they both essentially boil down to `!SourceManager.isInSystemHeader(Node.getBeginLoc())`:7c5c58cdc6/clang/include/clang/ASTMatchers/ASTMatchers.h (L283-L291)7c5c58cdc6/clang/lib/ASTMatchers/ASTMatchFinder.cpp (L1363-L1372)I audited all the uses of `isExpansionInSystemHeader` in the codebase, and the only one I found that wasn't redundant was one in `TaggedUnionMemberCountCheck.cpp`.
82 lines
3.1 KiB
C++
82 lines
3.1 KiB
C++
//===----------------------------------------------------------------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "NonZeroEnumToBoolConversionCheck.h"
|
|
#include "../utils/Matchers.h"
|
|
#include "../utils/OptionsUtils.h"
|
|
#include "clang/AST/ASTContext.h"
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
namespace clang::tidy::bugprone {
|
|
|
|
namespace {
|
|
|
|
AST_MATCHER(EnumDecl, isCompleteAndHasNoZeroValue) {
|
|
const EnumDecl *Definition = Node.getDefinition();
|
|
return Definition && Node.isComplete() &&
|
|
llvm::none_of(Definition->enumerators(),
|
|
[](const EnumConstantDecl *Value) {
|
|
return Value->getInitVal().isZero();
|
|
});
|
|
}
|
|
|
|
} // namespace
|
|
|
|
NonZeroEnumToBoolConversionCheck::NonZeroEnumToBoolConversionCheck(
|
|
StringRef Name, ClangTidyContext *Context)
|
|
: ClangTidyCheck(Name, Context),
|
|
EnumIgnoreList(
|
|
utils::options::parseStringList(Options.get("EnumIgnoreList", ""))) {}
|
|
|
|
void NonZeroEnumToBoolConversionCheck::storeOptions(
|
|
ClangTidyOptions::OptionMap &Opts) {
|
|
Options.store(Opts, "EnumIgnoreList",
|
|
utils::options::serializeStringList(EnumIgnoreList));
|
|
}
|
|
|
|
bool NonZeroEnumToBoolConversionCheck::isLanguageVersionSupported(
|
|
const LangOptions &LangOpts) const {
|
|
return LangOpts.CPlusPlus;
|
|
}
|
|
|
|
void NonZeroEnumToBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
|
|
// Excluding bitwise operators (binary and overload) to avoid false-positives
|
|
// in code like this 'if (e & SUCCESS) {'.
|
|
auto ExcludedOperators = binaryOperation(hasAnyOperatorName(
|
|
"|", "&", "^", "<<", ">>", "~", "|=", "&=", "^=", "<<=", ">>="));
|
|
|
|
Finder->addMatcher(
|
|
castExpr(hasCastKind(CK_IntegralToBoolean), hasType(booleanType()),
|
|
hasSourceExpression(
|
|
expr(hasType(qualType(hasCanonicalType(hasDeclaration(
|
|
enumDecl(isCompleteAndHasNoZeroValue(),
|
|
unless(matchers::matchesAnyListedRegexName(
|
|
EnumIgnoreList)))
|
|
.bind("enum"))))),
|
|
unless(declRefExpr(to(enumConstantDecl()))),
|
|
unless(ignoringParenImpCasts(ExcludedOperators)))),
|
|
unless(hasAncestor(staticAssertDecl())))
|
|
.bind("cast"),
|
|
this);
|
|
}
|
|
|
|
void NonZeroEnumToBoolConversionCheck::check(
|
|
const MatchFinder::MatchResult &Result) {
|
|
const auto *Cast = Result.Nodes.getNodeAs<CastExpr>("cast");
|
|
const auto *Enum = Result.Nodes.getNodeAs<EnumDecl>("enum");
|
|
|
|
diag(Cast->getExprLoc(), "conversion of %0 into 'bool' will always return "
|
|
"'true', enum doesn't have a zero-value enumerator")
|
|
<< Enum;
|
|
diag(Enum->getLocation(), "enum is defined here", DiagnosticIDs::Note);
|
|
}
|
|
|
|
} // namespace clang::tidy::bugprone
|