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

via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 11 10:05:05 PDT 2025


Author: Tommy Chen
Date: 2025-03-11T18:05:01+01:00
New Revision: d22d14375d4410cdb441e04016531962e3abb44e

URL: https://github.com/llvm/llvm-project/commit/d22d14375d4410cdb441e04016531962e3abb44e
DIFF: https://github.com/llvm/llvm-project/commit/d22d14375d4410cdb441e04016531962e3abb44e.diff

LOG: [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

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp
    clang-tools-extra/docs/ReleaseNotes.rst
    clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-numbers.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp
index 1548fc454cfb3..38ef7712aa4ef 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp
@@ -91,8 +91,11 @@ struct MatchBuilder {
 
   auto matchMathCall(const StringRef FunctionName,
                      const Matcher<clang::Expr> ArgumentMatcher) const {
+    auto HasAnyPrecisionName = hasAnyName(
+        FunctionName, (FunctionName + "l").str(),
+        (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 0f9da5ec5ee83..8f8e6e3c0165d 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -142,6 +142,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 
diff erent 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, 
diff ers 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, 
diff ers 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]


        


More information about the cfe-commits mailing list