clang-tools  10.0.0svn
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
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 
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
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.
Definition: SourceCode.cpp:229
===– 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&#39;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...