Skip to content

Commit b7677bb

Browse files
author
MalavikaSamak
committed
Add documentation for the newly introduced CheckOption for loop.
1 parent 52e4413 commit b7677bb

File tree

4 files changed

+15
-7
lines changed

4 files changed

+15
-7
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ SizeofExpressionCheck::SizeofExpressionCheck(StringRef Name,
7373
WarnOnSizeOfPointer(Options.get("WarnOnSizeOfPointer", false)),
7474
WarnOnOffsetDividedBySizeOf(
7575
Options.get("WarnOnOffsetDividedBySizeOf", true)),
76-
WarnOnLoopExprSizeOf(Options.get("WarnOnLoopExprSizeOf", true)) {}
76+
WarnOnSizeOfInLoopTermination(Options.get("WarnOnSizeOfInLoopTermination", true)) {}
7777

7878
void SizeofExpressionCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
7979
Options.store(Opts, "WarnOnSizeOfConstant", WarnOnSizeOfConstant);
@@ -87,7 +87,7 @@ void SizeofExpressionCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
8787
Options.store(Opts, "WarnOnSizeOfPointer", WarnOnSizeOfPointer);
8888
Options.store(Opts, "WarnOnOffsetDividedBySizeOf",
8989
WarnOnOffsetDividedBySizeOf);
90-
Options.store(Opts, "WarnOnLoopExprSizeOf", WarnOnLoopExprSizeOf);
90+
Options.store(Opts, "WarnOnSizeOfInLoopTermination", WarnOnSizeOfInLoopTermination);
9191
}
9292

9393
void SizeofExpressionCheck::registerMatchers(MatchFinder *Finder) {
@@ -137,9 +137,9 @@ void SizeofExpressionCheck::registerMatchers(MatchFinder *Finder) {
137137
this);
138138
}
139139

140-
if (WarnOnLoopExprSizeOf) {
140+
if (WarnOnSizeOfInLoopTermination) {
141141
Finder->addMatcher(
142-
LoopExpr(has(binaryOperator(has(SizeOfExpr)))).bind("loop-expr"), this);
142+
LoopExpr(has(binaryOperator(has(SizeOfExpr.bind("loop-expr"))))), this);
143143
}
144144

145145
// Detect sizeof(kPtr) where kPtr is 'const char* kPtr = "abc"';

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class SizeofExpressionCheck : public ClangTidyCheck {
3232
const bool WarnOnSizeOfPointerToAggregate;
3333
const bool WarnOnSizeOfPointer;
3434
const bool WarnOnOffsetDividedBySizeOf;
35-
const bool WarnOnLoopExprSizeOf;
35+
const bool WarnOnSizeOfInLoopTermination;
3636
};
3737

3838
} // namespace clang::tidy::bugprone

clang-tools-extra/docs/clang-tidy/checks/bugprone/sizeof-expression.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,11 @@ Options
316316
When `true`, the check will warn on pointer arithmetic where the
317317
element count is obtained from a division with ``sizeof(...)``,
318318
e.g., ``Ptr + Bytes / sizeof(*T)``. Default is `true`.
319+
320+
.. option:: WarnOnSizeOfInLoopTermination
321+
322+
When `true`, the check will warn about incorrect use of sizeof expression
323+
in loop termination condition. The warning triggers if the sizeof expression
324+
appears to be incorrectly used to determine the number of array/buffer elements.
325+
e.g, ``long arr[10]; for(int i = 0; i < sizeof(arr); i++) { ... }. Default
326+
is `true`.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ struct B {
174174

175175
int square(int num, struct B b) {
176176
struct A arr[10];
177-
// CHECK-MESSAGES: :[[@LINE+1]]:5: warning: suspicious usage of 'sizeof' in the loop [bugprone-sizeof-expression]
177+
// CHECK-MESSAGES: :[[@LINE+1]]:24: warning: suspicious usage of 'sizeof' in the loop [bugprone-sizeof-expression]
178178
for(int i = 0; i < sizeof(arr); i++) {
179179
struct A a = arr[i];
180180
}
@@ -188,7 +188,7 @@ int square(int num, struct B b) {
188188
struct A a = arr[i];
189189
}
190190

191-
// CHECK-MESSAGES: :[[@LINE+1]]:5: warning: suspicious usage of 'sizeof' in the loop [bugprone-sizeof-expression]
191+
// CHECK-MESSAGES: :[[@LINE+1]]:24: warning: suspicious usage of 'sizeof' in the loop [bugprone-sizeof-expression]
192192
for(int j = 0; j < sizeof(b.a); j++) {
193193

194194
}

0 commit comments

Comments
 (0)