aboutsummaryrefslogtreecommitdiff
path: root/clang-tools-extra
diff options
context:
space:
mode:
authorShourya Goel <shouryagoel10000@gmail.com>2024-02-09 22:14:04 +0530
committerGitHub <noreply@github.com>2024-02-09 17:44:04 +0100
commita7520d9727d2638047e5c464b2937581f64e2ce5 (patch)
treef1405717e2c0ff32e9f034162b9316daf67582aa /clang-tools-extra
parent4bf9fa5fb50497878edf8e277574ea9fb7d6bb7f (diff)
downloadllvm-a7520d9727d2638047e5c464b2937581f64e2ce5.zip
llvm-a7520d9727d2638047e5c464b2937581f64e2ce5.tar.gz
llvm-a7520d9727d2638047e5c464b2937581f64e2ce5.tar.bz2
[Clang-tidy] bugprone-too-small-loop-variable - false-negative when const variable is used as loop bound (#81183)
Changed LibASTMatcher to give an appropriate warning when a const loop bound is initialized with a function declaration. Fixes: #79580
Diffstat (limited to 'clang-tools-extra')
-rw-r--r--clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp12
-rw-r--r--clang-tools-extra/docs/ReleaseNotes.rst4
-rw-r--r--clang-tools-extra/docs/clang-tidy/checks/bugprone/too-small-loop-variable.rst4
-rw-r--r--clang-tools-extra/test/clang-tidy/checkers/bugprone/too-small-loop-variable.cpp12
4 files changed, 28 insertions, 4 deletions
diff --git a/clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp
index 8ba8b89..a73d46f 100644
--- a/clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp
@@ -82,10 +82,14 @@ void TooSmallLoopVariableCheck::registerMatchers(MatchFinder *Finder) {
// We are interested in only those cases when the loop bound is a variable
// value (not const, enum, etc.).
StatementMatcher LoopBoundMatcher =
- expr(ignoringParenImpCasts(allOf(hasType(isInteger()),
- unless(integerLiteral()),
- unless(hasType(isConstQualified())),
- unless(hasType(enumType())))))
+ expr(ignoringParenImpCasts(allOf(
+ hasType(isInteger()), unless(integerLiteral()),
+ unless(allOf(
+ hasType(isConstQualified()),
+ declRefExpr(to(varDecl(anyOf(
+ hasInitializer(ignoringParenImpCasts(integerLiteral())),
+ isConstexpr(), isConstinit())))))),
+ unless(hasType(enumType())))))
.bind(LoopUpperBoundName);
// We use the loop increment expression only to make sure we found the right
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index e50914a..dff8dd2 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -117,6 +117,10 @@ Changes in existing checks
options `HeaderFileExtensions` and `ImplementationFileExtensions` by the
global options of the same name.
+- Improved :doc:`bugprone-too-small-loop-variable
+ <clang-tidy/checks/bugprone/too-small-loop-variable>` support by correctly
+ implementing the check for const loop boundary.
+
- Cleaned up :doc:`cppcoreguidelines-prefer-member-initializer
<clang-tidy/checks/cppcoreguidelines/prefer-member-initializer>`
by removing enforcement of rule `C.48
diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/too-small-loop-variable.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/too-small-loop-variable.rst
index 0f45cc2..2c3ded9 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/too-small-loop-variable.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/too-small-loop-variable.rst
@@ -28,6 +28,10 @@ In a real use case size means a container's size which depends on the user input
This algorithm works for a small amount of objects, but will lead to freeze for
a larger user input.
+It's recommended to enable the compiler warning
+`-Wtautological-constant-out-of-range-compare` as well, since check does not
+inspect compile-time constant loop boundaries to avoid overlaps with the warning.
+
.. option:: MagnitudeBitsUpperLimit
Upper limit for the magnitude bits of the loop variable. If it's set the check
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/too-small-loop-variable.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/too-small-loop-variable.cpp
index 3229deb..113150b 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/too-small-loop-variable.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/too-small-loop-variable.cpp
@@ -93,6 +93,18 @@ void voidBadForLoopWithMacroBound() {
}
}
+unsigned int getVal() {
+ return 300;
+}
+
+// The iteration's upper bound has a function declaration.
+void voidBadForLoop8() {
+ const unsigned int l = getVal();
+ for (unsigned char i = 0; i < l; ++i) {
+ // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: loop variable has narrower type 'unsigned char' than iteration's upper bound 'const unsigned int' [bugprone-too-small-loop-variable]
+ }
+}
+
////////////////////////////////////////////////////////////////////////////////
/// Correct loops: we should not warn here.