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 <llvm/ADT/APFloat.h> 14 #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 {
44 bool isSyntheticValue(
const clang::SourceManager *SourceManager,
45 const IntegerLiteral *Literal)
const;
48 void checkBoundMatch(
const ast_matchers::MatchFinder::MatchResult &Result,
49 const char *BoundName) {
50 const L *MatchedLiteral = Result.Nodes.getNodeAs<L>(BoundName);
54 if (Result.SourceManager->isMacroBodyExpansion(
55 MatchedLiteral->getLocation()))
58 if (isIgnoredValue(MatchedLiteral))
61 if (isConstant(Result, *MatchedLiteral))
64 if (isSyntheticValue(Result.SourceManager, MatchedLiteral))
67 const StringRef LiteralSourceText = Lexer::getSourceText(
71 diag(MatchedLiteral->getLocation(),
72 "%0 is a magic number; consider replacing it with a named constant")
76 const bool IgnoreAllFloatingPointValues;
77 const bool IgnorePowersOf2IntegerValues;
79 constexpr
static unsigned SensibleNumberOfMagicValueExceptions = 16;
81 constexpr
static llvm::APFloat::roundingMode DefaultRoundingMode =
82 llvm::APFloat::rmNearestTiesToEven;
84 llvm::SmallVector<int64_t, SensibleNumberOfMagicValueExceptions>
86 llvm::SmallVector<float, SensibleNumberOfMagicValueExceptions>
87 IgnoredFloatingPointValues;
88 llvm::SmallVector<double, SensibleNumberOfMagicValueExceptions>
89 IgnoredDoublePointValues;
96 #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.
Base class for all clang-tidy checks.
const LangOptions & getLangOpts() const
Returns the language options from the context.
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< Range > getTokenRange(const SourceManager &SM, const LangOptions &LangOpts, SourceLocation TokLoc)
Returns the taken range at TokLoc.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
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...