Skip to content

Commit 52e4413

Browse files
author
MalavikaSamak
committed
Place holder message for sizeof operator in loops.
1 parent fec9be9 commit 52e4413

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ SizeofExpressionCheck::SizeofExpressionCheck(StringRef Name,
7272
Options.get("WarnOnSizeOfPointerToAggregate", true)),
7373
WarnOnSizeOfPointer(Options.get("WarnOnSizeOfPointer", false)),
7474
WarnOnOffsetDividedBySizeOf(
75-
Options.get("WarnOnOffsetDividedBySizeOf", true)) {}
75+
Options.get("WarnOnOffsetDividedBySizeOf", true)),
76+
WarnOnLoopExprSizeOf(Options.get("WarnOnLoopExprSizeOf", true)) {}
7677

7778
void SizeofExpressionCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
7879
Options.store(Opts, "WarnOnSizeOfConstant", WarnOnSizeOfConstant);
@@ -86,13 +87,19 @@ void SizeofExpressionCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
8687
Options.store(Opts, "WarnOnSizeOfPointer", WarnOnSizeOfPointer);
8788
Options.store(Opts, "WarnOnOffsetDividedBySizeOf",
8889
WarnOnOffsetDividedBySizeOf);
90+
Options.store(Opts, "WarnOnLoopExprSizeOf", WarnOnLoopExprSizeOf);
8991
}
9092

9193
void SizeofExpressionCheck::registerMatchers(MatchFinder *Finder) {
9294
// FIXME:
9395
// Some of the checks should not match in template code to avoid false
9496
// positives if sizeof is applied on template argument.
9597

98+
auto LoopExpr =
99+
[](const ast_matchers::internal::Matcher<Stmt> &InnerMatcher) {
100+
return stmt(anyOf(forStmt(InnerMatcher), whileStmt(InnerMatcher)));
101+
};
102+
96103
const auto IntegerExpr = ignoringParenImpCasts(integerLiteral());
97104
const auto ConstantExpr = ignoringParenImpCasts(
98105
anyOf(integerLiteral(), unaryOperator(hasUnaryOperand(IntegerExpr)),
@@ -130,6 +137,11 @@ void SizeofExpressionCheck::registerMatchers(MatchFinder *Finder) {
130137
this);
131138
}
132139

140+
if (WarnOnLoopExprSizeOf) {
141+
Finder->addMatcher(
142+
LoopExpr(has(binaryOperator(has(SizeOfExpr)))).bind("loop-expr"), this);
143+
}
144+
133145
// Detect sizeof(kPtr) where kPtr is 'const char* kPtr = "abc"';
134146
const auto CharPtrType = pointerType(pointee(isAnyCharacter()));
135147
const auto ConstStrLiteralDecl =
@@ -353,6 +365,9 @@ void SizeofExpressionCheck::check(const MatchFinder::MatchResult &Result) {
353365
diag(E->getBeginLoc(),
354366
"suspicious usage of 'sizeof(char*)'; do you mean 'strlen'?")
355367
<< E->getSourceRange();
368+
} else if (const auto *E = Result.Nodes.getNodeAs<Stmt>("loop-expr")) {
369+
diag(E->getBeginLoc(), "suspicious usage of 'sizeof' in the loop")
370+
<< E->getSourceRange();
356371
} else if (const auto *E = Result.Nodes.getNodeAs<Expr>("sizeof-pointer")) {
357372
if (Result.Nodes.getNodeAs<Type>("struct-type")) {
358373
diag(E->getBeginLoc(),

clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class SizeofExpressionCheck : public ClangTidyCheck {
3232
const bool WarnOnSizeOfPointerToAggregate;
3333
const bool WarnOnSizeOfPointer;
3434
const bool WarnOnOffsetDividedBySizeOf;
35+
const bool WarnOnLoopExprSizeOf;
3536
};
3637

3738
} // namespace clang::tidy::bugprone

clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,37 @@ int Test2(MyConstChar* A) {
164164
return sum;
165165
}
166166

167+
struct A {
168+
int array[10];
169+
};
170+
171+
struct B {
172+
struct A a;
173+
};
174+
175+
int square(int num, struct B b) {
176+
struct A arr[10];
177+
// CHECK-MESSAGES: :[[@LINE+1]]:5: warning: suspicious usage of 'sizeof' in the loop [bugprone-sizeof-expression]
178+
for(int i = 0; i < sizeof(arr); i++) {
179+
struct A a = arr[i];
180+
}
181+
// CHECK-MESSAGES: :[[@LINE+2]]:24: warning: suspicious usage of 'sizeof(K)'; did you mean 'K'? [bugprone-sizeof-expression]
182+
// CHECK-MESSAGES: :[[@LINE+1]]:34: warning: suspicious usage of 'sizeof(...)/sizeof(...)'; numerator is not a multiple of denominator [bugprone-sizeof-expression]
183+
for(int i = 0; i < sizeof(10)/sizeof(A); i++) {
184+
struct A a = arr[i];
185+
}
186+
187+
for(int i = 0; i < sizeof(arr)/sizeof(A); i++) {
188+
struct A a = arr[i];
189+
}
190+
191+
// CHECK-MESSAGES: :[[@LINE+1]]:5: warning: suspicious usage of 'sizeof' in the loop [bugprone-sizeof-expression]
192+
for(int j = 0; j < sizeof(b.a); j++) {
193+
194+
}
195+
return 2;
196+
}
197+
167198
template <int T>
168199
int Foo() { int A[T]; return sizeof(T); }
169200
// CHECK-MESSAGES: :[[@LINE-1]]:30: warning: suspicious usage of 'sizeof(K)'

0 commit comments

Comments
 (0)