[clang-tools-extra] [clang-tidy] support string::contains (PR #110351)
@@ -32,7 +33,8 @@ void ContainerContainsCheck::registerMatchers(MatchFinder *Finder) { const auto FindCall = cxxMemberCallExpr( - argumentCountIs(1), + anyOf(argumentCountIs(1), +allOf(argumentCountIs(2), hasArgument(1, cxxDefaultArgExpr(, dl8sd11 wrote: I tried to match the two arguments but I fail to remove the second argument in the Fixit hint. Could you suggest how to get the location after the first argument. I tried binding the first argument (e.g. "test"), but the source range seems to be the first character (") instead of the whole argument ("test"). https://github.com/llvm/llvm-project/pull/110351 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] support string::contains (PR #110351)
https://github.com/dl8sd11 updated https://github.com/llvm/llvm-project/pull/110351 >From b98e9a4d50d74c298096d2bd2d70ff4c0ef5c4a4 Mon Sep 17 00:00:00 2001 From: dl8sd11 Date: Sat, 28 Sep 2024 07:37:50 + Subject: [PATCH 1/4] [clang-tidy] support string::contains --- .../readability/ContainerContainsCheck.cpp| 18 +-- .../readability/container-contains.cpp| 49 +++ 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/clang-tools-extra/clang-tidy/readability/ContainerContainsCheck.cpp b/clang-tools-extra/clang-tidy/readability/ContainerContainsCheck.cpp index 05d4c87bc73cef..a5e5960eaa6a3c 100644 --- a/clang-tools-extra/clang-tidy/readability/ContainerContainsCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/ContainerContainsCheck.cpp @@ -9,6 +9,7 @@ #include "ContainerContainsCheck.h" #include "clang/AST/ASTContext.h" #include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" using namespace clang::ast_matchers; @@ -32,7 +33,8 @@ void ContainerContainsCheck::registerMatchers(MatchFinder *Finder) { const auto FindCall = cxxMemberCallExpr( - argumentCountIs(1), + anyOf(argumentCountIs(1), +allOf(argumentCountIs(2), hasArgument(1, cxxDefaultArgExpr(, callee(cxxMethodDecl( hasName("find"), hasParameter(0, hasType(hasUnqualifiedDesugaredType( @@ -51,6 +53,12 @@ void ContainerContainsCheck::registerMatchers(MatchFinder *Finder) { const auto Literal0 = integerLiteral(equals(0)); const auto Literal1 = integerLiteral(equals(1)); + const auto StringLikeClass = cxxRecordDecl( + hasAnyName("::std::basic_string", "::std::basic_string_view", + "::absl::string_view")); + const auto StringNpos = declRefExpr( + to(varDecl(hasName("npos"), hasDeclContext(StringLikeClass; + auto AddSimpleMatcher = [&](auto Matcher) { Finder->addMatcher( traverse(TK_IgnoreUnlessSpelledInSource, std::move(Matcher)), this); @@ -94,12 +102,14 @@ void ContainerContainsCheck::registerMatchers(MatchFinder *Finder) { binaryOperator(hasLHS(Literal1), hasOperatorName(">"), hasRHS(CountCall)) .bind("negativeComparison")); - // Find membership tests based on `find() == end()`. + // Find membership tests based on `find() == end() or find() == npos`. AddSimpleMatcher( - binaryOperator(hasOperatorName("!="), hasOperands(FindCall, EndCall)) + binaryOperator(hasOperatorName("!="), + hasOperands(FindCall, anyOf(EndCall, StringNpos))) .bind("positiveComparison")); AddSimpleMatcher( - binaryOperator(hasOperatorName("=="), hasOperands(FindCall, EndCall)) + binaryOperator(hasOperatorName("=="), + hasOperands(FindCall, anyOf(EndCall, StringNpos))) .bind("negativeComparison")); } diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/container-contains.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/container-contains.cpp index 9a9b233e07229b..69cc5c88479040 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability/container-contains.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/container-contains.cpp @@ -29,6 +29,43 @@ struct multimap { bool contains(const Key &K) const; }; +using size_t = decltype(sizeof(int)); + +// Lightweight standin for std::string_view. +template +class basic_string_view { +public: + basic_string_view(); + basic_string_view(const basic_string_view &); + basic_string_view(const C *); + ~basic_string_view(); + int find(basic_string_view s, int pos = 0); + int find(const C *s, int pos = 0); + int find(const C *s, int pos, int n); + int find(char c, int pos = 0); + static constexpr size_t npos = -1; +}; +typedef basic_string_view string_view; + +// Lightweight standin for std::string. +template +class basic_string { +public: + basic_string(); + basic_string(const basic_string &); + basic_string(const C *); + ~basic_string(); + int find(basic_string s, int pos = 0); + int find(const C *s, int pos = 0); + int find(const C *s, int pos, int n); + int find(char c, int pos = 0); + bool contains(const C *s) const; + bool contains(C s) const; + bool contains(basic_string_view s) const; + static constexpr size_t npos = -1; +}; +typedef basic_string string; + } // namespace std // Check that we detect various common ways to check for membership @@ -453,3 +490,15 @@ void testOperandPermutations(std::map& Map) { // CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use 'contains' to check for membership [readability-container-contains] // CHECK-FIXES: if (!Map.contains(0)) {}; } + +void testStringNops(std::string Str, std::string SubStr) { + if (Str.find("test") == std::string::npos) {}; + // CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use 'contains' to check for membership [readability-contai
[clang-tools-extra] [clang-tidy] support string::contains (PR #110351)
https://github.com/dl8sd11 updated https://github.com/llvm/llvm-project/pull/110351 >From b98e9a4d50d74c298096d2bd2d70ff4c0ef5c4a4 Mon Sep 17 00:00:00 2001 From: dl8sd11 Date: Sat, 28 Sep 2024 07:37:50 + Subject: [PATCH 1/5] [clang-tidy] support string::contains --- .../readability/ContainerContainsCheck.cpp| 18 +-- .../readability/container-contains.cpp| 49 +++ 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/clang-tools-extra/clang-tidy/readability/ContainerContainsCheck.cpp b/clang-tools-extra/clang-tidy/readability/ContainerContainsCheck.cpp index 05d4c87bc73cef..a5e5960eaa6a3c 100644 --- a/clang-tools-extra/clang-tidy/readability/ContainerContainsCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/ContainerContainsCheck.cpp @@ -9,6 +9,7 @@ #include "ContainerContainsCheck.h" #include "clang/AST/ASTContext.h" #include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" using namespace clang::ast_matchers; @@ -32,7 +33,8 @@ void ContainerContainsCheck::registerMatchers(MatchFinder *Finder) { const auto FindCall = cxxMemberCallExpr( - argumentCountIs(1), + anyOf(argumentCountIs(1), +allOf(argumentCountIs(2), hasArgument(1, cxxDefaultArgExpr(, callee(cxxMethodDecl( hasName("find"), hasParameter(0, hasType(hasUnqualifiedDesugaredType( @@ -51,6 +53,12 @@ void ContainerContainsCheck::registerMatchers(MatchFinder *Finder) { const auto Literal0 = integerLiteral(equals(0)); const auto Literal1 = integerLiteral(equals(1)); + const auto StringLikeClass = cxxRecordDecl( + hasAnyName("::std::basic_string", "::std::basic_string_view", + "::absl::string_view")); + const auto StringNpos = declRefExpr( + to(varDecl(hasName("npos"), hasDeclContext(StringLikeClass; + auto AddSimpleMatcher = [&](auto Matcher) { Finder->addMatcher( traverse(TK_IgnoreUnlessSpelledInSource, std::move(Matcher)), this); @@ -94,12 +102,14 @@ void ContainerContainsCheck::registerMatchers(MatchFinder *Finder) { binaryOperator(hasLHS(Literal1), hasOperatorName(">"), hasRHS(CountCall)) .bind("negativeComparison")); - // Find membership tests based on `find() == end()`. + // Find membership tests based on `find() == end() or find() == npos`. AddSimpleMatcher( - binaryOperator(hasOperatorName("!="), hasOperands(FindCall, EndCall)) + binaryOperator(hasOperatorName("!="), + hasOperands(FindCall, anyOf(EndCall, StringNpos))) .bind("positiveComparison")); AddSimpleMatcher( - binaryOperator(hasOperatorName("=="), hasOperands(FindCall, EndCall)) + binaryOperator(hasOperatorName("=="), + hasOperands(FindCall, anyOf(EndCall, StringNpos))) .bind("negativeComparison")); } diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/container-contains.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/container-contains.cpp index 9a9b233e07229b..69cc5c88479040 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability/container-contains.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/container-contains.cpp @@ -29,6 +29,43 @@ struct multimap { bool contains(const Key &K) const; }; +using size_t = decltype(sizeof(int)); + +// Lightweight standin for std::string_view. +template +class basic_string_view { +public: + basic_string_view(); + basic_string_view(const basic_string_view &); + basic_string_view(const C *); + ~basic_string_view(); + int find(basic_string_view s, int pos = 0); + int find(const C *s, int pos = 0); + int find(const C *s, int pos, int n); + int find(char c, int pos = 0); + static constexpr size_t npos = -1; +}; +typedef basic_string_view string_view; + +// Lightweight standin for std::string. +template +class basic_string { +public: + basic_string(); + basic_string(const basic_string &); + basic_string(const C *); + ~basic_string(); + int find(basic_string s, int pos = 0); + int find(const C *s, int pos = 0); + int find(const C *s, int pos, int n); + int find(char c, int pos = 0); + bool contains(const C *s) const; + bool contains(C s) const; + bool contains(basic_string_view s) const; + static constexpr size_t npos = -1; +}; +typedef basic_string string; + } // namespace std // Check that we detect various common ways to check for membership @@ -453,3 +490,15 @@ void testOperandPermutations(std::map& Map) { // CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use 'contains' to check for membership [readability-container-contains] // CHECK-FIXES: if (!Map.contains(0)) {}; } + +void testStringNops(std::string Str, std::string SubStr) { + if (Str.find("test") == std::string::npos) {}; + // CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use 'contains' to check for membership [readability-contai
[clang-tools-extra] [clang-tidy] support string::contains (PR #110351)
@@ -29,6 +29,43 @@ struct multimap { bool contains(const Key &K) const; }; +using size_t = decltype(sizeof(int)); + +// Lightweight standin for std::string_view. +template +class basic_string_view { +public: + basic_string_view(); + basic_string_view(const basic_string_view &); + basic_string_view(const C *); + ~basic_string_view(); + int find(basic_string_view s, int pos = 0); + int find(const C *s, int pos = 0); + int find(const C *s, int pos, int n); + int find(char c, int pos = 0); + static constexpr size_t npos = -1; +}; +typedef basic_string_view string_view; + +// Lightweight standin for std::string. +template +class basic_string { +public: + basic_string(); + basic_string(const basic_string &); + basic_string(const C *); + ~basic_string(); + int find(basic_string s, int pos = 0); + int find(const C *s, int pos = 0); + int find(const C *s, int pos, int n); + int find(char c, int pos = 0); + bool contains(const C *s) const; + bool contains(C s) const; + bool contains(basic_string_view s) const; + static constexpr size_t npos = -1; +}; +typedef basic_string string; + dl8sd11 wrote: The mock header does not contain the "contains" method(s). Could I modify that one in this PR or should I create a separate one? https://github.com/llvm/llvm-project/pull/110351 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] support string::contains (PR #110351)
@@ -94,12 +102,14 @@ void ContainerContainsCheck::registerMatchers(MatchFinder *Finder) { binaryOperator(hasLHS(Literal1), hasOperatorName(">"), hasRHS(CountCall)) .bind("negativeComparison")); - // Find membership tests based on `find() == end()`. + // Find membership tests based on `find() == end() or find() == npos`. dl8sd11 wrote: Thanks for catching. https://github.com/llvm/llvm-project/pull/110351 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] support string::contains (PR #110351)
@@ -453,3 +490,15 @@ void testOperandPermutations(std::map& Map) { // CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use 'contains' to check for membership [readability-container-contains] // CHECK-FIXES: if (!Map.contains(0)) {}; } + +void testStringNops(std::string Str, std::string SubStr) { + if (Str.find("test") == std::string::npos) {}; + // CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use 'contains' to check for membership [readability-container-contains] + // CHECK-FIXES: if (!Str.contains("test")) {}; + + if (Str.find('c') != std::string::npos) {}; + // CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use 'contains' to check for membership [readability-container-contains] + // CHECK-FIXES: if (Str.contains('c')) {}; + + if (Str.find(SubStr) != std::string::npos) {}; +} dl8sd11 wrote: Done. https://github.com/llvm/llvm-project/pull/110351 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] support string::contains (PR #110351)
@@ -51,6 +53,12 @@ void ContainerContainsCheck::registerMatchers(MatchFinder *Finder) { const auto Literal0 = integerLiteral(equals(0)); const auto Literal1 = integerLiteral(equals(1)); + const auto StringLikeClass = cxxRecordDecl( + hasAnyName("::std::basic_string", "::std::basic_string_view", + "::absl::string_view")); dl8sd11 wrote: Agree! https://github.com/llvm/llvm-project/pull/110351 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] exclude CXXParenListInitExpr from RedundantCastingCheck (PR #109741)
https://github.com/dl8sd11 edited https://github.com/llvm/llvm-project/pull/109741 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] exclude CXXParenListInitExpr from RedundantCastingCheck (PR #109741)
https://github.com/dl8sd11 updated https://github.com/llvm/llvm-project/pull/109741 >From 13bc00c2ffb4238903b57c0a3c77424ed35279cc Mon Sep 17 00:00:00 2001 From: dl8sd11 Date: Mon, 23 Sep 2024 17:52:25 + Subject: [PATCH 1/3] [clang-tidy] eclude CXXParenListInitExpr Exclude CXXParenListInitExpr from RedundantCastingCheck because there are false positive cases. Currently, we can't think of positive cases for CXXParenListInitExpr. This can be improve by following the initListExpr method if we can come up with some positive cases. --- .../clang-tidy/readability/RedundantCastingCheck.cpp | 5 + .../checkers/readability/redundant-casting.cpp | 10 +++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp index b9ff0e81cbc522..fb85eb1628afbd 100644 --- a/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp @@ -108,6 +108,10 @@ void RedundantCastingCheck::registerMatchers(MatchFinder *Finder) { auto BitfieldMemberExpr = memberExpr(member(fieldDecl(isBitField(; + const ast_matchers::internal::VariadicDynCastAllOfMatcher< + Stmt, CXXParenListInitExpr> + cxxParenListInitExpr; // NOLINT(readability-identifier-naming) + Finder->addMatcher( explicitCastExpr( unless(hasCastKind(CK_ConstructorConversion)), @@ -117,6 +121,7 @@ void RedundantCastingCheck::registerMatchers(MatchFinder *Finder) { hasDestinationType(qualType().bind("dstType")), hasSourceExpression(anyOf( expr(unless(initListExpr()), unless(BitfieldMemberExpr), + unless(cxxParenListInitExpr()), hasType(qualType().bind("srcType"))) .bind("source"), initListExpr(unless(hasInit(1, expr())), diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-casting.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-casting.cpp index 30cac6bd5cca06..d94452d84a5841 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-casting.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-casting.cpp @@ -1,8 +1,8 @@ -// RUN: %check_clang_tidy -std=c++11-or-later %s readability-redundant-casting %t -- -- -fno-delayed-template-parsing -// RUN: %check_clang_tidy -std=c++11-or-later -check-suffix=,MACROS %s readability-redundant-casting %t -- \ +// RUN: %check_clang_tidy -std=c++20-or-later %s readability-redundant-casting %t -- -- -fno-delayed-template-parsing +// RUN: %check_clang_tidy -std=c++20-or-later -check-suffix=,MACROS %s readability-redundant-casting %t -- \ // RUN: -config='{CheckOptions: { readability-redundant-casting.IgnoreMacros: false }}' \ // RUN: -- -fno-delayed-template-parsing -// RUN: %check_clang_tidy -std=c++11-or-later -check-suffix=,ALIASES %s readability-redundant-casting %t -- \ +// RUN: %check_clang_tidy -std=c++20-or-later -check-suffix=,ALIASES %s readability-redundant-casting %t -- \ // RUN: -config='{CheckOptions: { readability-redundant-casting.IgnoreTypeAliases: true }}' \ // RUN: -- -fno-delayed-template-parsing @@ -57,6 +57,10 @@ void testDiffrentTypesCast(B& value) { A& a7 = static_cast(value); } +void testParenListInitExpr(A value) { + B b = static_cast(value); +} + void testCastingWithAuto() { auto a = getA(); A& a8 = static_cast(a); >From cca8471cab0ceefb9bf1bf5eb40c3f25272ced16 Mon Sep 17 00:00:00 2001 From: dl8sd11 Date: Tue, 24 Sep 2024 17:26:20 + Subject: [PATCH 2/3] [clang-tidy] update ReleaseNotes.rst --- clang-tools-extra/docs/ReleaseNotes.rst | 4 1 file changed, 4 insertions(+) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 8f7b0b5333f3a1..069230461e21b9 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -183,6 +183,10 @@ Changes in existing checks by adding the option `UseUpperCaseLiteralSuffix` to select the case of the literal suffix in fixes. +- Improved :doc:`readability-redundant-casting + ` check to + exclude `CXXParenListInitExpr` for the source expression matcher. + - Improved :doc:`readability-redundant-smartptr-get ` check to remove `->`, when redundant `get()` is removed. >From 09197edae72a6ce76aff713613d28d0a381100ac Mon Sep 17 00:00:00 2001 From: dl8sd11 Date: Tue, 24 Sep 2024 17:28:55 + Subject: [PATCH 3/3] amend! [clang-tidy] eclude CXXParenListInitExpr [clang-tidy] exclude CXXParenListInitExpr Exclude CXXParenListInitExpr from RedundantCastingCheck because there are false positive cases. Currently, we can't think of positive cases for CXXParenListInitExpr. This can be improve by following the initListExpr method if we can come up with some positiv
[clang-tools-extra] [clang-tidy] exclude CXXParenListInitExpr from RedundantCastingCheck (PR #109741)
https://github.com/dl8sd11 updated https://github.com/llvm/llvm-project/pull/109741 >From 13bc00c2ffb4238903b57c0a3c77424ed35279cc Mon Sep 17 00:00:00 2001 From: dl8sd11 Date: Mon, 23 Sep 2024 17:52:25 + Subject: [PATCH 1/5] [clang-tidy] eclude CXXParenListInitExpr Exclude CXXParenListInitExpr from RedundantCastingCheck because there are false positive cases. Currently, we can't think of positive cases for CXXParenListInitExpr. This can be improve by following the initListExpr method if we can come up with some positive cases. --- .../clang-tidy/readability/RedundantCastingCheck.cpp | 5 + .../checkers/readability/redundant-casting.cpp | 10 +++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp index b9ff0e81cbc522..fb85eb1628afbd 100644 --- a/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp @@ -108,6 +108,10 @@ void RedundantCastingCheck::registerMatchers(MatchFinder *Finder) { auto BitfieldMemberExpr = memberExpr(member(fieldDecl(isBitField(; + const ast_matchers::internal::VariadicDynCastAllOfMatcher< + Stmt, CXXParenListInitExpr> + cxxParenListInitExpr; // NOLINT(readability-identifier-naming) + Finder->addMatcher( explicitCastExpr( unless(hasCastKind(CK_ConstructorConversion)), @@ -117,6 +121,7 @@ void RedundantCastingCheck::registerMatchers(MatchFinder *Finder) { hasDestinationType(qualType().bind("dstType")), hasSourceExpression(anyOf( expr(unless(initListExpr()), unless(BitfieldMemberExpr), + unless(cxxParenListInitExpr()), hasType(qualType().bind("srcType"))) .bind("source"), initListExpr(unless(hasInit(1, expr())), diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-casting.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-casting.cpp index 30cac6bd5cca06..d94452d84a5841 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-casting.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-casting.cpp @@ -1,8 +1,8 @@ -// RUN: %check_clang_tidy -std=c++11-or-later %s readability-redundant-casting %t -- -- -fno-delayed-template-parsing -// RUN: %check_clang_tidy -std=c++11-or-later -check-suffix=,MACROS %s readability-redundant-casting %t -- \ +// RUN: %check_clang_tidy -std=c++20-or-later %s readability-redundant-casting %t -- -- -fno-delayed-template-parsing +// RUN: %check_clang_tidy -std=c++20-or-later -check-suffix=,MACROS %s readability-redundant-casting %t -- \ // RUN: -config='{CheckOptions: { readability-redundant-casting.IgnoreMacros: false }}' \ // RUN: -- -fno-delayed-template-parsing -// RUN: %check_clang_tidy -std=c++11-or-later -check-suffix=,ALIASES %s readability-redundant-casting %t -- \ +// RUN: %check_clang_tidy -std=c++20-or-later -check-suffix=,ALIASES %s readability-redundant-casting %t -- \ // RUN: -config='{CheckOptions: { readability-redundant-casting.IgnoreTypeAliases: true }}' \ // RUN: -- -fno-delayed-template-parsing @@ -57,6 +57,10 @@ void testDiffrentTypesCast(B& value) { A& a7 = static_cast(value); } +void testParenListInitExpr(A value) { + B b = static_cast(value); +} + void testCastingWithAuto() { auto a = getA(); A& a8 = static_cast(a); >From cca8471cab0ceefb9bf1bf5eb40c3f25272ced16 Mon Sep 17 00:00:00 2001 From: dl8sd11 Date: Tue, 24 Sep 2024 17:26:20 + Subject: [PATCH 2/5] [clang-tidy] update ReleaseNotes.rst --- clang-tools-extra/docs/ReleaseNotes.rst | 4 1 file changed, 4 insertions(+) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 8f7b0b5333f3a1..069230461e21b9 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -183,6 +183,10 @@ Changes in existing checks by adding the option `UseUpperCaseLiteralSuffix` to select the case of the literal suffix in fixes. +- Improved :doc:`readability-redundant-casting + ` check to + exclude `CXXParenListInitExpr` for the source expression matcher. + - Improved :doc:`readability-redundant-smartptr-get ` check to remove `->`, when redundant `get()` is removed. >From 09197edae72a6ce76aff713613d28d0a381100ac Mon Sep 17 00:00:00 2001 From: dl8sd11 Date: Tue, 24 Sep 2024 17:28:55 + Subject: [PATCH 3/5] amend! [clang-tidy] eclude CXXParenListInitExpr [clang-tidy] exclude CXXParenListInitExpr Exclude CXXParenListInitExpr from RedundantCastingCheck because there are false positive cases. Currently, we can't think of positive cases for CXXParenListInitExpr. This can be improve by following the initListExpr method if we can come up with some positiv
[clang-tools-extra] [clang-tidy] exclude CXXParenListInitExpr from RedundantCastingCheck (PR #109741)
@@ -183,6 +183,10 @@ Changes in existing checks by adding the option `UseUpperCaseLiteralSuffix` to select the case of the literal suffix in fixes. +- Improved :doc:`readability-redundant-casting + ` check to + exclude `CXXParenListInitExpr` for the source expression matcher. dl8sd11 wrote: Thanks for the suggestion. https://github.com/llvm/llvm-project/pull/109741 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] exclude CXXParenListInitExpr from RedundantCastingCheck (PR #109741)
https://github.com/dl8sd11 updated https://github.com/llvm/llvm-project/pull/109741 >From 13bc00c2ffb4238903b57c0a3c77424ed35279cc Mon Sep 17 00:00:00 2001 From: dl8sd11 Date: Mon, 23 Sep 2024 17:52:25 + Subject: [PATCH 1/4] [clang-tidy] eclude CXXParenListInitExpr Exclude CXXParenListInitExpr from RedundantCastingCheck because there are false positive cases. Currently, we can't think of positive cases for CXXParenListInitExpr. This can be improve by following the initListExpr method if we can come up with some positive cases. --- .../clang-tidy/readability/RedundantCastingCheck.cpp | 5 + .../checkers/readability/redundant-casting.cpp | 10 +++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp index b9ff0e81cbc522..fb85eb1628afbd 100644 --- a/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp @@ -108,6 +108,10 @@ void RedundantCastingCheck::registerMatchers(MatchFinder *Finder) { auto BitfieldMemberExpr = memberExpr(member(fieldDecl(isBitField(; + const ast_matchers::internal::VariadicDynCastAllOfMatcher< + Stmt, CXXParenListInitExpr> + cxxParenListInitExpr; // NOLINT(readability-identifier-naming) + Finder->addMatcher( explicitCastExpr( unless(hasCastKind(CK_ConstructorConversion)), @@ -117,6 +121,7 @@ void RedundantCastingCheck::registerMatchers(MatchFinder *Finder) { hasDestinationType(qualType().bind("dstType")), hasSourceExpression(anyOf( expr(unless(initListExpr()), unless(BitfieldMemberExpr), + unless(cxxParenListInitExpr()), hasType(qualType().bind("srcType"))) .bind("source"), initListExpr(unless(hasInit(1, expr())), diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-casting.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-casting.cpp index 30cac6bd5cca06..d94452d84a5841 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-casting.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-casting.cpp @@ -1,8 +1,8 @@ -// RUN: %check_clang_tidy -std=c++11-or-later %s readability-redundant-casting %t -- -- -fno-delayed-template-parsing -// RUN: %check_clang_tidy -std=c++11-or-later -check-suffix=,MACROS %s readability-redundant-casting %t -- \ +// RUN: %check_clang_tidy -std=c++20-or-later %s readability-redundant-casting %t -- -- -fno-delayed-template-parsing +// RUN: %check_clang_tidy -std=c++20-or-later -check-suffix=,MACROS %s readability-redundant-casting %t -- \ // RUN: -config='{CheckOptions: { readability-redundant-casting.IgnoreMacros: false }}' \ // RUN: -- -fno-delayed-template-parsing -// RUN: %check_clang_tidy -std=c++11-or-later -check-suffix=,ALIASES %s readability-redundant-casting %t -- \ +// RUN: %check_clang_tidy -std=c++20-or-later -check-suffix=,ALIASES %s readability-redundant-casting %t -- \ // RUN: -config='{CheckOptions: { readability-redundant-casting.IgnoreTypeAliases: true }}' \ // RUN: -- -fno-delayed-template-parsing @@ -57,6 +57,10 @@ void testDiffrentTypesCast(B& value) { A& a7 = static_cast(value); } +void testParenListInitExpr(A value) { + B b = static_cast(value); +} + void testCastingWithAuto() { auto a = getA(); A& a8 = static_cast(a); >From cca8471cab0ceefb9bf1bf5eb40c3f25272ced16 Mon Sep 17 00:00:00 2001 From: dl8sd11 Date: Tue, 24 Sep 2024 17:26:20 + Subject: [PATCH 2/4] [clang-tidy] update ReleaseNotes.rst --- clang-tools-extra/docs/ReleaseNotes.rst | 4 1 file changed, 4 insertions(+) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 8f7b0b5333f3a1..069230461e21b9 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -183,6 +183,10 @@ Changes in existing checks by adding the option `UseUpperCaseLiteralSuffix` to select the case of the literal suffix in fixes. +- Improved :doc:`readability-redundant-casting + ` check to + exclude `CXXParenListInitExpr` for the source expression matcher. + - Improved :doc:`readability-redundant-smartptr-get ` check to remove `->`, when redundant `get()` is removed. >From 09197edae72a6ce76aff713613d28d0a381100ac Mon Sep 17 00:00:00 2001 From: dl8sd11 Date: Tue, 24 Sep 2024 17:28:55 + Subject: [PATCH 3/4] amend! [clang-tidy] eclude CXXParenListInitExpr [clang-tidy] exclude CXXParenListInitExpr Exclude CXXParenListInitExpr from RedundantCastingCheck because there are false positive cases. Currently, we can't think of positive cases for CXXParenListInitExpr. This can be improve by following the initListExpr method if we can come up with some positiv
[clang-tools-extra] [clang-tidy] exclude CXXParenListInitExpr from RedundantCastingCheck (PR #109741)
@@ -183,6 +183,10 @@ Changes in existing checks by adding the option `UseUpperCaseLiteralSuffix` to select the case of the literal suffix in fixes. +- Improved :doc:`readability-redundant-casting + ` check to + exclude `CXXParenListInitExpr` for the source expression matcher. dl8sd11 wrote: Thanks, I agree it should be transparent to the user. https://github.com/llvm/llvm-project/pull/109741 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] support string::contains (PR #110351)
https://github.com/dl8sd11 created https://github.com/llvm/llvm-project/pull/110351 Starting from c++23, we can replace `std::string::find() == std::string::npos` with `std::string.contains()` . #109327 Currently, this is WIP because there are two limitations: 1. False positive: SubStr type is `const std::string&` which does not match `std::string::contains(basic_string_view)` type. ``` std::string SubStr; if (Str.find(SubStr) != std::string::npos) {}; ``` 2. Currently, the fixit for `std::string::find("test", 0)` is incorrect. >From b98e9a4d50d74c298096d2bd2d70ff4c0ef5c4a4 Mon Sep 17 00:00:00 2001 From: dl8sd11 Date: Sat, 28 Sep 2024 07:37:50 + Subject: [PATCH] [clang-tidy] support string::contains --- .../readability/ContainerContainsCheck.cpp| 18 +-- .../readability/container-contains.cpp| 49 +++ 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/clang-tools-extra/clang-tidy/readability/ContainerContainsCheck.cpp b/clang-tools-extra/clang-tidy/readability/ContainerContainsCheck.cpp index 05d4c87bc73cef..a5e5960eaa6a3c 100644 --- a/clang-tools-extra/clang-tidy/readability/ContainerContainsCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/ContainerContainsCheck.cpp @@ -9,6 +9,7 @@ #include "ContainerContainsCheck.h" #include "clang/AST/ASTContext.h" #include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" using namespace clang::ast_matchers; @@ -32,7 +33,8 @@ void ContainerContainsCheck::registerMatchers(MatchFinder *Finder) { const auto FindCall = cxxMemberCallExpr( - argumentCountIs(1), + anyOf(argumentCountIs(1), +allOf(argumentCountIs(2), hasArgument(1, cxxDefaultArgExpr(, callee(cxxMethodDecl( hasName("find"), hasParameter(0, hasType(hasUnqualifiedDesugaredType( @@ -51,6 +53,12 @@ void ContainerContainsCheck::registerMatchers(MatchFinder *Finder) { const auto Literal0 = integerLiteral(equals(0)); const auto Literal1 = integerLiteral(equals(1)); + const auto StringLikeClass = cxxRecordDecl( + hasAnyName("::std::basic_string", "::std::basic_string_view", + "::absl::string_view")); + const auto StringNpos = declRefExpr( + to(varDecl(hasName("npos"), hasDeclContext(StringLikeClass; + auto AddSimpleMatcher = [&](auto Matcher) { Finder->addMatcher( traverse(TK_IgnoreUnlessSpelledInSource, std::move(Matcher)), this); @@ -94,12 +102,14 @@ void ContainerContainsCheck::registerMatchers(MatchFinder *Finder) { binaryOperator(hasLHS(Literal1), hasOperatorName(">"), hasRHS(CountCall)) .bind("negativeComparison")); - // Find membership tests based on `find() == end()`. + // Find membership tests based on `find() == end() or find() == npos`. AddSimpleMatcher( - binaryOperator(hasOperatorName("!="), hasOperands(FindCall, EndCall)) + binaryOperator(hasOperatorName("!="), + hasOperands(FindCall, anyOf(EndCall, StringNpos))) .bind("positiveComparison")); AddSimpleMatcher( - binaryOperator(hasOperatorName("=="), hasOperands(FindCall, EndCall)) + binaryOperator(hasOperatorName("=="), + hasOperands(FindCall, anyOf(EndCall, StringNpos))) .bind("negativeComparison")); } diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/container-contains.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/container-contains.cpp index 9a9b233e07229b..69cc5c88479040 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability/container-contains.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/container-contains.cpp @@ -29,6 +29,43 @@ struct multimap { bool contains(const Key &K) const; }; +using size_t = decltype(sizeof(int)); + +// Lightweight standin for std::string_view. +template +class basic_string_view { +public: + basic_string_view(); + basic_string_view(const basic_string_view &); + basic_string_view(const C *); + ~basic_string_view(); + int find(basic_string_view s, int pos = 0); + int find(const C *s, int pos = 0); + int find(const C *s, int pos, int n); + int find(char c, int pos = 0); + static constexpr size_t npos = -1; +}; +typedef basic_string_view string_view; + +// Lightweight standin for std::string. +template +class basic_string { +public: + basic_string(); + basic_string(const basic_string &); + basic_string(const C *); + ~basic_string(); + int find(basic_string s, int pos = 0); + int find(const C *s, int pos = 0); + int find(const C *s, int pos, int n); + int find(char c, int pos = 0); + bool contains(const C *s) const; + bool contains(C s) const; + bool contains(basic_string_view s) const; + static constexpr size_t npos = -1; +}; +typedef basic_string string; + } // namespace std // Check that we detect various common ways to check for membership @@ -453,3 +490,
[clang-tools-extra] [clang-tidy] exclude CXXParenListInitExpr from RedundantCastingCheck (PR #109741)
https://github.com/dl8sd11 updated https://github.com/llvm/llvm-project/pull/109741 >From 13bc00c2ffb4238903b57c0a3c77424ed35279cc Mon Sep 17 00:00:00 2001 From: dl8sd11 Date: Mon, 23 Sep 2024 17:52:25 + Subject: [PATCH 1/6] [clang-tidy] eclude CXXParenListInitExpr Exclude CXXParenListInitExpr from RedundantCastingCheck because there are false positive cases. Currently, we can't think of positive cases for CXXParenListInitExpr. This can be improve by following the initListExpr method if we can come up with some positive cases. --- .../clang-tidy/readability/RedundantCastingCheck.cpp | 5 + .../checkers/readability/redundant-casting.cpp | 10 +++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp index b9ff0e81cbc522..fb85eb1628afbd 100644 --- a/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp @@ -108,6 +108,10 @@ void RedundantCastingCheck::registerMatchers(MatchFinder *Finder) { auto BitfieldMemberExpr = memberExpr(member(fieldDecl(isBitField(; + const ast_matchers::internal::VariadicDynCastAllOfMatcher< + Stmt, CXXParenListInitExpr> + cxxParenListInitExpr; // NOLINT(readability-identifier-naming) + Finder->addMatcher( explicitCastExpr( unless(hasCastKind(CK_ConstructorConversion)), @@ -117,6 +121,7 @@ void RedundantCastingCheck::registerMatchers(MatchFinder *Finder) { hasDestinationType(qualType().bind("dstType")), hasSourceExpression(anyOf( expr(unless(initListExpr()), unless(BitfieldMemberExpr), + unless(cxxParenListInitExpr()), hasType(qualType().bind("srcType"))) .bind("source"), initListExpr(unless(hasInit(1, expr())), diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-casting.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-casting.cpp index 30cac6bd5cca06..d94452d84a5841 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-casting.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-casting.cpp @@ -1,8 +1,8 @@ -// RUN: %check_clang_tidy -std=c++11-or-later %s readability-redundant-casting %t -- -- -fno-delayed-template-parsing -// RUN: %check_clang_tidy -std=c++11-or-later -check-suffix=,MACROS %s readability-redundant-casting %t -- \ +// RUN: %check_clang_tidy -std=c++20-or-later %s readability-redundant-casting %t -- -- -fno-delayed-template-parsing +// RUN: %check_clang_tidy -std=c++20-or-later -check-suffix=,MACROS %s readability-redundant-casting %t -- \ // RUN: -config='{CheckOptions: { readability-redundant-casting.IgnoreMacros: false }}' \ // RUN: -- -fno-delayed-template-parsing -// RUN: %check_clang_tidy -std=c++11-or-later -check-suffix=,ALIASES %s readability-redundant-casting %t -- \ +// RUN: %check_clang_tidy -std=c++20-or-later -check-suffix=,ALIASES %s readability-redundant-casting %t -- \ // RUN: -config='{CheckOptions: { readability-redundant-casting.IgnoreTypeAliases: true }}' \ // RUN: -- -fno-delayed-template-parsing @@ -57,6 +57,10 @@ void testDiffrentTypesCast(B& value) { A& a7 = static_cast(value); } +void testParenListInitExpr(A value) { + B b = static_cast(value); +} + void testCastingWithAuto() { auto a = getA(); A& a8 = static_cast(a); >From cca8471cab0ceefb9bf1bf5eb40c3f25272ced16 Mon Sep 17 00:00:00 2001 From: dl8sd11 Date: Tue, 24 Sep 2024 17:26:20 + Subject: [PATCH 2/6] [clang-tidy] update ReleaseNotes.rst --- clang-tools-extra/docs/ReleaseNotes.rst | 4 1 file changed, 4 insertions(+) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 8f7b0b5333f3a1..069230461e21b9 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -183,6 +183,10 @@ Changes in existing checks by adding the option `UseUpperCaseLiteralSuffix` to select the case of the literal suffix in fixes. +- Improved :doc:`readability-redundant-casting + ` check to + exclude `CXXParenListInitExpr` for the source expression matcher. + - Improved :doc:`readability-redundant-smartptr-get ` check to remove `->`, when redundant `get()` is removed. >From 09197edae72a6ce76aff713613d28d0a381100ac Mon Sep 17 00:00:00 2001 From: dl8sd11 Date: Tue, 24 Sep 2024 17:28:55 + Subject: [PATCH 3/6] amend! [clang-tidy] eclude CXXParenListInitExpr [clang-tidy] exclude CXXParenListInitExpr Exclude CXXParenListInitExpr from RedundantCastingCheck because there are false positive cases. Currently, we can't think of positive cases for CXXParenListInitExpr. This can be improve by following the initListExpr method if we can come up with some positiv
[clang-tools-extra] [clang-tidy] exclude CXXParenListInitExpr from RedundantCastingCheck (PR #109741)
@@ -1,8 +1,8 @@ -// RUN: %check_clang_tidy -std=c++11-or-later %s readability-redundant-casting %t -- -- -fno-delayed-template-parsing -// RUN: %check_clang_tidy -std=c++11-or-later -check-suffix=,MACROS %s readability-redundant-casting %t -- \ +// RUN: %check_clang_tidy -std=c++20-or-later %s readability-redundant-casting %t -- -- -fno-delayed-template-parsing +// RUN: %check_clang_tidy -std=c++20-or-later -check-suffix=,MACROS %s readability-redundant-casting %t -- \ // RUN: -config='{CheckOptions: { readability-redundant-casting.IgnoreMacros: false }}' \ // RUN: -- -fno-delayed-template-parsing -// RUN: %check_clang_tidy -std=c++11-or-later -check-suffix=,ALIASES %s readability-redundant-casting %t -- \ +// RUN: %check_clang_tidy -std=c++20-or-later -check-suffix=,ALIASES %s readability-redundant-casting %t -- \ dl8sd11 wrote: Currently, there is only one test on C++20. I decide to add the `ifdef` as suggested by @felix642 in the [issue](https://github.com/llvm/llvm-project/issues/108846#issuecomment-2361389241). https://github.com/llvm/llvm-project/pull/109741 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] support different precisions (PR #130540)
https://github.com/dl8sd11 updated https://github.com/llvm/llvm-project/pull/130540 >From 092135bbb3536167f0cad11e7320e52886c022cc Mon Sep 17 00:00:00 2001 From: dl8sd11 Date: Mon, 10 Mar 2025 02:56:14 + Subject: [PATCH 1/3] [clang-tidy] support different precisions Support float and long double versions of the math functions for UseStdNumbersCheck. For example, after this commit the check is able to catch `sqrtf(2)` and `expl(1)`. --- .../modernize/UseStdNumbersCheck.cpp | 6 +- clang-tools-extra/docs/ReleaseNotes.rst | 5 + .../checkers/modernize/use-std-numbers.cpp| 21 +++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp index 1548fc454cfb3..32645e31e8899 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp @@ -91,8 +91,12 @@ struct MatchBuilder { auto matchMathCall(const StringRef FunctionName, const Matcher ArgumentMatcher) const { +auto HasAnyPrecisionName = +anyOf(hasName(FunctionName), hasName((FunctionName + "l").str()), + hasName((FunctionName + "f") + .str())); // Support long double(l) and float(f). return expr(ignoreParenAndFloatingCasting( -callExpr(callee(functionDecl(hasName(FunctionName), +callExpr(callee(functionDecl(HasAnyPrecisionName, hasParameter(0, hasType(isArithmetic(), hasArgument(0, ArgumentMatcher; } diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 7d37a4b03222c..2f7ff2ec41f4b 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -165,6 +165,11 @@ Changes in existing checks ` check to support replacing member function calls too. +- Improved :doc:`modernize-use-std-numbers + ` check to support math functions + of different precisions. + + - Improved :doc:`misc-unconventional-assign-operator ` check to avoid false positive for C++23 deducing this. diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-numbers.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-numbers.cpp index 6c5762da5e2e8..11121ae6d8e93 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-numbers.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-numbers.cpp @@ -9,12 +9,17 @@ namespace bar { template auto sqrt(T val) { return sqrt(static_cast(val)); } +float sqrtf(float Arg); +long double sqrtl(long double Arg); + static constexpr double e = 2.718281828459045235360287471352662497757247093; // CHECK-MESSAGES-ALL: :[[@LINE-1]]:33: warning: prefer 'std::numbers::e' to this literal, differs by '0.00e+00' [modernize-use-std-numbers] // CHECK-FIXES-ALL: static constexpr double e = std::numbers::e; } +float expf(float Arg); double exp(double Arg); +long double expl(long double Arg); double log(double Arg); double log2(double Arg); @@ -110,6 +115,14 @@ void foo(){ // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::sqrt2_v' to this formula [modernize-use-std-numbers] // CHECK-FIXES-ALL: std::numbers::sqrt2_v; +bar::sqrtf(2.0F); +// CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::sqrt2_v' to this formula [modernize-use-std-numbers] +// CHECK-FIXES-ALL: std::numbers::sqrt2_v; + +bar::sqrtl(2.0); +// CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::sqrt2_v' to this formula [modernize-use-std-numbers] +// CHECK-FIXES-ALL: std::numbers::sqrt2_v; + sink(MY_PI); // CHECK-MESSAGES-ALL: :[[@LINE-1]]:10: warning: prefer 'std::numbers::pi' to this macro, differs by '5.36e-08' [modernize-use-std-numbers] // CHECK-FIXES-ALL: sink(std::numbers::pi); @@ -155,6 +168,14 @@ void foo(){ // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::e' to this formula [modernize-use-std-numbers] // CHECK-FIXES-ALL: std::numbers::e; +expf(1); +// CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::e_v' to this formula [modernize-use-std-numbers] +// CHECK-FIXES-ALL: std::numbers::e_v; + +expl(1); +// CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::e_v' to this formula [modernize-use-std-numbers] +// CHECK-FIXES-ALL: std::numbers::e_v; + log2(exp(1)); // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::log2e' to this formula [modernize-use-std-numbers] // CHECK-MESSAGES-ALL: :[[@LINE-2]]:10: warning: prefer 'std::numbers::e' to this formula [modernize-use-std-numbers] >From 9797852645f5e0d58de20139887e9edbcd06fe44 Mon Sep 17 00:00:00
[clang-tools-extra] [clang-tidy] support different precisions (PR #130540)
@@ -91,8 +91,12 @@ struct MatchBuilder { auto matchMathCall(const StringRef FunctionName, const Matcher ArgumentMatcher) const { +auto HasAnyPrecisionName = +anyOf(hasName(FunctionName), hasName((FunctionName + "l").str()), dl8sd11 wrote: Thanks for pointing out the two functions! https://github.com/llvm/llvm-project/pull/130540 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] support different precisions (PR #130540)
https://github.com/dl8sd11 created https://github.com/llvm/llvm-project/pull/130540 Support float and long double versions of the math functions for UseStdNumbersCheck. For example, after this commit the check is able to catch `sqrtf(2)` and `expl(1)`. Fixes: #130325 >From 092135bbb3536167f0cad11e7320e52886c022cc Mon Sep 17 00:00:00 2001 From: dl8sd11 Date: Mon, 10 Mar 2025 02:56:14 + Subject: [PATCH] [clang-tidy] support different precisions Support float and long double versions of the math functions for UseStdNumbersCheck. For example, after this commit the check is able to catch `sqrtf(2)` and `expl(1)`. --- .../modernize/UseStdNumbersCheck.cpp | 6 +- clang-tools-extra/docs/ReleaseNotes.rst | 5 + .../checkers/modernize/use-std-numbers.cpp| 21 +++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp index 1548fc454cfb3..32645e31e8899 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp @@ -91,8 +91,12 @@ struct MatchBuilder { auto matchMathCall(const StringRef FunctionName, const Matcher ArgumentMatcher) const { +auto HasAnyPrecisionName = +anyOf(hasName(FunctionName), hasName((FunctionName + "l").str()), + hasName((FunctionName + "f") + .str())); // Support long double(l) and float(f). return expr(ignoreParenAndFloatingCasting( -callExpr(callee(functionDecl(hasName(FunctionName), +callExpr(callee(functionDecl(HasAnyPrecisionName, hasParameter(0, hasType(isArithmetic(), hasArgument(0, ArgumentMatcher; } diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 7d37a4b03222c..2f7ff2ec41f4b 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -165,6 +165,11 @@ Changes in existing checks ` check to support replacing member function calls too. +- Improved :doc:`modernize-use-std-numbers + ` check to support math functions + of different precisions. + + - Improved :doc:`misc-unconventional-assign-operator ` check to avoid false positive for C++23 deducing this. diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-numbers.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-numbers.cpp index 6c5762da5e2e8..11121ae6d8e93 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-numbers.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-numbers.cpp @@ -9,12 +9,17 @@ namespace bar { template auto sqrt(T val) { return sqrt(static_cast(val)); } +float sqrtf(float Arg); +long double sqrtl(long double Arg); + static constexpr double e = 2.718281828459045235360287471352662497757247093; // CHECK-MESSAGES-ALL: :[[@LINE-1]]:33: warning: prefer 'std::numbers::e' to this literal, differs by '0.00e+00' [modernize-use-std-numbers] // CHECK-FIXES-ALL: static constexpr double e = std::numbers::e; } +float expf(float Arg); double exp(double Arg); +long double expl(long double Arg); double log(double Arg); double log2(double Arg); @@ -110,6 +115,14 @@ void foo(){ // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::sqrt2_v' to this formula [modernize-use-std-numbers] // CHECK-FIXES-ALL: std::numbers::sqrt2_v; +bar::sqrtf(2.0F); +// CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::sqrt2_v' to this formula [modernize-use-std-numbers] +// CHECK-FIXES-ALL: std::numbers::sqrt2_v; + +bar::sqrtl(2.0); +// CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::sqrt2_v' to this formula [modernize-use-std-numbers] +// CHECK-FIXES-ALL: std::numbers::sqrt2_v; + sink(MY_PI); // CHECK-MESSAGES-ALL: :[[@LINE-1]]:10: warning: prefer 'std::numbers::pi' to this macro, differs by '5.36e-08' [modernize-use-std-numbers] // CHECK-FIXES-ALL: sink(std::numbers::pi); @@ -155,6 +168,14 @@ void foo(){ // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::e' to this formula [modernize-use-std-numbers] // CHECK-FIXES-ALL: std::numbers::e; +expf(1); +// CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::e_v' to this formula [modernize-use-std-numbers] +// CHECK-FIXES-ALL: std::numbers::e_v; + +expl(1); +// CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::e_v' to this formula [modernize-use-std-numbers] +// CHECK-FIXES-ALL: std::numbers::e_v; + log2(exp(1)); // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::log2e' to this formula [modernize-use-std-numbers] /
[clang-tools-extra] [clang-tidy] support different precisions (PR #130540)
https://github.com/dl8sd11 updated https://github.com/llvm/llvm-project/pull/130540 >From 092135bbb3536167f0cad11e7320e52886c022cc Mon Sep 17 00:00:00 2001 From: dl8sd11 Date: Mon, 10 Mar 2025 02:56:14 + Subject: [PATCH] [clang-tidy] support different precisions Support float and long double versions of the math functions for UseStdNumbersCheck. For example, after this commit the check is able to catch `sqrtf(2)` and `expl(1)`. --- .../modernize/UseStdNumbersCheck.cpp | 6 +- clang-tools-extra/docs/ReleaseNotes.rst | 5 + .../checkers/modernize/use-std-numbers.cpp| 21 +++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp index 1548fc454cfb3..32645e31e8899 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp @@ -91,8 +91,12 @@ struct MatchBuilder { auto matchMathCall(const StringRef FunctionName, const Matcher ArgumentMatcher) const { +auto HasAnyPrecisionName = +anyOf(hasName(FunctionName), hasName((FunctionName + "l").str()), + hasName((FunctionName + "f") + .str())); // Support long double(l) and float(f). return expr(ignoreParenAndFloatingCasting( -callExpr(callee(functionDecl(hasName(FunctionName), +callExpr(callee(functionDecl(HasAnyPrecisionName, hasParameter(0, hasType(isArithmetic(), hasArgument(0, ArgumentMatcher; } diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 7d37a4b03222c..2f7ff2ec41f4b 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -165,6 +165,11 @@ Changes in existing checks ` check to support replacing member function calls too. +- Improved :doc:`modernize-use-std-numbers + ` check to support math functions + of different precisions. + + - Improved :doc:`misc-unconventional-assign-operator ` check to avoid false positive for C++23 deducing this. diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-numbers.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-numbers.cpp index 6c5762da5e2e8..11121ae6d8e93 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-numbers.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-numbers.cpp @@ -9,12 +9,17 @@ namespace bar { template auto sqrt(T val) { return sqrt(static_cast(val)); } +float sqrtf(float Arg); +long double sqrtl(long double Arg); + static constexpr double e = 2.718281828459045235360287471352662497757247093; // CHECK-MESSAGES-ALL: :[[@LINE-1]]:33: warning: prefer 'std::numbers::e' to this literal, differs by '0.00e+00' [modernize-use-std-numbers] // CHECK-FIXES-ALL: static constexpr double e = std::numbers::e; } +float expf(float Arg); double exp(double Arg); +long double expl(long double Arg); double log(double Arg); double log2(double Arg); @@ -110,6 +115,14 @@ void foo(){ // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::sqrt2_v' to this formula [modernize-use-std-numbers] // CHECK-FIXES-ALL: std::numbers::sqrt2_v; +bar::sqrtf(2.0F); +// CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::sqrt2_v' to this formula [modernize-use-std-numbers] +// CHECK-FIXES-ALL: std::numbers::sqrt2_v; + +bar::sqrtl(2.0); +// CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::sqrt2_v' to this formula [modernize-use-std-numbers] +// CHECK-FIXES-ALL: std::numbers::sqrt2_v; + sink(MY_PI); // CHECK-MESSAGES-ALL: :[[@LINE-1]]:10: warning: prefer 'std::numbers::pi' to this macro, differs by '5.36e-08' [modernize-use-std-numbers] // CHECK-FIXES-ALL: sink(std::numbers::pi); @@ -155,6 +168,14 @@ void foo(){ // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::e' to this formula [modernize-use-std-numbers] // CHECK-FIXES-ALL: std::numbers::e; +expf(1); +// CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::e_v' to this formula [modernize-use-std-numbers] +// CHECK-FIXES-ALL: std::numbers::e_v; + +expl(1); +// CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::e_v' to this formula [modernize-use-std-numbers] +// CHECK-FIXES-ALL: std::numbers::e_v; + log2(exp(1)); // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::log2e' to this formula [modernize-use-std-numbers] // CHECK-MESSAGES-ALL: :[[@LINE-2]]:10: warning: prefer 'std::numbers::e' to this formula [modernize-use-std-numbers] ___ cfe-commits mailing li
[clang-tools-extra] [clang-tidy] support different precisions (PR #130540)
https://github.com/dl8sd11 ready_for_review https://github.com/llvm/llvm-project/pull/130540 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] support different precisions (PR #130540)
@@ -136,6 +136,10 @@ Changes in existing checks ` check by updating suppress warnings logic for ``nullptr`` in ``std::find``. +- Improved :doc:`modernize-use-std-numbers + ` check to support math functions dl8sd11 wrote: Done. https://github.com/llvm/llvm-project/pull/130540 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits