blob: e59ca17596701f2251b22c085f8702d4df585f4c [file] [log] [blame]
Aaron Ballmanbc8f5ac2018-08-12 14:35:13 +00001//===--- MagicNumbersCheck.h - clang-tidy-----------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Aaron Ballmanbc8f5ac2018-08-12 14:35:13 +00006//
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
Alexander Kornienko478fc5c2019-03-25 12:38:26 +000012#include "../ClangTidyCheck.h"
Aaron Ballmanbc8f5ac2018-08-12 14:35:13 +000013#include <llvm/ADT/APFloat.h>
14#include <llvm/ADT/SmallVector.h>
15#include <vector>
16
17namespace clang {
18namespace tidy {
19namespace 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
25class MagicNumbersCheck : public ClangTidyCheck {
26public:
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
32private:
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
44 bool isSyntheticValue(const clang::SourceManager *SourceManager,
45 const IntegerLiteral *Literal) const;
46
47 template <typename L>
48 void checkBoundMatch(const ast_matchers::MatchFinder::MatchResult &Result,
49 const char *BoundName) {
50 const L *MatchedLiteral = Result.Nodes.getNodeAs<L>(BoundName);
51 if (!MatchedLiteral)
52 return;
53
54 if (Result.SourceManager->isMacroBodyExpansion(
55 MatchedLiteral->getLocation()))
56 return;
57
58 if (isIgnoredValue(MatchedLiteral))
59 return;
60
61 if (isConstant(Result, *MatchedLiteral))
62 return;
63
64 if (isSyntheticValue(Result.SourceManager, MatchedLiteral))
65 return;
66
67 const StringRef LiteralSourceText = Lexer::getSourceText(
68 CharSourceRange::getTokenRange(MatchedLiteral->getSourceRange()),
69 *Result.SourceManager, getLangOpts());
70
71 diag(MatchedLiteral->getLocation(),
72 "%0 is a magic number; consider replacing it with a named constant")
73 << LiteralSourceText;
74 }
75
76 const bool IgnoreAllFloatingPointValues;
77 const bool IgnorePowersOf2IntegerValues;
78
79 constexpr static unsigned SensibleNumberOfMagicValueExceptions = 16;
80
81 constexpr static llvm::APFloat::roundingMode DefaultRoundingMode =
82 llvm::APFloat::rmNearestTiesToEven;
83
84 llvm::SmallVector<int64_t, SensibleNumberOfMagicValueExceptions>
85 IgnoredIntegerValues;
86 llvm::SmallVector<float, SensibleNumberOfMagicValueExceptions>
87 IgnoredFloatingPointValues;
88 llvm::SmallVector<double, SensibleNumberOfMagicValueExceptions>
89 IgnoredDoublePointValues;
90};
91
92} // namespace readability
93} // namespace tidy
94} // namespace clang
95
96#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_MAGICNUMBERSCHECK_H