[clang-tidy] Rename and move 'cert-oop57-cpp' to 'bugprone-raw-memory-call-on-non-trivial-type' (#162039)

closes #157294
This commit is contained in:
Dasha Buka 2025-11-02 14:08:50 -08:00 committed by GitHub
parent eb4360b5c4
commit 6b147b46fe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 74 additions and 57 deletions

View File

@ -61,6 +61,7 @@
#include "ParentVirtualCallCheck.h"
#include "PointerArithmeticOnPolymorphicObjectCheck.h"
#include "PosixReturnCheck.h"
#include "RawMemoryCallOnNonTrivialTypeCheck.h"
#include "RedundantBranchConditionCheck.h"
#include "ReservedIdentifierCheck.h"
#include "ReturnConstRefFromParameterCheck.h"
@ -216,6 +217,8 @@ public:
CheckFactories.registerCheck<ParentVirtualCallCheck>(
"bugprone-parent-virtual-call");
CheckFactories.registerCheck<PosixReturnCheck>("bugprone-posix-return");
CheckFactories.registerCheck<RawMemoryCallOnNonTrivialTypeCheck>(
"bugprone-raw-memory-call-on-non-trivial-type");
CheckFactories.registerCheck<ReservedIdentifierCheck>(
"bugprone-reserved-identifier");
CheckFactories.registerCheck<SharedPtrArrayMismatchCheck>(

View File

@ -62,6 +62,7 @@ add_clang_library(clangTidyBugproneModule STATIC
ParentVirtualCallCheck.cpp
PointerArithmeticOnPolymorphicObjectCheck.cpp
PosixReturnCheck.cpp
RawMemoryCallOnNonTrivialTypeCheck.cpp
RedundantBranchConditionCheck.cpp
ReservedIdentifierCheck.cpp
ReturnConstRefFromParameterCheck.cpp

View File

@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//
#include "NonTrivialTypesLibcMemoryCallsCheck.h"
#include "RawMemoryCallOnNonTrivialTypeCheck.h"
#include "../utils/OptionsUtils.h"
#include "clang/AST/Decl.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
@ -17,7 +17,7 @@
using namespace clang::ast_matchers;
namespace clang::tidy::cert {
namespace clang::tidy::bugprone {
namespace {
AST_MATCHER(CXXRecordDecl, isTriviallyDefaultConstructible) {
@ -48,22 +48,21 @@ static constexpr llvm::StringRef ComparisonOperators[] = {
"operator==", "operator!=", "operator<",
"operator>", "operator<=", "operator>="};
NonTrivialTypesLibcMemoryCallsCheck::NonTrivialTypesLibcMemoryCallsCheck(
RawMemoryCallOnNonTrivialTypeCheck::RawMemoryCallOnNonTrivialTypeCheck(
StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
MemSetNames(Options.get("MemSetNames", "")),
MemCpyNames(Options.get("MemCpyNames", "")),
MemCmpNames(Options.get("MemCmpNames", "")) {}
void NonTrivialTypesLibcMemoryCallsCheck::storeOptions(
void RawMemoryCallOnNonTrivialTypeCheck::storeOptions(
ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "MemSetNames", MemSetNames);
Options.store(Opts, "MemCpyNames", MemCpyNames);
Options.store(Opts, "MemCmpNames", MemCmpNames);
}
void NonTrivialTypesLibcMemoryCallsCheck::registerMatchers(
MatchFinder *Finder) {
void RawMemoryCallOnNonTrivialTypeCheck::registerMatchers(MatchFinder *Finder) {
using namespace ast_matchers::internal;
auto IsStructPointer = [](Matcher<CXXRecordDecl> Constraint = anything(),
bool Bind = false) {
@ -103,7 +102,7 @@ void NonTrivialTypesLibcMemoryCallsCheck::registerMatchers(
this);
}
void NonTrivialTypesLibcMemoryCallsCheck::check(
void RawMemoryCallOnNonTrivialTypeCheck::check(
const MatchFinder::MatchResult &Result) {
if (const auto *Caller = Result.Nodes.getNodeAs<CallExpr>("lazyConstruct")) {
diag(Caller->getBeginLoc(), "calling %0 on a non-trivially default "
@ -122,4 +121,4 @@ void NonTrivialTypesLibcMemoryCallsCheck::check(
}
}
} // namespace clang::tidy::cert
} // namespace clang::tidy::bugprone

View File

@ -6,22 +6,21 @@
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_NONTRIVIALTYPESLIBCMEMORYCALLSCHECK_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_NONTRIVIALTYPESLIBCMEMORYCALLSCHECK_H
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_RAWMEMORYCALLONNONTRIVIALTYPECHECK_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_RAWMEMORYCALLONNONTRIVIALTYPECHECK_H
#include "../ClangTidyCheck.h"
namespace clang::tidy::cert {
namespace clang::tidy::bugprone {
/// Flags use of the `C` standard library functions 'memset', 'memcpy' and
/// Flags use of the C standard library functions 'memset', 'memcpy' and
/// 'memcmp' and similar derivatives on non-trivial types.
///
/// For the user-facing documentation see:
/// https://clang.llvm.org/extra/clang-tidy/checks/cert/oop57-cpp.html
class NonTrivialTypesLibcMemoryCallsCheck : public ClangTidyCheck {
/// https://clang.llvm.org/extra/clang-tidy/checks/bugprone/raw-memory-call-on-non-trivial-type.html
class RawMemoryCallOnNonTrivialTypeCheck : public ClangTidyCheck {
public:
NonTrivialTypesLibcMemoryCallsCheck(StringRef Name,
ClangTidyContext *Context);
RawMemoryCallOnNonTrivialTypeCheck(StringRef Name, ClangTidyContext *Context);
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
return LangOpts.CPlusPlus && !LangOpts.ObjC;
}
@ -35,6 +34,6 @@ private:
const StringRef MemCmpNames;
};
} // namespace clang::tidy::cert
} // namespace clang::tidy::bugprone
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_NONTRIVIALTYPESLIBCMEMORYCALLSCHECK_H
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_RAWMEMORYCALLONNONTRIVIALTYPECHECK_H

View File

@ -12,6 +12,7 @@
#include "../bugprone/BadSignalToKillThreadCheck.h"
#include "../bugprone/CommandProcessorCheck.h"
#include "../bugprone/PointerArithmeticOnPolymorphicObjectCheck.h"
#include "../bugprone/RawMemoryCallOnNonTrivialTypeCheck.h"
#include "../bugprone/ReservedIdentifierCheck.h"
#include "../bugprone/SignalHandlerCheck.h"
#include "../bugprone/SignedCharMisuseCheck.h"
@ -39,7 +40,6 @@
#include "FloatLoopCounter.h"
#include "LimitedRandomnessCheck.h"
#include "MutatingCopyCheck.h"
#include "NonTrivialTypesLibcMemoryCallsCheck.h"
#include "ProperlySeededRandomGeneratorCheck.h"
#include "ThrownExceptionTypeCheck.h"
@ -278,7 +278,7 @@ public:
"cert-oop11-cpp");
CheckFactories.registerCheck<bugprone::UnhandledSelfAssignmentCheck>(
"cert-oop54-cpp");
CheckFactories.registerCheck<NonTrivialTypesLibcMemoryCallsCheck>(
CheckFactories.registerCheck<bugprone::RawMemoryCallOnNonTrivialTypeCheck>(
"cert-oop57-cpp");
CheckFactories.registerCheck<MutatingCopyCheck>("cert-oop58-cpp");

View File

@ -10,7 +10,6 @@ add_clang_library(clangTidyCERTModule STATIC
FloatLoopCounter.cpp
LimitedRandomnessCheck.cpp
MutatingCopyCheck.cpp
NonTrivialTypesLibcMemoryCallsCheck.cpp
ProperlySeededRandomGeneratorCheck.cpp
ThrownExceptionTypeCheck.cpp

View File

@ -264,6 +264,11 @@ New check aliases
<clang-tidy/checks/bugprone/throwing-static-initialization>`
keeping initial check as an alias to the new one.
- Renamed :doc:`cert-oop57-cpp <clang-tidy/checks/cert/oop57-cpp>` to
:doc:`bugprone-raw-memory-call-on-non-trivial-type
<clang-tidy/checks/bugprone/raw-memory-call-on-non-trivial-type>`
keeping initial check as an alias to the new one.
Changes in existing checks
^^^^^^^^^^^^^^^^^^^^^^^^^^

View File

@ -0,0 +1,35 @@
.. title:: clang-tidy - bugprone-raw-memory-call-on-non-trivial-type
bugprone-raw-memory-call-on-non-trivial-type
============================================
Flags use of the C standard library functions ``memset``, ``memcpy`` and
``memcmp`` and similar derivatives on non-trivial types.
The check will detect the following functions: ``memset``, ``std::memset``,
``std::memcpy``, ``memcpy``, ``std::memmove``, ``memmove``, ``std::strcpy``,
``strcpy``, ``memccpy``, ``stpncpy``, ``strncpy``, ``std::memcmp``, ``memcmp``,
``std::strcmp``, ``strcmp``, ``strncmp``.
Options
-------
.. option:: MemSetNames
Specify extra functions to flag that act similarly to ``memset``. Specify
names in a semicolon-delimited list. Default is an empty string.
.. option:: MemCpyNames
Specify extra functions to flag that act similarly to ``memcpy``. Specify
names in a semicolon-delimited list. Default is an empty string.
.. option:: MemCmpNames
Specify extra functions to flag that act similarly to ``memcmp``. Specify
names in a semicolon-delimited list. Default is an empty string.
This check corresponds to the CERT C++ Coding Standard rule
`OOP57-CPP. Prefer special member functions and overloaded operators to C
Standard Library functions
<https://wiki.sei.cmu.edu/confluence/display/cplusplus/OOP57-CPP.+Prefer+special+member+functions+and+overloaded+operators+to+C+Standard+Library+functions>`_.

View File

@ -1,38 +1,13 @@
.. title:: clang-tidy - cert-oop57-cpp
.. meta::
:http-equiv=refresh: 5;URL=../bugprone/raw-memory-call-on-non-trivial-type.html
cert-oop57-cpp
==============
Flags use of the `C` standard library functions ``memset``, ``memcpy`` and
``memcmp`` and similar derivatives on non-trivial types.
Options
-------
.. option:: MemSetNames
Specify extra functions to flag that act similarly to ``memset``.
Specify names in a semicolon delimited list.
Default is an empty string.
The check will detect the following functions:
`memset`, `std::memset`.
.. option:: MemCpyNames
Specify extra functions to flag that act similarly to ``memcpy``.
Specify names in a semicolon delimited list.
Default is an empty string.
The check will detect the following functions:
`std::memcpy`, `memcpy`, `std::memmove`, `memmove`, `std::strcpy`,
`strcpy`, `memccpy`, `stpncpy`, `strncpy`.
.. option:: MemCmpNames
Specify extra functions to flag that act similarly to ``memcmp``.
Specify names in a semicolon delimited list.
Default is an empty string.
The check will detect the following functions:
`std::memcmp`, `memcmp`, `std::strcmp`, `strcmp`, `strncmp`.
The `cert-oop57-cpp` check is an alias, please see
`bugprone-raw-memory-call-on-non-trivial-type <../bugprone/raw-memory-call-on-non-trivial-type.html>`_
for more information.
This check corresponds to the CERT C++ Coding Standard rule
`OOP57-CPP. Prefer special member functions and overloaded operators to C

View File

@ -129,6 +129,7 @@ Clang-Tidy Checks
:doc:`bugprone-parent-virtual-call <bugprone/parent-virtual-call>`, "Yes"
:doc:`bugprone-pointer-arithmetic-on-polymorphic-object <bugprone/pointer-arithmetic-on-polymorphic-object>`,
:doc:`bugprone-posix-return <bugprone/posix-return>`, "Yes"
:doc:`bugprone-raw-memory-call-on-non-trivial-type <bugprone/raw-memory-call-on-non-trivial-type>`,
:doc:`bugprone-redundant-branch-condition <bugprone/redundant-branch-condition>`, "Yes"
:doc:`bugprone-reserved-identifier <bugprone/reserved-identifier>`, "Yes"
:doc:`bugprone-return-const-ref-from-parameter <bugprone/return-const-ref-from-parameter>`,
@ -180,7 +181,6 @@ Clang-Tidy Checks
:doc:`cert-mem57-cpp <cert/mem57-cpp>`,
:doc:`cert-msc50-cpp <cert/msc50-cpp>`,
:doc:`cert-msc51-cpp <cert/msc51-cpp>`,
:doc:`cert-oop57-cpp <cert/oop57-cpp>`,
:doc:`cert-oop58-cpp <cert/oop58-cpp>`,
:doc:`concurrency-mt-unsafe <concurrency/mt-unsafe>`,
:doc:`concurrency-thread-canceltype-asynchronous <concurrency/thread-canceltype-asynchronous>`,
@ -442,8 +442,8 @@ Check aliases
:doc:`cert-dcl51-cpp <cert/dcl51-cpp>`, :doc:`bugprone-reserved-identifier <bugprone/reserved-identifier>`, "Yes"
:doc:`cert-dcl54-cpp <cert/dcl54-cpp>`, :doc:`misc-new-delete-overloads <misc/new-delete-overloads>`,
:doc:`cert-dcl59-cpp <cert/dcl59-cpp>`, :doc:`google-build-namespaces <google/build-namespaces>`,
:doc:`cert-err09-cpp <cert/err09-cpp>`, :doc:`misc-throw-by-value-catch-by-reference <misc/throw-by-value-catch-by-reference>`,
:doc:`cert-env33-c <cert/env33-c>`, :doc:`bugprone-command-processor <bugprone/command-processor>`,
:doc:`cert-err09-cpp <cert/err09-cpp>`, :doc:`misc-throw-by-value-catch-by-reference <misc/throw-by-value-catch-by-reference>`,
:doc:`cert-err34-c <cert/err34-c>`, :doc:`bugprone-unchecked-string-to-number-conversion <bugprone/unchecked-string-to-number-conversion>`,
:doc:`cert-err52-cpp <cert/err52-cpp>`, :doc:`modernize-avoid-setjmp-longjmp <modernize/avoid-setjmp-longjmp>`,
:doc:`cert-err58-cpp <cert/err58-cpp>`, :doc:`bugprone-throwing-static-initialization <bugprone/throwing-static-initialization>`,
@ -459,6 +459,7 @@ Check aliases
:doc:`cert-msc54-cpp <cert/msc54-cpp>`, :doc:`bugprone-signal-handler <bugprone/signal-handler>`,
:doc:`cert-oop11-cpp <cert/oop11-cpp>`, :doc:`performance-move-constructor-init <performance/move-constructor-init>`,
:doc:`cert-oop54-cpp <cert/oop54-cpp>`, :doc:`bugprone-unhandled-self-assignment <bugprone/unhandled-self-assignment>`,
:doc:`cert-oop57-cpp <cert/oop57-cpp>`, :doc:`bugprone-raw-memory-call-on-non-trivial-type <bugprone/raw-memory-call-on-non-trivial-type>`,
:doc:`cert-pos44-c <cert/pos44-c>`, :doc:`bugprone-bad-signal-to-kill-thread <bugprone/bad-signal-to-kill-thread>`,
:doc:`cert-pos47-c <cert/pos47-c>`, :doc:`concurrency-thread-canceltype-asynchronous <concurrency/thread-canceltype-asynchronous>`,
:doc:`cert-sig30-c <cert/sig30-c>`, :doc:`bugprone-signal-handler <bugprone/signal-handler>`,

View File

@ -1,8 +1,8 @@
// RUN: %check_clang_tidy %s cert-oop57-cpp %t -- \
// RUN: %check_clang_tidy %s bugprone-raw-memory-call-on-non-trivial-type %t -- \
// RUN: -config='{CheckOptions: \
// RUN: {cert-oop57-cpp.MemSetNames: mymemset, \
// RUN: cert-oop57-cpp.MemCpyNames: mymemcpy, \
// RUN: cert-oop57-cpp.MemCmpNames: mymemcmp}}' \
// RUN: {bugprone-raw-memory-call-on-non-trivial-type.MemSetNames: mymemset, \
// RUN: bugprone-raw-memory-call-on-non-trivial-type.MemCpyNames: mymemcpy, \
// RUN: bugprone-raw-memory-call-on-non-trivial-type.MemCmpNames: mymemcmp}}' \
// RUN: --
void mymemset(void *, unsigned char, decltype(sizeof(int)));