| 1 |
//===--- MagicNumbersCheck.h - clang-tidy-----------------------*- C++ -*-===// |
| 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 |
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_MAGICNUMBERSCHECK_H |
| 10 |
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_MAGICNUMBERSCHECK_H |
| 11 |
|
| 12 |
#include "../ClangTidyCheck.h" |
| 13 |
#include "clang/Lex/Lexer.h" |
| 14 |
#include <llvm/ADT/APFloat.h> |
| 15 |
#include <llvm/ADT/SmallVector.h> |
| 16 |
|
| 17 |
namespace clang::tidy::readability { |
| 18 |
|
| 19 |
/// Detects magic numbers, integer and floating point literals embedded in code. |
| 20 |
/// |
| 21 |
/// For the user-facing documentation see: |
| 22 |
/// http://clang.llvm.org/extra/clang-tidy/checks/readability/magic-numbers.html |
| 23 |
class MagicNumbersCheck : public ClangTidyCheck { |
| 24 |
public: |
| 25 |
MagicNumbersCheck(StringRef Name, ClangTidyContext *Context); |
| 26 |
void storeOptions(ClangTidyOptions::OptionMap &Opts) override; |
| 27 |
void registerMatchers(ast_matchers::MatchFinder *Finder) override; |
| 28 |
void check(const ast_matchers::MatchFinder::MatchResult &Result) override; |
| 29 |
|
| 30 |
private: |
| 31 |
bool isConstant(const clang::ast_matchers::MatchFinder::MatchResult &Result, |
| 32 |
const clang::Expr &ExprResult) const; |
| 33 |
|
| 34 |
bool isIgnoredValue(const IntegerLiteral *Literal) const; |
| 35 |
bool isIgnoredValue(const FloatingLiteral *Literal) const; |
| 36 |
|
| 37 |
bool isSyntheticValue(const clang::SourceManager *, |
| 38 |
const FloatingLiteral *) const { |
| 39 |
return false; |
| 40 |
} |
| 41 |
bool isSyntheticValue(const clang::SourceManager *SourceManager, |
| 42 |
const IntegerLiteral *Literal) const; |
| 43 |
|
| 44 |
bool isBitFieldWidth(const clang::ast_matchers::MatchFinder::MatchResult &, |
| 45 |
const FloatingLiteral &) const { |
| 46 |
return false; |
| 47 |
} |
| 48 |
|
| 49 |
bool isBitFieldWidth(const clang::ast_matchers::MatchFinder::MatchResult &Result, |
| 50 |
const IntegerLiteral &Literal) const; |
| 51 |
|
| 52 |
template <typename L> |
| 53 |
void checkBoundMatch(const ast_matchers::MatchFinder::MatchResult &Result, |
| 54 |
const char *BoundName) { |
| 55 |
const L *MatchedLiteral = Result.Nodes.getNodeAs<L>(BoundName); |
| 56 |
if (!MatchedLiteral) |
| 57 |
return; |
| 58 |
|
| 59 |
if (Result.SourceManager->isMacroBodyExpansion( |
| 60 |
MatchedLiteral->getLocation())) |
| 61 |
return; |
| 62 |
|
| 63 |
if (isIgnoredValue(MatchedLiteral)) |
| 64 |
return; |
| 65 |
|
| 66 |
if (isConstant(Result, *MatchedLiteral)) |
| 67 |
return; |
| 68 |
|
| 69 |
if (isSyntheticValue(Result.SourceManager, MatchedLiteral)) |
| 70 |
return; |
| 71 |
|
| 72 |
if (isBitFieldWidth(Result, *MatchedLiteral)) |
| 73 |
return; |
| 74 |
|
| 75 |
const StringRef LiteralSourceText = Lexer::getSourceText( |
| 76 |
CharSourceRange::getTokenRange(MatchedLiteral->getSourceRange()), |
| 77 |
*Result.SourceManager, getLangOpts()); |
| 78 |
|
| 79 |
diag(MatchedLiteral->getLocation(), |
| 80 |
"%0 is a magic number; consider replacing it with a named constant") |
| 81 |
<< LiteralSourceText; |
| 82 |
} |
| 83 |
|
| 84 |
const bool IgnoreAllFloatingPointValues; |
| 85 |
const bool IgnoreBitFieldsWidths; |
| 86 |
const bool IgnorePowersOf2IntegerValues; |
| 87 |
const StringRef RawIgnoredIntegerValues; |
| 88 |
const StringRef RawIgnoredFloatingPointValues; |
| 89 |
|
| 90 |
constexpr static unsigned SensibleNumberOfMagicValueExceptions = 16; |
| 91 |
|
| 92 |
constexpr static llvm::APFloat::roundingMode DefaultRoundingMode = |
| 93 |
llvm::APFloat::rmNearestTiesToEven; |
| 94 |
|
| 95 |
llvm::SmallVector<int64_t, SensibleNumberOfMagicValueExceptions> |
| 96 |
IgnoredIntegerValues; |
| 97 |
llvm::SmallVector<float, SensibleNumberOfMagicValueExceptions> |
| 98 |
IgnoredFloatingPointValues; |
| 99 |
llvm::SmallVector<double, SensibleNumberOfMagicValueExceptions> |
| 100 |
IgnoredDoublePointValues; |
| 101 |
}; |
| 102 |
|
| 103 |
} // namespace clang::tidy::readability |
| 104 |
|
| 105 |
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_MAGICNUMBERSCHECK_H |