15 #include "../utils/OptionsUtils.h" 16 #include "clang/AST/ASTContext.h" 17 #include "clang/ASTMatchers/ASTMatchFinder.h" 18 #include "llvm/ADT/STLExtras.h" 26 bool isUsedToInitializeAConstant(
const MatchFinder::MatchResult &
Result,
27 const DynTypedNode &Node) {
29 const auto *AsDecl = Node.get<clang::DeclaratorDecl>();
31 if (AsDecl->getType().isConstQualified())
34 return AsDecl->isImplicit();
37 if (Node.get<clang::EnumConstantDecl>() !=
nullptr)
40 return llvm::any_of(Result.Context->getParents(Node),
41 [&
Result](
const DynTypedNode &Parent) {
42 return isUsedToInitializeAConstant(Result, Parent);
50 namespace readability {
57 IgnoreAllFloatingPointValues(
58 Options.get(
"IgnoreAllFloatingPointValues", false)),
59 IgnorePowersOf2IntegerValues(
60 Options.get(
"IgnorePowersOf2IntegerValues", false)) {
62 const std::vector<std::string> IgnoredIntegerValuesInput =
64 Options.
get(
"IgnoredIntegerValues", DefaultIgnoredIntegerValues));
65 IgnoredIntegerValues.resize(IgnoredIntegerValuesInput.size());
66 llvm::transform(IgnoredIntegerValuesInput, IgnoredIntegerValues.begin(),
67 [](
const std::string &Value) {
return std::stoll(Value); });
68 llvm::sort(IgnoredIntegerValues);
70 if (!IgnoreAllFloatingPointValues) {
72 const std::vector<std::string> IgnoredFloatingPointValuesInput =
74 "IgnoredFloatingPointValues", DefaultIgnoredFloatingPointValues));
75 IgnoredFloatingPointValues.reserve(IgnoredFloatingPointValuesInput.size());
76 IgnoredDoublePointValues.reserve(IgnoredFloatingPointValuesInput.size());
77 for (
const auto &InputValue : IgnoredFloatingPointValuesInput) {
78 llvm::APFloat FloatValue(llvm::APFloat::IEEEsingle());
79 FloatValue.convertFromString(InputValue, DefaultRoundingMode);
80 IgnoredFloatingPointValues.push_back(FloatValue.convertToFloat());
82 llvm::APFloat DoubleValue(llvm::APFloat::IEEEdouble());
83 DoubleValue.convertFromString(InputValue, DefaultRoundingMode);
84 IgnoredDoublePointValues.push_back(DoubleValue.convertToDouble());
86 llvm::sort(IgnoredFloatingPointValues.begin(),
87 IgnoredFloatingPointValues.end());
88 llvm::sort(IgnoredDoublePointValues.begin(),
89 IgnoredDoublePointValues.end());
94 Options.
store(Opts,
"IgnoredIntegerValues", DefaultIgnoredIntegerValues);
96 DefaultIgnoredFloatingPointValues);
100 Finder->addMatcher(integerLiteral().bind(
"integer"),
this);
101 if (!IgnoreAllFloatingPointValues)
102 Finder->addMatcher(floatLiteral().bind(
"float"),
this);
106 checkBoundMatch<IntegerLiteral>(
Result,
"integer");
107 checkBoundMatch<FloatingLiteral>(
Result,
"float");
110 bool MagicNumbersCheck::isConstant(
const MatchFinder::MatchResult &Result,
111 const Expr &ExprResult)
const {
113 Result.Context->getParents(ExprResult),
114 [&
Result](
const DynTypedNode &Parent) {
115 return isUsedToInitializeAConstant(Result, Parent) ||
118 Parent.get<SubstNonTypeTemplateParmExpr>();
122 bool MagicNumbersCheck::isIgnoredValue(
const IntegerLiteral *Literal)
const {
123 const llvm::APInt IntValue = Literal->getValue();
124 const int64_t Value = IntValue.getZExtValue();
128 if (IgnorePowersOf2IntegerValues && IntValue.isPowerOf2())
131 return std::binary_search(IgnoredIntegerValues.begin(),
132 IgnoredIntegerValues.end(), Value);
135 bool MagicNumbersCheck::isIgnoredValue(
const FloatingLiteral *Literal)
const {
136 const llvm::APFloat FloatValue = Literal->getValue();
137 if (FloatValue.isZero())
140 if (&FloatValue.getSemantics() == &llvm::APFloat::IEEEsingle()) {
141 const float Value = FloatValue.convertToFloat();
142 return std::binary_search(IgnoredFloatingPointValues.begin(),
143 IgnoredFloatingPointValues.end(), Value);
146 if (&FloatValue.getSemantics() == &llvm::APFloat::IEEEdouble()) {
147 const double Value = FloatValue.convertToDouble();
148 return std::binary_search(IgnoredDoublePointValues.begin(),
149 IgnoredDoublePointValues.end(), Value);
155 bool MagicNumbersCheck::isSyntheticValue(
const SourceManager *SourceManager,
156 const IntegerLiteral *Literal)
const {
157 const std::pair<FileID, unsigned> FileOffset =
158 SourceManager->getDecomposedLoc(Literal->getLocation());
159 if (FileOffset.first.isInvalid())
162 const StringRef BufferIdentifier =
163 SourceManager->getBuffer(FileOffset.first)->getBufferIdentifier();
165 return BufferIdentifier.empty();
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.
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 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.
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
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...