[clang-tools-extra] [clang-tidy] support different precisions (PR #130540)

via cfe-commits cfe-commits at lists.llvm.org
Sun Mar 9 20:28:57 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-tidy

Author: Tommy Chen (dl8sd11)

<details>
<summary>Changes</summary>

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

---
Full diff: https://github.com/llvm/llvm-project/pull/130540.diff


3 Files Affected:

- (modified) clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp (+5-1) 
- (modified) clang-tools-extra/docs/ReleaseNotes.rst (+4) 
- (modified) clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-numbers.cpp (+21) 


``````````diff
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]

``````````

</details>


https://github.com/llvm/llvm-project/pull/130540


More information about the cfe-commits mailing list