10#include "../utils/Matchers.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
21 return Node.getValue().ugt(N);
24AST_MATCHER_P2(Expr, hasSizeOfDescendant,
int, Depth,
25 ast_matchers::internal::Matcher<Expr>, InnerMatcher) {
29 const Expr *
E =
Node.IgnoreParenImpCasts();
30 if (InnerMatcher.matches(*
E, Finder,
Builder))
33 if (
const auto *
CE = dyn_cast<CastExpr>(
E)) {
34 const auto M = hasSizeOfDescendant(Depth - 1, InnerMatcher);
35 return M.matches(*
CE->getSubExpr(), Finder,
Builder);
37 if (
const auto *UE = dyn_cast<UnaryOperator>(
E)) {
38 const auto M = hasSizeOfDescendant(Depth - 1, InnerMatcher);
39 return M.matches(*UE->getSubExpr(), Finder,
Builder);
41 if (
const auto *BE = dyn_cast<BinaryOperator>(
E)) {
42 const auto LHS = hasSizeOfDescendant(Depth - 1, InnerMatcher);
43 const auto RHS = hasSizeOfDescendant(Depth - 1, InnerMatcher);
44 return LHS.matches(*BE->getLHS(), Finder,
Builder) ||
45 RHS.matches(*BE->getRHS(), Finder,
Builder);
51CharUnits getSizeOfType(
const ASTContext &Ctx,
const Type *Ty) {
52 if (!Ty || Ty->isIncompleteType() || Ty->isDependentType() ||
53 isa<DependentSizedArrayType>(Ty) || !Ty->isConstantSizeType())
54 return CharUnits::Zero();
55 return Ctx.getTypeSizeInChars(Ty);
63 WarnOnSizeOfConstant(Options.get(
"WarnOnSizeOfConstant", true)),
64 WarnOnSizeOfIntegerExpression(
65 Options.get(
"WarnOnSizeOfIntegerExpression", false)),
66 WarnOnSizeOfThis(Options.get(
"WarnOnSizeOfThis", true)),
67 WarnOnSizeOfCompareToConstant(
68 Options.get(
"WarnOnSizeOfCompareToConstant", true)),
69 WarnOnSizeOfPointerToAggregate(
70 Options.get(
"WarnOnSizeOfPointerToAggregate", true)),
71 WarnOnSizeOfPointer(Options.get(
"WarnOnSizeOfPointer", false)) {}
74 Options.
store(Opts,
"WarnOnSizeOfConstant", WarnOnSizeOfConstant);
76 WarnOnSizeOfIntegerExpression);
77 Options.
store(Opts,
"WarnOnSizeOfThis", WarnOnSizeOfThis);
79 WarnOnSizeOfCompareToConstant);
81 WarnOnSizeOfPointerToAggregate);
82 Options.
store(Opts,
"WarnOnSizeOfPointer", WarnOnSizeOfPointer);
90 const auto IntegerExpr = ignoringParenImpCasts(integerLiteral());
91 const auto ConstantExpr = ignoringParenImpCasts(
92 anyOf(integerLiteral(), unaryOperator(hasUnaryOperand(IntegerExpr)),
93 binaryOperator(hasLHS(IntegerExpr), hasRHS(IntegerExpr))));
94 const auto IntegerCallExpr = ignoringParenImpCasts(callExpr(
95 anyOf(hasType(isInteger()), hasType(hasCanonicalType(enumType()))),
96 unless(isInTemplateInstantiation())));
97 const auto SizeOfExpr = sizeOfExpr(hasArgumentOfType(
98 hasUnqualifiedDesugaredType(type().bind(
"sizeof-arg-type"))));
99 const auto SizeOfZero =
100 sizeOfExpr(has(ignoringParenImpCasts(integerLiteral(equals(0)))));
105 if (WarnOnSizeOfConstant) {
107 expr(sizeOfExpr(has(ignoringParenImpCasts(ConstantExpr))),
109 .bind(
"sizeof-constant"),
114 if (WarnOnSizeOfIntegerExpression) {
115 Finder->addMatcher(sizeOfExpr(ignoringParenImpCasts(has(IntegerCallExpr)))
116 .bind(
"sizeof-integer-call"),
121 if (WarnOnSizeOfThis) {
122 Finder->addMatcher(sizeOfExpr(has(ignoringParenImpCasts(cxxThisExpr())))
123 .bind(
"sizeof-this"),
128 const auto CharPtrType = pointerType(pointee(isAnyCharacter()));
129 const auto ConstStrLiteralDecl =
130 varDecl(isDefinition(), hasType(hasCanonicalType(CharPtrType)),
131 hasInitializer(ignoringParenImpCasts(stringLiteral())));
132 const auto VarWithConstStrLiteralDecl = expr(
133 hasType(hasCanonicalType(CharPtrType)),
134 ignoringParenImpCasts(declRefExpr(hasDeclaration(ConstStrLiteralDecl))));
136 sizeOfExpr(has(ignoringParenImpCasts(VarWithConstStrLiteralDecl)))
137 .bind(
"sizeof-charp"),
155 if (WarnOnSizeOfPointerToAggregate || WarnOnSizeOfPointer) {
156 const auto ArrayExpr =
157 ignoringParenImpCasts(hasType(hasCanonicalType(arrayType())));
158 const auto ArrayCastExpr = expr(anyOf(
159 unaryOperator(hasUnaryOperand(ArrayExpr), unless(hasOperatorName(
"*"))),
160 binaryOperator(hasEitherOperand(ArrayExpr)),
161 castExpr(hasSourceExpression(ArrayExpr))));
162 const auto PointerToArrayExpr =
163 hasType(hasCanonicalType(pointerType(pointee(arrayType()))));
165 const auto PointerToStructType =
166 hasUnqualifiedDesugaredType(pointerType(pointee(recordType())));
167 const auto PointerToStructTypeWithBinding =
168 type(PointerToStructType).bind(
"struct-type");
169 const auto PointerToStructExpr =
170 expr(hasType(hasCanonicalType(PointerToStructType)));
172 const auto PointerToDetectedExpr =
174 ? expr(hasType(hasUnqualifiedDesugaredType(pointerType())))
175 : expr(anyOf(ArrayCastExpr, PointerToArrayExpr,
176 PointerToStructExpr));
178 const auto ZeroLiteral = ignoringParenImpCasts(integerLiteral(equals(0)));
179 const auto SubscriptExprWithZeroIndex =
180 arraySubscriptExpr(hasIndex(ZeroLiteral));
181 const auto DerefExpr =
182 ignoringParenImpCasts(unaryOperator(hasOperatorName(
"*")));
185 expr(sizeOfExpr(anyOf(has(ignoringParenImpCasts(
186 expr(PointerToDetectedExpr, unless(DerefExpr),
187 unless(SubscriptExprWithZeroIndex),
188 unless(VarWithConstStrLiteralDecl),
189 unless(cxxThisExpr())))),
190 has(PointerToStructTypeWithBinding))))
191 .bind(
"sizeof-pointer"),
196 if (WarnOnSizeOfCompareToConstant) {
198 binaryOperator(matchers::isRelationalOperator(),
199 hasOperands(ignoringParenImpCasts(SizeOfExpr),
200 ignoringParenImpCasts(integerLiteral(anyOf(
201 equals(0), isBiggerThan(0x80000))))))
202 .bind(
"sizeof-compare-constant"),
209 has(ignoringParenImpCasts(
210 binaryOperator(hasOperatorName(
",")).bind(
"sizeof-comma-binop"))))
211 .bind(
"sizeof-comma-expr"),
221 const auto ElemType =
222 arrayType(hasElementType(recordType().bind(
"elem-type")));
223 const auto ElemPtrType = pointerType(pointee(type().bind(
"elem-ptr-type")));
227 hasOperatorName(
"/"),
228 hasLHS(ignoringParenImpCasts(sizeOfExpr(hasArgumentOfType(
229 hasCanonicalType(type(anyOf(ElemType, ElemPtrType, type()))
230 .bind(
"num-type")))))),
231 hasRHS(ignoringParenImpCasts(sizeOfExpr(
232 hasArgumentOfType(hasCanonicalType(type().bind(
"denom-type")))))))
233 .bind(
"sizeof-divide-expr"),
237 Finder->addMatcher(binaryOperator(hasOperatorName(
"*"),
238 hasLHS(ignoringParenImpCasts(SizeOfExpr)),
239 hasRHS(ignoringParenImpCasts(SizeOfExpr)))
240 .bind(
"sizeof-multiply-sizeof"),
244 binaryOperator(hasOperatorName(
"*"),
245 hasOperands(ignoringParenImpCasts(SizeOfExpr),
246 ignoringParenImpCasts(binaryOperator(
247 hasOperatorName(
"*"),
249 ignoringParenImpCasts(SizeOfExpr))))))
250 .bind(
"sizeof-multiply-sizeof"),
255 Finder->addMatcher(sizeOfExpr(has(ignoringParenImpCasts(hasSizeOfDescendant(
256 8, allOf(SizeOfExpr, unless(SizeOfZero))))))
257 .bind(
"sizeof-sizeof-expr"),
262 const auto PtrDiffExpr = binaryOperator(
263 hasOperatorName(
"-"),
264 hasLHS(hasType(hasUnqualifiedDesugaredType(pointerType(pointee(
265 hasUnqualifiedDesugaredType(type().bind(
"left-ptr-type"))))))),
266 hasRHS(hasType(hasUnqualifiedDesugaredType(pointerType(pointee(
267 hasUnqualifiedDesugaredType(type().bind(
"right-ptr-type"))))))));
271 hasAnyOperatorName(
"==",
"!=",
"<",
"<=",
">",
">=",
"+",
"-"),
272 hasOperands(anyOf(ignoringParenImpCasts(
273 SizeOfExpr.bind(
"sizeof-ptr-mul-expr")),
274 ignoringParenImpCasts(binaryOperator(
275 hasOperatorName(
"*"),
276 hasEitherOperand(ignoringParenImpCasts(
277 SizeOfExpr.bind(
"sizeof-ptr-mul-expr")))))),
278 ignoringParenImpCasts(PtrDiffExpr)))
279 .bind(
"sizeof-in-ptr-arithmetic-mul"),
284 hasOperatorName(
"/"), hasLHS(ignoringParenImpCasts(PtrDiffExpr)),
285 hasRHS(ignoringParenImpCasts(SizeOfExpr.bind(
"sizeof-ptr-div-expr"))))
286 .bind(
"sizeof-in-ptr-arithmetic-div"),
291 const ASTContext &Ctx = *Result.Context;
293 if (
const auto *
E = Result.Nodes.getNodeAs<Expr>(
"sizeof-constant")) {
294 diag(
E->getBeginLoc(),
"suspicious usage of 'sizeof(K)'; did you mean 'K'?")
295 <<
E->getSourceRange();
296 }
else if (
const auto *
E =
297 Result.Nodes.getNodeAs<Expr>(
"sizeof-integer-call")) {
298 diag(
E->getBeginLoc(),
"suspicious usage of 'sizeof()' on an expression "
300 <<
E->getSourceRange();
301 }
else if (
const auto *
E = Result.Nodes.getNodeAs<Expr>(
"sizeof-this")) {
302 diag(
E->getBeginLoc(),
303 "suspicious usage of 'sizeof(this)'; did you mean 'sizeof(*this)'")
304 <<
E->getSourceRange();
305 }
else if (
const auto *
E = Result.Nodes.getNodeAs<Expr>(
"sizeof-charp")) {
306 diag(
E->getBeginLoc(),
307 "suspicious usage of 'sizeof(char*)'; do you mean 'strlen'?")
308 <<
E->getSourceRange();
309 }
else if (
const auto *
E = Result.Nodes.getNodeAs<Expr>(
"sizeof-pointer")) {
310 if (Result.Nodes.getNodeAs<
Type>(
"struct-type")) {
311 diag(
E->getBeginLoc(),
312 "suspicious usage of 'sizeof(A*)' on pointer-to-aggregate type; did "
313 "you mean 'sizeof(A)'?")
314 <<
E->getSourceRange();
316 diag(
E->getBeginLoc(),
"suspicious usage of 'sizeof()' on an expression "
318 <<
E->getSourceRange();
320 }
else if (
const auto *
E = Result.Nodes.getNodeAs<BinaryOperator>(
321 "sizeof-compare-constant")) {
322 diag(
E->getOperatorLoc(),
323 "suspicious comparison of 'sizeof(expr)' to a constant")
324 <<
E->getLHS()->getSourceRange() <<
E->getRHS()->getSourceRange();
325 }
else if (
const auto *
E =
326 Result.Nodes.getNodeAs<Expr>(
"sizeof-comma-expr")) {
328 Result.Nodes.getNodeAs<BinaryOperator>(
"sizeof-comma-binop");
330 diag(BO->getOperatorLoc(),
"suspicious usage of 'sizeof(..., ...)'")
331 <<
E->getSourceRange();
332 }
else if (
const auto *
E =
333 Result.Nodes.getNodeAs<BinaryOperator>(
"sizeof-divide-expr")) {
334 const auto *NumTy = Result.Nodes.getNodeAs<
Type>(
"num-type");
335 const auto *DenomTy = Result.Nodes.getNodeAs<
Type>(
"denom-type");
336 const auto *ElementTy = Result.Nodes.getNodeAs<
Type>(
"elem-type");
337 const auto *PointedTy = Result.Nodes.getNodeAs<
Type>(
"elem-ptr-type");
339 CharUnits NumeratorSize = getSizeOfType(Ctx, NumTy);
340 CharUnits DenominatorSize = getSizeOfType(Ctx, DenomTy);
341 CharUnits ElementSize = getSizeOfType(Ctx, ElementTy);
343 if (DenominatorSize > CharUnits::Zero() &&
344 !NumeratorSize.isMultipleOf(DenominatorSize)) {
345 diag(
E->getOperatorLoc(),
"suspicious usage of 'sizeof(...)/sizeof(...)';"
346 " numerator is not a multiple of denominator")
347 <<
E->getLHS()->getSourceRange() <<
E->getRHS()->getSourceRange();
348 }
else if (ElementSize > CharUnits::Zero() &&
349 DenominatorSize > CharUnits::Zero() &&
350 ElementSize != DenominatorSize) {
352 diag(
E->getOperatorLoc(),
353 "suspicious usage of 'sizeof(array)/sizeof(...)';"
354 " denominator differs from the size of array elements")
355 <<
E->getLHS()->getSourceRange() <<
E->getRHS()->getSourceRange();
356 }
else if (NumTy && DenomTy && NumTy == DenomTy) {
357 diag(
E->getOperatorLoc(),
358 "suspicious usage of 'sizeof(...)/sizeof(...)'; both expressions "
359 "have the same type")
360 <<
E->getLHS()->getSourceRange() <<
E->getRHS()->getSourceRange();
361 }
else if (!WarnOnSizeOfPointer) {
363 if (PointedTy && DenomTy && PointedTy == DenomTy) {
364 diag(
E->getOperatorLoc(),
365 "suspicious usage of 'sizeof(...)/sizeof(...)'; size of pointer "
366 "is divided by size of pointed type")
367 <<
E->getLHS()->getSourceRange() <<
E->getRHS()->getSourceRange();
368 }
else if (NumTy && DenomTy && NumTy->isPointerType() &&
369 DenomTy->isPointerType()) {
370 diag(
E->getOperatorLoc(),
371 "suspicious usage of 'sizeof(...)/sizeof(...)'; both expressions "
372 "have pointer types")
373 <<
E->getLHS()->getSourceRange() <<
E->getRHS()->getSourceRange();
376 }
else if (
const auto *
E =
377 Result.Nodes.getNodeAs<Expr>(
"sizeof-sizeof-expr")) {
378 diag(
E->getBeginLoc(),
"suspicious usage of 'sizeof(sizeof(...))'")
379 <<
E->getSourceRange();
380 }
else if (
const auto *
E = Result.Nodes.getNodeAs<BinaryOperator>(
381 "sizeof-multiply-sizeof")) {
382 diag(
E->getOperatorLoc(),
"suspicious 'sizeof' by 'sizeof' multiplication")
383 <<
E->getLHS()->getSourceRange() <<
E->getRHS()->getSourceRange();
384 }
else if (
const auto *
E = Result.Nodes.getNodeAs<BinaryOperator>(
385 "sizeof-in-ptr-arithmetic-mul")) {
386 const auto *LPtrTy = Result.Nodes.getNodeAs<
Type>(
"left-ptr-type");
387 const auto *RPtrTy = Result.Nodes.getNodeAs<
Type>(
"right-ptr-type");
388 const auto *SizeofArgTy = Result.Nodes.getNodeAs<
Type>(
"sizeof-arg-type");
389 const auto *SizeOfExpr =
390 Result.Nodes.getNodeAs<UnaryExprOrTypeTraitExpr>(
"sizeof-ptr-mul-expr");
392 if ((LPtrTy == RPtrTy) && (LPtrTy == SizeofArgTy)) {
393 diag(SizeOfExpr->getBeginLoc(),
"suspicious usage of 'sizeof(...)' in "
394 "pointer arithmetic")
395 << SizeOfExpr->getSourceRange() <<
E->getOperatorLoc()
396 <<
E->getLHS()->getSourceRange() <<
E->getRHS()->getSourceRange();
398 }
else if (
const auto *
E = Result.Nodes.getNodeAs<BinaryOperator>(
399 "sizeof-in-ptr-arithmetic-div")) {
400 const auto *LPtrTy = Result.Nodes.getNodeAs<
Type>(
"left-ptr-type");
401 const auto *RPtrTy = Result.Nodes.getNodeAs<
Type>(
"right-ptr-type");
402 const auto *SizeofArgTy = Result.Nodes.getNodeAs<
Type>(
"sizeof-arg-type");
403 const auto *SizeOfExpr =
404 Result.Nodes.getNodeAs<UnaryExprOrTypeTraitExpr>(
"sizeof-ptr-div-expr");
406 if ((LPtrTy == RPtrTy) && (LPtrTy == SizeofArgTy)) {
407 diag(SizeOfExpr->getBeginLoc(),
"suspicious usage of 'sizeof(...)' in "
408 "pointer arithmetic")
409 << SizeOfExpr->getSourceRange() <<
E->getOperatorLoc()
410 <<
E->getLHS()->getSourceRange() <<
E->getRHS()->getSourceRange();
llvm::SmallString< 256U > Name
CodeCompletionBuilder Builder
::clang::DynTypedNode Node
const google::protobuf::Message & M
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.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
SizeofExpressionCheck(StringRef Name, ClangTidyContext *Context)
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
AST_MATCHER_P(FunctionDecl, parameterCountGE, unsigned, N)
Matches functions that have at least the specified amount of parameters.
llvm::StringMap< ClangTidyValue > OptionMap