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>
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 {
49 bool isBitFieldWidth(
const clang::ast_matchers::MatchFinder::MatchResult &Result,
50 const IntegerLiteral &Literal)
const;
53 void checkBoundMatch(
const ast_matchers::MatchFinder::MatchResult &Result,
54 const char *BoundName) {
55 const L *MatchedLiteral = Result.Nodes.getNodeAs<L>(BoundName);
59 if (Result.SourceManager->isMacroBodyExpansion(
60 MatchedLiteral->getLocation()))
63 if (isIgnoredValue(MatchedLiteral))
66 if (isConstant(Result, *MatchedLiteral))
69 if (isSyntheticValue(Result.SourceManager, MatchedLiteral))
72 if (isBitFieldWidth(Result, *MatchedLiteral))
75 const StringRef LiteralSourceText = Lexer::getSourceText(
76 CharSourceRange::getTokenRange(MatchedLiteral->getSourceRange()),
79 diag(MatchedLiteral->getLocation(),
80 "%0 is a magic number; consider replacing it with a named constant")
84 const bool IgnoreAllFloatingPointValues;
85 const bool IgnoreBitFieldsWidths;
86 const bool IgnorePowersOf2IntegerValues;
87 const bool IgnoreTypeAliases;
88 const StringRef RawIgnoredIntegerValues;
89 const StringRef RawIgnoredFloatingPointValues;
91 constexpr static unsigned SensibleNumberOfMagicValueExceptions = 16;
93 constexpr static llvm::APFloat::roundingMode DefaultRoundingMode =
94 llvm::APFloat::rmNearestTiesToEven;
96 llvm::SmallVector<int64_t, SensibleNumberOfMagicValueExceptions>
98 llvm::SmallVector<float, SensibleNumberOfMagicValueExceptions>
99 IgnoredFloatingPointValues;
100 llvm::SmallVector<double, SensibleNumberOfMagicValueExceptions>
101 IgnoredDoublePointValues;
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
const LangOptions & getLangOpts() const
Returns the language options from the context.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Detects magic numbers, integer and floating point literals embedded in code.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
llvm::StringMap< ClangTidyValue > OptionMap