16 #include "../utils/OptionsUtils.h" 17 #include "clang/AST/ASTContext.h" 18 #include "clang/ASTMatchers/ASTMatchFinder.h" 19 #include "llvm/ADT/STLExtras.h" 27 bool isUsedToInitializeAConstant(
const MatchFinder::MatchResult &
Result,
28 const DynTypedNode &Node) {
30 const auto *AsDecl = Node.get<clang::DeclaratorDecl>();
32 if (AsDecl->getType().isConstQualified())
35 return AsDecl->isImplicit();
38 if (Node.get<clang::EnumConstantDecl>() !=
nullptr)
41 return llvm::any_of(Result.Context->getParents(Node),
42 [&
Result](
const DynTypedNode &Parent) {
43 return isUsedToInitializeAConstant(Result, Parent);
51 namespace readability {
58 IgnoreAllFloatingPointValues(
59 Options.get(
"IgnoreAllFloatingPointValues", false)),
60 IgnorePowersOf2IntegerValues(
61 Options.get(
"IgnorePowersOf2IntegerValues", false)) {
63 const std::vector<std::string> IgnoredIntegerValuesInput =
65 Options.
get(
"IgnoredIntegerValues", DefaultIgnoredIntegerValues));
66 IgnoredIntegerValues.resize(IgnoredIntegerValuesInput.size());
67 llvm::transform(IgnoredIntegerValuesInput, IgnoredIntegerValues.begin(),
68 [](
const std::string &Value) {
return std::stoll(Value); });
69 llvm::sort(IgnoredIntegerValues);
71 if (!IgnoreAllFloatingPointValues) {
73 const std::vector<std::string> IgnoredFloatingPointValuesInput =
75 "IgnoredFloatingPointValues", DefaultIgnoredFloatingPointValues));
76 IgnoredFloatingPointValues.reserve(IgnoredFloatingPointValuesInput.size());
77 IgnoredDoublePointValues.reserve(IgnoredFloatingPointValuesInput.size());
78 for (
const auto &InputValue : IgnoredFloatingPointValuesInput) {
79 llvm::APFloat FloatValue(llvm::APFloat::IEEEsingle());
80 FloatValue.convertFromString(InputValue, DefaultRoundingMode);
81 IgnoredFloatingPointValues.push_back(FloatValue.convertToFloat());
83 llvm::APFloat DoubleValue(llvm::APFloat::IEEEdouble());
84 DoubleValue.convertFromString(InputValue, DefaultRoundingMode);
85 IgnoredDoublePointValues.push_back(DoubleValue.convertToDouble());
87 llvm::sort(IgnoredFloatingPointValues.begin(),
88 IgnoredFloatingPointValues.end());
89 llvm::sort(IgnoredDoublePointValues.begin(),
90 IgnoredDoublePointValues.end());
95 Options.
store(Opts,
"IgnoredIntegerValues", DefaultIgnoredIntegerValues);
97 DefaultIgnoredFloatingPointValues);
101 Finder->addMatcher(integerLiteral().bind(
"integer"),
this);
102 if (!IgnoreAllFloatingPointValues)
103 Finder->addMatcher(floatLiteral().bind(
"float"),
this);
107 checkBoundMatch<IntegerLiteral>(
Result,
"integer");
108 checkBoundMatch<FloatingLiteral>(
Result,
"float");
111 bool MagicNumbersCheck::isConstant(
const MatchFinder::MatchResult &Result,
112 const Expr &ExprResult)
const {
114 Result.Context->getParents(ExprResult),
115 [&
Result](
const DynTypedNode &Parent) {
116 return isUsedToInitializeAConstant(Result, Parent) ||
119 Parent.get<SubstNonTypeTemplateParmExpr>();
123 bool MagicNumbersCheck::isIgnoredValue(
const IntegerLiteral *Literal)
const {
124 const llvm::APInt IntValue = Literal->getValue();
125 const int64_t Value = IntValue.getZExtValue();
129 if (IgnorePowersOf2IntegerValues && IntValue.isPowerOf2())
132 return std::binary_search(IgnoredIntegerValues.begin(),
133 IgnoredIntegerValues.end(), Value);
136 bool MagicNumbersCheck::isIgnoredValue(
const FloatingLiteral *Literal)
const {
137 const llvm::APFloat FloatValue = Literal->getValue();
138 if (FloatValue.isZero())
141 if (&FloatValue.getSemantics() == &llvm::APFloat::IEEEsingle()) {
142 const float Value = FloatValue.convertToFloat();
143 return std::binary_search(IgnoredFloatingPointValues.begin(),
144 IgnoredFloatingPointValues.end(), Value);
147 if (&FloatValue.getSemantics() == &llvm::APFloat::IEEEdouble()) {
148 const double Value = FloatValue.convertToDouble();
149 return std::binary_search(IgnoredDoublePointValues.begin(),
150 IgnoredDoublePointValues.end(), Value);
156 bool MagicNumbersCheck::isSyntheticValue(
const SourceManager *SourceManager,
157 const IntegerLiteral *Literal)
const {
158 const std::pair<FileID, unsigned> FileOffset =
159 SourceManager->getDecomposedLoc(Literal->getLocation());
160 if (FileOffset.first.isInvalid())
163 const StringRef BufferIdentifier =
164 SourceManager->getBuffer(FileOffset.first)->getBufferIdentifier();
166 return BufferIdentifier.empty();
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.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
std::string get(StringRef LocalName, StringRef Default) const
Read a named option from the Context.
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.
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
const char DefaultIgnoredFloatingPointValues[]
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...