Lines Matching +full:max +full:- +full:size

1 //===--- EnumSizeCheck.cpp - clang-tidy -----------------------------------===//
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
30 const std::uint64_t Max8 = std::numeric_limits<std::int8_t>::max();
33 const std::uint64_t Max16 = std::numeric_limits<std::int16_t>::max();
36 const std::uint64_t Max32 = std::numeric_limits<std::int32_t>::max();
39 getNewType(std::size_t Size, std::uint64_t Min, std::uint64_t Max) noexcept { in getNewType() argument
41 if (Min <= Min8 && Max <= Max8) { in getNewType()
45 if (Min <= Min16 && Max <= Max16 && Size > sizeof(std::int16_t)) { in getNewType()
49 if (Min <= Min32 && Max <= Max32 && Size > sizeof(std::int32_t)) { in getNewType()
56 if (Max) { in getNewType()
57 if (Max <= std::numeric_limits<std::uint8_t>::max()) { in getNewType()
61 if (Max <= std::numeric_limits<std::uint16_t>::max() && in getNewType()
62 Size > sizeof(std::uint16_t)) { in getNewType()
66 if (Max <= std::numeric_limits<std::uint32_t>::max() && in getNewType()
67 Size > sizeof(std::uint32_t)) { in getNewType()
96 Finder->addMatcher( in registerMatchers()
106 const QualType BaseType = MatchedDecl->getIntegerType().getCanonicalType(); in check()
107 if (!BaseType->isIntegerType()) in check()
110 const std::uint32_t Size = Result.Context->getTypeSize(BaseType) / 8U; in check() local
111 if (1U == Size) in check()
117 for (const auto &It : MatchedDecl->enumerators()) { in check()
118 const llvm::APSInt &InitVal = It->getInitVal(); in check()
120 MaxV = std::max<std::uint64_t>(MaxV, InitVal.getZExtValue()); in check()
122 MinV = std::max<std::uint64_t>(MinV, InitVal.abs().getZExtValue()); in check()
126 auto NewType = getNewType(Size, MinV, MaxV); in check()
127 if (!NewType.first || Size <= NewType.second) in check()
130 diag(MatchedDecl->getLocation(), in check()
131 "enum %0 uses a larger base type (%1, size: %2 %select{byte|bytes}5) " in check()
133 "%select{byte|bytes}6) as the base type to reduce its size") in check()
134 << MatchedDecl << MatchedDecl->getIntegerType() << Size << NewType.first in check()
135 << NewType.second << (Size > 1U) << (NewType.second > 1U); in check()