| 1 | //===--- MagicNumbersCheck.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 | // A checker for magic numbers: integer or floating point literals embedded |
| 10 | // in the code, outside the definition of a constant or an enumeration. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "MagicNumbersCheck.h" |
| 15 | #include "../utils/OptionsUtils.h" |
| 16 | #include "clang/AST/ASTContext.h" |
| 17 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 18 | #include "llvm/ADT/STLExtras.h" |
| 19 | #include <algorithm> |
| 20 | |
| 21 | using namespace clang::ast_matchers; |
| 22 | |
| 23 | namespace clang { |
| 24 | |
| 25 | static bool isUsedToInitializeAConstant(const MatchFinder::MatchResult &Result, |
| 26 | const DynTypedNode &Node) { |
| 27 | |
| 28 | const auto *AsDecl = Node.get<DeclaratorDecl>(); |
| 29 | if (AsDecl) { |
| 30 | if (AsDecl->getType().isConstQualified()) |
| 31 | return true; |
| 32 | |
| 33 | return AsDecl->isImplicit(); |
| 34 | } |
| 35 | |
| 36 | if (Node.get<EnumConstantDecl>()) |
| 37 | return true; |
| 38 | |
| 39 | return llvm::any_of(Result.Context->getParents(Node), |
| 40 | [&Result](const DynTypedNode &Parent) { |
| 41 | return isUsedToInitializeAConstant(Result, Parent); |
| 42 | }); |
| 43 | } |
| 44 | |
| 45 | static bool isUsedToDefineABitField(const MatchFinder::MatchResult &Result, |
| 46 | const DynTypedNode &Node) { |
| 47 | const auto *AsFieldDecl = Node.get<FieldDecl>(); |
| 48 | if (AsFieldDecl && AsFieldDecl->isBitField()) |
| 49 | return true; |
| 50 | |
| 51 | return llvm::any_of(Result.Context->getParents(Node), |
| 52 | [&Result](const DynTypedNode &Parent) { |
| 53 | return isUsedToDefineABitField(Result, Parent); |
| 54 | }); |
| 55 | } |
| 56 | |
| 57 | namespace tidy::readability { |
| 58 | |
| 59 | const char DefaultIgnoredIntegerValues[] = "1;2;3;4;"; |
| 60 | const char DefaultIgnoredFloatingPointValues[] = "1.0;100.0;"; |
| 61 | |
| 62 | MagicNumbersCheck::MagicNumbersCheck(StringRef Name, ClangTidyContext *Context) |
| 63 | : ClangTidyCheck(Name, Context), |
| 64 | IgnoreAllFloatingPointValues( |
| 65 | Options.get("IgnoreAllFloatingPointValues", false)), |
| 66 | IgnoreBitFieldsWidths(Options.get("IgnoreBitFieldsWidths", true)), |
| 67 | IgnorePowersOf2IntegerValues( |
| 68 | Options.get("IgnorePowersOf2IntegerValues", false)), |
| 69 | RawIgnoredIntegerValues( |
| 70 | Options.get("IgnoredIntegerValues", DefaultIgnoredIntegerValues)), |
| 71 | RawIgnoredFloatingPointValues(Options.get( |
| 72 | "IgnoredFloatingPointValues", DefaultIgnoredFloatingPointValues)) { |
| 73 | // Process the set of ignored integer values. |
| 74 | const std::vector<StringRef> IgnoredIntegerValuesInput = |
| 75 | utils::options::parseStringList(RawIgnoredIntegerValues); |
| 76 | IgnoredIntegerValues.resize(IgnoredIntegerValuesInput.size()); |
| 77 | llvm::transform(IgnoredIntegerValuesInput, IgnoredIntegerValues.begin(), |
| 78 | [](StringRef Value) { |
| 79 | int64_t Res; |
| 80 | Value.getAsInteger(10, Res); |
| 81 | return Res; |
| 82 | }); |
| 83 | llvm::sort(IgnoredIntegerValues); |
| 84 | |
| 85 | if (!IgnoreAllFloatingPointValues) { |
| 86 | // Process the set of ignored floating point values. |
| 87 | const std::vector<StringRef> IgnoredFloatingPointValuesInput = |
| 88 | utils::options::parseStringList(RawIgnoredFloatingPointValues); |
| 89 | IgnoredFloatingPointValues.reserve(IgnoredFloatingPointValuesInput.size()); |
| 90 | IgnoredDoublePointValues.reserve(IgnoredFloatingPointValuesInput.size()); |
| 91 | for (const auto &InputValue : IgnoredFloatingPointValuesInput) { |
| 92 | llvm::APFloat FloatValue(llvm::APFloat::IEEEsingle()); |
| 93 | auto StatusOrErr = |
| 94 | FloatValue.convertFromString(InputValue, DefaultRoundingMode); |
| 95 | assert(StatusOrErr && "Invalid floating point representation"); |
| 96 | consumeError(StatusOrErr.takeError()); |
| 97 | IgnoredFloatingPointValues.push_back(FloatValue.convertToFloat()); |
| 98 | |
| 99 | llvm::APFloat DoubleValue(llvm::APFloat::IEEEdouble()); |
| 100 | StatusOrErr = |
| 101 | DoubleValue.convertFromString(InputValue, DefaultRoundingMode); |
| 102 | assert(StatusOrErr && "Invalid floating point representation"); |
| 103 | consumeError(StatusOrErr.takeError()); |
| 104 | IgnoredDoublePointValues.push_back(DoubleValue.convertToDouble()); |
| 105 | } |
| 106 | llvm::sort(IgnoredFloatingPointValues); |
| 107 | llvm::sort(IgnoredDoublePointValues); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | void MagicNumbersCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { |
| 112 | Options.store(Opts, "IgnoreAllFloatingPointValues", |
| 113 | IgnoreAllFloatingPointValues); |
| 114 | Options.store(Opts, "IgnoreBitFieldsWidths", IgnoreBitFieldsWidths); |
| 115 | Options.store(Opts, "IgnorePowersOf2IntegerValues", |
| 116 | IgnorePowersOf2IntegerValues); |
| 117 | Options.store(Opts, "IgnoredIntegerValues", RawIgnoredIntegerValues); |
| 118 | Options.store(Opts, "IgnoredFloatingPointValues", |
| 119 | RawIgnoredFloatingPointValues); |
| 120 | } |
| 121 | |
| 122 | void MagicNumbersCheck::registerMatchers(MatchFinder *Finder) { |
| 123 | Finder->addMatcher(integerLiteral().bind("integer"), this); |
| 124 | if (!IgnoreAllFloatingPointValues) |
| 125 | Finder->addMatcher(floatLiteral().bind("float"), this); |
| 126 | } |
| 127 | |
| 128 | void MagicNumbersCheck::check(const MatchFinder::MatchResult &Result) { |
| 129 | |
| 130 | TraversalKindScope RAII(*Result.Context, TK_AsIs); |
| 131 | |
| 132 | checkBoundMatch<IntegerLiteral>(Result, "integer"); |
| 133 | checkBoundMatch<FloatingLiteral>(Result, "float"); |
| 134 | } |
| 135 | |
| 136 | bool MagicNumbersCheck::isConstant(const MatchFinder::MatchResult &Result, |
| 137 | const Expr &ExprResult) const { |
| 138 | return llvm::any_of( |
| 139 | Result.Context->getParents(ExprResult), |
| 140 | [&Result](const DynTypedNode &Parent) { |
| 141 | if (isUsedToInitializeAConstant(Result, Parent)) |
| 142 | return true; |
| 143 | |
| 144 | // Ignore this instance, because this matches an |
| 145 | // expanded class enumeration value. |
| 146 | if (Parent.get<CStyleCastExpr>() && |
| 147 | llvm::any_of( |
| 148 | Result.Context->getParents(Parent), |
| 149 | [](const DynTypedNode &GrandParent) { |
| 150 | return GrandParent.get<SubstNonTypeTemplateParmExpr>() != |
| 151 | nullptr; |
| 152 | })) |
| 153 | return true; |
| 154 | |
| 155 | // Ignore this instance, because this match reports the |
| 156 | // location where the template is defined, not where it |
| 157 | // is instantiated. |
| 158 | if (Parent.get<SubstNonTypeTemplateParmExpr>()) |
| 159 | return true; |
| 160 | |
| 161 | // Don't warn on string user defined literals: |
| 162 | // std::string s = "Hello World"s; |
| 163 | if (const auto *UDL = Parent.get<UserDefinedLiteral>()) |
| 164 | if (UDL->getLiteralOperatorKind() == UserDefinedLiteral::LOK_String) |
| 165 | return true; |
| 166 | |
| 167 | return false; |
| 168 | }); |
| 169 | } |
| 170 | |
| 171 | bool MagicNumbersCheck::isIgnoredValue(const IntegerLiteral *Literal) const { |
| 172 | const llvm::APInt IntValue = Literal->getValue(); |
| 173 | const int64_t Value = IntValue.getZExtValue(); |
| 174 | if (Value == 0) |
| 175 | return true; |
| 176 | |
| 177 | if (IgnorePowersOf2IntegerValues && IntValue.isPowerOf2()) |
| 178 | return true; |
| 179 | |
| 180 | return std::binary_search(IgnoredIntegerValues.begin(), |
| 181 | IgnoredIntegerValues.end(), Value); |
| 182 | } |
| 183 | |
| 184 | bool MagicNumbersCheck::isIgnoredValue(const FloatingLiteral *Literal) const { |
| 185 | const llvm::APFloat FloatValue = Literal->getValue(); |
| 186 | if (FloatValue.isZero()) |
| 187 | return true; |
| 188 | |
| 189 | if (&FloatValue.getSemantics() == &llvm::APFloat::IEEEsingle()) { |
| 190 | const float Value = FloatValue.convertToFloat(); |
| 191 | return std::binary_search(IgnoredFloatingPointValues.begin(), |
| 192 | IgnoredFloatingPointValues.end(), Value); |
| 193 | } |
| 194 | |
| 195 | if (&FloatValue.getSemantics() == &llvm::APFloat::IEEEdouble()) { |
| 196 | const double Value = FloatValue.convertToDouble(); |
| 197 | return std::binary_search(IgnoredDoublePointValues.begin(), |
| 198 | IgnoredDoublePointValues.end(), Value); |
| 199 | } |
| 200 | |
| 201 | return false; |
| 202 | } |
| 203 | |
| 204 | bool MagicNumbersCheck::isSyntheticValue(const SourceManager *SourceManager, |
| 205 | const IntegerLiteral *Literal) const { |
| 206 | const std::pair<FileID, unsigned> FileOffset = |
| 207 | SourceManager->getDecomposedLoc(Literal->getLocation()); |
| 208 | if (FileOffset.first.isInvalid()) |
| 209 | return false; |
| 210 | |
| 211 | const StringRef BufferIdentifier = |
| 212 | SourceManager->getBufferOrFake(FileOffset.first).getBufferIdentifier(); |
| 213 | |
| 214 | return BufferIdentifier.empty(); |
| 215 | } |
| 216 | |
| 217 | bool MagicNumbersCheck::isBitFieldWidth( |
| 218 | const clang::ast_matchers::MatchFinder::MatchResult &Result, |
| 219 | const IntegerLiteral &Literal) const { |
| 220 | return IgnoreBitFieldsWidths && |
| 221 | llvm::any_of(Result.Context->getParents(Literal), |
| 222 | [&Result](const DynTypedNode &Parent) { |
| 223 | return isUsedToDefineABitField(Result, Parent); |
| 224 | }); |
| 225 | } |
| 226 | |
| 227 | } // namespace tidy::readability |
| 228 | } // namespace clang |