Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
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 @@ -43,6 +43,7 @@ add_clang_library(clangTidyBugproneModule STATIC
InfiniteLoopCheck.cpp
IntegerDivisionCheck.cpp
LambdaFunctionNameCheck.cpp
LibcMemoryCallsOnNonTrivialTypesCheck.cpp
MacroParenthesesCheck.cpp
MacroRepeatedSideEffectsCheck.cpp
MisleadingSetterOfReferenceCheck.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "NonTrivialTypesLibcMemoryCallsCheck.h"
#include "LibcMemoryCallsOnNonTrivialTypesCheck.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,21 +48,21 @@ static constexpr llvm::StringRef ComparisonOperators[] = {
"operator==", "operator!=", "operator<",
"operator>", "operator<=", "operator>="};

NonTrivialTypesLibcMemoryCallsCheck::NonTrivialTypesLibcMemoryCallsCheck(
LibcMemoryCallsOnNonTrivialTypesCheck::LibcMemoryCallsOnNonTrivialTypesCheck(
StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
MemSetNames(Options.get("MemSetNames", "")),
MemCpyNames(Options.get("MemCpyNames", "")),
MemCmpNames(Options.get("MemCmpNames", "")) {}

void NonTrivialTypesLibcMemoryCallsCheck::storeOptions(
void LibcMemoryCallsOnNonTrivialTypesCheck::storeOptions(
ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "MemSetNames", MemSetNames);
Options.store(Opts, "MemCpyNames", MemCpyNames);
Options.store(Opts, "MemCmpNames", MemCmpNames);
}

void NonTrivialTypesLibcMemoryCallsCheck::registerMatchers(
void LibcMemoryCallsOnNonTrivialTypesCheck::registerMatchers(
MatchFinder *Finder) {
using namespace ast_matchers::internal;
auto IsStructPointer = [](Matcher<CXXRecordDecl> Constraint = anything(),
Expand Down Expand Up @@ -103,7 +103,7 @@ void NonTrivialTypesLibcMemoryCallsCheck::registerMatchers(
this);
}

void NonTrivialTypesLibcMemoryCallsCheck::check(
void LibcMemoryCallsOnNonTrivialTypesCheck::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 +122,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,21 +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_NONTRIVIALTYPESLIBCMEMORYCALLSCHECK_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_NONTRIVIALTYPESLIBCMEMORYCALLSCHECK_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/libc-memory-calls-on-nontrivial-types.html
class LibcMemoryCallsOnNonTrivialTypesCheck : public ClangTidyCheck {
public:
NonTrivialTypesLibcMemoryCallsCheck(StringRef Name,
LibcMemoryCallsOnNonTrivialTypesCheck(StringRef Name,
ClangTidyContext *Context);
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
return LangOpts.CPlusPlus && !LangOpts.ObjC;
Expand All @@ -35,6 +35,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_NONTRIVIALTYPESLIBCMEMORYCALLSCHECK_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 @@ -11,6 +11,7 @@
#include "../ClangTidyModuleRegistry.h"
#include "../bugprone/BadSignalToKillThreadCheck.h"
#include "../bugprone/CommandProcessorCheck.h"
#include "../bugprone/LibcMemoryCallsOnNonTrivialTypesCheck.h"
#include "../bugprone/PointerArithmeticOnPolymorphicObjectCheck.h"
#include "../bugprone/ReservedIdentifierCheck.h"
#include "../bugprone/SignalHandlerCheck.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::LibcMemoryCallsOnNonTrivialTypesCheck>(
"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 @@ -241,6 +241,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-libc-memory-calls-on-nontrivial-types
<clang-tidy/checks/bugprone/libc-memory-calls-on-nontrivial-types>`
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,40 @@
.. title:: clang-tidy - bugprone-libc-memory-calls-on-nontrivial-types

bugprone-libc-memory-calls-on-nontrivial-types
==============================================

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``.

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>`_.
40 changes: 5 additions & 35 deletions clang-tools-extra/docs/clang-tidy/checks/cert/oop57-cpp.rst
Original file line number Diff line number Diff line change
@@ -1,40 +1,10 @@
.. title:: clang-tidy - cert-oop57-cpp
.. meta::
:http-equiv=refresh: 5;URL=../bugprone/libc-memory-calls-on-nontrivial-types.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`.

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>`_.
The cert-oop57-cpp check is an alias, please see
`bugprone-libc-memory-calls-on-nontrivial-types <../bugprone/libc-memory-calls-on-nontrivial-types.html>`_
for more information.
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-libc-memory-calls-on-nontrivial-types <bugprone/libc-memory-calls-on-nontrivial-types>`,
: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 @@ -440,8 +440,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 @@ -457,6 +457,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-libc-memory-calls-on-nontrivial-types <bugprone/libc-memory-calls-on-nontrivial-types>`,
: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 libc-memory-calls-on-nontrivial-types %t -- \
// RUN: -config='{CheckOptions: \
// RUN: {cert-oop57-cpp.MemSetNames: mymemset, \
// RUN: cert-oop57-cpp.MemCpyNames: mymemcpy, \
// RUN: cert-oop57-cpp.MemCmpNames: mymemcmp}}' \
// RUN: {libc-memory-calls-on-nontrivial-types.MemSetNames: mymemset, \
// RUN: libc-memory-calls-on-nontrivial-types.MemCpyNames: mymemcpy, \
// RUN: libc-memory-calls-on-nontrivial-types.MemCmpNames: mymemcmp}}' \
// RUN: --

void mymemset(void *, unsigned char, decltype(sizeof(int)));
Expand Down