28 void check(
const ast_matchers::MatchFinder::MatchResult &Result)
override;
31 bool isConstant(
const clang::ast_matchers::MatchFinder::MatchResult &Result,
32 const clang::Expr &ExprResult)
const;
34 bool isIgnoredValue(
const IntegerLiteral *Literal)
const;
35 bool isIgnoredValue(
const FloatingLiteral *Literal)
const;
37 bool isSyntheticValue(
const clang::SourceManager *,
38 const FloatingLiteral *)
const {
41 bool isSyntheticValue(
const clang::SourceManager *SourceManager,
42 const IntegerLiteral *Literal)
const;
44 bool isBitFieldWidth(
const clang::ast_matchers::MatchFinder::MatchResult &,
45 const FloatingLiteral &)
const {
50 isBitFieldWidth(
const clang::ast_matchers::MatchFinder::MatchResult &Result,
51 const IntegerLiteral &Literal)
const;
53 bool isUserDefinedLiteral(
54 const clang::ast_matchers::MatchFinder::MatchResult &Result,
55 const clang::Expr &Literal)
const;
58 void checkBoundMatch(
const ast_matchers::MatchFinder::MatchResult &Result,
59 const char *BoundName) {
60 const L *MatchedLiteral = Result.Nodes.getNodeAs<L>(BoundName);
64 if (Result.SourceManager->isMacroBodyExpansion(
65 MatchedLiteral->getLocation()))
68 if (isIgnoredValue(MatchedLiteral))
71 if (isConstant(Result, *MatchedLiteral))
74 if (isSyntheticValue(Result.SourceManager, MatchedLiteral))
77 if (isBitFieldWidth(Result, *MatchedLiteral))
80 if (IgnoreUserDefinedLiterals &&
81 isUserDefinedLiteral(Result, *MatchedLiteral))
84 const StringRef LiteralSourceText = Lexer::getSourceText(
85 CharSourceRange::getTokenRange(MatchedLiteral->getSourceRange()),
86 *Result.SourceManager, getLangOpts());
88 diag(MatchedLiteral->getLocation(),
89 "%0 is a magic number; consider replacing it with a named constant")
93 const bool IgnoreAllFloatingPointValues;
94 const bool IgnoreBitFieldsWidths;
95 const bool IgnorePowersOf2IntegerValues;
96 const bool IgnoreTypeAliases;
97 const bool IgnoreUserDefinedLiterals;
98 const StringRef RawIgnoredIntegerValues;
99 const StringRef RawIgnoredFloatingPointValues;
101 constexpr static unsigned SensibleNumberOfMagicValueExceptions = 16;
103 constexpr static llvm::APFloat::roundingMode DefaultRoundingMode =
104 llvm::APFloat::rmNearestTiesToEven;
106 llvm::SmallVector<int64_t, SensibleNumberOfMagicValueExceptions>
107 IgnoredIntegerValues;
108 llvm::SmallVector<float, SensibleNumberOfMagicValueExceptions>
109 IgnoredFloatingPointValues;
110 llvm::SmallVector<double, SensibleNumberOfMagicValueExceptions>
111 IgnoredDoublePointValues;
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.