[clang-tools-extra] FIX : bugprone-too-small-loop-variable - false-negative when const variable is used as loop bound (PR #81183)

Shourya Goel via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 9 03:02:18 PST 2024


https://github.com/Sh0g0-1758 updated https://github.com/llvm/llvm-project/pull/81183

>From 88dac6713284ee4f0b7ce73c944f78085412645f Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Fri, 9 Feb 2024 01:21:14 +0530
Subject: [PATCH 1/8] Fix : bugprone-too-small-loop-variable

---
 .../bugprone/TooSmallLoopVariableCheck.cpp           | 12 ++++++------
 clang-tools-extra/docs/ReleaseNotes.rst              |  3 +++
 .../checks/bugprone/too-small-loop-variable.rst      |  2 ++
 .../checkers/bugprone/too-small-loop-variable.cpp    | 12 ++++++++++++
 4 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp
index 8ba8b893e03a6..8f6547f4ea9e6 100644
--- a/clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp
@@ -81,12 +81,12 @@ 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())))))
-          .bind(LoopUpperBoundName);
+ StatementMatcher LoopBoundMatcher =
+       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
   // loop variable.
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index e50914aed5f07..17b1349d20db8 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -169,6 +169,9 @@ Miscellaneous
   option is specified. Now ``clang-apply-replacements`` applies formatting only with
   the option.
 
+- Fixed incorrect implementation of ``too-small-loop-variable`` check when a const loop
+  variable is initialized with a function declaration.
+
 Improvements to include-fixer
 -----------------------------
 
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 0f45cc2fe1146..9ee23be45cfea 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
@@ -44,3 +44,5 @@ a larger user input.
     for (unsigned i = 0; i < size; ++i) {} // no warning with MagnitudeBitsUpperLimit = 31 on a system where unsigned is 32-bit
     for (int i = 0; i < size; ++i) {} // warning with MagnitudeBitsUpperLimit = 31 on a system where int is 32-bit
   }
+
+``-Wtautological-constant-out-of-range-compare`` compiler warning should also be used.
\ No newline at end of file
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 3229deb93bada..113150b168650 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.
 

>From 41acf22b481ba6800c1d07e96d1d3ba8052b20a7 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Fri, 9 Feb 2024 01:21:39 +0530
Subject: [PATCH 2/8] Ran git clang Formatter

---
 .../bugprone/TooSmallLoopVariableCheck.cpp       | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp
index 8f6547f4ea9e6..a73d46f01d9b2 100644
--- a/clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp
@@ -81,12 +81,16 @@ 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(allOf(hasType(isConstQualified()), declRefExpr(to(varDecl(anyOf(hasInitializer(ignoringParenImpCasts(integerLiteral())), isConstexpr(), isConstinit())))))),
-                                        unless(hasType(enumType())))))
-           .bind(LoopUpperBoundName);
+  StatementMatcher LoopBoundMatcher =
+      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
   // loop variable.

>From 1a603c40aed5e7352bea68602c418215e2e80351 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Fri, 9 Feb 2024 11:49:51 +0530
Subject: [PATCH 3/8] Changed Documentation

---
 clang-tools-extra/docs/ReleaseNotes.rst                    | 7 ++++---
 .../clang-tidy/checks/bugprone/too-small-loop-variable.rst | 4 ++--
 2 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 17b1349d20db8..42bcabdcf5692 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -156,6 +156,10 @@ Changes in existing checks
   `AllowStringArrays` option, enabling the exclusion of array types with deduced
   length initialized from string literals.
 
+- Improved :doc:`bugprone-too-small-loop-variable
+  <clang-tidy/checks/bugprone/too-small-loop-variable>` check by correctly 
+  implementing the check for const loop variables with a function declaration.
+
 Removed checks
 ^^^^^^^^^^^^^^
 
@@ -169,9 +173,6 @@ Miscellaneous
   option is specified. Now ``clang-apply-replacements`` applies formatting only with
   the option.
 
-- Fixed incorrect implementation of ``too-small-loop-variable`` check when a const loop
-  variable is initialized with a function declaration.
-
 Improvements to include-fixer
 -----------------------------
 
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 9ee23be45cfea..9a2e4f6ddd46d 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,8 @@ 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
@@ -44,5 +46,3 @@ a larger user input.
     for (unsigned i = 0; i < size; ++i) {} // no warning with MagnitudeBitsUpperLimit = 31 on a system where unsigned is 32-bit
     for (int i = 0; i < size; ++i) {} // warning with MagnitudeBitsUpperLimit = 31 on a system where int is 32-bit
   }
-
-``-Wtautological-constant-out-of-range-compare`` compiler warning should also be used.
\ No newline at end of file

>From 49f11c1d941769a20f2bb7b33d853939a224289f Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Fri, 9 Feb 2024 11:51:47 +0530
Subject: [PATCH 4/8] Changed Doc

---
 clang-tools-extra/docs/ReleaseNotes.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 42bcabdcf5692..d8b26d8b195fb 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -158,7 +158,7 @@ Changes in existing checks
 
 - Improved :doc:`bugprone-too-small-loop-variable
   <clang-tidy/checks/bugprone/too-small-loop-variable>` check by correctly 
