16#include "clang/AST/ASTContext.h"
17#include "clang/AST/ASTTypeTraits.h"
18#include "clang/AST/Type.h"
19#include "clang/ASTMatchers/ASTMatchFinder.h"
20#include "llvm/ADT/STLExtras.h"
27 const DynTypedNode &Node) {
28 const auto *AsDecl = Node.get<DeclaratorDecl>();
30 if (AsDecl->getType().isConstQualified())
33 return AsDecl->isImplicit();
36 if (Node.get<EnumConstantDecl>())
39 return llvm::any_of(Result.Context->getParents(Node),
40 [&Result](
const DynTypedNode &Parent) {
41 return isUsedToInitializeAConstant(Result, Parent);
46 const DynTypedNode &Node) {
47 if (Node.get<TypeAliasDecl>() || Node.get<TypedefNameDecl>())
50 return llvm::any_of(Result.Context->getParents(Node),
51 [&Result](
const DynTypedNode &Parent) {
52 return isUsedToDefineATypeAlias(Result, Parent);
57 const DynTypedNode &Node) {
58 const auto *AsFieldDecl = Node.get<FieldDecl>();
59 if (AsFieldDecl && AsFieldDecl->isBitField())
62 return llvm::any_of(Result.Context->getParents(Node),
63 [&Result](
const DynTypedNode &Parent) {
64 return isUsedToDefineABitField(Result, Parent);
75 IgnoreAllFloatingPointValues(
76 Options.get(
"IgnoreAllFloatingPointValues", false)),
77 IgnoreBitFieldsWidths(Options.get(
"IgnoreBitFieldsWidths", true)),
78 IgnorePowersOf2IntegerValues(
79 Options.get(
"IgnorePowersOf2IntegerValues", false)),
80 IgnoreTypeAliases(Options.get(
"IgnoreTypeAliases", false)),
81 IgnoreUserDefinedLiterals(
82 Options.get(
"IgnoreUserDefinedLiterals", false)),
83 RawIgnoredIntegerValues(
85 RawIgnoredFloatingPointValues(Options.get(
88 const std::vector<StringRef> IgnoredIntegerValuesInput =
90 IgnoredIntegerValues.resize(IgnoredIntegerValuesInput.size());
91 llvm::transform(IgnoredIntegerValuesInput, IgnoredIntegerValues.begin(),
94 Value.getAsInteger(10, Res);
97 llvm::sort(IgnoredIntegerValues);
99 if (!IgnoreAllFloatingPointValues) {
101 const std::vector<StringRef> IgnoredFloatingPointValuesInput =
103 IgnoredFloatingPointValues.reserve(IgnoredFloatingPointValuesInput.size());
104 IgnoredDoublePointValues.reserve(IgnoredFloatingPointValuesInput.size());
105 for (
const auto &InputValue : IgnoredFloatingPointValuesInput) {
106 llvm::APFloat FloatValue(llvm::APFloat::IEEEsingle());
108 FloatValue.convertFromString(InputValue, DefaultRoundingMode);
109 assert(StatusOrErr &&
"Invalid floating point representation");
110 consumeError(StatusOrErr.takeError());
111 IgnoredFloatingPointValues.push_back(FloatValue.convertToFloat());
113 llvm::APFloat DoubleValue(llvm::APFloat::IEEEdouble());
115 DoubleValue.convertFromString(InputValue, DefaultRoundingMode);
116 assert(StatusOrErr &&
"Invalid floating point representation");
117 consumeError(StatusOrErr.takeError());
118 IgnoredDoublePointValues.push_back(DoubleValue.convertToDouble());
120 llvm::sort(IgnoredFloatingPointValues);
121 llvm::sort(IgnoredDoublePointValues);
126 Options.store(Opts,
"IgnoreAllFloatingPointValues",
127 IgnoreAllFloatingPointValues);
128 Options.store(Opts,
"IgnoreBitFieldsWidths", IgnoreBitFieldsWidths);
129 Options.store(Opts,
"IgnorePowersOf2IntegerValues",
130 IgnorePowersOf2IntegerValues);
131 Options.store(Opts,
"IgnoreTypeAliases", IgnoreTypeAliases);
132 Options.store(Opts,
"IgnoreUserDefinedLiterals", IgnoreUserDefinedLiterals);
133 Options.store(Opts,
"IgnoredIntegerValues", RawIgnoredIntegerValues);
134 Options.store(Opts,
"IgnoredFloatingPointValues",
135 RawIgnoredFloatingPointValues);
139 Finder->addMatcher(integerLiteral().bind(
"integer"),
this);
140 if (!IgnoreAllFloatingPointValues)
141 Finder->addMatcher(floatLiteral().bind(
"float"),
this);
145 const TraversalKindScope RAII(*Result.Context, TK_AsIs);
147 checkBoundMatch<IntegerLiteral>(Result,
"integer");
148 checkBoundMatch<FloatingLiteral>(Result,
"float");
151bool MagicNumbersCheck::isConstant(
const MatchFinder::MatchResult &Result,
152 const Expr &ExprResult)
const {
154 Result.Context->getParents(ExprResult),
155 [
this, &Result](
const DynTypedNode &Parent) {
156 if (isUsedToInitializeAConstant(Result, Parent))
159 if (IgnoreTypeAliases && isUsedToDefineATypeAlias(Result, Parent))
164 if (Parent.get<CStyleCastExpr>() &&
166 Result.Context->getParents(Parent),
167 [](const DynTypedNode &GrandParent) {
168 return GrandParent.get<SubstNonTypeTemplateParmExpr>() !=
176 if (Parent.get<SubstNonTypeTemplateParmExpr>())
181 if (
const auto *UDL = Parent.get<UserDefinedLiteral>())
182 if (UDL->getLiteralOperatorKind() == UserDefinedLiteral::LOK_String)
189bool MagicNumbersCheck::isIgnoredValue(
const IntegerLiteral *Literal)
const {
190 if (Literal->getType()->isBitIntType()) {
193 const llvm::APInt IntValue = Literal->getValue();
194 const int64_t Value = IntValue.getZExtValue();
198 if (IgnorePowersOf2IntegerValues && IntValue.isPowerOf2())
201 return llvm::binary_search(IgnoredIntegerValues, Value);
204bool MagicNumbersCheck::isIgnoredValue(
const FloatingLiteral *Literal)
const {
205 const llvm::APFloat FloatValue = Literal->getValue();
206 if (FloatValue.isZero())
209 if (&FloatValue.getSemantics() == &llvm::APFloat::IEEEsingle()) {
210 const float Value = FloatValue.convertToFloat();
211 return llvm::binary_search(IgnoredFloatingPointValues, Value);
214 if (&FloatValue.getSemantics() == &llvm::APFloat::IEEEdouble()) {
215 const double Value = FloatValue.convertToDouble();
216 return llvm::binary_search(IgnoredDoublePointValues, Value);
222bool MagicNumbersCheck::isSyntheticValue(
const SourceManager *SourceManager,
223 const IntegerLiteral *Literal)
const {
224 const std::pair<FileID, unsigned> FileOffset =
225 SourceManager->getDecomposedLoc(Literal->getLocation());
226 if (FileOffset.first.isInvalid())
229 const StringRef BufferIdentifier =
230 SourceManager->getBufferOrFake(FileOffset.first).getBufferIdentifier();
232 return BufferIdentifier.empty();
235bool MagicNumbersCheck::isBitFieldWidth(
236 const clang::ast_matchers::MatchFinder::MatchResult &Result,
237 const IntegerLiteral &Literal)
const {
238 return IgnoreBitFieldsWidths &&
239 llvm::any_of(Result.Context->getParents(Literal),
240 [&Result](
const DynTypedNode &Parent) {
241 return isUsedToDefineABitField(Result, Parent);
245bool MagicNumbersCheck::isUserDefinedLiteral(
246 const clang::ast_matchers::MatchFinder::MatchResult &Result,
247 const clang::Expr &Literal)
const {
248 const DynTypedNodeList
Parents = Result.Context->getParents(Literal);
251 return Parents[0].get<UserDefinedLiteral>() !=
nullptr;
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
MagicNumbersCheck(StringRef Name, ClangTidyContext *Context)
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
const char DefaultIgnoredIntegerValues[]
const char DefaultIgnoredFloatingPointValues[]
std::vector< StringRef > parseStringList(StringRef Option)
Parse a semicolon separated list of strings.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
static bool isUsedToDefineATypeAlias(const MatchFinder::MatchResult &Result, const DynTypedNode &Node)
static bool isUsedToDefineABitField(const MatchFinder::MatchResult &Result, const DynTypedNode &Node)
static bool isUsedToInitializeAConstant(const MatchFinder::MatchResult &Result, const DynTypedNode &Node)
llvm::StringMap< ClangTidyValue > OptionMap