15 #include "../utils/OptionsUtils.h" 16 #include "clang/AST/ASTContext.h" 17 #include "clang/ASTMatchers/ASTMatchFinder.h" 18 #include "llvm/ADT/STLExtras.h" 27 const DynTypedNode &Node) {
29 const auto *AsDecl = Node.get<DeclaratorDecl>();
31 if (AsDecl->getType().isConstQualified())
34 return AsDecl->isImplicit();
37 if (Node.get<EnumConstantDecl>())
40 return llvm::any_of(Result.Context->getParents(Node),
41 [&Result](
const DynTypedNode &
Parent) {
47 const DynTypedNode &Node) {
48 const auto *AsFieldDecl = Node.get<FieldDecl>();
49 if (AsFieldDecl && AsFieldDecl->isBitField())
52 return llvm::any_of(Result.Context->getParents(Node),
53 [&Result](
const DynTypedNode &
Parent) {
59 namespace readability {
66 IgnoreAllFloatingPointValues(
67 Options.get(
"IgnoreAllFloatingPointValues", false)),
68 IgnoreBitFieldsWidths(Options.get(
"IgnoreBitFieldsWidths", true)),
69 IgnorePowersOf2IntegerValues(
70 Options.get(
"IgnorePowersOf2IntegerValues", false)) {
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);
80 if (!IgnoreAllFloatingPointValues) {
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());
90 FloatValue.convertFromString(InputValue, DefaultRoundingMode);
91 assert(StatusOrErr &&
"Invalid floating point representation");
92 consumeError(StatusOrErr.takeError());
93 IgnoredFloatingPointValues.push_back(FloatValue.convertToFloat());
95 llvm::APFloat DoubleValue(llvm::APFloat::IEEEdouble());
97 DoubleValue.convertFromString(InputValue, DefaultRoundingMode);
98 assert(StatusOrErr &&
"Invalid floating point representation");
99 consumeError(StatusOrErr.takeError());
100 IgnoredDoublePointValues.push_back(DoubleValue.convertToDouble());
102 llvm::sort(IgnoredFloatingPointValues.begin(),
103 IgnoredFloatingPointValues.end());
104 llvm::sort(IgnoredDoublePointValues.begin(),
105 IgnoredDoublePointValues.end());
110 Options.
store(Opts,
"IgnoredIntegerValues", DefaultIgnoredIntegerValues);
112 DefaultIgnoredFloatingPointValues);
116 Finder->addMatcher(integerLiteral().bind(
"integer"),
this);
117 if (!IgnoreAllFloatingPointValues)
118 Finder->addMatcher(floatLiteral().bind(
"float"),
this);
122 checkBoundMatch<IntegerLiteral>(Result,
"integer");
123 checkBoundMatch<FloatingLiteral>(Result,
"float");
126 bool MagicNumbersCheck::isConstant(
const MatchFinder::MatchResult &Result,
127 const Expr &ExprResult)
const {
129 Result.Context->getParents(ExprResult),
130 [&Result](
const DynTypedNode &
Parent) {
136 if (
Parent.get<CStyleCastExpr>() &&
138 Result.Context->getParents(
Parent),
139 [](
const DynTypedNode &GrandParent) {
140 return GrandParent.get<SubstNonTypeTemplateParmExpr>() !=
148 if (
Parent.get<SubstNonTypeTemplateParmExpr>())
153 if (
const auto *UDL =
Parent.get<UserDefinedLiteral>())
154 if (UDL->getLiteralOperatorKind() == UserDefinedLiteral::LOK_String)
161 bool MagicNumbersCheck::isIgnoredValue(
const IntegerLiteral *Literal)
const {
162 const llvm::APInt IntValue = Literal->getValue();
163 const int64_t Value = IntValue.getZExtValue();
167 if (IgnorePowersOf2IntegerValues && IntValue.isPowerOf2())
170 return std::binary_search(IgnoredIntegerValues.begin(),
171 IgnoredIntegerValues.end(), Value);
174 bool MagicNumbersCheck::isIgnoredValue(
const FloatingLiteral *Literal)
const {
175 const llvm::APFloat FloatValue = Literal->getValue();
176 if (FloatValue.isZero())
179 if (&FloatValue.getSemantics() == &llvm::APFloat::IEEEsingle()) {
180 const float Value = FloatValue.convertToFloat();
181 return std::binary_search(IgnoredFloatingPointValues.begin(),
182 IgnoredFloatingPointValues.end(), Value);
185 if (&FloatValue.getSemantics() == &llvm::APFloat::IEEEdouble()) {
186 const double Value = FloatValue.convertToDouble();
187 return std::binary_search(IgnoredDoublePointValues.begin(),
188 IgnoredDoublePointValues.end(), Value);
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())
201 const StringRef BufferIdentifier =
202 SourceManager->getBuffer(FileOffset.first)->getBufferIdentifier();
204 return BufferIdentifier.empty();
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) {
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.
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...