Skip to content
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
236cee6
[clang-tidy] Add new alias 'libc-memory-calls-on-nontrivial-types' fo…
Oct 5, 2025
292cafb
Update clang-tools-extra/docs/clang-tidy/checks/bugprone/libc-memory-…
dvbuka Oct 6, 2025
a928e61
Format function names properly
dvbuka Oct 6, 2025
d0ba4b3
Update clang-tools-extra/docs/clang-tidy/checks/cert/oop57-cpp.rst
dvbuka Oct 20, 2025
95fd6b5
Update clang-tools-extra/docs/clang-tidy/checks/cert/oop57-cpp.rst
dvbuka Oct 20, 2025
f590f55
Add new line
dvbuka Oct 20, 2025
57a3583
Rename to check to raw-memory-call-on-nontrivial-type
dvbuka Oct 20, 2025
1e01e2c
Rename more references to new check name
dvbuka Oct 20, 2025
ad1afc4
Update title formatting for raw-memory-call check
dvbuka Oct 20, 2025
691131b
Rename last reference to RAWMEMORYCALLONNONTRIVIALTYPE
dvbuka Oct 20, 2025
bb8c690
nontrivial => non-trivial
dvbuka Oct 20, 2025
8f10a75
Update raw-memory-call-on-non-trivial-type.rst
dvbuka Oct 20, 2025
59ca5d0
Add Check to class name, register check in bugprone, and update name …
dvbuka Oct 20, 2025
11047c4
Clang format changes
dvbuka Oct 20, 2025
e326e07
Move extra functions description out of each individual option
dvbuka Oct 22, 2025
5e54e7d
Merge branch 'main' into alias-for-oop57-cpp
dvbuka Oct 22, 2025
70f1319
Revert "Move extra functions description out of each individual option"
dvbuka Oct 22, 2025
ef26db9
Move default checks out of options
dvbuka Oct 22, 2025
f82761e
Update clang-tools-extra/docs/clang-tidy/checks/bugprone/raw-memory-c…
dvbuka Oct 22, 2025
0a1cdaf
Update clang-tools-extra/docs/clang-tidy/checks/bugprone/raw-memory-c…
dvbuka Oct 22, 2025
13f2caf
Update clang-tools-extra/docs/clang-tidy/checks/bugprone/raw-memory-c…
dvbuka Oct 22, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -216,6 +217,8 @@ class BugproneModule : public ClangTidyModule {
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>(
Expand Down
1 change: 1 addition & 0 deletions clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ add_clang_library(clangTidyBugproneModule STATIC
ParentVirtualCallCheck.cpp
PointerArithmeticOnPolymorphicObjectCheck.cpp
PosixReturnCheck.cpp
RawMemoryCallOnNonTrivialTypeCheck.cpp
RedundantBranchConditionCheck.cpp
ReservedIdentifierCheck.cpp
ReturnConstRefFromParameterCheck.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "NonTrivialTypesLibcMemoryCallsCheck.h"
#include "RawMemoryCallOnNonTrivialTypeCheck.h"
#include "../utils/OptionsUtils.h"
#include "clang/AST/Decl.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
Expand All @@ -17,7 +17,7 @@

using namespace clang::ast_matchers;

namespace clang::tidy::cert {
namespace clang::tidy::bugprone {

namespace {
AST_MATCHER(CXXRecordDecl, isTriviallyDefaultConstructible) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 "
Expand All @@ -122,4 +121,4 @@ void NonTrivialTypesLibcMemoryCallsCheck::check(
}
}

} // namespace clang::tidy::cert
} // namespace clang::tidy::bugprone
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -35,6 +34,6 @@ class NonTrivialTypesLibcMemoryCallsCheck : public ClangTidyCheck {
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
4 changes: 2 additions & 2 deletions clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -39,7 +40,6 @@
#include "FloatLoopCounter.h"
#include "LimitedRandomnessCheck.h"
#include "MutatingCopyCheck.h"
#include "NonTrivialTypesLibcMemoryCallsCheck.h"
#include "ProperlySeededRandomGeneratorCheck.h"
#include "ThrownExceptionTypeCheck.h"

Expand Down Expand Up @@ -278,7 +278,7 @@ class CERTModule : public ClangTidyModule {
"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");

Expand Down
1 change: 0 additions & 1 deletion clang-tools-extra/clang-tidy/cert/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ add_clang_library(clangTidyCERTModule STATIC
FloatLoopCounter.cpp
LimitedRandomnessCheck.cpp
MutatingCopyCheck.cpp
NonTrivialTypesLibcMemoryCallsCheck.cpp
ProperlySeededRandomGeneratorCheck.cpp
ThrownExceptionTypeCheck.cpp

Expand Down
5 changes: 5 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,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
^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
.. 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>`_.
35 changes: 5 additions & 30 deletions clang-tools-extra/docs/clang-tidy/checks/cert/oop57-cpp.rst
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 3 additions & 2 deletions clang-tools-extra/docs/clang-tidy/checks/list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ Clang-Tidy Checks
:doc:`bugprone-non-zero-enum-to-bool-conversion <bugprone/non-zero-enum-to-bool-conversion>`,
:doc:`bugprone-nondeterministic-pointer-iteration-order <bugprone/nondeterministic-pointer-iteration-order>`,
:doc:`bugprone-not-null-terminated-result <bugprone/not-null-terminated-result>`, "Yes"
:doc:`bugprone-raw-memory-call-on-non-trivial-type <bugprone/raw-memory-call-on-non-trivial-type>`,
:doc:`bugprone-optional-value-conversion <bugprone/optional-value-conversion>`, "Yes"
:doc:`bugprone-parent-virtual-call <bugprone/parent-virtual-call>`, "Yes"
:doc:`bugprone-pointer-arithmetic-on-polymorphic-object <bugprone/pointer-arithmetic-on-polymorphic-object>`,
Expand Down Expand Up @@ -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>`,
Expand Down Expand Up @@ -441,8 +441,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>`,
Expand All @@ -458,6 +458,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>`,
Expand Down
Original file line number Diff line number Diff line change
@@ -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)));
Expand Down