https://github.com/brenfwd updated https://github.com/llvm/llvm-project/pull/156763
>From 1082254d96f3c427f4cb867fcf964c19655e1876 Mon Sep 17 00:00:00 2001 From: Brenden Forward <bforw...@google.com> Date: Tue, 2 Sep 2025 18:16:46 -0700 Subject: [PATCH 1/7] Add google-runtime-float Clang-Tidy check --- .../clang-tidy/google/CMakeLists.txt | 1 + .../clang-tidy/google/FloatTypesCheck.cpp | 80 +++++++++++++++++++ .../clang-tidy/google/FloatTypesCheck.h | 44 ++++++++++ .../clang-tidy/google/GoogleTidyModule.cpp | 3 + clang-tools-extra/docs/ReleaseNotes.rst | 6 ++ .../checks/google/runtime-float.rst | 11 +++ .../docs/clang-tidy/checks/list.rst | 3 +- .../checkers/google/runtime-float.cpp | 39 +++++++++ 8 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 clang-tools-extra/clang-tidy/google/FloatTypesCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/google/FloatTypesCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/google/runtime-float.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/google/runtime-float.cpp diff --git a/clang-tools-extra/clang-tidy/google/CMakeLists.txt b/clang-tools-extra/clang-tidy/google/CMakeLists.txt index 2470c089ef7ca..1d4229ebb7b09 100644 --- a/clang-tools-extra/clang-tidy/google/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/google/CMakeLists.txt @@ -11,6 +11,7 @@ add_clang_library(clangTidyGoogleModule STATIC DefaultArgumentsCheck.cpp ExplicitConstructorCheck.cpp ExplicitMakePairCheck.cpp + FloatTypesCheck.cpp FunctionNamingCheck.cpp GlobalNamesInHeadersCheck.cpp GlobalVariableDeclarationCheck.cpp diff --git a/clang-tools-extra/clang-tidy/google/FloatTypesCheck.cpp b/clang-tools-extra/clang-tidy/google/FloatTypesCheck.cpp new file mode 100644 index 0000000000000..b8afe803230d3 --- /dev/null +++ b/clang-tools-extra/clang-tidy/google/FloatTypesCheck.cpp @@ -0,0 +1,80 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "FloatTypesCheck.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/Lex/Lexer.h" + +namespace clang { + +using namespace ast_matchers; + +namespace { + +AST_MATCHER(TypeLoc, isTypeLocValidAndNotInMacro) { + const SourceLocation Loc = Node.getBeginLoc(); + return Loc.isValid() && !Loc.isMacroID(); +} + +AST_MATCHER(FloatingLiteral, isFLValidAndNotInMacro) { + const SourceLocation Loc = Node.getBeginLoc(); + return Loc.isValid() && !Loc.isMacroID(); +} + +AST_MATCHER(TypeLoc, isLongDoubleType) { + TypeLoc TL = Node; + if (auto QualLoc = Node.getAs<QualifiedTypeLoc>()) + TL = QualLoc.getUnqualifiedLoc(); + + const auto BuiltinLoc = TL.getAs<BuiltinTypeLoc>(); + if (!BuiltinLoc) + return false; + + return BuiltinLoc.getTypePtr()->getKind() == BuiltinType::LongDouble; +} + +AST_MATCHER(FloatingLiteral, isLongDoubleLiteral) { + if (auto *BT = dyn_cast<BuiltinType>(Node.getType().getTypePtr())) + return BT->getKind() == BuiltinType::LongDouble; + return false; +} + +} // namespace + +namespace tidy::google::runtime { + +void RuntimeFloatCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher(typeLoc(loc(realFloatingPointType()), + isTypeLocValidAndNotInMacro(), isLongDoubleType()) + .bind("longDoubleTypeLoc"), + this); + Finder->addMatcher( + floatLiteral(isFLValidAndNotInMacro(), isLongDoubleLiteral()) + .bind("longDoubleFloatLiteral"), + this); + IdentTable = std::make_unique<IdentifierTable>(getLangOpts()); +} + +void RuntimeFloatCheck::check(const MatchFinder::MatchResult &Result) { + if (const auto *TL = Result.Nodes.getNodeAs<TypeLoc>("longDoubleTypeLoc")) { + diag(TL->getBeginLoc(), + "consider replacing %0 with a 64-bit or 128-bit float type") + << TL->getType(); + } + + if (const auto *FL = + Result.Nodes.getNodeAs<FloatingLiteral>("longDoubleFloatLiteral")) { + diag(FL->getBeginLoc(), + "%0 type from literal suffix 'L' should not be used") + << FL->getType(); + } +} + +} // namespace tidy::google::runtime + +} // namespace clang diff --git a/clang-tools-extra/clang-tidy/google/FloatTypesCheck.h b/clang-tools-extra/clang-tidy/google/FloatTypesCheck.h new file mode 100644 index 0000000000000..54327b6d7e53b --- /dev/null +++ b/clang-tools-extra/clang-tidy/google/FloatTypesCheck.h @@ -0,0 +1,44 @@ + +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_FLOATTYPESCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_FLOATTYPESCHECK_H + +#include "../ClangTidyCheck.h" + +namespace clang { + +class IdentifierTable; + +namespace tidy::google::runtime { + +/// Finds usages of `long double` and suggests replacing them with other +/// floating-point types. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/google/runtime-float.html +class RuntimeFloatCheck : public ClangTidyCheck { +public: + RuntimeFloatCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { + return LangOpts.CPlusPlus && !LangOpts.ObjC; + } + +private: + std::unique_ptr<IdentifierTable> IdentTable; +}; + +} // namespace tidy::google::runtime + +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_FLOATTYPESCHECK_H diff --git a/clang-tools-extra/clang-tidy/google/GoogleTidyModule.cpp b/clang-tools-extra/clang-tidy/google/GoogleTidyModule.cpp index 5343e2b3a5975..eb5666be62bcf 100644 --- a/clang-tools-extra/clang-tidy/google/GoogleTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/google/GoogleTidyModule.cpp @@ -19,6 +19,7 @@ #include "DefaultArgumentsCheck.h" #include "ExplicitConstructorCheck.h" #include "ExplicitMakePairCheck.h" +#include "FloatTypesCheck.h" #include "FunctionNamingCheck.h" #include "GlobalNamesInHeadersCheck.h" #include "GlobalVariableDeclarationCheck.h" @@ -57,6 +58,8 @@ class GoogleModule : public ClangTidyModule { "google-objc-function-naming"); CheckFactories.registerCheck<objc::GlobalVariableDeclarationCheck>( "google-objc-global-variable-declaration"); + CheckFactories.registerCheck<runtime::RuntimeFloatCheck>( + "google-runtime-float"); CheckFactories.registerCheck<runtime::IntegerTypesCheck>( "google-runtime-int"); CheckFactories.registerCheck<runtime::OverloadedUnaryAndCheck>( diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 32e4dfb8aa329..af62797e9ea91 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -142,6 +142,12 @@ New checks Finds calls to ``operator[]`` in STL containers and suggests replacing them with safe alternatives. +- New :doc:`google-runtime-float + <clang-tidy/checks/google/runtime-float>` check. + + Checks for and warns of uses of the ``long double`` type, which is + problematic since it produces non-portable code. + - New :doc:`llvm-mlir-op-builder <clang-tidy/checks/llvm/use-new-mlir-op-builder>` check. diff --git a/clang-tools-extra/docs/clang-tidy/checks/google/runtime-float.rst b/clang-tools-extra/docs/clang-tidy/checks/google/runtime-float.rst new file mode 100644 index 0000000000000..5a5f9a4e6ae0f --- /dev/null +++ b/clang-tools-extra/docs/clang-tidy/checks/google/runtime-float.rst @@ -0,0 +1,11 @@ +.. title:: clang-tidy - google-runtime-float + +google-runtime-float +==================== + +Finds uses of ``long double`` and suggests replacing them with 64-bit +or 128-bit floating-point types. + +The corresponding style guide rule: +https://google.github.io/styleguide/cppguide.html#Floating-Point_Types + diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst index 5e3ffc4f8aca3..9c60c0766dbc4 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -238,6 +238,7 @@ Clang-Tidy Checks :doc:`google-readability-avoid-underscore-in-googletest-name <google/readability-avoid-underscore-in-googletest-name>`, :doc:`google-readability-casting <google/readability-casting>`, :doc:`google-readability-todo <google/readability-todo>`, + :doc:`google-runtime-float <google/runtime-float>`, "Yes" :doc:`google-runtime-int <google/runtime-int>`, :doc:`google-runtime-operator <google/runtime-operator>`, :doc:`google-upgrade-googletest-case <google/upgrade-googletest-case>`, "Yes" @@ -249,12 +250,12 @@ Clang-Tidy Checks :doc:`linuxkernel-must-check-errs <linuxkernel/must-check-errs>`, :doc:`llvm-header-guard <llvm/header-guard>`, :doc:`llvm-include-order <llvm/include-order>`, "Yes" - :doc:`llvm-use-new-mlir-op-builder <llvm/use-new-mlir-op-builder>`, "Yes" :doc:`llvm-namespace-comment <llvm/namespace-comment>`, :doc:`llvm-prefer-isa-or-dyn-cast-in-conditionals <llvm/prefer-isa-or-dyn-cast-in-conditionals>`, "Yes" :doc:`llvm-prefer-register-over-unsigned <llvm/prefer-register-over-unsigned>`, "Yes" :doc:`llvm-prefer-static-over-anonymous-namespace <llvm/prefer-static-over-anonymous-namespace>`, :doc:`llvm-twine-local <llvm/twine-local>`, "Yes" + :doc:`llvm-use-new-mlir-op-builder <llvm/use-new-mlir-op-builder>`, "Yes" :doc:`llvmlibc-callee-namespace <llvmlibc/callee-namespace>`, :doc:`llvmlibc-implementation-in-namespace <llvmlibc/implementation-in-namespace>`, :doc:`llvmlibc-inline-function-decl <llvmlibc/inline-function-decl>`, "Yes" diff --git a/clang-tools-extra/test/clang-tidy/checkers/google/runtime-float.cpp b/clang-tools-extra/test/clang-tidy/checkers/google/runtime-float.cpp new file mode 100644 index 0000000000000..4ddf623fb955a --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/google/runtime-float.cpp @@ -0,0 +1,39 @@ +// RUN: %check_clang_tidy %s google-runtime-float %t + +long double foo; +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: consider replacing 'long double' with a 64-bit or 128-bit float type [google-runtime-float] + +typedef long double MyLongDouble; +// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: consider replacing 'long double' with a 64-bit or 128-bit float type [google-runtime-float] + +typedef long double MyOtherLongDouble; // NOLINT + +template <typename T> +void tmpl() { T i; } + +long volatile double v = 10; +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: consider replacing 'volatile long double' with a 64-bit or 128-bit float type [google-runtime-float] + +long double h(long const double aaa, long double bbb = 0.5L) { + // CHECK-MESSAGES: :[[@LINE-1]]:1: warning: consider replacing 'long double' with a 64-bit or 128-bit float type [google-runtime-float] + // CHECK-MESSAGES: :[[@LINE-2]]:15: warning: consider replacing 'const long double' with a 64-bit or 128-bit float type [google-runtime-float] + // CHECK-MESSAGES: :[[@LINE-3]]:38: warning: consider replacing 'long double' with a 64-bit or 128-bit float type [google-runtime-float] + // CHECK-MESSAGES: :[[@LINE-4]]:56: warning: 'long double' type from literal suffix 'L' should not be used [google-runtime-float] + double x = 0.1; + double y = 0.2L; + // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: 'long double' type from literal suffix 'L' should not be used [google-runtime-float] +#define ldtype long double + ldtype z; + tmpl<long double>(); + // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: consider replacing 'long double' with a 64-bit or 128-bit float type [google-runtime-float] + return 0; +} + +struct S{}; +constexpr S operator"" _baz(unsigned long long) { + long double j; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: consider replacing 'long double' with a 64-bit or 128-bit float type [google-runtime-float] + MyOtherLongDouble x; + return S{}; +} + >From 8ef1f3014946699965ec0d66a796c29410020ca4 Mon Sep 17 00:00:00 2001 From: Brenden Forward <bforw...@google.com> Date: Wed, 3 Sep 2025 16:23:45 -0700 Subject: [PATCH 2/7] Fix whitespace --- .../docs/clang-tidy/checks/google/runtime-float.rst | 1 - .../test/clang-tidy/checkers/google/runtime-float.cpp | 1 - 2 files changed, 2 deletions(-) diff --git a/clang-tools-extra/docs/clang-tidy/checks/google/runtime-float.rst b/clang-tools-extra/docs/clang-tidy/checks/google/runtime-float.rst index 5a5f9a4e6ae0f..f641440a10331 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/google/runtime-float.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/google/runtime-float.rst @@ -8,4 +8,3 @@ or 128-bit floating-point types. The corresponding style guide rule: https://google.github.io/styleguide/cppguide.html#Floating-Point_Types - diff --git a/clang-tools-extra/test/clang-tidy/checkers/google/runtime-float.cpp b/clang-tools-extra/test/clang-tidy/checkers/google/runtime-float.cpp index 4ddf623fb955a..732f1d6f00f71 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/google/runtime-float.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/google/runtime-float.cpp @@ -36,4 +36,3 @@ constexpr S operator"" _baz(unsigned long long) { MyOtherLongDouble x; return S{}; } - >From 26369d1446b10604e39a3948efffa5f7f07c32a0 Mon Sep 17 00:00:00 2001 From: Brenden Forward <bforw...@google.com> Date: Wed, 3 Sep 2025 16:48:22 -0700 Subject: [PATCH 3/7] const-qualify values and remove unused member --- .../clang-tidy/google/FloatTypesCheck.cpp | 5 ++--- .../clang-tidy/google/FloatTypesCheck.h | 14 ++------------ 2 files changed, 4 insertions(+), 15 deletions(-) diff --git a/clang-tools-extra/clang-tidy/google/FloatTypesCheck.cpp b/clang-tools-extra/clang-tidy/google/FloatTypesCheck.cpp index b8afe803230d3..ba0e04df50cab 100644 --- a/clang-tools-extra/clang-tidy/google/FloatTypesCheck.cpp +++ b/clang-tools-extra/clang-tidy/google/FloatTypesCheck.cpp @@ -28,7 +28,7 @@ AST_MATCHER(FloatingLiteral, isFLValidAndNotInMacro) { AST_MATCHER(TypeLoc, isLongDoubleType) { TypeLoc TL = Node; - if (auto QualLoc = Node.getAs<QualifiedTypeLoc>()) + if (const auto QualLoc = Node.getAs<QualifiedTypeLoc>()) TL = QualLoc.getUnqualifiedLoc(); const auto BuiltinLoc = TL.getAs<BuiltinTypeLoc>(); @@ -39,7 +39,7 @@ AST_MATCHER(TypeLoc, isLongDoubleType) { } AST_MATCHER(FloatingLiteral, isLongDoubleLiteral) { - if (auto *BT = dyn_cast<BuiltinType>(Node.getType().getTypePtr())) + if (const auto *BT = dyn_cast<BuiltinType>(Node.getType().getTypePtr())) return BT->getKind() == BuiltinType::LongDouble; return false; } @@ -57,7 +57,6 @@ void RuntimeFloatCheck::registerMatchers(MatchFinder *Finder) { floatLiteral(isFLValidAndNotInMacro(), isLongDoubleLiteral()) .bind("longDoubleFloatLiteral"), this); - IdentTable = std::make_unique<IdentifierTable>(getLangOpts()); } void RuntimeFloatCheck::check(const MatchFinder::MatchResult &Result) { diff --git a/clang-tools-extra/clang-tidy/google/FloatTypesCheck.h b/clang-tools-extra/clang-tidy/google/FloatTypesCheck.h index 54327b6d7e53b..8215309634287 100644 --- a/clang-tools-extra/clang-tidy/google/FloatTypesCheck.h +++ b/clang-tools-extra/clang-tidy/google/FloatTypesCheck.h @@ -1,4 +1,3 @@ - //===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. @@ -12,11 +11,7 @@ #include "../ClangTidyCheck.h" -namespace clang { - -class IdentifierTable; - -namespace tidy::google::runtime { +namespace clang::tidy::google::runtime { /// Finds usages of `long double` and suggests replacing them with other /// floating-point types. @@ -32,13 +27,8 @@ class RuntimeFloatCheck : public ClangTidyCheck { bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { return LangOpts.CPlusPlus && !LangOpts.ObjC; } - -private: - std::unique_ptr<IdentifierTable> IdentTable; }; -} // namespace tidy::google::runtime - -} // namespace clang +} // namespace clang::tidy::google::runtime #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_FLOATTYPESCHECK_H >From 99acf39e99f8dda614e1b7257dda887fc7032c74 Mon Sep 17 00:00:00 2001 From: Brenden Forward <bforw...@google.com> Date: Fri, 5 Sep 2025 10:33:43 -0700 Subject: [PATCH 4/7] Change diagnostics and docs entries --- .../clang-tidy/google/FloatTypesCheck.cpp | 7 +++---- .../checks/google/runtime-float.rst | 4 ++-- .../docs/clang-tidy/checks/list.rst | 2 +- .../checkers/google/runtime-float.cpp | 21 ++++++++++--------- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/clang-tools-extra/clang-tidy/google/FloatTypesCheck.cpp b/clang-tools-extra/clang-tidy/google/FloatTypesCheck.cpp index ba0e04df50cab..922658cba993b 100644 --- a/clang-tools-extra/clang-tidy/google/FloatTypesCheck.cpp +++ b/clang-tools-extra/clang-tidy/google/FloatTypesCheck.cpp @@ -61,15 +61,14 @@ void RuntimeFloatCheck::registerMatchers(MatchFinder *Finder) { void RuntimeFloatCheck::check(const MatchFinder::MatchResult &Result) { if (const auto *TL = Result.Nodes.getNodeAs<TypeLoc>("longDoubleTypeLoc")) { - diag(TL->getBeginLoc(), - "consider replacing %0 with a 64-bit or 128-bit float type") + diag(TL->getBeginLoc(), "%0 type is not portable and should not be used") << TL->getType(); } if (const auto *FL = Result.Nodes.getNodeAs<FloatingLiteral>("longDoubleFloatLiteral")) { - diag(FL->getBeginLoc(), - "%0 type from literal suffix 'L' should not be used") + diag(FL->getBeginLoc(), "%0 type from literal suffix 'L' is not portable " + "and should not be used") << FL->getType(); } } diff --git a/clang-tools-extra/docs/clang-tidy/checks/google/runtime-float.rst b/clang-tools-extra/docs/clang-tidy/checks/google/runtime-float.rst index f641440a10331..f8dc4b55de069 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/google/runtime-float.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/google/runtime-float.rst @@ -3,8 +3,8 @@ google-runtime-float ==================== -Finds uses of ``long double`` and suggests replacing them with 64-bit -or 128-bit floating-point types. +Finds uses of ``long double`` and suggests against their use due to +lack of portability. The corresponding style guide rule: https://google.github.io/styleguide/cppguide.html#Floating-Point_Types diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst index 9c60c0766dbc4..23e1d5caaf013 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -238,7 +238,7 @@ Clang-Tidy Checks :doc:`google-readability-avoid-underscore-in-googletest-name <google/readability-avoid-underscore-in-googletest-name>`, :doc:`google-readability-casting <google/readability-casting>`, :doc:`google-readability-todo <google/readability-todo>`, - :doc:`google-runtime-float <google/runtime-float>`, "Yes" + :doc:`google-runtime-float <google/runtime-float>`, :doc:`google-runtime-int <google/runtime-int>`, :doc:`google-runtime-operator <google/runtime-operator>`, :doc:`google-upgrade-googletest-case <google/upgrade-googletest-case>`, "Yes" diff --git a/clang-tools-extra/test/clang-tidy/checkers/google/runtime-float.cpp b/clang-tools-extra/test/clang-tidy/checkers/google/runtime-float.cpp index 732f1d6f00f71..09284e195fc8e 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/google/runtime-float.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/google/runtime-float.cpp @@ -1,10 +1,10 @@ // RUN: %check_clang_tidy %s google-runtime-float %t long double foo; -// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: consider replacing 'long double' with a 64-bit or 128-bit float type [google-runtime-float] +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: 'long double' type is not portable and should not be used [google-runtime-float] typedef long double MyLongDouble; -// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: consider replacing 'long double' with a 64-bit or 128-bit float type [google-runtime-float] +// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: 'long double' type is not portable and should not be used [google-runtime-float] typedef long double MyOtherLongDouble; // NOLINT @@ -12,27 +12,28 @@ template <typename T> void tmpl() { T i; } long volatile double v = 10; -// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: consider replacing 'volatile long double' with a 64-bit or 128-bit float type [google-runtime-float] +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: 'volatile long double' type is not portable and should not be used [google-runtime-float] long double h(long const double aaa, long double bbb = 0.5L) { - // CHECK-MESSAGES: :[[@LINE-1]]:1: warning: consider replacing 'long double' with a 64-bit or 128-bit float type [google-runtime-float] - // CHECK-MESSAGES: :[[@LINE-2]]:15: warning: consider replacing 'const long double' with a 64-bit or 128-bit float type [google-runtime-float] - // CHECK-MESSAGES: :[[@LINE-3]]:38: warning: consider replacing 'long double' with a 64-bit or 128-bit float type [google-runtime-float] - // CHECK-MESSAGES: :[[@LINE-4]]:56: warning: 'long double' type from literal suffix 'L' should not be used [google-runtime-float] + // CHECK-MESSAGES: :[[@LINE-1]]:1: warning: 'long double' type is not portable and should not be used [google-runtime-float] + // CHECK-MESSAGES: :[[@LINE-2]]:15: warning: 'const long double' type is not portable and should not be used [google-runtime-float] + // CHECK-MESSAGES: :[[@LINE-3]]:38: warning: 'long double' type is not portable and should not be used [google-runtime-float] + // CHECK-MESSAGES: :[[@LINE-4]]:56: warning: 'long double' type from literal suffix 'L' is not portable and should not be used [google-runtime-float] double x = 0.1; double y = 0.2L; - // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: 'long double' type from literal suffix 'L' should not be used [google-runtime-float] + // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: 'long double' type from literal suffix 'L' is not portable and should not be used [google-runtime-float] #define ldtype long double ldtype z; tmpl<long double>(); - // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: consider replacing 'long double' with a 64-bit or 128-bit float type [google-runtime-float] + // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: 'long double' type is not portable and should not be used [google-runtime-float] return 0; } struct S{}; constexpr S operator"" _baz(unsigned long long) { long double j; - // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: consider replacing 'long double' with a 64-bit or 128-bit float type [google-runtime-float] + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'long double' type is not portable and should not be used [google-runtime-float] MyOtherLongDouble x; return S{}; } + >From 746d847e865d8f550a171af34d2dd43450d3b30f Mon Sep 17 00:00:00 2001 From: Brenden Forward <bforw...@google.com> Date: Fri, 5 Sep 2025 10:38:50 -0700 Subject: [PATCH 5/7] Fix class description comment --- clang-tools-extra/clang-tidy/google/FloatTypesCheck.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/clang-tools-extra/clang-tidy/google/FloatTypesCheck.h b/clang-tools-extra/clang-tidy/google/FloatTypesCheck.h index 8215309634287..30c0a92b2e7ee 100644 --- a/clang-tools-extra/clang-tidy/google/FloatTypesCheck.h +++ b/clang-tools-extra/clang-tidy/google/FloatTypesCheck.h @@ -13,8 +13,7 @@ namespace clang::tidy::google::runtime { -/// Finds usages of `long double` and suggests replacing them with other -/// floating-point types. +/// Finds usages of `long double` and warns due to portability issues. /// /// For the user-facing documentation see: /// http://clang.llvm.org/extra/clang-tidy/checks/google/runtime-float.html >From 7ff71ddd9eac6a59e2a7fbaf7137c7195e46c812 Mon Sep 17 00:00:00 2001 From: Brenden Forward <bforw...@google.com> Date: Fri, 5 Sep 2025 10:57:04 -0700 Subject: [PATCH 6/7] Synchronize descriptions --- clang-tools-extra/clang-tidy/google/FloatTypesCheck.h | 3 ++- clang-tools-extra/docs/ReleaseNotes.rst | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/clang-tools-extra/clang-tidy/google/FloatTypesCheck.h b/clang-tools-extra/clang-tidy/google/FloatTypesCheck.h index 30c0a92b2e7ee..b5534c046e68f 100644 --- a/clang-tools-extra/clang-tidy/google/FloatTypesCheck.h +++ b/clang-tools-extra/clang-tidy/google/FloatTypesCheck.h @@ -13,7 +13,8 @@ namespace clang::tidy::google::runtime { -/// Finds usages of `long double` and warns due to portability issues. +/// Finds usages of `long double` and suggests against their use due to lack +/// of portability. /// /// For the user-facing documentation see: /// http://clang.llvm.org/extra/clang-tidy/checks/google/runtime-float.html diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index af62797e9ea91..f6222acfb1003 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -145,8 +145,8 @@ New checks - New :doc:`google-runtime-float <clang-tidy/checks/google/runtime-float>` check. - Checks for and warns of uses of the ``long double`` type, which is - problematic since it produces non-portable code. + Finds uses of ``long double`` and suggests against their use due to lack of + portability. - New :doc:`llvm-mlir-op-builder <clang-tidy/checks/llvm/use-new-mlir-op-builder>` check. >From aa454715e8e57b91d951743a38959fdece770a34 Mon Sep 17 00:00:00 2001 From: Brenden Forward <bforw...@google.com> Date: Fri, 5 Sep 2025 13:49:49 -0700 Subject: [PATCH 7/7] Use variadic matcher and improve null-checking --- .../clang-tidy/google/FloatTypesCheck.cpp | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/clang-tools-extra/clang-tidy/google/FloatTypesCheck.cpp b/clang-tools-extra/clang-tidy/google/FloatTypesCheck.cpp index 922658cba993b..3d5fb021db296 100644 --- a/clang-tools-extra/clang-tidy/google/FloatTypesCheck.cpp +++ b/clang-tools-extra/clang-tidy/google/FloatTypesCheck.cpp @@ -16,12 +16,9 @@ using namespace ast_matchers; namespace { -AST_MATCHER(TypeLoc, isTypeLocValidAndNotInMacro) { - const SourceLocation Loc = Node.getBeginLoc(); - return Loc.isValid() && !Loc.isMacroID(); -} - -AST_MATCHER(FloatingLiteral, isFLValidAndNotInMacro) { +AST_POLYMORPHIC_MATCHER(isValidAndNotInMacro, + AST_POLYMORPHIC_SUPPORTED_TYPES(TypeLoc, + FloatingLiteral)) { const SourceLocation Loc = Node.getBeginLoc(); return Loc.isValid() && !Loc.isMacroID(); } @@ -35,11 +32,14 @@ AST_MATCHER(TypeLoc, isLongDoubleType) { if (!BuiltinLoc) return false; - return BuiltinLoc.getTypePtr()->getKind() == BuiltinType::LongDouble; + if (const auto *BT = BuiltinLoc.getTypePtr()) + return BT->getKind() == BuiltinType::LongDouble; + return false; } AST_MATCHER(FloatingLiteral, isLongDoubleLiteral) { - if (const auto *BT = dyn_cast<BuiltinType>(Node.getType().getTypePtr())) + if (const auto *BT = + dyn_cast_if_present<BuiltinType>(Node.getType().getTypePtr())) return BT->getKind() == BuiltinType::LongDouble; return false; } @@ -50,13 +50,12 @@ namespace tidy::google::runtime { void RuntimeFloatCheck::registerMatchers(MatchFinder *Finder) { Finder->addMatcher(typeLoc(loc(realFloatingPointType()), - isTypeLocValidAndNotInMacro(), isLongDoubleType()) + isValidAndNotInMacro(), isLongDoubleType()) .bind("longDoubleTypeLoc"), this); - Finder->addMatcher( - floatLiteral(isFLValidAndNotInMacro(), isLongDoubleLiteral()) - .bind("longDoubleFloatLiteral"), - this); + Finder->addMatcher(floatLiteral(isValidAndNotInMacro(), isLongDoubleLiteral()) + .bind("longDoubleFloatLiteral"), + this); } void RuntimeFloatCheck::check(const MatchFinder::MatchResult &Result) { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits