[clang-tools-extra] [clang-tidy] support different precisions (PR #130540)
Tommy Chen via cfe-commits
cfe-commits at lists.llvm.org
Mon Mar 10 19:18:30 PDT 2025
https://github.com/dl8sd11 updated https://github.com/llvm/llvm-project/pull/130540
>From 092135bbb3536167f0cad11e7320e52886c022cc Mon Sep 17 00:00:00 2001
From: dl8sd11 <gcchen at google.com>
Date: Mon, 10 Mar 2025 02:56:14 +0000
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<clang::Expr> 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
<clang-tidy/checks/modernize/use-std-format>` check to support replacing
member function calls too.
+- Improved :doc:`modernize-use-std-numbers
+ <clang-tidy/checks/modernize/use-std-numbers>` check to support math functions
+ of different precisions.
+
+
- Improved :doc:`misc-unconventional-assign-operator
<clang-tidy/checks/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 <typename T>
auto sqrt(T val) { return sqrt(static_cast<double>(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<float>' to this formula [modernize-use-std-numbers]
// CHECK-FIXES-ALL: std::numbers::sqrt2_v<float>;
+ bar::sqrtf(2.0F);
+ // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::sqrt2_v<float>' to this formula [modernize-use-std-numbers]
+ // CHECK-FIXES-ALL: std::numbers::sqrt2_v<float>;
+
+ bar::sqrtl(2.0);
+ // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::sqrt2_v<long double>' to this formula [modernize-use-std-numbers]
+ // CHECK-FIXES-ALL: std::numbers::sqrt2_v<long double>;
+
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<float>' to this formula [modernize-use-std-numbers]
+ // CHECK-FIXES-ALL: std::numbers::e_v<float>;
+
+ expl(1);
+ // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::e_v<long double>' to this formula [modernize-use-std-numbers]
+ // CHECK-FIXES-ALL: std::numbers::e_v<long double>;
+
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 2001
From: dl8sd11 <gcchen at google.com>
Date: Tue, 11 Mar 2025 02:17:05 +0000
Subject: [PATCH 2/3] [clang-tidy] use hasAnyName
---
.../clang-tidy/modernize/UseStdNumbersCheck.cpp | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp
index 32645e31e8899..38ef7712aa4ef 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp
@@ -91,10 +91,9 @@ struct MatchBuilder {
auto matchMathCall(const StringRef FunctionName,
const Matcher<clang::Expr> ArgumentMatcher) const {
- auto HasAnyPrecisionName =
- anyOf(hasName(FunctionName), hasName((FunctionName + "l").str()),
- hasName((FunctionName + "f")
- .str())); // Support long double(l) and float(f).
+ auto HasAnyPrecisionName = hasAnyName(
+ FunctionName, (FunctionName + "l").str(),
+ (FunctionName + "f").str()); // Support long double(l) and float(f).
return expr(ignoreParenAndFloatingCasting(
callExpr(callee(functionDecl(HasAnyPrecisionName,
hasParameter(0, hasType(isArithmetic())))),
>From 4c04f4aa56454a51835ac554d5f82ad293defcf9 Mon Sep 17 00:00:00 2001
From: dl8sd11 <gcchen at google.com>
Date: Tue, 11 Mar 2025 02:18:09 +0000
Subject: [PATCH 3/3] [clang-tidy] wrapping at 80 chars
---
clang-tools-extra/docs/ReleaseNotes.rst | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index ff5a2ebe6878f..fb018e924d50c 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -137,8 +137,8 @@ Changes in existing checks
warnings logic for ``nullptr`` in ``std::find``.
- Improved :doc:`modernize-use-std-numbers
- <clang-tidy/checks/modernize/use-std-numbers>` check to support math functions
- of different precisions.
+ <clang-tidy/checks/modernize/use-std-numbers>` check to support math
+ functions of different precisions.
- Improved :doc:`misc-use-internal-linkage
<clang-tidy/checks/misc/use-internal-linkage>` check by fix false positives
More information about the cfe-commits
mailing list