-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[clang-tidy] Warn about misuse of sizeof operator in loops. #143205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
52e4413
b7677bb
df65e16
66f7a1e
66345a4
432af1a
2e0e01e
a6cd351
46ee530
62d3858
2db451f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -72,7 +72,9 @@ SizeofExpressionCheck::SizeofExpressionCheck(StringRef Name, | |
| Options.get("WarnOnSizeOfPointerToAggregate", true)), | ||
| WarnOnSizeOfPointer(Options.get("WarnOnSizeOfPointer", false)), | ||
| WarnOnOffsetDividedBySizeOf( | ||
| Options.get("WarnOnOffsetDividedBySizeOf", true)) {} | ||
| Options.get("WarnOnOffsetDividedBySizeOf", true)), | ||
| WarnOnSizeOfInLoopTermination( | ||
| Options.get("WarnOnSizeOfInLoopTermination", true)) {} | ||
|
|
||
| void SizeofExpressionCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { | ||
| Options.store(Opts, "WarnOnSizeOfConstant", WarnOnSizeOfConstant); | ||
|
|
@@ -86,13 +88,20 @@ void SizeofExpressionCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { | |
| Options.store(Opts, "WarnOnSizeOfPointer", WarnOnSizeOfPointer); | ||
| Options.store(Opts, "WarnOnOffsetDividedBySizeOf", | ||
| WarnOnOffsetDividedBySizeOf); | ||
| Options.store(Opts, "WarnOnSizeOfInLoopTermination", | ||
| WarnOnSizeOfInLoopTermination); | ||
| } | ||
|
|
||
| void SizeofExpressionCheck::registerMatchers(MatchFinder *Finder) { | ||
| // FIXME: | ||
| // Some of the checks should not match in template code to avoid false | ||
| // positives if sizeof is applied on template argument. | ||
|
|
||
| auto LoopExpr = | ||
| [](const ast_matchers::internal::Matcher<Stmt> &InnerMatcher) { | ||
| return stmt(anyOf(forStmt(InnerMatcher), whileStmt(InnerMatcher))); | ||
| }; | ||
|
|
||
| const auto IntegerExpr = ignoringParenImpCasts(integerLiteral()); | ||
| const auto ConstantExpr = ignoringParenImpCasts( | ||
| anyOf(integerLiteral(), unaryOperator(hasUnaryOperand(IntegerExpr)), | ||
|
|
@@ -130,6 +139,11 @@ void SizeofExpressionCheck::registerMatchers(MatchFinder *Finder) { | |
| this); | ||
| } | ||
|
|
||
| if (WarnOnSizeOfInLoopTermination) { | ||
| Finder->addMatcher( | ||
| LoopExpr(has(binaryOperator(has(SizeOfExpr)))).bind("loop-expr"), this); | ||
| } | ||
|
|
||
| // Detect sizeof(kPtr) where kPtr is 'const char* kPtr = "abc"'; | ||
| const auto CharPtrType = pointerType(pointee(isAnyCharacter())); | ||
| const auto ConstStrLiteralDecl = | ||
|
|
@@ -353,6 +367,19 @@ void SizeofExpressionCheck::check(const MatchFinder::MatchResult &Result) { | |
| diag(E->getBeginLoc(), | ||
| "suspicious usage of 'sizeof(char*)'; do you mean 'strlen'?") | ||
| << E->getSourceRange(); | ||
| } else if (const auto *E = Result.Nodes.getNodeAs<Stmt>("loop-expr")) { | ||
| auto *SizeofArgTy = Result.Nodes.getNodeAs<Type>("sizeof-arg-type"); | ||
| if (const auto member = dyn_cast<MemberPointerType>(SizeofArgTy)) { | ||
| SizeofArgTy = member->getPointeeType().getTypePtr(); | ||
| } | ||
|
|
||
| if (const auto type = dyn_cast<ArrayType>(SizeofArgTy)) { | ||
| CharUnits sSize = Ctx.getTypeSizeInChars(type->getElementType()); | ||
| if (!sSize.isOne()) { | ||
| diag(E->getBeginLoc(), "suspicious usage of 'sizeof' in the loop") | ||
| << E->getSourceRange(); | ||
|
||
| } | ||
| } | ||
| } else if (const auto *E = Result.Nodes.getNodeAs<Expr>("sizeof-pointer")) { | ||
| if (Result.Nodes.getNodeAs<Type>("struct-type")) { | ||
| diag(E->getBeginLoc(), | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.