-  implementing the check for const loop variables with a function declaration.
+  implementing the check for const loop variable initialized with a function declaration.
 
 Removed checks
 ^^^^^^^^^^^^^^

>From 3c8e3f0e12185645c0509fe338797bde15744c70 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Fri, 9 Feb 2024 16:24:11 +0530
Subject: [PATCH 5/8] reduced duplications

---
 .../clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp    | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp
index a73d46f01d9b2..c9d99ff7b54a6 100644
--- a/clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp
@@ -84,12 +84,9 @@ void TooSmallLoopVariableCheck::registerMatchers(MatchFinder *Finder) {
   StatementMatcher LoopBoundMatcher =
       expr(ignoringParenImpCasts(allOf(
                hasType(isInteger()), unless(integerLiteral()),
-               unless(allOf(
-                   hasType(isConstQualified()),
-                   declRefExpr(to(varDecl(anyOf(
-                       hasInitializer(ignoringParenImpCasts(integerLiteral())),
-                       isConstexpr(), isConstinit())))))),
-               unless(hasType(enumType())))))
+unless(declRefExpr(to(enumConstantDecl()))),
+unless(allOf(hasType(isConstQualified()),
+                    declRefExpr(to(varDecl(anyOf(hasInitializer(ignoringParenImpCasts(declRefExpr(to(enumConstantDecl())))), isConstexpr(), isConstinit())))))
           .bind(LoopUpperBoundName);
 
   // We use the loop increment expression only to make sure we found the right

>From 656f2b482a4994653bf4a935111d1222ac18bec7 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Fri, 9 Feb 2024 16:25:30 +0530
Subject: [PATCH 6/8] formatter

---
 .../clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp         | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp
index c9d99ff7b54a6..ba92b4500e2f5 100644
--- a/clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp
@@ -84,8 +84,8 @@ void TooSmallLoopVariableCheck::registerMatchers(MatchFinder *Finder) {
   StatementMatcher LoopBoundMatcher =
       expr(ignoringParenImpCasts(allOf(
                hasType(isInteger()), unless(integerLiteral()),
-unless(declRefExpr(to(enumConstantDecl()))),
-unless(allOf(hasType(isConstQualified()),
+              unless(declRefExpr(to(enumConstantDecl()))),
+              unless(allOf(hasType(isConstQualified()),
                     declRefExpr(to(varDecl(anyOf(hasInitializer(ignoringParenImpCasts(declRefExpr(to(enumConstantDecl())))), isConstexpr(), isConstinit())))))
           .bind(LoopUpperBoundName);
 

>From 7c3caa3a906ea1e061c0c2218b5dad0281db43f9 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Fri, 9 Feb 2024 16:26:45 +0530
Subject: [PATCH 7/8] Colon addition

---
 .../clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp           | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp
index ba92b4500e2f5..720c01edfd54d 100644
--- a/clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp
@@ -86,7 +86,7 @@ void TooSmallLoopVariableCheck::registerMatchers(MatchFinder *Finder) {
                hasType(isInteger()), unless(integerLiteral()),
               unless(declRefExpr(to(enumConstantDecl()))),
               unless(allOf(hasType(isConstQualified()),
-                    declRefExpr(to(varDecl(anyOf(hasInitializer(ignoringParenImpCasts(declRefExpr(to(enumConstantDecl())))), isConstexpr(), isConstinit())))))
+                    declRefExpr(to(varDecl(anyOf(hasInitializer(ignoringParenImpCasts(declRefExpr(to(enumConstantDecl())))), isConstexpr(), isConstinit())))))))))
           .bind(LoopUpperBoundName);
 
   // We use the loop increment expression only to make sure we found the right

>From c8f6a406702a1e72664241c9e58dde85a305ed73 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Fri, 9 Feb 2024 16:27:07 +0530
Subject: [PATCH 8/8] Ran git clang Formatter

---
 .../bugprone/TooSmallLoopVariableCheck.cpp          | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp
index 720c01edfd54d..031c9ebc53a74 100644
--- a/clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp
@@ -82,11 +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(declRefExpr(to(enumConstantDecl()))),
-              unless(allOf(hasType(isConstQualified()),
-                    declRefExpr(to(varDecl(anyOf(hasInitializer(ignoringParenImpCasts(declRefExpr(to(enumConstantDecl())))), isConstexpr(), isConstinit())))))))))
+      expr(ignoringParenImpCasts(
+               allOf(hasType(isInteger()), unless(integerLiteral()),
+                     unless(declRefExpr(to(enumConstantDecl()))),
+                     unless(allOf(hasType(isConstQualified()),
+                                  declRefExpr(to(varDecl(anyOf(
+                                      hasInitializer(ignoringParenImpCasts(
+                                          declRefExpr(to(enumConstantDecl())))),
+                                      isConstexpr(), isConstinit())))))))))
           .bind(LoopUpperBoundName);
 
   // We use the loop increment expression only to make sure we found the right



More information about the cfe-commits mailing list