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 <shouryagoel10...@gmail.com> Date: Fri, 9 Feb 2024 01:21:14 +0530 Subject: [PATCH 01/10] 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 8ba8b893e03a6f..8f6547f4ea9e65 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 e50914aed5f07a..17b1349d20db8d 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 0f45cc2fe11463..9ee23be45cfea0 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 3229deb93bada8..113150b168650b 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 <shouryagoel10...@gmail.com> Date: Fri, 9 Feb 2024 01:21:39 +0530 Subject: [PATCH 02/10] 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 8f6547f4ea9e65..a73d46f01d9b2d 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 <shouryagoel10...@gmail.com> Date: Fri, 9 Feb 2024 11:49:51 +0530 Subject: [PATCH 03/10] 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 17b1349d20db8d..42bcabdcf5692a 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 9ee23be45cfea0..9a2e4f6ddd46d1 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 <shouryagoel10...@gmail.com> Date: Fri, 9 Feb 2024 11:51:47 +0530 Subject: [PATCH 04/10] 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 42bcabdcf5692a..d8b26d8b195fb1 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 <shouryagoel10...@gmail.com> Date: Fri, 9 Feb 2024 16:24:11 +0530 Subject: [PATCH 05/10] 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 a73d46f01d9b2d..c9d99ff7b54a65 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 <shouryagoel10...@gmail.com> Date: Fri, 9 Feb 2024 16:25:30 +0530 Subject: [PATCH 06/10] 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 c9d99ff7b54a65..ba92b4500e2f55 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 <shouryagoel10...@gmail.com> Date: Fri, 9 Feb 2024 16:26:45 +0530 Subject: [PATCH 07/10] 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 ba92b4500e2f55..720c01edfd54dd 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 <shouryagoel10...@gmail.com> Date: Fri, 9 Feb 2024 16:27:07 +0530 Subject: [PATCH 08/10] 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 720c01edfd54dd..031c9ebc53a74c 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 >From 73bac387e93944f77b58c47b9b60a96cda4f9e55 Mon Sep 17 00:00:00 2001 From: Sh0g0-1758 <shouryagoel10...@gmail.com> Date: Fri, 9 Feb 2024 16:43:04 +0530 Subject: [PATCH 09/10] Revert --- .../bugprone/TooSmallLoopVariableCheck.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp index 031c9ebc53a74c..a73d46f01d9b2d 100644 --- a/clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp @@ -82,14 +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(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 >From 8b783e64d7ebe29c829351a63b997a95f0ae058c Mon Sep 17 00:00:00 2001 From: Sh0g0-1758 <shouryagoel10...@gmail.com> Date: Fri, 9 Feb 2024 19:57:28 +0530 Subject: [PATCH 10/10] Rearranged doc in alphabetical order --- clang-tools-extra/docs/ReleaseNotes.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index d8b26d8b195fb1..093f1fd61ddc90 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>` check by correctly + implementing the check for const loop variable initialized with a function declaration. + - Cleaned up :doc:`cppcoreguidelines-prefer-member-initializer <clang-tidy/checks/cppcoreguidelines/prefer-member-initializer>` by removing enforcement of rule `C.48 @@ -156,10 +160,6 @@ 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 variable initialized with a function declaration. - Removed checks ^^^^^^^^^^^^^^ _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits