clang-tools 23.0.0git
MagicNumbersCheck.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_MAGICNUMBERSCHECK_H
10#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_MAGICNUMBERSCHECK_H
11
12#include "../ClangTidyCheck.h"
13#include "clang/Lex/Lexer.h"
14#include <llvm/ADT/APFloat.h>
15#include <llvm/ADT/SmallVector.h>
16
18
19/// Detects magic numbers, integer and floating point literals embedded in code.
20///
21/// For the user-facing documentation see:
22/// https://clang.llvm.org/extra/clang-tidy/checks/readability/magic-numbers.html
24public:
25 MagicNumbersCheck(StringRef Name, ClangTidyContext *Context);
26 void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
27 void registerMatchers(ast_matchers::MatchFinder *Finder) override;
28 void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
29
30private:
31 bool isConstant(const ast_matchers::MatchFinder::MatchResult &Result,
32 const Expr &ExprResult) const;
33
34 bool isIgnoredValue(const IntegerLiteral *Literal) const;
35 bool isIgnoredValue(const FloatingLiteral *Literal) const;
36
37 bool isSyntheticValue(const SourceManager *, const FloatingLiteral *) const {
38 return false;
39 }
40 bool isSyntheticValue(const SourceManager *SourceManager,
41 const IntegerLiteral *Literal) const;
42
43 bool isBitFieldWidth(const ast_matchers::MatchFinder::MatchResult &,
44 const FloatingLiteral &) const {
45 return false;
46 }
47
48 bool isBitFieldWidth(const ast_matchers::MatchFinder::MatchResult &Result,
49 const IntegerLiteral &Literal) const;
50
51 bool
52 isUserDefinedLiteral(const ast_matchers::MatchFinder::MatchResult &Result,
53 const Expr &Literal) const;
54
55 template <typename L>
56 void checkBoundMatch(const ast_matchers::MatchFinder::MatchResult &Result,
57 const char *BoundName) {
58 const L *MatchedLiteral = Result.Nodes.getNodeAs<L>(BoundName);
59 if (!MatchedLiteral)
60 return;
61
62 if (Result.SourceManager->isMacroBodyExpansion(
63 MatchedLiteral->getLocation()))
64 return;
65
66 if (isIgnoredValue(MatchedLiteral))
67 return;
68
69 if (isConstant(Result, *MatchedLiteral))
70 return;
71
72 if (isSyntheticValue(Result.SourceManager, MatchedLiteral))
73 return;
74
75 if (isBitFieldWidth(Result, *MatchedLiteral))
76 return;
77
78 if (IgnoreUserDefinedLiterals &&
79 isUserDefinedLiteral(Result, *MatchedLiteral))
80 return;
81
82 const StringRef LiteralSourceText = Lexer::getSourceText(
83 CharSourceRange::getTokenRange(MatchedLiteral->getSourceRange()),
84 *Result.SourceManager, getLangOpts());
85
86 diag(MatchedLiteral->getLocation(),
87 "%0 is a magic number; consider replacing it with a named constant")
88 << LiteralSourceText;
89 }
90
91 const bool IgnoreAllFloatingPointValues;
92 const bool IgnoreBitFieldsWidths;
93 const bool IgnorePowersOf2IntegerValues;
94 const bool IgnoreTypeAliases;
95 const bool IgnoreUserDefinedLiterals;
96 const StringRef RawIgnoredIntegerValues;
97 const StringRef RawIgnoredFloatingPointValues;
98
99 constexpr static unsigned SensibleNumberOfMagicValueExceptions = 16;
100
101 constexpr static llvm::APFloat::roundingMode DefaultRoundingMode =
102 llvm::APFloat::rmNearestTiesToEven;
103
105 IgnoredIntegerValues;
107 IgnoredFloatingPointValues;
109 IgnoredDoublePointValues;
110};
111
112} // namespace clang::tidy::readability
113
114#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_MAGICNUMBERSCHECK_H
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
MagicNumbersCheck(StringRef Name, ClangTidyContext *Context)
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
llvm::StringMap< ClangTidyValue > OptionMap