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) {
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) {
42 return isUsedToInitializeAConstant(Result, Parent);
47 const DynTypedNode &Node) {
49 if (Node.get<TypeAliasDecl>() || Node.get<TypedefNameDecl>())
52 return llvm::any_of(Result.Context->getParents(Node),
53 [&Result](
const DynTypedNode &Parent) {
54 return isUsedToDefineATypeAlias(Result, Parent);
59 const DynTypedNode &Node) {
60 const auto *AsFieldDecl = Node.get<FieldDecl>();
61 if (AsFieldDecl && AsFieldDecl->isBitField())
64 return llvm::any_of(Result.Context->getParents(Node),
65 [&Result](
const DynTypedNode &Parent) {
66 return isUsedToDefineABitField(Result, Parent);
77 IgnoreAllFloatingPointValues(
78 Options.get(
"IgnoreAllFloatingPointValues", false)),
79 IgnoreBitFieldsWidths(Options.get(
"IgnoreBitFieldsWidths", true)),
80 IgnorePowersOf2IntegerValues(
81 Options.get(
"IgnorePowersOf2IntegerValues", false)),
82 IgnoreTypeAliases(Options.get(
"IgnoreTypeAliases", false)),
83 IgnoreUserDefinedLiterals(
84 Options.get(
"IgnoreUserDefinedLiterals", false)),
85 RawIgnoredIntegerValues(
87 RawIgnoredFloatingPointValues(Options.get(
90 const std::vector<StringRef> IgnoredIntegerValuesInput =
92 IgnoredIntegerValues.resize(IgnoredIntegerValuesInput.size());
93 llvm::transform(IgnoredIntegerValuesInput, IgnoredIntegerValues.begin(),
96 Value.getAsInteger(10, Res);
99 llvm::sort(IgnoredIntegerValues);
101 if (!IgnoreAllFloatingPointValues) {
103 const std::vector<StringRef> IgnoredFloatingPointValuesInput =
105 IgnoredFloatingPointValues.reserve(IgnoredFloatingPointValuesInput.size());
106 IgnoredDoublePointValues.reserve(IgnoredFloatingPointValuesInput.size());
107 for (
const auto &InputValue : IgnoredFloatingPointValuesInput) {
108 llvm::APFloat FloatValue(llvm::APFloat::IEEEsingle());
110 FloatValue.convertFromString(InputValue, DefaultRoundingMode);
111 assert(StatusOrErr &&
"Invalid floating point representation");
112 consumeError(StatusOrErr.takeError());
113 IgnoredFloatingPointValues.push_back(FloatValue.convertToFloat());
115 llvm::APFloat DoubleValue(llvm::APFloat::IEEEdouble());
117 DoubleValue.convertFromString(InputValue, DefaultRoundingMode);
118 assert(StatusOrErr &&
"Invalid floating point representation");
119 consumeError(StatusOrErr.takeError());
120 IgnoredDoublePointValues.push_back(DoubleValue.convertToDouble());
122 llvm::sort(IgnoredFloatingPointValues);
123 llvm::sort(IgnoredDoublePointValues);
128 Options.store(Opts,
"IgnoreAllFloatingPointValues",
129 IgnoreAllFloatingPointValues);
130 Options.store(Opts,
"IgnoreBitFieldsWidths", IgnoreBitFieldsWidths);
131 Options.store(Opts,
"IgnorePowersOf2IntegerValues",
132 IgnorePowersOf2IntegerValues);
133 Options.store(Opts,
"IgnoreTypeAliases", IgnoreTypeAliases);
134 Options.store(Opts,
"IgnoreUserDefinedLiterals", IgnoreUserDefinedLiterals);
135 Options.store(Opts,
"IgnoredIntegerValues", RawIgnoredIntegerValues);
136 Options.store(Opts,
"IgnoredFloatingPointValues",
137 RawIgnoredFloatingPointValues);
141 Finder->addMatcher(integerLiteral().bind(
"integer"),
this);
142 if (!IgnoreAllFloatingPointValues)
143 Finder->addMatcher(floatLiteral().bind(
"float"),
this);
148 TraversalKindScope RAII(*Result.Context, TK_AsIs);
150 checkBoundMatch<IntegerLiteral>(Result,
"integer");
151 checkBoundMatch<FloatingLiteral>(Result,
"float");
154bool MagicNumbersCheck::isConstant(
const MatchFinder::MatchResult &Result,
155 const Expr &ExprResult)
const {
157 Result.Context->getParents(ExprResult),
158 [
this, &Result](
const DynTypedNode &Parent) {
159 if (isUsedToInitializeAConstant(Result, Parent))
162 if (IgnoreTypeAliases && isUsedToDefineATypeAlias(Result, Parent))
167 if (Parent.get<CStyleCastExpr>() &&
169 Result.Context->getParents(Parent),
170 [](const DynTypedNode &GrandParent) {
171 return GrandParent.get<SubstNonTypeTemplateParmExpr>() !=
179 if (Parent.get<SubstNonTypeTemplateParmExpr>())
184 if (
const auto *UDL = Parent.get<UserDefinedLiteral>())
185 if (UDL->getLiteralOperatorKind() == UserDefinedLiteral::LOK_String)
192bool MagicNumbersCheck::isIgnoredValue(
const IntegerLiteral *Literal)
const {
193 if (Literal->getType()->isBitIntType()) {
196 const llvm::APInt IntValue = Literal->getValue();
197 const int64_t Value = IntValue.getZExtValue();
201 if (IgnorePowersOf2IntegerValues && IntValue.isPowerOf2())
204 return llvm::binary_search(IgnoredIntegerValues, Value);
207bool MagicNumbersCheck::isIgnoredValue(
const FloatingLiteral *Literal)
const {
208 const llvm::APFloat FloatValue = Literal->getValue();
209 if (FloatValue.isZero())
212 if (&FloatValue.getSemantics() == &llvm::APFloat::IEEEsingle()) {
213 const float Value = FloatValue.convertToFloat();
214 return llvm::binary_search(IgnoredFloatingPointValues, Value);
217 if (&FloatValue.getSemantics() == &llvm::APFloat::IEEEdouble()) {
218 const double Value = FloatValue.convertToDouble();
219 return llvm::binary_search(IgnoredDoublePointValues, Value);
225bool MagicNumbersCheck::isSyntheticValue(
const SourceManager *SourceManager,
226 const IntegerLiteral *Literal)
const {
227 const std::pair<FileID, unsigned> FileOffset =
228 SourceManager->getDecomposedLoc(Literal->getLocation());
229 if (FileOffset.first.isInvalid())
232 const StringRef BufferIdentifier =
233 SourceManager->getBufferOrFake(FileOffset.first).getBufferIdentifier();
235 return BufferIdentifier.empty();
238bool MagicNumbersCheck::isBitFieldWidth(
239 const clang::ast_matchers::MatchFinder::MatchResult &Result,
240 const IntegerLiteral &Literal)
const {
241 return IgnoreBitFieldsWidths &&
242 llvm::any_of(Result.Context->getParents(Literal),
243 [&Result](
const DynTypedNode &Parent) {
244 return isUsedToDefineABitField(Result, Parent);
248bool MagicNumbersCheck::isUserDefinedLiteral(
249 const clang::ast_matchers::MatchFinder::MatchResult &Result,
250 const clang::Expr &Literal)
const {
251 DynTypedNodeList
Parents = Result.Context->getParents(Literal);
254 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