11 #include "../utils/Matchers.h" 12 #include "clang/AST/ASTContext.h" 13 #include "clang/ASTMatchers/ASTMatchFinder.h" 24 return Node.getValue().getZExtValue() > N;
27 AST_MATCHER_P2(Expr, hasSizeOfDescendant,
int, Depth,
28 ast_matchers::internal::Matcher<Expr>, InnerMatcher) {
32 const Expr *E = Node.IgnoreParenImpCasts();
33 if (InnerMatcher.matches(*E, Finder, Builder))
36 if (
const auto *CE = dyn_cast<CastExpr>(E)) {
37 const auto M = hasSizeOfDescendant(Depth - 1, InnerMatcher);
38 return M.matches(*CE->getSubExpr(), Finder, Builder);
39 }
else if (
const auto *UE = dyn_cast<UnaryOperator>(E)) {
40 const auto M = hasSizeOfDescendant(Depth - 1, InnerMatcher);
41 return M.matches(*UE->getSubExpr(), Finder, Builder);
42 }
else if (
const auto *BE = dyn_cast<BinaryOperator>(E)) {
43 const auto LHS = hasSizeOfDescendant(Depth - 1, InnerMatcher);
44 const auto RHS = hasSizeOfDescendant(Depth - 1, InnerMatcher);
45 return LHS.matches(*BE->getLHS(), Finder, Builder) ||
46 RHS.matches(*BE->getRHS(), Finder, Builder);
52 CharUnits getSizeOfType(
const ASTContext &
Ctx,
const Type *Ty) {
53 if (!Ty || Ty->isIncompleteType() || Ty->isDependentType() ||
54 isa<DependentSizedArrayType>(Ty) || !Ty->isConstantSizeType())
55 return CharUnits::Zero();
56 return Ctx.getTypeSizeInChars(Ty);
61 SizeofExpressionCheck::SizeofExpressionCheck(StringRef
Name,
64 WarnOnSizeOfConstant(Options.get(
"WarnOnSizeOfConstant", 1) != 0),
65 WarnOnSizeOfIntegerExpression(
66 Options.get(
"WarnOnSizeOfIntegerExpression", 0) != 0),
67 WarnOnSizeOfThis(Options.get(
"WarnOnSizeOfThis", 1) != 0),
68 WarnOnSizeOfCompareToConstant(
69 Options.get(
"WarnOnSizeOfCompareToConstant", 1) != 0) {}
72 Options.
store(Opts,
"WarnOnSizeOfConstant", WarnOnSizeOfConstant);
74 WarnOnSizeOfIntegerExpression);
75 Options.
store(Opts,
"WarnOnSizeOfThis", WarnOnSizeOfThis);
77 WarnOnSizeOfCompareToConstant);
81 const auto IntegerExpr = ignoringParenImpCasts(integerLiteral());
82 const auto ConstantExpr = expr(ignoringParenImpCasts(
83 anyOf(integerLiteral(), unaryOperator(hasUnaryOperand(IntegerExpr)),
84 binaryOperator(hasLHS(IntegerExpr), hasRHS(IntegerExpr)))));
85 const auto IntegerCallExpr = expr(ignoringParenImpCasts(
86 callExpr(anyOf(hasType(isInteger()), hasType(enumType())),
87 unless(isInTemplateInstantiation()))));
88 const auto SizeOfExpr =
89 expr(anyOf(sizeOfExpr(has(type())), sizeOfExpr(has(expr()))));
90 const auto SizeOfZero = expr(
91 sizeOfExpr(has(ignoringParenImpCasts(expr(integerLiteral(equals(0)))))));
96 if (WarnOnSizeOfConstant) {
98 expr(sizeOfExpr(has(ignoringParenImpCasts(ConstantExpr))),
100 .bind(
"sizeof-constant"),
105 if (WarnOnSizeOfIntegerExpression) {
107 expr(sizeOfExpr(ignoringParenImpCasts(has(IntegerCallExpr))))
108 .bind(
"sizeof-integer-call"),
113 if (WarnOnSizeOfThis) {
115 expr(sizeOfExpr(has(ignoringParenImpCasts(expr(cxxThisExpr())))))
116 .bind(
"sizeof-this"),
121 const auto CharPtrType = pointerType(pointee(isAnyCharacter()));
122 const auto ConstStrLiteralDecl =
123 varDecl(isDefinition(), hasType(qualType(hasCanonicalType(CharPtrType))),
124 hasInitializer(ignoringParenImpCasts(stringLiteral())));
125 Finder->addMatcher(expr(sizeOfExpr(has(ignoringParenImpCasts(expr(
126 hasType(qualType(hasCanonicalType(CharPtrType))),
127 ignoringParenImpCasts(declRefExpr(
128 hasDeclaration(ConstStrLiteralDecl))))))))
129 .bind(
"sizeof-charp"),
133 const auto ArrayExpr = expr(ignoringParenImpCasts(
134 expr(hasType(qualType(hasCanonicalType(arrayType()))))));
135 const auto ArrayCastExpr = expr(anyOf(
136 unaryOperator(hasUnaryOperand(ArrayExpr), unless(hasOperatorName(
"*"))),
137 binaryOperator(hasEitherOperand(ArrayExpr)),
138 castExpr(hasSourceExpression(ArrayExpr))));
139 const auto PointerToArrayExpr = expr(ignoringParenImpCasts(expr(
140 hasType(qualType(hasCanonicalType(pointerType(pointee(arrayType()))))))));
142 const auto StructAddrOfExpr =
143 unaryOperator(hasOperatorName(
"&"),
144 hasUnaryOperand(ignoringParenImpCasts(expr(
145 hasType(qualType(hasCanonicalType(recordType())))))));
148 expr(sizeOfExpr(has(expr(ignoringParenImpCasts(
149 anyOf(ArrayCastExpr, PointerToArrayExpr, StructAddrOfExpr))))))
150 .bind(
"sizeof-pointer-to-aggregate"),
154 if (WarnOnSizeOfCompareToConstant) {
156 binaryOperator(matchers::isRelationalOperator(),
157 hasEitherOperand(ignoringParenImpCasts(SizeOfExpr)),
158 hasEitherOperand(ignoringParenImpCasts(
159 anyOf(integerLiteral(equals(0)),
160 integerLiteral(isBiggerThan(0x80000))))))
161 .bind(
"sizeof-compare-constant"),
166 Finder->addMatcher(expr(sizeOfExpr(has(expr(ignoringParenImpCasts(
167 binaryOperator(hasOperatorName(
",")))))))
168 .bind(
"sizeof-comma-expr"),
172 const auto ElemType =
173 arrayType(hasElementType(recordType().bind(
"elem-type")));
174 const auto ElemPtrType = pointerType(pointee(type().bind(
"elem-ptr-type")));
175 const auto NumType = qualType(hasCanonicalType(
176 type(anyOf(ElemType, ElemPtrType, type())).bind(
"num-type")));
177 const auto DenomType = qualType(hasCanonicalType(type().bind(
"denom-type")));
180 binaryOperator(hasOperatorName(
"/"),
181 hasLHS(expr(ignoringParenImpCasts(
182 anyOf(sizeOfExpr(has(NumType)),
183 sizeOfExpr(has(expr(hasType(NumType)))))))),
184 hasRHS(expr(ignoringParenImpCasts(
185 anyOf(sizeOfExpr(has(DenomType)),
186 sizeOfExpr(has(expr(hasType(DenomType)))))))))
187 .bind(
"sizeof-divide-expr"),
191 Finder->addMatcher(binaryOperator(hasOperatorName(
"*"),
192 hasLHS(ignoringParenImpCasts(SizeOfExpr)),
193 hasRHS(ignoringParenImpCasts(SizeOfExpr)))
194 .bind(
"sizeof-multiply-sizeof"),
198 binaryOperator(hasOperatorName(
"*"),
199 hasEitherOperand(ignoringParenImpCasts(SizeOfExpr)),
200 hasEitherOperand(ignoringParenImpCasts(binaryOperator(
201 hasOperatorName(
"*"),
202 hasEitherOperand(ignoringParenImpCasts(SizeOfExpr))))))
203 .bind(
"sizeof-multiply-sizeof"),
209 expr(sizeOfExpr(has(ignoringParenImpCasts(expr(
210 hasSizeOfDescendant(8, expr(SizeOfExpr, unless(SizeOfZero))))))))
211 .bind(
"sizeof-sizeof-expr"),
216 const ASTContext &Ctx = *Result.Context;
218 if (
const auto *E = Result.Nodes.getNodeAs<Expr>(
"sizeof-constant")) {
219 diag(E->getBeginLoc(),
220 "suspicious usage of 'sizeof(K)'; did you mean 'K'?");
221 }
else if (
const auto *E =
222 Result.Nodes.getNodeAs<Expr>(
"sizeof-integer-call")) {
223 diag(E->getBeginLoc(),
"suspicious usage of 'sizeof()' on an expression " 224 "that results in an integer");
225 }
else if (
const auto *E = Result.Nodes.getNodeAs<Expr>(
"sizeof-this")) {
226 diag(E->getBeginLoc(),
227 "suspicious usage of 'sizeof(this)'; did you mean 'sizeof(*this)'");
228 }
else if (
const auto *E = Result.Nodes.getNodeAs<Expr>(
"sizeof-charp")) {
229 diag(E->getBeginLoc(),
230 "suspicious usage of 'sizeof(char*)'; do you mean 'strlen'?");
231 }
else if (
const auto *E =
232 Result.Nodes.getNodeAs<Expr>(
"sizeof-pointer-to-aggregate")) {
233 diag(E->getBeginLoc(),
234 "suspicious usage of 'sizeof(A*)'; pointer to aggregate");
235 }
else if (
const auto *E =
236 Result.Nodes.getNodeAs<Expr>(
"sizeof-compare-constant")) {
237 diag(E->getBeginLoc(),
238 "suspicious comparison of 'sizeof(expr)' to a constant");
239 }
else if (
const auto *E =
240 Result.Nodes.getNodeAs<Expr>(
"sizeof-comma-expr")) {
241 diag(E->getBeginLoc(),
"suspicious usage of 'sizeof(..., ...)'");
242 }
else if (
const auto *E =
243 Result.Nodes.getNodeAs<Expr>(
"sizeof-divide-expr")) {
244 const auto *NumTy = Result.Nodes.getNodeAs<Type>(
"num-type");
245 const auto *DenomTy = Result.Nodes.getNodeAs<Type>(
"denom-type");
246 const auto *ElementTy = Result.Nodes.getNodeAs<Type>(
"elem-type");
247 const auto *PointedTy = Result.Nodes.getNodeAs<Type>(
"elem-ptr-type");
249 CharUnits NumeratorSize = getSizeOfType(Ctx, NumTy);
250 CharUnits DenominatorSize = getSizeOfType(Ctx, DenomTy);
251 CharUnits ElementSize = getSizeOfType(Ctx, ElementTy);
253 if (DenominatorSize > CharUnits::Zero() &&
254 !NumeratorSize.isMultipleOf(DenominatorSize)) {
255 diag(E->getBeginLoc(),
"suspicious usage of 'sizeof(...)/sizeof(...)';" 256 " numerator is not a multiple of denominator");
257 }
else if (ElementSize > CharUnits::Zero() &&
258 DenominatorSize > CharUnits::Zero() &&
259 ElementSize != DenominatorSize) {
260 diag(E->getBeginLoc(),
"suspicious usage of 'sizeof(...)/sizeof(...)';" 261 " numerator is not a multiple of denominator");
262 }
else if (NumTy && DenomTy && NumTy == DenomTy) {
263 diag(E->getBeginLoc(),
264 "suspicious usage of sizeof pointer 'sizeof(T)/sizeof(T)'");
265 }
else if (PointedTy && DenomTy && PointedTy == DenomTy) {
266 diag(E->getBeginLoc(),
267 "suspicious usage of sizeof pointer 'sizeof(T*)/sizeof(T)'");
268 }
else if (NumTy && DenomTy && NumTy->isPointerType() &&
269 DenomTy->isPointerType()) {
270 diag(E->getBeginLoc(),
271 "suspicious usage of sizeof pointer 'sizeof(P*)/sizeof(Q*)'");
273 }
else if (
const auto *E =
274 Result.Nodes.getNodeAs<Expr>(
"sizeof-sizeof-expr")) {
275 diag(E->getBeginLoc(),
"suspicious usage of 'sizeof(sizeof(...))'");
276 }
else if (
const auto *E =
277 Result.Nodes.getNodeAs<Expr>(
"sizeof-multiply-sizeof")) {
278 diag(E->getBeginLoc(),
"suspicious 'sizeof' by 'sizeof' multiplication");
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
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.
Base class for all clang-tidy checks.
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
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
AST_MATCHER_P(FunctionDecl, throws, internal::Matcher< Type >, InnerMatcher)
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...
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.