[clang-tidy] Rename cert-flp30-c to bugprone-float-loop-counter (#166571)

Closes [#157291](https://github.com/llvm/llvm-project/issues/157291)

---------

Co-authored-by: Victor Chernyakin <chernyakin.victor.j@outlook.com>
This commit is contained in:
mitchell 2025-11-06 17:26:30 +08:00 committed by GitHub
parent 8b3a124ad8
commit 22242ae072
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 48 additions and 24 deletions

View File

@ -30,6 +30,7 @@
#include "EasilySwappableParametersCheck.h"
#include "EmptyCatchCheck.h"
#include "ExceptionEscapeCheck.h"
#include "FloatLoopCounterCheck.h"
#include "FoldInitTypeCheck.h"
#include "ForwardDeclarationNamespaceCheck.h"
#include "ForwardingReferenceOverloadCheck.h"
@ -153,6 +154,8 @@ public:
CheckFactories.registerCheck<EmptyCatchCheck>("bugprone-empty-catch");
CheckFactories.registerCheck<ExceptionEscapeCheck>(
"bugprone-exception-escape");
CheckFactories.registerCheck<FloatLoopCounterCheck>(
"bugprone-float-loop-counter");
CheckFactories.registerCheck<FoldInitTypeCheck>("bugprone-fold-init-type");
CheckFactories.registerCheck<ForwardDeclarationNamespaceCheck>(
"bugprone-forward-declaration-namespace");

View File

@ -26,6 +26,7 @@ add_clang_library(clangTidyBugproneModule STATIC
EasilySwappableParametersCheck.cpp
EmptyCatchCheck.cpp
ExceptionEscapeCheck.cpp
FloatLoopCounterCheck.cpp
FoldInitTypeCheck.cpp
ForwardDeclarationNamespaceCheck.cpp
ForwardingReferenceOverloadCheck.cpp

View File

@ -6,16 +6,16 @@
//
//===----------------------------------------------------------------------===//
#include "FloatLoopCounter.h"
#include "FloatLoopCounterCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
using namespace clang::ast_matchers;
namespace clang::tidy::cert {
namespace clang::tidy::bugprone {
void FloatLoopCounter::registerMatchers(MatchFinder *Finder) {
void FloatLoopCounterCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(
forStmt(hasIncrement(forEachDescendant(
declRefExpr(hasType(realFloatingPointType()),
@ -29,7 +29,7 @@ void FloatLoopCounter::registerMatchers(MatchFinder *Finder) {
this);
}
void FloatLoopCounter::check(const MatchFinder::MatchResult &Result) {
void FloatLoopCounterCheck::check(const MatchFinder::MatchResult &Result) {
const auto *FS = Result.Nodes.getNodeAs<ForStmt>("for");
diag(FS->getInc()->getBeginLoc(), "loop induction expression should not have "
@ -43,4 +43,4 @@ void FloatLoopCounter::check(const MatchFinder::MatchResult &Result) {
DiagnosticIDs::Note);
}
} // namespace clang::tidy::cert
} // namespace clang::tidy::bugprone

View File

@ -6,27 +6,27 @@
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_FLOAT_LOOP_COUNTER_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_FLOAT_LOOP_COUNTER_H
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_FLOATLOOPCOUNTERCHECK_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_FLOATLOOPCOUNTERCHECK_H
#include "../ClangTidyCheck.h"
namespace clang::tidy::cert {
namespace clang::tidy::bugprone {
/// This check diagnoses when the loop induction expression of a for loop has
/// floating-point type. The check corresponds to:
/// https://www.securecoding.cert.org/confluence/display/c/FLP30-C.+Do+not+use+floating-point+variables+as+loop+counters
///
/// For the user-facing documentation see:
/// https://clang.llvm.org/extra/clang-tidy/checks/cert/flp30-c.html
class FloatLoopCounter : public ClangTidyCheck {
/// https://clang.llvm.org/extra/clang-tidy/checks/bugprone/float-loop-counter.html
class FloatLoopCounterCheck : public ClangTidyCheck {
public:
FloatLoopCounter(StringRef Name, ClangTidyContext *Context)
FloatLoopCounterCheck(StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context) {}
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
};
} // namespace clang::tidy::cert
} // namespace clang::tidy::bugprone
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_FLOAT_LOOP_COUNTER_H
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_FLOATLOOPCOUNTERCHECK_H

View File

@ -12,6 +12,7 @@
#include "../bugprone/BadSignalToKillThreadCheck.h"
#include "../bugprone/CommandProcessorCheck.h"
#include "../bugprone/DefaultOperatorNewOnOveralignedTypeCheck.h"
#include "../bugprone/FloatLoopCounterCheck.h"
#include "../bugprone/PointerArithmeticOnPolymorphicObjectCheck.h"
#include "../bugprone/RawMemoryCallOnNonTrivialTypeCheck.h"
#include "../bugprone/ReservedIdentifierCheck.h"
@ -37,7 +38,6 @@
#include "../performance/MoveConstructorInitCheck.h"
#include "../readability/EnumInitialValueCheck.h"
#include "../readability/UppercaseLiteralSuffixCheck.h"
#include "FloatLoopCounter.h"
#include "LimitedRandomnessCheck.h"
#include "MutatingCopyCheck.h"
#include "ProperlySeededRandomGeneratorCheck.h"
@ -310,7 +310,8 @@ public:
CheckFactories.registerCheck<bugprone::SuspiciousMemoryComparisonCheck>(
"cert-exp42-c");
// FLP
CheckFactories.registerCheck<FloatLoopCounter>("cert-flp30-c");
CheckFactories.registerCheck<bugprone::FloatLoopCounterCheck>(
"cert-flp30-c");
CheckFactories.registerCheck<bugprone::SuspiciousMemoryComparisonCheck>(
"cert-flp37-c");
// FIO

View File

@ -5,7 +5,6 @@ set(LLVM_LINK_COMPONENTS
add_clang_library(clangTidyCERTModule STATIC
CERTTidyModule.cpp
FloatLoopCounter.cpp
LimitedRandomnessCheck.cpp
MutatingCopyCheck.cpp
ProperlySeededRandomGeneratorCheck.cpp

View File

@ -269,6 +269,11 @@ New check aliases
<clang-tidy/checks/bugprone/throwing-static-initialization>`
keeping initial check as an alias to the new one.
- Renamed :doc:`cert-flp30-c <clang-tidy/checks/cert/flp30-c>` to
:doc:`bugprone-float-loop-counter
<clang-tidy/checks/bugprone/float-loop-counter>`
keeping initial check as an alias to the new one.
- Renamed :doc:`cert-mem57-cpp <clang-tidy/checks/cert/mem57-cpp>` to
:doc:`bugprone-default-operator-new-on-overaligned-type
<clang-tidy/checks/bugprone/default-operator-new-on-overaligned-type>`

View File

@ -0,0 +1,13 @@
.. title:: clang-tidy - bugprone-float-loop-counter
bugprone-float-loop-counter
===========================
Flags ``for`` loops where the induction expression has a floating-point type.
References
----------
This check corresponds to the CERT C Coding Standard rule
`FLP30-C. Do not use floating-point variables as loop counters
<https://www.securecoding.cert.org/confluence/display/c/FLP30-C.+Do+not+use+floating-point+variables+as+loop+counters>`_.

View File

@ -3,8 +3,9 @@
cert-flp30-c
============
This check flags ``for`` loops where the induction expression has a
floating-point type.
The `cert-flp30-c` check is an alias, please see
`bugprone-float-loop-counter <../bugprone/float-loop-counter.html>`_
for more information
This check corresponds to the CERT C Coding Standard rule
`FLP30-C. Do not use floating-point variables as loop counters

View File

@ -98,6 +98,7 @@ Clang-Tidy Checks
:doc:`bugprone-easily-swappable-parameters <bugprone/easily-swappable-parameters>`,
:doc:`bugprone-empty-catch <bugprone/empty-catch>`,
:doc:`bugprone-exception-escape <bugprone/exception-escape>`,
:doc:`bugprone-float-loop-counter <bugprone/float-loop-counter>`,
:doc:`bugprone-fold-init-type <bugprone/fold-init-type>`,
:doc:`bugprone-forward-declaration-namespace <bugprone/forward-declaration-namespace>`,
:doc:`bugprone-forwarding-reference-overload <bugprone/forwarding-reference-overload>`,
@ -178,7 +179,6 @@ Clang-Tidy Checks
:doc:`bugprone-virtual-near-miss <bugprone/virtual-near-miss>`, "Yes"
:doc:`cert-err33-c <cert/err33-c>`,
:doc:`cert-err60-cpp <cert/err60-cpp>`,
:doc:`cert-flp30-c <cert/flp30-c>`,
:doc:`cert-msc50-cpp <cert/msc50-cpp>`,
:doc:`cert-msc51-cpp <cert/msc51-cpp>`,
:doc:`cert-oop58-cpp <cert/oop58-cpp>`,
@ -451,6 +451,7 @@ Check aliases
:doc:`cert-err61-cpp <cert/err61-cpp>`, :doc:`misc-throw-by-value-catch-by-reference <misc/throw-by-value-catch-by-reference>`,
:doc:`cert-exp42-c <cert/exp42-c>`, :doc:`bugprone-suspicious-memory-comparison <bugprone/suspicious-memory-comparison>`,
:doc:`cert-fio38-c <cert/fio38-c>`, :doc:`misc-non-copyable-objects <misc/non-copyable-objects>`,
:doc:`cert-flp30-c <cert/flp30-c>`, :doc:`bugprone-float-loop-counter <bugprone/float-loop-counter>`,
:doc:`cert-flp37-c <cert/flp37-c>`, :doc:`bugprone-suspicious-memory-comparison <bugprone/suspicious-memory-comparison>`,
:doc:`cert-int09-c <cert/int09-c>`, :doc:`readability-enum-initial-value <readability/enum-initial-value>`, "Yes"
:doc:`cert-mem57-cpp <cert/mem57-cpp>`, :doc:`bugprone-default-operator-new-on-overaligned-type <bugprone/default-operator-new-on-overaligned-type>`,

View File

@ -1,4 +1,4 @@
// RUN: %check_clang_tidy %s cert-flp30-c %t
// RUN: %check_clang_tidy %s bugprone-float-loop-counter %t
float g(void);
int c(float);
@ -7,16 +7,16 @@ float f = 1.0f;
void match(void) {
for (float x = 0.1f; x <= 1.0f; x += 0.1f) {}
// CHECK-MESSAGES: :[[@LINE-1]]:35: warning: loop induction expression should not have floating-point type [cert-flp30-c]
// CHECK-MESSAGES: :[[@LINE-1]]:35: warning: loop induction expression should not have floating-point type [bugprone-float-loop-counter]
for (; f > 0; --f) {}
// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: loop induction expression should not have floating-point type [cert-flp30-c]
// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: loop induction expression should not have floating-point type [bugprone-float-loop-counter]
for (float x = 0.0f; c(x); x = g()) {}
// CHECK-MESSAGES: :[[@LINE-1]]:30: warning: loop induction expression should not have floating-point type [cert-flp30-c]
// CHECK-MESSAGES: :[[@LINE-1]]:30: warning: loop induction expression should not have floating-point type [bugprone-float-loop-counter]
for (int i=0; i < 10 && f < 2.0f; f++, i++) {}
// CHECK-MESSAGES: :[[@LINE-1]]:37: warning: loop induction expression should not have floating-point type [cert-flp30-c]
// CHECK-MESSAGES: :[[@LINE-1]]:37: warning: loop induction expression should not have floating-point type [bugprone-float-loop-counter]
// CHECK-MESSAGES: :5:1: note: floating-point type loop induction variable
}