-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[Clang-Tidy] Add google-runtime-float Clang-Tidy check #156763
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1082254
Add google-runtime-float Clang-Tidy check
brenfwd 5ae76a0
Merge branch 'main' into add-clang-tidy-long-double
brenfwd 8ef1f30
Fix whitespace
brenfwd 26369d1
const-qualify values and remove unused member
brenfwd 99acf39
Change diagnostics and docs entries
brenfwd 746d847
Fix class description comment
brenfwd 7ff71dd
Synchronize descriptions
brenfwd aa45471
Use variadic matcher and improve null-checking
brenfwd bde6086
Add long int test
brenfwd c10d242
Merge branch 'main' into add-clang-tidy-long-double
brenfwd c225d7c
Fix word wrapping in doc
brenfwd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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(); | ||
} | ||
brenfwd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
AST_MATCHER(TypeLoc, isLongDoubleType) { | ||
TypeLoc TL = Node; | ||
if (const auto QualLoc = Node.getAs<QualifiedTypeLoc>()) | ||
TL = QualLoc.getUnqualifiedLoc(); | ||
|
||
const auto BuiltinLoc = TL.getAs<BuiltinTypeLoc>(); | ||
if (!BuiltinLoc) | ||
return false; | ||
|
||
return BuiltinLoc.getTypePtr()->getKind() == BuiltinType::LongDouble; | ||
brenfwd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
AST_MATCHER(FloatingLiteral, isLongDoubleLiteral) { | ||
if (const auto *BT = dyn_cast<BuiltinType>(Node.getType().getTypePtr())) | ||
brenfwd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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); | ||
} | ||
|
||
void RuntimeFloatCheck::check(const MatchFinder::MatchResult &Result) { | ||
if (const auto *TL = Result.Nodes.getNodeAs<TypeLoc>("longDoubleTypeLoc")) { | ||
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' is not portable " | ||
"and should not be used") | ||
<< FL->getType(); | ||
} | ||
} | ||
|
||
} // namespace tidy::google::runtime | ||
|
||
} // namespace clang |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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::tidy::google::runtime { | ||
vbvictor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/// 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 { | ||
vbvictor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return LangOpts.CPlusPlus && !LangOpts.ObjC; | ||
} | ||
}; | ||
|
||
} // namespace clang::tidy::google::runtime | ||
|
||
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_FLOATTYPESCHECK_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
clang-tools-extra/docs/clang-tidy/checks/google/runtime-float.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
.. title:: clang-tidy - google-runtime-float | ||
|
||
google-runtime-float | ||
==================== | ||
|
||
Finds uses of ``long double`` and suggests against their use due to | ||
brenfwd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
brenfwd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
lack of portability. | ||
|
||
The corresponding style guide rule: | ||
https://google.github.io/styleguide/cppguide.html#Floating-Point_Types |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
clang-tools-extra/test/clang-tidy/checkers/google/runtime-float.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// RUN: %check_clang_tidy %s google-runtime-float %t | ||
|
||
long double foo; | ||
// 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: 'long double' type is not portable and should not be used [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: '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: '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' 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: '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: 'long double' type is not portable and should not be used [google-runtime-float] | ||
MyOtherLongDouble x; | ||
return S{}; | ||
} | ||
|
||
brenfwd marked this conversation as resolved.
Show resolved
Hide resolved
brenfwd marked this conversation as resolved.
Show resolved
Hide resolved
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.