|
| 1 | +//===--- UseNumericLimitsCheck.cpp - clang-tidy ---------------------------===// |
| 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 "UseNumericLimitsCheck.h" |
| 10 | +#include "clang/AST/ASTContext.h" |
| 11 | +#include "clang/ASTMatchers/ASTMatchFinder.h" |
| 12 | +#include "clang/Lex/Preprocessor.h" |
| 13 | +#include <cmath> |
| 14 | +#include <limits> |
| 15 | + |
| 16 | +using namespace clang::ast_matchers; |
| 17 | + |
| 18 | +namespace clang::tidy::readability { |
| 19 | + |
| 20 | +UseNumericLimitsCheck::UseNumericLimitsCheck(StringRef Name, |
| 21 | + ClangTidyContext *Context) |
| 22 | + : ClangTidyCheck(Name, Context), |
| 23 | + SignedConstants{ |
| 24 | + {std::numeric_limits<int8_t>::min(), |
| 25 | + "std::numeric_limits<int8_t>::min()"}, |
| 26 | + {std::numeric_limits<int8_t>::max(), |
| 27 | + "std::numeric_limits<int8_t>::max()"}, |
| 28 | + {std::numeric_limits<int16_t>::min(), |
| 29 | + "std::numeric_limits<int16_t>::min()"}, |
| 30 | + {std::numeric_limits<int16_t>::max(), |
| 31 | + "std::numeric_limits<int16_t>::max()"}, |
| 32 | + {std::numeric_limits<int32_t>::min(), |
| 33 | + "std::numeric_limits<int32_t>::min()"}, |
| 34 | + {std::numeric_limits<int32_t>::max(), |
| 35 | + "std::numeric_limits<int32_t>::max()"}, |
| 36 | + {std::numeric_limits<int64_t>::min(), |
| 37 | + "std::numeric_limits<int64_t>::min()"}, |
| 38 | + {std::numeric_limits<int64_t>::max(), |
| 39 | + "std::numeric_limits<int64_t>::max()"}, |
| 40 | + }, |
| 41 | + UnsignedConstants{ |
| 42 | + {std::numeric_limits<uint8_t>::max(), |
| 43 | + "std::numeric_limits<uint8_t>::max()"}, |
| 44 | + {std::numeric_limits<uint16_t>::max(), |
| 45 | + "std::numeric_limits<uint16_t>::max()"}, |
| 46 | + {std::numeric_limits<uint32_t>::max(), |
| 47 | + "std::numeric_limits<uint32_t>::max()"}, |
| 48 | + {std::numeric_limits<uint64_t>::max(), |
| 49 | + "std::numeric_limits<uint64_t>::max()"}, |
| 50 | + }, |
| 51 | + Inserter(Options.getLocalOrGlobal("IncludeStyle", |
| 52 | + utils::IncludeSorter::IS_LLVM), |
| 53 | + areDiagsSelfContained()) {} |
| 54 | + |
| 55 | +void UseNumericLimitsCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { |
| 56 | + Options.store(Opts, "IncludeStyle", Inserter.getStyle()); |
| 57 | +} |
| 58 | + |
| 59 | +void UseNumericLimitsCheck::registerMatchers(MatchFinder *Finder) { |
| 60 | + auto PositiveIntegerMatcher = [](auto Value) { |
| 61 | + return unaryOperator(hasOperatorName("+"), |
| 62 | + hasUnaryOperand(integerLiteral(equals(Value)) |
| 63 | + .bind("positive-integer-literal"))) |
| 64 | + .bind("unary-op"); |
| 65 | + }; |
| 66 | + |
| 67 | + auto NegativeIntegerMatcher = [](auto Value) { |
| 68 | + return unaryOperator(hasOperatorName("-"), |
| 69 | + hasUnaryOperand(integerLiteral(equals(-Value)) |
| 70 | + .bind("negative-integer-literal"))) |
| 71 | + .bind("unary-op"); |
| 72 | + }; |
| 73 | + |
| 74 | + auto BareIntegerMatcher = [](auto Value) { |
| 75 | + return integerLiteral(allOf(unless(hasParent(unaryOperator( |
| 76 | + hasAnyOperatorName("-", "+")))), |
| 77 | + equals(Value))) |
| 78 | + .bind("bare-integer-literal"); |
| 79 | + }; |
| 80 | + |
| 81 | + for (const auto &[Value, _] : SignedConstants) { |
| 82 | + if (Value < 0) { |
| 83 | + Finder->addMatcher(NegativeIntegerMatcher(Value), this); |
| 84 | + } else { |
| 85 | + Finder->addMatcher( |
| 86 | + expr(anyOf(PositiveIntegerMatcher(Value), BareIntegerMatcher(Value))), |
| 87 | + this); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + for (const auto &[Value, _] : UnsignedConstants) { |
| 92 | + Finder->addMatcher( |
| 93 | + expr(anyOf(PositiveIntegerMatcher(Value), BareIntegerMatcher(Value))), |
| 94 | + this); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +void UseNumericLimitsCheck::registerPPCallbacks( |
| 99 | + const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) { |
| 100 | + Inserter.registerPreprocessor(PP); |
| 101 | +} |
| 102 | + |
| 103 | +void UseNumericLimitsCheck::check(const MatchFinder::MatchResult &Result) { |
| 104 | + const IntegerLiteral *MatchedDecl = nullptr; |
| 105 | + |
| 106 | + const IntegerLiteral *NegativeMatchedDecl = |
| 107 | + Result.Nodes.getNodeAs<IntegerLiteral>("negative-integer-literal"); |
| 108 | + const IntegerLiteral *PositiveMatchedDecl = |
| 109 | + Result.Nodes.getNodeAs<IntegerLiteral>("positive-integer-literal"); |
| 110 | + const IntegerLiteral *BareMatchedDecl = |
| 111 | + Result.Nodes.getNodeAs<IntegerLiteral>("bare-integer-literal"); |
| 112 | + |
| 113 | + if (NegativeMatchedDecl != nullptr) |
| 114 | + MatchedDecl = NegativeMatchedDecl; |
| 115 | + else if (PositiveMatchedDecl != nullptr) |
| 116 | + MatchedDecl = PositiveMatchedDecl; |
| 117 | + else if (BareMatchedDecl != nullptr) |
| 118 | + MatchedDecl = BareMatchedDecl; |
| 119 | + |
| 120 | + const llvm::APInt MatchedIntegerConstant = MatchedDecl->getValue(); |
| 121 | + |
| 122 | + auto Fixer = [&](auto SourceValue, auto Value, |
| 123 | + const std::string &Replacement) { |
| 124 | + static_assert(std::is_same_v<decltype(SourceValue), decltype(Value)>, |
| 125 | + "The types of SourceValue and Value must match"); |
| 126 | + |
| 127 | + SourceLocation Location = MatchedDecl->getExprLoc(); |
| 128 | + SourceRange Range{MatchedDecl->getBeginLoc(), MatchedDecl->getEndLoc()}; |
| 129 | + |
| 130 | + // Only valid if unary operator is present |
| 131 | + const UnaryOperator *UnaryOpExpr = |
| 132 | + Result.Nodes.getNodeAs<UnaryOperator>("unary-op"); |
| 133 | + |
| 134 | + if (MatchedDecl == NegativeMatchedDecl && -SourceValue == Value) { |
| 135 | + Range = SourceRange(UnaryOpExpr->getBeginLoc(), UnaryOpExpr->getEndLoc()); |
| 136 | + Location = UnaryOpExpr->getExprLoc(); |
| 137 | + SourceValue = -SourceValue; |
| 138 | + } else if (MatchedDecl == PositiveMatchedDecl && SourceValue == Value) { |
| 139 | + Range = SourceRange(UnaryOpExpr->getBeginLoc(), UnaryOpExpr->getEndLoc()); |
| 140 | + Location = UnaryOpExpr->getExprLoc(); |
| 141 | + } else if (MatchedDecl != BareMatchedDecl || SourceValue != Value) { |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + diag(Location, |
| 146 | + "the constant '%0' is being utilized; consider using '%1' instead") |
| 147 | + << SourceValue << Replacement |
| 148 | + << FixItHint::CreateReplacement(Range, Replacement) |
| 149 | + << Inserter.createIncludeInsertion( |
| 150 | + Result.SourceManager->getFileID(Location), "<limits>"); |
| 151 | + }; |
| 152 | + |
| 153 | + for (const auto &[Value, Replacement] : SignedConstants) |
| 154 | + Fixer(MatchedIntegerConstant.getSExtValue(), Value, Replacement); |
| 155 | + |
| 156 | + for (const auto &[Value, Replacement] : UnsignedConstants) |
| 157 | + Fixer(MatchedIntegerConstant.getZExtValue(), Value, Replacement); |
| 158 | +} |
| 159 | + |
| 160 | +} // namespace clang::tidy::readability |
0 commit comments