forked from OSchip/llvm-project
[clang-tidy] Extend cert-oop57-cpp to check non-zero memset values
Clang Tidy check cert-oop57-cpp now checks for arbitrary-valued arguments in memset expressions containing non-trivially default-constructible instances. Previously it only checked literal 0 values. Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D126186
This commit is contained in:
parent
57203af167
commit
d33f199910
|
@ -80,7 +80,7 @@ void NonTrivialTypesLibcMemoryCallsCheck::registerMatchers(
|
||||||
auto IsRecordSizeOf =
|
auto IsRecordSizeOf =
|
||||||
expr(sizeOfExpr(hasArgumentOfType(equalsBoundNode("Record"))));
|
expr(sizeOfExpr(hasArgumentOfType(equalsBoundNode("Record"))));
|
||||||
auto ArgChecker = [&](Matcher<CXXRecordDecl> RecordConstraint,
|
auto ArgChecker = [&](Matcher<CXXRecordDecl> RecordConstraint,
|
||||||
BindableMatcher<Stmt> SecondArg) {
|
BindableMatcher<Stmt> SecondArg = expr()) {
|
||||||
return allOf(argumentCountIs(3),
|
return allOf(argumentCountIs(3),
|
||||||
hasArgument(0, IsStructPointer(RecordConstraint, true)),
|
hasArgument(0, IsStructPointer(RecordConstraint, true)),
|
||||||
hasArgument(1, SecondArg), hasArgument(2, IsRecordSizeOf));
|
hasArgument(1, SecondArg), hasArgument(2, IsRecordSizeOf));
|
||||||
|
@ -89,8 +89,7 @@ void NonTrivialTypesLibcMemoryCallsCheck::registerMatchers(
|
||||||
Finder->addMatcher(
|
Finder->addMatcher(
|
||||||
callExpr(callee(namedDecl(hasAnyName(
|
callExpr(callee(namedDecl(hasAnyName(
|
||||||
utils::options::parseListPair(BuiltinMemSet, MemSetNames)))),
|
utils::options::parseListPair(BuiltinMemSet, MemSetNames)))),
|
||||||
ArgChecker(unless(isTriviallyDefaultConstructible()),
|
ArgChecker(unless(isTriviallyDefaultConstructible())))
|
||||||
expr(integerLiteral(equals(0)))))
|
|
||||||
.bind("lazyConstruct"),
|
.bind("lazyConstruct"),
|
||||||
this);
|
this);
|
||||||
Finder->addMatcher(
|
Finder->addMatcher(
|
||||||
|
|
|
@ -156,6 +156,9 @@ Changes in existing checks
|
||||||
<clang-tidy/checks/bugprone-sizeof-expression>` when `sizeof(...)` is
|
<clang-tidy/checks/bugprone-sizeof-expression>` when `sizeof(...)` is
|
||||||
compared against a `__int128_t`.
|
compared against a `__int128_t`.
|
||||||
|
|
||||||
|
- Made :doc:`cert-oop57-cpp <clang-tidy/checks/cert-oop57-cpp>` more sensitive
|
||||||
|
by checking for an arbitrary expression in the second argument of `memset`.
|
||||||
|
|
||||||
- Improved :doc:`cppcoreguidelines-prefer-member-initializer
|
- Improved :doc:`cppcoreguidelines-prefer-member-initializer
|
||||||
<clang-tidy/checks/cppcoreguidelines-prefer-member-initializer>` check.
|
<clang-tidy/checks/cppcoreguidelines-prefer-member-initializer>` check.
|
||||||
|
|
||||||
|
|
|
@ -88,3 +88,17 @@ void baz(const NonTrivial &Other) {
|
||||||
mymemcmp(&Data, &Other, sizeof(Data));
|
mymemcmp(&Data, &Other, sizeof(Data));
|
||||||
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: consider using comparison operators instead of calling 'mymemcmp'
|
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: consider using comparison operators instead of calling 'mymemcmp'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void nonNullSetValue() {
|
||||||
|
NonTrivial Data;
|
||||||
|
// Check non-null-valued second argument.
|
||||||
|
std::memset(&Data, 1, sizeof(Data));
|
||||||
|
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: calling 'memset' on a non-trivially default constructible class is undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
void nonLiteralSetValue(char C) {
|
||||||
|
NonTrivial Data;
|
||||||
|
// Check non-literal second argument.
|
||||||
|
std::memset(&Data, C, sizeof(Data));
|
||||||
|
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: calling 'memset' on a non-trivially default constructible class is undefined
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue