Skip to content

Commit cad9305

Browse files
rebase
Created using spr 1.3.6
2 parents e95feae + 25c7bbb commit cad9305

File tree

73 files changed

+2599
-581
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+2599
-581
lines changed

clang-tools-extra/clang-tidy/google/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ add_clang_library(clangTidyGoogleModule STATIC
1111
DefaultArgumentsCheck.cpp
1212
ExplicitConstructorCheck.cpp
1313
ExplicitMakePairCheck.cpp
14+
FloatTypesCheck.cpp
1415
FunctionNamingCheck.cpp
1516
GlobalNamesInHeadersCheck.cpp
1617
GlobalVariableDeclarationCheck.cpp
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "FloatTypesCheck.h"
10+
#include "clang/ASTMatchers/ASTMatchFinder.h"
11+
#include "clang/Lex/Lexer.h"
12+
13+
namespace clang {
14+
15+
using namespace ast_matchers;
16+
17+
namespace {
18+
19+
AST_POLYMORPHIC_MATCHER(isValidAndNotInMacro,
20+
AST_POLYMORPHIC_SUPPORTED_TYPES(TypeLoc,
21+
FloatingLiteral)) {
22+
const SourceLocation Loc = Node.getBeginLoc();
23+
return Loc.isValid() && !Loc.isMacroID();
24+
}
25+
26+
AST_MATCHER(TypeLoc, isLongDoubleType) {
27+
TypeLoc TL = Node;
28+
if (const auto QualLoc = Node.getAs<QualifiedTypeLoc>())
29+
TL = QualLoc.getUnqualifiedLoc();
30+
31+
const auto BuiltinLoc = TL.getAs<BuiltinTypeLoc>();
32+
if (!BuiltinLoc)
33+
return false;
34+
35+
if (const auto *BT = BuiltinLoc.getTypePtr())
36+
return BT->getKind() == BuiltinType::LongDouble;
37+
return false;
38+
}
39+
40+
AST_MATCHER(FloatingLiteral, isLongDoubleLiteral) {
41+
if (const auto *BT =
42+
dyn_cast_if_present<BuiltinType>(Node.getType().getTypePtr()))
43+
return BT->getKind() == BuiltinType::LongDouble;
44+
return false;
45+
}
46+
47+
} // namespace
48+
49+
namespace tidy::google::runtime {
50+
51+
void RuntimeFloatCheck::registerMatchers(MatchFinder *Finder) {
52+
Finder->addMatcher(typeLoc(loc(realFloatingPointType()),
53+
isValidAndNotInMacro(), isLongDoubleType())
54+
.bind("longDoubleTypeLoc"),
55+
this);
56+
Finder->addMatcher(floatLiteral(isValidAndNotInMacro(), isLongDoubleLiteral())
57+
.bind("longDoubleFloatLiteral"),
58+
this);
59+
}
60+
61+
void RuntimeFloatCheck::check(const MatchFinder::MatchResult &Result) {
62+
if (const auto *TL = Result.Nodes.getNodeAs<TypeLoc>("longDoubleTypeLoc")) {
63+
diag(TL->getBeginLoc(), "%0 type is not portable and should not be used")
64+
<< TL->getType();
65+
}
66+
67+
if (const auto *FL =
68+
Result.Nodes.getNodeAs<FloatingLiteral>("longDoubleFloatLiteral")) {
69+
diag(FL->getBeginLoc(), "%0 type from literal suffix 'L' is not portable "
70+
"and should not be used")
71+
<< FL->getType();
72+
}
73+
}
74+
75+
} // namespace tidy::google::runtime
76+
77+
} // namespace clang
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_FLOATTYPESCHECK_H
10+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_FLOATTYPESCHECK_H
11+
12+
#include "../ClangTidyCheck.h"
13+
14+
namespace clang::tidy::google::runtime {
15+
16+
/// Finds usages of `long double` and suggests against their use due to lack
17+
/// of portability.
18+
///
19+
/// For the user-facing documentation see:
20+
/// http://clang.llvm.org/extra/clang-tidy/checks/google/runtime-float.html
21+
class RuntimeFloatCheck : public ClangTidyCheck {
22+
public:
23+
RuntimeFloatCheck(StringRef Name, ClangTidyContext *Context)
24+
: ClangTidyCheck(Name, Context) {}
25+
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
26+
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
27+
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
28+
return LangOpts.CPlusPlus && !LangOpts.ObjC;
29+
}
30+
};
31+
32+
} // namespace clang::tidy::google::runtime
33+
34+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_FLOATTYPESCHECK_H

clang-tools-extra/clang-tidy/google/GoogleTidyModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "DefaultArgumentsCheck.h"
2020
#include "ExplicitConstructorCheck.h"
2121
#include "ExplicitMakePairCheck.h"
22+
#include "FloatTypesCheck.h"
2223
#include "FunctionNamingCheck.h"
2324
#include "GlobalNamesInHeadersCheck.h"
2425
#include "GlobalVariableDeclarationCheck.h"
@@ -57,6 +58,8 @@ class GoogleModule : public ClangTidyModule {
5758
"google-objc-function-naming");
5859
CheckFactories.registerCheck<objc::GlobalVariableDeclarationCheck>(
5960
"google-objc-global-variable-declaration");
61+
CheckFactories.registerCheck<runtime::RuntimeFloatCheck>(
62+
"google-runtime-float");
6063
CheckFactories.registerCheck<runtime::IntegerTypesCheck>(
6164
"google-runtime-int");
6265
CheckFactories.registerCheck<runtime::OverloadedUnaryAndCheck>(

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ New checks
155155
Finds calls to ``operator[]`` in STL containers and suggests replacing them
156156
with safe alternatives.
157157

158+
- New :doc:`google-runtime-float
159+
<clang-tidy/checks/google/runtime-float>` check.
160+
161+
Finds uses of ``long double`` and suggests against their use due to lack of
162+
portability.
163+
158164
- New :doc:`llvm-mlir-op-builder
159165
<clang-tidy/checks/llvm/use-new-mlir-op-builder>` check.
160166

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.. title:: clang-tidy - google-runtime-float
2+
3+
google-runtime-float
4+
====================
5+
6+
Finds uses of ``long double`` and suggests against their use due to lack of
7+
portability.
8+
9+
The corresponding style guide rule:
10+
https://google.github.io/styleguide/cppguide.html#Floating-Point_Types

clang-tools-extra/docs/clang-tidy/checks/list.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ Clang-Tidy Checks
238238
:doc:`google-readability-avoid-underscore-in-googletest-name <google/readability-avoid-underscore-in-googletest-name>`,
239239
:doc:`google-readability-casting <google/readability-casting>`,
240240
:doc:`google-readability-todo <google/readability-todo>`,
241+
:doc:`google-runtime-float <google/runtime-float>`,
241242
:doc:`google-runtime-int <google/runtime-int>`,
242243
:doc:`google-runtime-operator <google/runtime-operator>`,
243244
:doc:`google-upgrade-googletest-case <google/upgrade-googletest-case>`, "Yes"
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// RUN: %check_clang_tidy %s google-runtime-float %t
2+
3+
long double foo;
4+
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: 'long double' type is not portable and should not be used [google-runtime-float]
5+
6+
typedef long double MyLongDouble;
7+
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: 'long double' type is not portable and should not be used [google-runtime-float]
8+
9+
typedef long double MyOtherLongDouble; // NOLINT
10+
11+
template <typename T>
12+
void tmpl() { T i; }
13+
14+
long volatile double v = 10;
15+
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: 'volatile long double' type is not portable and should not be used [google-runtime-float]
16+
17+
long double h(long const double aaa, long double bbb = 0.5L) {
18+
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: 'long double' type is not portable and should not be used [google-runtime-float]
19+
// CHECK-MESSAGES: :[[@LINE-2]]:15: warning: 'const long double' type is not portable and should not be used [google-runtime-float]
20+
// CHECK-MESSAGES: :[[@LINE-3]]:38: warning: 'long double' type is not portable and should not be used [google-runtime-float]
21+
// CHECK-MESSAGES: :[[@LINE-4]]:56: warning: 'long double' type from literal suffix 'L' is not portable and should not be used [google-runtime-float]
22+
double x = 0.1;
23+
double y = 0.2L;
24+
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: 'long double' type from literal suffix 'L' is not portable and should not be used [google-runtime-float]
25+
#define ldtype long double
26+
ldtype z;
27+
tmpl<long double>();
28+
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: 'long double' type is not portable and should not be used [google-runtime-float]
29+
return 0;
30+
}
31+
32+
struct S{};
33+
constexpr S operator"" _baz(unsigned long long) {
34+
long double j;
35+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'long double' type is not portable and should not be used [google-runtime-float]
36+
MyOtherLongDouble x;
37+
long int a = 1L;
38+
return S{};
39+
}
40+

clang/include/clang/Basic/Attr.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@ def PatchableFunctionEntry
954954
: InheritableAttr,
955955
TargetSpecificAttr<TargetArch<
956956
["aarch64", "aarch64_be", "loongarch32", "loongarch64", "riscv32",
957-
"riscv64", "x86", "x86_64", "ppc", "ppc64"]>> {
957+
"riscv64", "x86", "x86_64", "ppc", "ppc64", "ppc64le"]>> {
958958
let Spellings = [GCC<"patchable_function_entry">];
959959
let Subjects = SubjectList<[Function, ObjCMethod]>;
960960
let Args = [UnsignedArgument<"Count">, DefaultIntArgument<"Offset", 0>,

clang/include/clang/Basic/AttrDocs.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6719,7 +6719,7 @@ if omitted.``Section`` defaults to the ``-fpatchable-function-entry`` section n
67196719
set, or to ``__patchable_function_entries`` otherwise.
67206720

67216721
This attribute is only supported on
6722-
aarch64/aarch64-be/loongarch32/loongarch64/riscv32/riscv64/i386/x86-64/ppc/ppc64 targets.
6722+
aarch64/aarch64-be/loongarch32/loongarch64/riscv32/riscv64/i386/x86-64/ppc/ppc64/ppc64le targets.
67236723
For ppc/ppc64 targets, AIX is still not supported.
67246724
}];
67256725
}

0 commit comments

Comments
 (0)