12#include "clang/AST/ASTContext.h"
13#include "clang/ASTMatchers/ASTMatchFinder.h"
26AST_MATCHER(EnumDecl, hasEnumerators) {
return !Node.enumerators().empty(); }
28const std::uint64_t Min8 =
29 std::imaxabs(std::numeric_limits<std::int8_t>::min());
30const std::uint64_t Max8 = std::numeric_limits<std::int8_t>::max();
31const std::uint64_t Min16 =
32 std::imaxabs(std::numeric_limits<std::int16_t>::min());
33const std::uint64_t Max16 = std::numeric_limits<std::int16_t>::max();
34const std::uint64_t Min32 =
35 std::imaxabs(std::numeric_limits<std::int32_t>::min());
36const std::uint64_t Max32 = std::numeric_limits<std::int32_t>::max();
39static std::pair<const char *, std::uint32_t>
40getNewType(std::size_t Size, std::uint64_t Min, std::uint64_t Max)
noexcept {
42 if (Min <= Min8 && Max <= Max8)
43 return {
"std::int8_t",
sizeof(std::int8_t)};
45 if (Min <= Min16 && Max <= Max16 && Size >
sizeof(std::int16_t))
46 return {
"std::int16_t",
sizeof(std::int16_t)};
48 if (Min <= Min32 && Max <= Max32 && Size >
sizeof(std::int32_t))
49 return {
"std::int32_t",
sizeof(std::int32_t)};
55 if (Max <= std::numeric_limits<std::uint8_t>::max())
56 return {
"std::uint8_t",
sizeof(std::uint8_t)};
58 if (Max <= std::numeric_limits<std::uint16_t>::max() &&
59 Size >
sizeof(std::uint16_t)) {
60 return {
"std::uint16_t",
sizeof(std::uint16_t)};
63 if (Max <= std::numeric_limits<std::uint32_t>::max() &&
64 Size >
sizeof(std::uint32_t)) {
65 return {
"std::uint32_t",
sizeof(std::uint32_t)};
72 return {
"std::uint8_t",
sizeof(std::uint8_t)};
78 utils::options::parseStringList(Options.get(
"EnumIgnoreList",
""))) {}
81 Options.store(Opts,
"EnumIgnoreList",
86 const LangOptions &LangOpts)
const {
87 return LangOpts.CPlusPlus11;
92 enumDecl(unless(isExpansionInSystemHeader()), isDefinition(),
100 const auto *MatchedDecl = Result.Nodes.getNodeAs<EnumDecl>(
"e");
101 const QualType BaseType = MatchedDecl->getIntegerType().getCanonicalType();
102 if (!BaseType->isIntegerType())
105 const std::uint32_t Size = Result.Context->getTypeSize(BaseType) / 8U;
109 std::uint64_t MinV = 0U;
110 std::uint64_t MaxV = 0U;
112 for (
const auto &It : MatchedDecl->enumerators()) {
113 const llvm::APSInt &InitVal = It->getInitVal();
114 if ((InitVal.isUnsigned() || InitVal.isNonNegative()))
115 MaxV = std::max<std::uint64_t>(MaxV, InitVal.getZExtValue());
117 MinV = std::max<std::uint64_t>(MinV, InitVal.abs().getZExtValue());
121 if (!NewType.first || Size <= NewType.second)
124 diag(MatchedDecl->getLocation(),
125 "enum %0 uses a larger base type (%1, size: %2 %select{byte|bytes}5) "
126 "than necessary for its value set, consider using '%3' (%4 "
127 "%select{byte|bytes}6) as the base type to reduce its size")
128 << MatchedDecl << MatchedDecl->getIntegerType() << Size << NewType.first
129 << NewType.second << (Size > 1U) << (NewType.second > 1U);
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
AST_MATCHER(BinaryOperator, isRelationalOperator)
inline ::clang::ast_matchers::internal::Matcher< NamedDecl > matchesAnyListedRegexName(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