-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[clang-tidy] support different precisions #130540
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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)`.
|
@llvm/pr-subscribers-clang-tidy Author: Tommy Chen (dl8sd11) ChangesSupport float and long double versions of the math functions for UseStdNumbersCheck. Fixes: #130325 Full diff: https://github.com/llvm/llvm-project/pull/130540.diff 3 Files Affected:
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 a0cef3abb275f..ff5a2ebe6878f 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -136,6 +136,10 @@ Changes in existing checks
<clang-tidy/checks/modernize/use-ranges>` check by updating suppress
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.
+
- Improved :doc:`misc-use-internal-linkage
<clang-tidy/checks/misc/use-internal-linkage>` check by fix false positives
for function or variable in header file which contains macro expansion.
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]
|
|
@llvm/pr-subscribers-clang-tools-extra Author: Tommy Chen (dl8sd11) ChangesSupport float and long double versions of the math functions for UseStdNumbersCheck. Fixes: #130325 Full diff: https://github.com/llvm/llvm-project/pull/130540.diff 3 Files Affected:
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 a0cef3abb275f..ff5a2ebe6878f 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -136,6 +136,10 @@ Changes in existing checks
<clang-tidy/checks/modernize/use-ranges>` check by updating suppress
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.
+
- Improved :doc:`misc-use-internal-linkage
<clang-tidy/checks/misc/use-internal-linkage>` check by fix false positives
for function or variable in header file which contains macro expansion.
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]
|
PiotrZSL
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
vbvictor
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
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)andexpl(1).Fixes: #130325