Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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))));
}
Expand Down
4 changes: 4 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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]
Expand Down