10 #include "../utils/Matchers.h" 11 #include "clang/AST/ASTContext.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 23 return Node.getValue().getZExtValue() > N;
26 AST_MATCHER_P2(Expr, hasSizeOfDescendant,
int, Depth,
27 ast_matchers::internal::Matcher<Expr>, InnerMatcher) {
31 const Expr *
E = Node.IgnoreParenImpCasts();
32 if (InnerMatcher.matches(*E, Finder,
Builder))
35 if (
const auto *CE = dyn_cast<CastExpr>(E)) {
36 const auto M = hasSizeOfDescendant(Depth - 1, InnerMatcher);
37 return M.matches(*CE->getSubExpr(), Finder,
Builder);
38 }
else if (
const auto *UE = dyn_cast<UnaryOperator>(E)) {
39 const auto M = hasSizeOfDescendant(Depth - 1, InnerMatcher);
40 return M.matches(*UE->getSubExpr(), Finder,
Builder);
41 }
else 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);
51 CharUnits 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);
60 SizeofExpressionCheck::SizeofExpressionCheck(StringRef
Name,
63 WarnOnSizeOfConstant(Options.get(
"WarnOnSizeOfConstant", 1) != 0),
64 WarnOnSizeOfIntegerExpression(
65 Options.get(
"WarnOnSizeOfIntegerExpression", 0) != 0),
66 WarnOnSizeOfThis(Options.get(
"WarnOnSizeOfThis", 1) != 0),
67 WarnOnSizeOfCompareToConstant(
68 Options.get(
"WarnOnSizeOfCompareToConstant", 1) != 0) {}
71 Options.
store(Opts,
"WarnOnSizeOfConstant", WarnOnSizeOfConstant);
73 WarnOnSizeOfIntegerExpression);
74 Options.
store(Opts,
"WarnOnSizeOfThis", WarnOnSizeOfThis);
76 WarnOnSizeOfCompareToConstant);
80 const auto IntegerExpr = ignoringParenImpCasts(integerLiteral());
81 const auto ConstantExpr = expr(ignoringParenImpCasts(
82 anyOf(integerLiteral(), unaryOperator(hasUnaryOperand(IntegerExpr)),
83 binaryOperator(hasLHS(IntegerExpr), hasRHS(IntegerExpr)))));
84 const auto IntegerCallExpr = expr(ignoringParenImpCasts(
85 callExpr(anyOf(hasType(isInteger()), hasType(enumType())),
86 unless(isInTemplateInstantiation()))));
87 const auto SizeOfExpr = expr(anyOf(
89 has(hasUnqualifiedDesugaredType(type().bind(
"sizeof-arg-type")))),
90 sizeOfExpr(has(expr(hasType(
91 hasUnqualifiedDesugaredType(type().bind(
"sizeof-arg-type"))))))));
92 const auto SizeOfZero = expr(
93 sizeOfExpr(has(ignoringParenImpCasts(expr(integerLiteral(equals(0)))))));
98 if (WarnOnSizeOfConstant) {
100 expr(sizeOfExpr(has(ignoringParenImpCasts(ConstantExpr))),
102 .bind(
"sizeof-constant"),
107 if (WarnOnSizeOfIntegerExpression) {
109 expr(sizeOfExpr(ignoringParenImpCasts(has(IntegerCallExpr))))
110 .bind(
"sizeof-integer-call"),
115 if (WarnOnSizeOfThis) {
117 expr(sizeOfExpr(has(ignoringParenImpCasts(expr(cxxThisExpr())))))
118 .bind(
"sizeof-this"),
123 const auto CharPtrType = pointerType(pointee(isAnyCharacter()));
124 const auto ConstStrLiteralDecl =
125 varDecl(isDefinition(), hasType(qualType(hasCanonicalType(CharPtrType))),
126 hasInitializer(ignoringParenImpCasts(stringLiteral())));
127 Finder->addMatcher(expr(sizeOfExpr(has(ignoringParenImpCasts(expr(
128 hasType(qualType(hasCanonicalType(CharPtrType))),
129 ignoringParenImpCasts(declRefExpr(
130 hasDeclaration(ConstStrLiteralDecl))))))))
131 .bind(
"sizeof-charp"),
135 const auto ArrayExpr = expr(ignoringParenImpCasts(
136 expr(hasType(qualType(hasCanonicalType(arrayType()))))));
137 const auto ArrayCastExpr = expr(anyOf(
138 unaryOperator(hasUnaryOperand(ArrayExpr), unless(hasOperatorName(
"*"))),
139 binaryOperator(hasEitherOperand(ArrayExpr)),
140 castExpr(hasSourceExpression(ArrayExpr))));
141 const auto PointerToArrayExpr = expr(ignoringParenImpCasts(expr(
142 hasType(qualType(hasCanonicalType(pointerType(pointee(arrayType()))))))));
144 const auto StructAddrOfExpr =
145 unaryOperator(hasOperatorName(
"&"),
146 hasUnaryOperand(ignoringParenImpCasts(expr(
147 hasType(qualType(hasCanonicalType(recordType())))))));
148 const auto PointerToStructType = type(hasUnqualifiedDesugaredType(
149 pointerType(pointee(recordType()))));
150 const auto PointerToStructExpr = expr(ignoringParenImpCasts(expr(
151 hasType(qualType(hasCanonicalType(PointerToStructType))),
152 unless(cxxThisExpr()))));
155 expr(anyOf(sizeOfExpr(has(expr(ignoringParenImpCasts(
156 anyOf(ArrayCastExpr, PointerToArrayExpr, StructAddrOfExpr,
157 PointerToStructExpr))))),
158 sizeOfExpr(has(PointerToStructType))))
159 .bind(
"sizeof-pointer-to-aggregate"),
163 if (WarnOnSizeOfCompareToConstant) {
165 binaryOperator(matchers::isRelationalOperator(),
166 hasEitherOperand(ignoringParenImpCasts(SizeOfExpr)),
167 hasEitherOperand(ignoringParenImpCasts(
168 anyOf(integerLiteral(equals(0)),
169 integerLiteral(isBiggerThan(0x80000))))))
170 .bind(
"sizeof-compare-constant"),
175 Finder->addMatcher(expr(sizeOfExpr(has(expr(ignoringParenImpCasts(
176 binaryOperator(hasOperatorName(
",")))))))
177 .bind(
"sizeof-comma-expr"),
181 const auto ElemType =
182 arrayType(hasElementType(recordType().bind(
"elem-type")));
183 const auto ElemPtrType = pointerType(pointee(type().bind(
"elem-ptr-type")));
184 const auto NumType = qualType(hasCanonicalType(
185 type(anyOf(ElemType, ElemPtrType, type())).bind(
"num-type")));
186 const auto DenomType = qualType(hasCanonicalType(type().bind(
"denom-type")));
189 binaryOperator(hasOperatorName(
"/"),
190 hasLHS(expr(ignoringParenImpCasts(
191 anyOf(sizeOfExpr(has(NumType)),
192 sizeOfExpr(has(expr(hasType(NumType)))))))),
193 hasRHS(expr(ignoringParenImpCasts(
194 anyOf(sizeOfExpr(has(DenomType)),
195 sizeOfExpr(has(expr(hasType(DenomType)))))))))
196 .bind(
"sizeof-divide-expr"),
200 Finder->addMatcher(binaryOperator(hasOperatorName(
"*"),
201 hasLHS(ignoringParenImpCasts(SizeOfExpr)),
202 hasRHS(ignoringParenImpCasts(SizeOfExpr)))
203 .bind(
"sizeof-multiply-sizeof"),
207 binaryOperator(hasOperatorName(
"*"),
208 hasEitherOperand(ignoringParenImpCasts(SizeOfExpr)),
209 hasEitherOperand(ignoringParenImpCasts(binaryOperator(
210 hasOperatorName(
"*"),
211 hasEitherOperand(ignoringParenImpCasts(SizeOfExpr))))))
212 .bind(
"sizeof-multiply-sizeof"),
218 expr(sizeOfExpr(has(ignoringParenImpCasts(expr(
219 hasSizeOfDescendant(8, expr(SizeOfExpr, unless(SizeOfZero))))))))
220 .bind(
"sizeof-sizeof-expr"),
225 const auto PtrDiffExpr = binaryOperator(
226 hasOperatorName(
"-"),
227 hasLHS(expr(hasType(hasUnqualifiedDesugaredType(pointerType(pointee(
228 hasUnqualifiedDesugaredType(type().bind(
"left-ptr-type")))))))),
229 hasRHS(expr(hasType(hasUnqualifiedDesugaredType(pointerType(pointee(
230 hasUnqualifiedDesugaredType(type().bind(
"right-ptr-type")))))))));
234 anyOf(hasOperatorName(
"=="), hasOperatorName(
"!="),
235 hasOperatorName(
"<"), hasOperatorName(
"<="),
236 hasOperatorName(
">"), hasOperatorName(
">="),
237 hasOperatorName(
"+"), hasOperatorName(
"-")),
238 hasEitherOperand(expr(anyOf(
239 ignoringParenImpCasts(SizeOfExpr),
240 ignoringParenImpCasts(binaryOperator(
241 hasOperatorName(
"*"),
242 hasEitherOperand(ignoringParenImpCasts(SizeOfExpr))))))),
243 hasEitherOperand(ignoringParenImpCasts(PtrDiffExpr)))
244 .bind(
"sizeof-in-ptr-arithmetic-mul"),
247 Finder->addMatcher(binaryOperator(hasOperatorName(
"/"),
248 hasLHS(ignoringParenImpCasts(PtrDiffExpr)),
249 hasRHS(ignoringParenImpCasts(SizeOfExpr)))
250 .bind(
"sizeof-in-ptr-arithmetic-div"),
255 const ASTContext &Ctx = *Result.Context;
257 if (
const auto *E = Result.Nodes.getNodeAs<Expr>(
"sizeof-constant")) {
258 diag(E->getBeginLoc(),
259 "suspicious usage of 'sizeof(K)'; did you mean 'K'?");
260 }
else if (
const auto *E =
261 Result.Nodes.getNodeAs<Expr>(
"sizeof-integer-call")) {
262 diag(E->getBeginLoc(),
"suspicious usage of 'sizeof()' on an expression " 263 "that results in an integer");
264 }
else if (
const auto *E = Result.Nodes.getNodeAs<Expr>(
"sizeof-this")) {
265 diag(E->getBeginLoc(),
266 "suspicious usage of 'sizeof(this)'; did you mean 'sizeof(*this)'");
267 }
else if (
const auto *E = Result.Nodes.getNodeAs<Expr>(
"sizeof-charp")) {
268 diag(E->getBeginLoc(),
269 "suspicious usage of 'sizeof(char*)'; do you mean 'strlen'?");
270 }
else if (
const auto *E =
271 Result.Nodes.getNodeAs<Expr>(
"sizeof-pointer-to-aggregate")) {
272 diag(E->getBeginLoc(),
273 "suspicious usage of 'sizeof(A*)'; pointer to aggregate");
274 }
else if (
const auto *E =
275 Result.Nodes.getNodeAs<Expr>(
"sizeof-compare-constant")) {
276 diag(E->getBeginLoc(),
277 "suspicious comparison of 'sizeof(expr)' to a constant");
278 }
else if (
const auto *E =
279 Result.Nodes.getNodeAs<Expr>(
"sizeof-comma-expr")) {
280 diag(E->getBeginLoc(),
"suspicious usage of 'sizeof(..., ...)'");
281 }
else if (
const auto *E =
282 Result.Nodes.getNodeAs<Expr>(
"sizeof-divide-expr")) {
283 const auto *NumTy = Result.Nodes.getNodeAs<
Type>(
"num-type");
284 const auto *DenomTy = Result.Nodes.getNodeAs<
Type>(
"denom-type");
285 const auto *ElementTy = Result.Nodes.getNodeAs<
Type>(
"elem-type");
286 const auto *PointedTy = Result.Nodes.getNodeAs<
Type>(
"elem-ptr-type");
288 CharUnits NumeratorSize = getSizeOfType(Ctx, NumTy);
289 CharUnits DenominatorSize = getSizeOfType(Ctx, DenomTy);
290 CharUnits ElementSize = getSizeOfType(Ctx, ElementTy);
292 if (DenominatorSize > CharUnits::Zero() &&
293 !NumeratorSize.isMultipleOf(DenominatorSize)) {
294 diag(E->getBeginLoc(),
"suspicious usage of 'sizeof(...)/sizeof(...)';" 295 " numerator is not a multiple of denominator");
296 }
else if (ElementSize > CharUnits::Zero() &&
297 DenominatorSize > CharUnits::Zero() &&
298 ElementSize != DenominatorSize) {
299 diag(E->getBeginLoc(),
"suspicious usage of 'sizeof(...)/sizeof(...)';" 300 " numerator is not a multiple of denominator");
301 }
else if (NumTy && DenomTy && NumTy == DenomTy) {
302 diag(E->getBeginLoc(),
303 "suspicious usage of sizeof pointer 'sizeof(T)/sizeof(T)'");
304 }
else if (PointedTy && DenomTy && PointedTy == DenomTy) {
305 diag(E->getBeginLoc(),
306 "suspicious usage of sizeof pointer 'sizeof(T*)/sizeof(T)'");
307 }
else if (NumTy && DenomTy && NumTy->isPointerType() &&
308 DenomTy->isPointerType()) {
309 diag(E->getBeginLoc(),
310 "suspicious usage of sizeof pointer 'sizeof(P*)/sizeof(Q*)'");
312 }
else if (
const auto *E =
313 Result.Nodes.getNodeAs<Expr>(
"sizeof-sizeof-expr")) {
314 diag(E->getBeginLoc(),
"suspicious usage of 'sizeof(sizeof(...))'");
315 }
else if (
const auto *E =
316 Result.Nodes.getNodeAs<Expr>(
"sizeof-multiply-sizeof")) {
317 diag(E->getBeginLoc(),
"suspicious 'sizeof' by 'sizeof' multiplication");
318 }
else if (
const auto *E =
319 Result.Nodes.getNodeAs<Expr>(
"sizeof-in-ptr-arithmetic-mul")) {
320 const auto *LPtrTy = Result.Nodes.getNodeAs<
Type>(
"left-ptr-type");
321 const auto *RPtrTy = Result.Nodes.getNodeAs<
Type>(
"right-ptr-type");
322 const auto *SizeofArgTy = Result.Nodes.getNodeAs<
Type>(
"sizeof-arg-type");
324 if ((LPtrTy == RPtrTy) && (LPtrTy == SizeofArgTy)) {
325 diag(E->getBeginLoc(),
"suspicious usage of 'sizeof(...)' in " 326 "pointer arithmetic");
328 }
else if (
const auto *E =
329 Result.Nodes.getNodeAs<Expr>(
"sizeof-in-ptr-arithmetic-div")) {
330 const auto *LPtrTy = Result.Nodes.getNodeAs<
Type>(
"left-ptr-type");
331 const auto *RPtrTy = Result.Nodes.getNodeAs<
Type>(
"right-ptr-type");
332 const auto *SizeofArgTy = Result.Nodes.getNodeAs<
Type>(
"sizeof-arg-type");
334 if ((LPtrTy == RPtrTy) && (LPtrTy == SizeofArgTy)) {
335 diag(E->getBeginLoc(),
"suspicious usage of 'sizeof(...)' in " 336 "pointer arithmetic");
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Base class for all clang-tidy checks.
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.
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
CodeCompletionBuilder Builder
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
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.
AST_MATCHER_P(CXXMethodDecl, hasCanonicalDecl, ast_matchers::internal::Matcher< CXXMethodDecl >, InnerMatcher)