10 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_MAGICNUMBERSCHECK_H 11 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_MAGICNUMBERSCHECK_H 13 #include "../ClangTidy.h" 14 #include <llvm/ADT/APFloat.h> 15 #include <llvm/ADT/SmallVector.h> 20 namespace readability {
31 void check(
const ast_matchers::MatchFinder::MatchResult &
Result)
override;
34 bool isConstant(
const clang::ast_matchers::MatchFinder::MatchResult &Result,
35 const clang::Expr &ExprResult)
const;
37 bool isIgnoredValue(
const IntegerLiteral *Literal)
const;
38 bool isIgnoredValue(
const FloatingLiteral *Literal)
const;
40 bool isSyntheticValue(
const clang::SourceManager *,
41 const FloatingLiteral *)
const {
45 bool isSyntheticValue(
const clang::SourceManager *SourceManager,
46 const IntegerLiteral *Literal)
const;
49 void checkBoundMatch(
const ast_matchers::MatchFinder::MatchResult &Result,
50 const char *BoundName) {
51 const L *MatchedLiteral = Result.Nodes.getNodeAs<L>(BoundName);
55 if (Result.SourceManager->isMacroBodyExpansion(
56 MatchedLiteral->getLocation()))
59 if (isIgnoredValue(MatchedLiteral))
62 if (isConstant(Result, *MatchedLiteral))
65 if (isSyntheticValue(Result.SourceManager, MatchedLiteral))
68 const StringRef LiteralSourceText = Lexer::getSourceText(
69 CharSourceRange::getTokenRange(MatchedLiteral->getSourceRange()),
72 diag(MatchedLiteral->getLocation(),
73 "%0 is a magic number; consider replacing it with a named constant")
77 const bool IgnoreAllFloatingPointValues;
78 const bool IgnorePowersOf2IntegerValues;
80 constexpr
static unsigned SensibleNumberOfMagicValueExceptions = 16;
82 constexpr
static llvm::APFloat::roundingMode DefaultRoundingMode =
83 llvm::APFloat::rmNearestTiesToEven;
85 llvm::SmallVector<int64_t, SensibleNumberOfMagicValueExceptions>
87 llvm::SmallVector<float, SensibleNumberOfMagicValueExceptions>
88 IgnoredFloatingPointValues;
89 llvm::SmallVector<double, SensibleNumberOfMagicValueExceptions>
90 IgnoredDoublePointValues;
97 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_MAGICNUMBERSCHECK_H void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
LangOptions getLangOpts() const
Returns the language options from the context.
Base class for all clang-tidy checks.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Detects magic numbers, integer and floating point literals embedded in code.
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
MagicNumbersCheck(StringRef Name, ClangTidyContext *Context)
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...