diff --git a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp index 611717a64b768..e1856ff24cd86 100644 --- a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp @@ -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 @@ class BugproneModule : public ClangTidyModule { CheckFactories.registerCheck("bugprone-empty-catch"); CheckFactories.registerCheck( "bugprone-exception-escape"); + CheckFactories.registerCheck( + "bugprone-float-loop-counter"); CheckFactories.registerCheck("bugprone-fold-init-type"); CheckFactories.registerCheck( "bugprone-forward-declaration-namespace"); diff --git a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt index 6c96996458040..7d2e10887dfe5 100644 --- a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt @@ -26,6 +26,7 @@ add_clang_library(clangTidyBugproneModule STATIC EasilySwappableParametersCheck.cpp EmptyCatchCheck.cpp ExceptionEscapeCheck.cpp + FloatLoopCounterCheck.cpp FoldInitTypeCheck.cpp ForwardDeclarationNamespaceCheck.cpp ForwardingReferenceOverloadCheck.cpp diff --git a/clang-tools-extra/clang-tidy/cert/FloatLoopCounter.cpp b/clang-tools-extra/clang-tidy/bugprone/FloatLoopCounterCheck.cpp similarity index 86% rename from clang-tools-extra/clang-tidy/cert/FloatLoopCounter.cpp rename to clang-tools-extra/clang-tidy/bugprone/FloatLoopCounterCheck.cpp index 01299e0e5ab48..adf2d2b4bcc07 100644 --- a/clang-tools-extra/clang-tidy/cert/FloatLoopCounter.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/FloatLoopCounterCheck.cpp @@ -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("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 diff --git a/clang-tools-extra/clang-tidy/cert/FloatLoopCounter.h b/clang-tools-extra/clang-tidy/bugprone/FloatLoopCounterCheck.h similarity index 64% rename from clang-tools-extra/clang-tidy/cert/FloatLoopCounter.h rename to clang-tools-extra/clang-tidy/bugprone/FloatLoopCounterCheck.h index d00c036f00f24..43dd9c2c93515 100644 --- a/clang-tools-extra/clang-tidy/cert/FloatLoopCounter.h +++ b/clang-tools-extra/clang-tidy/bugprone/FloatLoopCounterCheck.h @@ -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 diff --git a/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp b/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp index 15592d5c03c06..a1f62707b107e 100644 --- a/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp @@ -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 @@ class CERTModule : public ClangTidyModule { CheckFactories.registerCheck( "cert-exp42-c"); // FLP - CheckFactories.registerCheck("cert-flp30-c"); + CheckFactories.registerCheck( + "cert-flp30-c"); CheckFactories.registerCheck( "cert-flp37-c"); // FIO diff --git a/clang-tools-extra/clang-tidy/cert/CMakeLists.txt b/clang-tools-extra/clang-tidy/cert/CMakeLists.txt index 27cf392ce0bb1..b25576a312724 100644 --- a/clang-tools-extra/clang-tidy/cert/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/cert/CMakeLists.txt @@ -5,7 +5,6 @@ set(LLVM_LINK_COMPONENTS add_clang_library(clangTidyCERTModule STATIC CERTTidyModule.cpp - FloatLoopCounter.cpp LimitedRandomnessCheck.cpp MutatingCopyCheck.cpp ProperlySeededRandomGeneratorCheck.cpp diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 88eb0ebff4501..8ba1d1a78bf91 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -269,6 +269,11 @@ New check aliases ` keeping initial check as an alias to the new one. +- Renamed :doc:`cert-flp30-c ` to + :doc:`bugprone-float-loop-counter + ` + keeping initial check as an alias to the new one. + - Renamed :doc:`cert-mem57-cpp ` to :doc:`bugprone-default-operator-new-on-overaligned-type ` diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/float-loop-counter.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/float-loop-counter.rst new file mode 100644 index 0000000000000..e663b40ab5a5d --- /dev/null +++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/float-loop-counter.rst @@ -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 +`_. diff --git a/clang-tools-extra/docs/clang-tidy/checks/cert/flp30-c.rst b/clang-tools-extra/docs/clang-tidy/checks/cert/flp30-c.rst index c37b63980be76..5f6eff447cc2f 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/cert/flp30-c.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/cert/flp30-c.rst @@ -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 diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst index 5094bcc90caf3..be509abab00a5 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -98,6 +98,7 @@ Clang-Tidy Checks :doc:`bugprone-easily-swappable-parameters `, :doc:`bugprone-empty-catch `, :doc:`bugprone-exception-escape `, + :doc:`bugprone-float-loop-counter `, :doc:`bugprone-fold-init-type `, :doc:`bugprone-forward-declaration-namespace `, :doc:`bugprone-forwarding-reference-overload `, @@ -178,7 +179,6 @@ Clang-Tidy Checks :doc:`bugprone-virtual-near-miss `, "Yes" :doc:`cert-err33-c `, :doc:`cert-err60-cpp `, - :doc:`cert-flp30-c `, :doc:`cert-msc50-cpp `, :doc:`cert-msc51-cpp `, :doc:`cert-oop58-cpp `, @@ -451,6 +451,7 @@ Check aliases :doc:`cert-err61-cpp `, :doc:`misc-throw-by-value-catch-by-reference `, :doc:`cert-exp42-c `, :doc:`bugprone-suspicious-memory-comparison `, :doc:`cert-fio38-c `, :doc:`misc-non-copyable-objects `, + :doc:`cert-flp30-c `, :doc:`bugprone-float-loop-counter `, :doc:`cert-flp37-c `, :doc:`bugprone-suspicious-memory-comparison `, :doc:`cert-int09-c `, :doc:`readability-enum-initial-value `, "Yes" :doc:`cert-mem57-cpp `, :doc:`bugprone-default-operator-new-on-overaligned-type `, diff --git a/clang-tools-extra/test/clang-tidy/checkers/cert/flp30-c.c b/clang-tools-extra/test/clang-tidy/checkers/bugprone/float-loop-counter.c similarity index 67% rename from clang-tools-extra/test/clang-tidy/checkers/cert/flp30-c.c rename to clang-tools-extra/test/clang-tidy/checkers/bugprone/float-loop-counter.c index b9985887a81c5..77812f01eace5 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/cert/flp30-c.c +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/float-loop-counter.c @@ -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 }