9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_MAGICNUMBERSCHECK_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_MAGICNUMBERSCHECK_H
12 #include "../ClangTidyCheck.h"
13 #include "clang/Lex/Lexer.h"
14 #include <llvm/ADT/APFloat.h>
15 #include <llvm/ADT/SmallVector.h>
19 namespace readability {
30 void check(
const ast_matchers::MatchFinder::MatchResult &Result)
override;
33 bool isConstant(
const clang::ast_matchers::MatchFinder::MatchResult &Result,
34 const clang::Expr &ExprResult)
const;
36 bool isIgnoredValue(
const IntegerLiteral *Literal)
const;
37 bool isIgnoredValue(
const FloatingLiteral *Literal)
const;
39 bool isSyntheticValue(
const clang::SourceManager *,
40 const FloatingLiteral *)
const {
43 bool isSyntheticValue(
const clang::SourceManager *SourceManager,
44 const IntegerLiteral *Literal)
const;
46 bool isBitFieldWidth(
const clang::ast_matchers::MatchFinder::MatchResult &,
47 const FloatingLiteral &)
const {
51 bool isBitFieldWidth(
const clang::ast_matchers::MatchFinder::MatchResult &Result,
52 const IntegerLiteral &Literal)
const;
55 void checkBoundMatch(
const ast_matchers::MatchFinder::MatchResult &Result,
56 const char *BoundName) {
57 const L *MatchedLiteral = Result.Nodes.getNodeAs<L>(BoundName);
61 if (Result.SourceManager->isMacroBodyExpansion(
62 MatchedLiteral->getLocation()))
65 if (isIgnoredValue(MatchedLiteral))
68 if (isConstant(Result, *MatchedLiteral))
71 if (isSyntheticValue(Result.SourceManager, MatchedLiteral))
74 if (isBitFieldWidth(Result, *MatchedLiteral))
78 CharSourceRange::getTokenRange(MatchedLiteral->getSourceRange()),
81 diag(MatchedLiteral->getLocation(),
82 "%0 is a magic number; consider replacing it with a named constant")
86 const bool IgnoreAllFloatingPointValues;
87 const bool IgnoreBitFieldsWidths;
88 const bool IgnorePowersOf2IntegerValues;
89 const StringRef RawIgnoredIntegerValues;
90 const StringRef RawIgnoredFloatingPointValues;
92 constexpr
static unsigned SensibleNumberOfMagicValueExceptions = 16;
94 constexpr
static llvm::APFloat::roundingMode DefaultRoundingMode =
95 llvm::APFloat::rmNearestTiesToEven;
97 llvm::SmallVector<int64_t, SensibleNumberOfMagicValueExceptions>
99 llvm::SmallVector<float, SensibleNumberOfMagicValueExceptions>
100 IgnoredFloatingPointValues;
101 llvm::SmallVector<double, SensibleNumberOfMagicValueExceptions>
102 IgnoredDoublePointValues;
109 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_MAGICNUMBERSCHECK_H