clang-tools  10.0.0git
MagicNumbersCheck.cpp
Go to the documentation of this file.
1 //===--- MagicNumbersCheck.cpp - clang-tidy-------------------------------===//
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 // A checker for magic numbers: integer or floating point literals embedded
10 // in the code, outside the definition of a constant or an enumeration.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "MagicNumbersCheck.h"
15 #include "../utils/OptionsUtils.h"
16 #include "clang/AST/ASTContext.h"
17 #include "clang/ASTMatchers/ASTMatchFinder.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include <algorithm>
20 
21 using namespace clang::ast_matchers;
22 using namespace clang::ast_type_traits;
23 
24 namespace clang {
25 
26 static bool isUsedToInitializeAConstant(const MatchFinder::MatchResult &Result,
27  const DynTypedNode &Node) {
28 
29  const auto *AsDecl = Node.get<DeclaratorDecl>();
30  if (AsDecl) {
31  if (AsDecl->getType().isConstQualified())
32  return true;
33 
34  return AsDecl->isImplicit();
35  }
36 
37  if (Node.get<EnumConstantDecl>())
38  return true;
39 
40  return llvm::any_of(Result.Context->getParents(Node),
41  [&Result](const DynTypedNode &Parent) {
42  return isUsedToInitializeAConstant(Result, Parent);
43  });
44 }
45 
46 static bool isUsedToDefineABitField(const MatchFinder::MatchResult &Result,
47  const DynTypedNode &Node) {
48  const auto *AsFieldDecl = Node.get<FieldDecl>();
49  if (AsFieldDecl && AsFieldDecl->isBitField())
50  return true;
51 
52  return llvm::any_of(Result.Context->getParents(Node),
53  [&Result](const DynTypedNode &Parent) {
54  return isUsedToDefineABitField(Result, Parent);
55  });
56 }
57 
58 namespace tidy {
59 namespace readability {
60 
61 const char DefaultIgnoredIntegerValues[] = "1;2;3;4;";
62 const char DefaultIgnoredFloatingPointValues[] = "1.0;100.0;";
63 
64 MagicNumbersCheck::MagicNumbersCheck(StringRef Name, ClangTidyContext *Context)
65  : ClangTidyCheck(Name, Context),
66  IgnoreAllFloatingPointValues(
67  Options.get("IgnoreAllFloatingPointValues", false)),
68  IgnoreBitFieldsWidths(Options.get("IgnoreBitFieldsWidths", true)),
69  IgnorePowersOf2IntegerValues(
70  Options.get("IgnorePowersOf2IntegerValues", false)) {
71  // Process the set of ignored integer values.
72  const std::vector<std::string> IgnoredIntegerValuesInput =
74  Options.get("IgnoredIntegerValues", DefaultIgnoredIntegerValues));
75  IgnoredIntegerValues.resize(IgnoredIntegerValuesInput.size());
76  llvm::transform(IgnoredIntegerValuesInput, IgnoredIntegerValues.begin(),
77  [](const std::string &Value) { return std::stoll(Value); });
78  llvm::sort(IgnoredIntegerValues);
79 
80  if (!IgnoreAllFloatingPointValues) {
81  // Process the set of ignored floating point values.
82  const std::vector<std::string> IgnoredFloatingPointValuesInput =
84  "IgnoredFloatingPointValues", DefaultIgnoredFloatingPointValues));
85  IgnoredFloatingPointValues.reserve(IgnoredFloatingPointValuesInput.size());
86  IgnoredDoublePointValues.reserve(IgnoredFloatingPointValuesInput.size());
87  for (const auto &InputValue : IgnoredFloatingPointValuesInput) {
88  llvm::APFloat FloatValue(llvm::APFloat::IEEEsingle());
89  auto StatusOrErr =
90  FloatValue.convertFromString(InputValue, DefaultRoundingMode);
91  assert(StatusOrErr && "Invalid floating point representation");
92  consumeError(StatusOrErr.takeError());
93  IgnoredFloatingPointValues.push_back(FloatValue.convertToFloat());
94 
95  llvm::APFloat DoubleValue(llvm::APFloat::IEEEdouble());
96  StatusOrErr =
97  DoubleValue.convertFromString(InputValue, DefaultRoundingMode);
98  assert(StatusOrErr && "Invalid floating point representation");
99  consumeError(StatusOrErr.takeError());
100  IgnoredDoublePointValues.push_back(DoubleValue.convertToDouble());
101  }
102  llvm::sort(IgnoredFloatingPointValues.begin(),
103  IgnoredFloatingPointValues.end());
104  llvm::sort(IgnoredDoublePointValues.begin(),
105  IgnoredDoublePointValues.end());
106  }
107 }
108 
110  Options.store(Opts, "IgnoredIntegerValues", DefaultIgnoredIntegerValues);
111  Options.store(Opts, "IgnoredFloatingPointValues",
112  DefaultIgnoredFloatingPointValues);
113 }
114 
115 void MagicNumbersCheck::registerMatchers(MatchFinder *Finder) {
116  Finder->addMatcher(integerLiteral().bind("integer"), this);
117  if (!IgnoreAllFloatingPointValues)
118  Finder->addMatcher(floatLiteral().bind("float"), this);
119 }
120 
121 void MagicNumbersCheck::check(const MatchFinder::MatchResult &Result) {
122  checkBoundMatch<IntegerLiteral>(Result, "integer");
123  checkBoundMatch<FloatingLiteral>(Result, "float");
124 }
125 
126 bool MagicNumbersCheck::isConstant(const MatchFinder::MatchResult &Result,
127  const Expr &ExprResult) const {
128  return llvm::any_of(
129  Result.Context->getParents(ExprResult),
130  [&Result](const DynTypedNode &Parent) {
131  if (isUsedToInitializeAConstant(Result, Parent))
132  return true;
133 
134  // Ignore this instance, because this matches an
135  // expanded class enumeration value.
136  if (Parent.get<CStyleCastExpr>() &&
137  llvm::any_of(
138  Result.Context->getParents(Parent),
139  [](const DynTypedNode &GrandParent) {
140  return GrandParent.get<SubstNonTypeTemplateParmExpr>() !=
141  nullptr;
142  }))
143  return true;
144 
145  // Ignore this instance, because this match reports the
146  // location where the template is defined, not where it
147  // is instantiated.
148  if (Parent.get<SubstNonTypeTemplateParmExpr>())
149  return true;
150 
151  // Don't warn on string user defined literals:
152  // std::string s = "Hello World"s;
153  if (const auto *UDL = Parent.get<UserDefinedLiteral>())
154  if (UDL->getLiteralOperatorKind() == UserDefinedLiteral::LOK_String)
155  return true;
156 
157  return false;
158  });
159 }
160 
161 bool MagicNumbersCheck::isIgnoredValue(const IntegerLiteral *Literal) const {
162  const llvm::APInt IntValue = Literal->getValue();
163  const int64_t Value = IntValue.getZExtValue();
164  if (Value == 0)
165  return true;
166 
167  if (IgnorePowersOf2IntegerValues && IntValue.isPowerOf2())
168  return true;
169 
170  return std::binary_search(IgnoredIntegerValues.begin(),
171  IgnoredIntegerValues.end(), Value);
172 }
173 
174 bool MagicNumbersCheck::isIgnoredValue(const FloatingLiteral *Literal) const {
175  const llvm::APFloat FloatValue = Literal->getValue();
176  if (FloatValue.isZero())
177  return true;
178 
179  if (&FloatValue.getSemantics() == &llvm::APFloat::IEEEsingle()) {
180  const float Value = FloatValue.convertToFloat();
181  return std::binary_search(IgnoredFloatingPointValues.begin(),
182  IgnoredFloatingPointValues.end(), Value);
183  }
184 
185  if (&FloatValue.getSemantics() == &llvm::APFloat::IEEEdouble()) {
186  const double Value = FloatValue.convertToDouble();
187  return std::binary_search(IgnoredDoublePointValues.begin(),
188  IgnoredDoublePointValues.end(), Value);
189  }
190 
191  return false;
192 }
193 
194 bool MagicNumbersCheck::isSyntheticValue(const SourceManager *SourceManager,
195  const IntegerLiteral *Literal) const {
196  const std::pair<FileID, unsigned> FileOffset =
197  SourceManager->getDecomposedLoc(Literal->getLocation());
198  if (FileOffset.first.isInvalid())
199  return false;
200 
201  const StringRef BufferIdentifier =
202  SourceManager->getBuffer(FileOffset.first)->getBufferIdentifier();
203 
204  return BufferIdentifier.empty();
205 }
206 
207 bool MagicNumbersCheck::isBitFieldWidth(
208  const clang::ast_matchers::MatchFinder::MatchResult &Result,
209  const IntegerLiteral &Literal) const {
210  return IgnoreBitFieldsWidths &&
211  llvm::any_of(Result.Context->getParents(Literal),
212  [&Result](const DynTypedNode &Parent) {
213  return isUsedToDefineABitField(Result, Parent);
214  });
215 }
216 
217 } // namespace readability
218 } // namespace tidy
219 } // namespace clang
static bool isUsedToDefineABitField(const MatchFinder::MatchResult &Result, const DynTypedNode &Node)
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
const Node * Parent
Base class for all clang-tidy checks.
std::vector< std::string > parseStringList(StringRef Option)
Parse a semicolon separated list of strings.
const char DefaultIgnoredIntegerValues[]
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
static bool isUsedToInitializeAConstant(const MatchFinder::MatchResult &Result, const DynTypedNode &Node)
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
const char DefaultIgnoredFloatingPointValues[]
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
std::string get(StringRef LocalName, StringRef Default) const
Read a named option from the Context.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...