Skip to content

Commit d22d143

Browse files
authored
[clang-tidy] support different precisions (#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
1 parent 517c677 commit d22d143

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,11 @@ struct MatchBuilder {
9191

9292
auto matchMathCall(const StringRef FunctionName,
9393
const Matcher<clang::Expr> ArgumentMatcher) const {
94+
auto HasAnyPrecisionName = hasAnyName(
95+
FunctionName, (FunctionName + "l").str(),
96+
(FunctionName + "f").str()); // Support long double(l) and float(f).
9497
return expr(ignoreParenAndFloatingCasting(
95-
callExpr(callee(functionDecl(hasName(FunctionName),
98+
callExpr(callee(functionDecl(HasAnyPrecisionName,
9699
hasParameter(0, hasType(isArithmetic())))),
97100
hasArgument(0, ArgumentMatcher))));
98101
}

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ Changes in existing checks
142142
<clang-tidy/checks/modernize/use-ranges>` check by updating suppress
143143
warnings logic for ``nullptr`` in ``std::find``.
144144

145+
- Improved :doc:`modernize-use-std-numbers
146+
<clang-tidy/checks/modernize/use-std-numbers>` check to support math
147+
functions of different precisions.
148+
145149
- Improved :doc:`misc-use-internal-linkage
146150
<clang-tidy/checks/misc/use-internal-linkage>` check by fix false positives
147151
for function or variable in header file which contains macro expansion.

clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-numbers.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,17 @@ namespace bar {
99
template <typename T>
1010
auto sqrt(T val) { return sqrt(static_cast<double>(val)); }
1111

12+
float sqrtf(float Arg);
13+
long double sqrtl(long double Arg);
14+
1215
static constexpr double e = 2.718281828459045235360287471352662497757247093;
1316
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:33: warning: prefer 'std::numbers::e' to this literal, differs by '0.00e+00' [modernize-use-std-numbers]
1417
// CHECK-FIXES-ALL: static constexpr double e = std::numbers::e;
1518
}
1619

20+
float expf(float Arg);
1721
double exp(double Arg);
22+
long double expl(long double Arg);
1823
double log(double Arg);
1924

2025
double log2(double Arg);
@@ -110,6 +115,14 @@ void foo(){
110115
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::sqrt2_v<float>' to this formula [modernize-use-std-numbers]
111116
// CHECK-FIXES-ALL: std::numbers::sqrt2_v<float>;
112117

118+
bar::sqrtf(2.0F);
119+
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::sqrt2_v<float>' to this formula [modernize-use-std-numbers]
120+
// CHECK-FIXES-ALL: std::numbers::sqrt2_v<float>;
121+
122+
bar::sqrtl(2.0);
123+
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::sqrt2_v<long double>' to this formula [modernize-use-std-numbers]
124+
// CHECK-FIXES-ALL: std::numbers::sqrt2_v<long double>;
125+
113126
sink(MY_PI);
114127
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:10: warning: prefer 'std::numbers::pi' to this macro, differs by '5.36e-08' [modernize-use-std-numbers]
115128
// CHECK-FIXES-ALL: sink(std::numbers::pi);
@@ -155,6 +168,14 @@ void foo(){
155168
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::e' to this formula [modernize-use-std-numbers]
156169
// CHECK-FIXES-ALL: std::numbers::e;
157170

171+
expf(1);
172+
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::e_v<float>' to this formula [modernize-use-std-numbers]
173+
// CHECK-FIXES-ALL: std::numbers::e_v<float>;
174+
175+
expl(1);
176+
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::e_v<long double>' to this formula [modernize-use-std-numbers]
177+
// CHECK-FIXES-ALL: std::numbers::e_v<long double>;
178+
158179
log2(exp(1));
159180
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::log2e' to this formula [modernize-use-std-numbers]
160181
// CHECK-MESSAGES-ALL: :[[@LINE-2]]:10: warning: prefer 'std::numbers::e' to this formula [modernize-use-std-numbers]

0 commit comments

Comments
 (0)