clang-tools 10.0.0

MagicNumbersCheck.h
Go to the documentation of this file.
1 //===--- MagicNumbersCheck.h - clang-tidy-----------------------*- C++ -*-===//
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 <llvm/ADT/APFloat.h>
14 #include <llvm/ADT/SmallVector.h>
15 #include <vector>
16
17 namespace clang {
18 namespace tidy {
19 namespace readability {
20
21 /// Detects magic numbers, integer and floating point literals embedded in code.
22 ///
23 /// For the user-facing documentation see:
24 /// http://clang.llvm.org/extra/clang-tidy/checks/readability-magic-numbers.html
25 class MagicNumbersCheck : public ClangTidyCheck {
26 public:
27 MagicNumbersCheck(StringRef Name, ClangTidyContext *Context);
28 void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
29 void registerMatchers(ast_matchers::MatchFinder *Finder) override;
30 void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
31
32 private:
33 bool isConstant(const clang::ast_matchers::MatchFinder::MatchResult &Result,
34 const clang::Expr &ExprResult) const;
35
36 bool isIgnoredValue(const IntegerLiteral *Literal) const;
37 bool isIgnoredValue(const FloatingLiteral *Literal) const;
38
39 bool isSyntheticValue(const clang::SourceManager *,
40 const FloatingLiteral *) const {
41 return false;
42 }
43 bool isSyntheticValue(const clang::SourceManager *SourceManager,
44 const IntegerLiteral *Literal) const;
45
46 bool isBitFieldWidth(const clang::ast_matchers::MatchFinder::MatchResult &,
47 const FloatingLiteral &) const {
48 return false;
49 }
50
51 bool isBitFieldWidth(const clang::ast_matchers::MatchFinder::MatchResult &Result,
52 const IntegerLiteral &Literal) const;
53
54 template <typename L>
55 void checkBoundMatch(const ast_matchers::MatchFinder::MatchResult &Result,
56 const char *BoundName) {
57 const L *MatchedLiteral = Result.Nodes.getNodeAs<L>(BoundName);
58 if (!MatchedLiteral)
59 return;
60
61 if (Result.SourceManager->isMacroBodyExpansion(
62 MatchedLiteral->getLocation()))
63 return;
64
65 if (isIgnoredValue(MatchedLiteral))
66 return;
67
68 if (isConstant(Result, *MatchedLiteral))
69 return;
70
71 if (isSyntheticValue(Result.SourceManager, MatchedLiteral))
72 return;
73
74 if (isBitFieldWidth(Result, *MatchedLiteral))
75 return;
76
77 const StringRef LiteralSourceText = Lexer::getSourceText(
78 CharSourceRange::getTokenRange(MatchedLiteral->getSourceRange()),
79 *Result.SourceManager, getLangOpts());
80
81 diag(MatchedLiteral->getLocation(),
82 "%0 is a magic number; consider replacing it with a named constant")
83 << LiteralSourceText;
84 }
85
86 const bool IgnoreAllFloatingPointValues;
87 const bool IgnoreBitFieldsWidths;
88 const bool IgnorePowersOf2IntegerValues;
89
90 constexpr static unsigned SensibleNumberOfMagicValueExceptions = 16;
91
92 constexpr static llvm::APFloat::roundingMode DefaultRoundingMode =
93 llvm::APFloat::rmNearestTiesToEven;
94
95 llvm::SmallVector<int64_t, SensibleNumberOfMagicValueExceptions>
96 IgnoredIntegerValues;
97 llvm::SmallVector<float, SensibleNumberOfMagicValueExceptions>
98 IgnoredFloatingPointValues;
99 llvm::SmallVector<double, SensibleNumberOfMagicValueExceptions>
100 IgnoredDoublePointValues;
101 };
102
103 } // namespace readability
104 } // namespace tidy
105 } // namespace clang
106
107 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_MAGICNUMBERSCHECK_H
clang::tidy::readability::MagicNumbersCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: MagicNumbersCheck.cpp:121
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:47
clang::tidy::ClangTidyCheck::getLangOpts
const LangOptions & getLangOpts() const
Returns the language options from the context.
Definition: ClangTidyCheck.h:188
clang::tidy::readability::MagicNumbersCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: MagicNumbersCheck.cpp:115
clang::tidy::readability::MagicNumbersCheck
Detects magic numbers, integer and floating point literals embedded in code.
Definition: MagicNumbersCheck.h:25
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, std::string > OptionMap
Definition: ClangTidyOptions.h:97
clang::clangd::getTokenRange
llvm::Optional< Range > getTokenRange(const SourceManager &SM, const LangOptions &LangOpts, SourceLocation TokLoc)
Returns the taken range at TokLoc.
Definition: SourceCode.cpp:227
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:77
clang::tidy::readability::MagicNumbersCheck::MagicNumbersCheck
MagicNumbersCheck(StringRef Name, ClangTidyContext *Context)
Definition: MagicNumbersCheck.cpp:64
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.
Definition: ClangTidyCheck.cpp:21
clang::tidy::readability::MagicNumbersCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition: MagicNumbersCheck.cpp:109