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 WarnOnSizeOfThis(Options.get(
"WarnOnSizeOfThis", 1) != 0),
66 WarnOnSizeOfCompareToConstant(
67 Options.get(
"WarnOnSizeOfCompareToConstant", 1) != 0) {}
70 Options.
store(Opts,
"WarnOnSizeOfConstant", WarnOnSizeOfConstant);
71 Options.
store(Opts,
"WarnOnSizeOfThis", WarnOnSizeOfThis);
73 WarnOnSizeOfCompareToConstant);
77 const auto IntegerExpr = ignoringParenImpCasts(integerLiteral());
78 const auto ConstantExpr = expr(ignoringParenImpCasts(
79 anyOf(integerLiteral(), unaryOperator(hasUnaryOperand(IntegerExpr)),
80 binaryOperator(hasLHS(IntegerExpr), hasRHS(IntegerExpr)))));
81 const auto SizeOfExpr =
82 expr(anyOf(sizeOfExpr(has(type())), sizeOfExpr(has(expr()))));
83 const auto SizeOfZero = expr(
84 sizeOfExpr(has(ignoringParenImpCasts(expr(integerLiteral(equals(0)))))));
89 if (WarnOnSizeOfConstant) {
91 expr(sizeOfExpr(has(ignoringParenImpCasts(ConstantExpr))),
93 .bind(
"sizeof-constant"),
98 if (WarnOnSizeOfThis) {
100 expr(sizeOfExpr(has(ignoringParenImpCasts(expr(cxxThisExpr())))))
101 .bind(
"sizeof-this"),
106 const auto CharPtrType = pointerType(pointee(isAnyCharacter()));
107 const auto ConstStrLiteralDecl =
108 varDecl(isDefinition(), hasType(qualType(hasCanonicalType(CharPtrType))),
109 hasInitializer(ignoringParenImpCasts(stringLiteral())));
110 Finder->addMatcher(expr(sizeOfExpr(has(ignoringParenImpCasts(expr(
111 hasType(qualType(hasCanonicalType(CharPtrType))),
112 ignoringParenImpCasts(declRefExpr(
113 hasDeclaration(ConstStrLiteralDecl))))))))
114 .bind(
"sizeof-charp"),
118 const auto ArrayExpr = expr(ignoringParenImpCasts(
119 expr(hasType(qualType(hasCanonicalType(arrayType()))))));
120 const auto ArrayCastExpr = expr(anyOf(
121 unaryOperator(hasUnaryOperand(ArrayExpr), unless(hasOperatorName(
"*"))),
122 binaryOperator(hasEitherOperand(ArrayExpr)),
123 castExpr(hasSourceExpression(ArrayExpr))));
124 const auto PointerToArrayExpr = expr(ignoringParenImpCasts(expr(
125 hasType(qualType(hasCanonicalType(pointerType(pointee(arrayType()))))))));
127 const auto StructAddrOfExpr =
128 unaryOperator(hasOperatorName(
"&"),
129 hasUnaryOperand(ignoringParenImpCasts(expr(
130 hasType(qualType(hasCanonicalType(recordType())))))));
133 expr(sizeOfExpr(has(expr(ignoringParenImpCasts(
134 anyOf(ArrayCastExpr, PointerToArrayExpr, StructAddrOfExpr))))))
135 .bind(
"sizeof-pointer-to-aggregate"),
139 if (WarnOnSizeOfCompareToConstant) {
141 binaryOperator(matchers::isRelationalOperator(),
142 hasEitherOperand(ignoringParenImpCasts(SizeOfExpr)),
143 hasEitherOperand(ignoringParenImpCasts(
144 anyOf(integerLiteral(equals(0)),
145 integerLiteral(isBiggerThan(0x80000))))))
146 .bind(
"sizeof-compare-constant"),
151 Finder->addMatcher(expr(sizeOfExpr(has(expr(ignoringParenImpCasts(
152 binaryOperator(hasOperatorName(
",")))))))
153 .bind(
"sizeof-comma-expr"),
157 const auto ElemType =
158 arrayType(hasElementType(recordType().bind(
"elem-type")));
159 const auto ElemPtrType = pointerType(pointee(type().bind(
"elem-ptr-type")));
160 const auto NumType = qualType(hasCanonicalType(
161 type(anyOf(ElemType, ElemPtrType, type())).bind(
"num-type")));
162 const auto DenomType = qualType(hasCanonicalType(type().bind(
"denom-type")));
165 binaryOperator(hasOperatorName(
"/"),
166 hasLHS(expr(ignoringParenImpCasts(
167 anyOf(sizeOfExpr(has(NumType)),
168 sizeOfExpr(has(expr(hasType(NumType)))))))),
169 hasRHS(expr(ignoringParenImpCasts(
170 anyOf(sizeOfExpr(has(DenomType)),
171 sizeOfExpr(has(expr(hasType(DenomType)))))))))
172 .bind(
"sizeof-divide-expr"),
176 Finder->addMatcher(binaryOperator(hasOperatorName(
"*"),
177 hasLHS(ignoringParenImpCasts(SizeOfExpr)),
178 hasRHS(ignoringParenImpCasts(SizeOfExpr)))
179 .bind(
"sizeof-multiply-sizeof"),
183 binaryOperator(hasOperatorName(
"*"),
184 hasEitherOperand(ignoringParenImpCasts(SizeOfExpr)),
185 hasEitherOperand(ignoringParenImpCasts(binaryOperator(
186 hasOperatorName(
"*"),
187 hasEitherOperand(ignoringParenImpCasts(SizeOfExpr))))))
188 .bind(
"sizeof-multiply-sizeof"),
194 expr(sizeOfExpr(has(ignoringParenImpCasts(expr(
195 hasSizeOfDescendant(8, expr(SizeOfExpr, unless(SizeOfZero))))))))
196 .bind(
"sizeof-sizeof-expr"),
201 const ASTContext &Ctx = *Result.Context;
203 if (
const auto *E = Result.Nodes.getNodeAs<Expr>(
"sizeof-constant")) {
204 diag(E->getLocStart(),
205 "suspicious usage of 'sizeof(K)'; did you mean 'K'?");
206 }
else if (
const auto *E = Result.Nodes.getNodeAs<Expr>(
"sizeof-this")) {
207 diag(E->getLocStart(),
208 "suspicious usage of 'sizeof(this)'; did you mean 'sizeof(*this)'");
209 }
else if (
const auto *E = Result.Nodes.getNodeAs<Expr>(
"sizeof-charp")) {
210 diag(E->getLocStart(),
211 "suspicious usage of 'sizeof(char*)'; do you mean 'strlen'?");
212 }
else if (
const auto *E =
213 Result.Nodes.getNodeAs<Expr>(
"sizeof-pointer-to-aggregate")) {
214 diag(E->getLocStart(),
215 "suspicious usage of 'sizeof(A*)'; pointer to aggregate");
216 }
else if (
const auto *E =
217 Result.Nodes.getNodeAs<Expr>(
"sizeof-compare-constant")) {
218 diag(E->getLocStart(),
219 "suspicious comparison of 'sizeof(expr)' to a constant");
220 }
else if (
const auto *E =
221 Result.Nodes.getNodeAs<Expr>(
"sizeof-comma-expr")) {
222 diag(E->getLocStart(),
"suspicious usage of 'sizeof(..., ...)'");
223 }
else if (
const auto *E =
224 Result.Nodes.getNodeAs<Expr>(
"sizeof-divide-expr")) {
225 const auto *NumTy = Result.Nodes.getNodeAs<Type>(
"num-type");
226 const auto *DenomTy = Result.Nodes.getNodeAs<Type>(
"denom-type");
227 const auto *ElementTy = Result.Nodes.getNodeAs<Type>(
"elem-type");
228 const auto *PointedTy = Result.Nodes.getNodeAs<Type>(
"elem-ptr-type");
230 CharUnits NumeratorSize = getSizeOfType(Ctx, NumTy);
231 CharUnits DenominatorSize = getSizeOfType(Ctx, DenomTy);
232 CharUnits ElementSize = getSizeOfType(Ctx, ElementTy);
234 if (DenominatorSize > CharUnits::Zero() &&
235 !NumeratorSize.isMultipleOf(DenominatorSize)) {
236 diag(E->getLocStart(),
"suspicious usage of 'sizeof(...)/sizeof(...)';" 237 " numerator is not a multiple of denominator");
238 }
else if (ElementSize > CharUnits::Zero() &&
239 DenominatorSize > CharUnits::Zero() &&
240 ElementSize != DenominatorSize) {
241 diag(E->getLocStart(),
"suspicious usage of 'sizeof(...)/sizeof(...)';" 242 " numerator is not a multiple of denominator");
243 }
else if (NumTy && DenomTy && NumTy == DenomTy) {
244 diag(E->getLocStart(),
245 "suspicious usage of sizeof pointer 'sizeof(T)/sizeof(T)'");
246 }
else if (PointedTy && DenomTy && PointedTy == DenomTy) {
247 diag(E->getLocStart(),
248 "suspicious usage of sizeof pointer 'sizeof(T*)/sizeof(T)'");
249 }
else if (NumTy && DenomTy && NumTy->isPointerType() &&
250 DenomTy->isPointerType()) {
251 diag(E->getLocStart(),
252 "suspicious usage of sizeof pointer 'sizeof(P*)/sizeof(Q*)'");
254 }
else if (
const auto *E =
255 Result.Nodes.getNodeAs<Expr>(
"sizeof-sizeof-expr")) {
256 diag(E->getLocStart(),
"suspicious usage of 'sizeof(sizeof(...))'");
257 }
else if (
const auto *E =
258 Result.Nodes.getNodeAs<Expr>(
"sizeof-multiply-sizeof")) {
259 diag(E->getLocStart(),
"suspicious 'sizeof' by 'sizeof' multiplication");
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.
std::map< std::string, std::string > OptionMap
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
AST_MATCHER_P(IntegerLiteral, isBiggerThan, unsigned, N)
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.