felix642 created this revision. Herald added subscribers: PiotrZSL, carlosgalvezp, xazax.hun. Herald added a reviewer: njames93. Herald added a project: All. felix642 requested review of this revision. Herald added a project: clang-tools-extra. Herald added a subscriber: cfe-commits.
The following code is safe and should not trigger the warning constexpr std::size_t k1Mb = 1024 * 1024; Fixes #64732 Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D158338 Files: clang-tools-extra/clang-tidy/bugprone/ImplicitWideningOfMultiplicationResultCheck.cpp clang-tools-extra/docs/ReleaseNotes.rst clang-tools-extra/test/clang-tidy/checkers/bugprone/implicit-widening-of-multiplication-result-int.cpp Index: clang-tools-extra/test/clang-tidy/checkers/bugprone/implicit-widening-of-multiplication-result-int.cpp =================================================================== --- clang-tools-extra/test/clang-tidy/checkers/bugprone/implicit-widening-of-multiplication-result-int.cpp +++ clang-tools-extra/test/clang-tidy/checkers/bugprone/implicit-widening-of-multiplication-result-int.cpp @@ -107,10 +107,28 @@ long n14(int a, int b, int c) { return a + b * c; } + long n15(int a, int b, int c) { return a * b + c; } +unsigned long n16() +{ + return (1024u) * 1024; +} + +long n17(int a) { + return a + 1024 * 1024; +} + +long n18(int a) +{ + return (a * 1024); + // CHECK-NOTES-ALL: :[[@LINE-1]]:11: warning: performing an implicit widening conversion to type 'long' of a multiplication performed in type 'int' + // CHECK-NOTES-ALL: :[[@LINE-2]]:11: note: make conversion explicit to silence this warning + // CHECK-NOTES-ALL: :[[@LINE-3]]:11: note: perform multiplication in a wider type +} + #ifdef __cplusplus template <typename T1, typename T2> T2 template_test(T1 a, T1 b) { Index: clang-tools-extra/docs/ReleaseNotes.rst =================================================================== --- clang-tools-extra/docs/ReleaseNotes.rst +++ clang-tools-extra/docs/ReleaseNotes.rst @@ -173,6 +173,10 @@ <clang-tidy/checks/bugprone/reserved-identifier>`, so that it does not warn on macros starting with underscore and lowercase letter. +- Improved :doc:`bugprone-implicit-widening-of-multiplication-result + <clang-tidy/checks/bugprone/bugprone-implicit-widening-of-multiplication-result>` + check to ignore false-positives with integer literals. + - Improved :doc:`bugprone-lambda-function-name <clang-tidy/checks/bugprone/lambda-function-name>` check by adding option `IgnoreMacros` to ignore warnings in macros. Index: clang-tools-extra/clang-tidy/bugprone/ImplicitWideningOfMultiplicationResultCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/bugprone/ImplicitWideningOfMultiplicationResultCheck.cpp +++ clang-tools-extra/clang-tidy/bugprone/ImplicitWideningOfMultiplicationResultCheck.cpp @@ -34,6 +34,13 @@ return BO->getLHS()->IgnoreParens(); } +static bool hasIntegerLiteralOperators(const Expr* E) +{ + const auto *BO = dyn_cast<BinaryOperator>(E); + return isa<IntegerLiteral>(BO->getLHS()->IgnoreParenImpCasts()) && + isa<IntegerLiteral>(BO->getRHS()->IgnoreParenImpCasts()); +} + ImplicitWideningOfMultiplicationResultCheck:: ImplicitWideningOfMultiplicationResultCheck(StringRef Name, ClangTidyContext *Context) @@ -89,6 +96,10 @@ if (!LHS) return; + // Widening multiplication on Integer Literals. + if(hasIntegerLiteralOperators(E)) + return; + // Ok, looks like we should diagnose this. diag(E->getBeginLoc(), "performing an implicit widening conversion to type " "%0 of a multiplication performed in type %1")
Index: clang-tools-extra/test/clang-tidy/checkers/bugprone/implicit-widening-of-multiplication-result-int.cpp =================================================================== --- clang-tools-extra/test/clang-tidy/checkers/bugprone/implicit-widening-of-multiplication-result-int.cpp +++ clang-tools-extra/test/clang-tidy/checkers/bugprone/implicit-widening-of-multiplication-result-int.cpp @@ -107,10 +107,28 @@ long n14(int a, int b, int c) { return a + b * c; } + long n15(int a, int b, int c) { return a * b + c; } +unsigned long n16() +{ + return (1024u) * 1024; +} + +long n17(int a) { + return a + 1024 * 1024; +} + +long n18(int a) +{ + return (a * 1024); + // CHECK-NOTES-ALL: :[[@LINE-1]]:11: warning: performing an implicit widening conversion to type 'long' of a multiplication performed in type 'int' + // CHECK-NOTES-ALL: :[[@LINE-2]]:11: note: make conversion explicit to silence this warning + // CHECK-NOTES-ALL: :[[@LINE-3]]:11: note: perform multiplication in a wider type +} + #ifdef __cplusplus template <typename T1, typename T2> T2 template_test(T1 a, T1 b) { Index: clang-tools-extra/docs/ReleaseNotes.rst =================================================================== --- clang-tools-extra/docs/ReleaseNotes.rst +++ clang-tools-extra/docs/ReleaseNotes.rst @@ -173,6 +173,10 @@ <clang-tidy/checks/bugprone/reserved-identifier>`, so that it does not warn on macros starting with underscore and lowercase letter. +- Improved :doc:`bugprone-implicit-widening-of-multiplication-result + <clang-tidy/checks/bugprone/bugprone-implicit-widening-of-multiplication-result>` + check to ignore false-positives with integer literals. + - Improved :doc:`bugprone-lambda-function-name <clang-tidy/checks/bugprone/lambda-function-name>` check by adding option `IgnoreMacros` to ignore warnings in macros. Index: clang-tools-extra/clang-tidy/bugprone/ImplicitWideningOfMultiplicationResultCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/bugprone/ImplicitWideningOfMultiplicationResultCheck.cpp +++ clang-tools-extra/clang-tidy/bugprone/ImplicitWideningOfMultiplicationResultCheck.cpp @@ -34,6 +34,13 @@ return BO->getLHS()->IgnoreParens(); } +static bool hasIntegerLiteralOperators(const Expr* E) +{ + const auto *BO = dyn_cast<BinaryOperator>(E); + return isa<IntegerLiteral>(BO->getLHS()->IgnoreParenImpCasts()) && + isa<IntegerLiteral>(BO->getRHS()->IgnoreParenImpCasts()); +} + ImplicitWideningOfMultiplicationResultCheck:: ImplicitWideningOfMultiplicationResultCheck(StringRef Name, ClangTidyContext *Context) @@ -89,6 +96,10 @@ if (!LHS) return; + // Widening multiplication on Integer Literals. + if(hasIntegerLiteralOperators(E)) + return; + // Ok, looks like we should diagnose this. diag(E->getBeginLoc(), "performing an implicit widening conversion to type " "%0 of a multiplication performed in type %1")
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits