gamesh411 updated this revision to Diff 432868. gamesh411 added a comment. Remove literal checking from the matcher for memset as well
There is no change in the result set on open source projects even without restricting the matches to literals. IMO this is more in line with the rule as it's written as @aaron.ballman mentioned. Updated the ReleaseNotes to reflect this. Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D126186/new/ https://reviews.llvm.org/D126186 Files: clang-tools-extra/clang-tidy/cert/NonTrivialTypesLibcMemoryCallsCheck.cpp clang-tools-extra/docs/ReleaseNotes.rst clang-tools-extra/test/clang-tidy/checkers/cert-oop57-cpp.cpp Index: clang-tools-extra/test/clang-tidy/checkers/cert-oop57-cpp.cpp =================================================================== --- clang-tools-extra/test/clang-tidy/checkers/cert-oop57-cpp.cpp +++ clang-tools-extra/test/clang-tidy/checkers/cert-oop57-cpp.cpp @@ -88,3 +88,17 @@ mymemcmp(&Data, &Other, sizeof(Data)); // 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 +} Index: clang-tools-extra/docs/ReleaseNotes.rst =================================================================== --- clang-tools-extra/docs/ReleaseNotes.rst +++ clang-tools-extra/docs/ReleaseNotes.rst @@ -156,6 +156,9 @@ <clang-tidy/checks/bugprone-sizeof-expression>` when `sizeof(...)` is 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 <clang-tidy/checks/cppcoreguidelines-prefer-member-initializer>` check. Index: clang-tools-extra/clang-tidy/cert/NonTrivialTypesLibcMemoryCallsCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/cert/NonTrivialTypesLibcMemoryCallsCheck.cpp +++ clang-tools-extra/clang-tidy/cert/NonTrivialTypesLibcMemoryCallsCheck.cpp @@ -80,7 +80,7 @@ auto IsRecordSizeOf = expr(sizeOfExpr(hasArgumentOfType(equalsBoundNode("Record")))); auto ArgChecker = [&](Matcher<CXXRecordDecl> RecordConstraint, - BindableMatcher<Stmt> SecondArg) { + BindableMatcher<Stmt> SecondArg = expr()) { return allOf(argumentCountIs(3), hasArgument(0, IsStructPointer(RecordConstraint, true)), hasArgument(1, SecondArg), hasArgument(2, IsRecordSizeOf)); @@ -89,8 +89,7 @@ Finder->addMatcher( callExpr(callee(namedDecl(hasAnyName( utils::options::parseListPair(BuiltinMemSet, MemSetNames)))), - ArgChecker(unless(isTriviallyDefaultConstructible()), - expr(integerLiteral(equals(0))))) + ArgChecker(unless(isTriviallyDefaultConstructible()))) .bind("lazyConstruct"), this); Finder->addMatcher(
Index: clang-tools-extra/test/clang-tidy/checkers/cert-oop57-cpp.cpp =================================================================== --- clang-tools-extra/test/clang-tidy/checkers/cert-oop57-cpp.cpp +++ clang-tools-extra/test/clang-tidy/checkers/cert-oop57-cpp.cpp @@ -88,3 +88,17 @@ mymemcmp(&Data, &Other, sizeof(Data)); // 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 +} Index: clang-tools-extra/docs/ReleaseNotes.rst =================================================================== --- clang-tools-extra/docs/ReleaseNotes.rst +++ clang-tools-extra/docs/ReleaseNotes.rst @@ -156,6 +156,9 @@ <clang-tidy/checks/bugprone-sizeof-expression>` when `sizeof(...)` is 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 <clang-tidy/checks/cppcoreguidelines-prefer-member-initializer>` check. Index: clang-tools-extra/clang-tidy/cert/NonTrivialTypesLibcMemoryCallsCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/cert/NonTrivialTypesLibcMemoryCallsCheck.cpp +++ clang-tools-extra/clang-tidy/cert/NonTrivialTypesLibcMemoryCallsCheck.cpp @@ -80,7 +80,7 @@ auto IsRecordSizeOf = expr(sizeOfExpr(hasArgumentOfType(equalsBoundNode("Record")))); auto ArgChecker = [&](Matcher<CXXRecordDecl> RecordConstraint, - BindableMatcher<Stmt> SecondArg) { + BindableMatcher<Stmt> SecondArg = expr()) { return allOf(argumentCountIs(3), hasArgument(0, IsStructPointer(RecordConstraint, true)), hasArgument(1, SecondArg), hasArgument(2, IsRecordSizeOf)); @@ -89,8 +89,7 @@ Finder->addMatcher( callExpr(callee(namedDecl(hasAnyName( utils::options::parseListPair(BuiltinMemSet, MemSetNames)))), - ArgChecker(unless(isTriviallyDefaultConstructible()), - expr(integerLiteral(equals(0))))) + ArgChecker(unless(isTriviallyDefaultConstructible()))) .bind("lazyConstruct"), this); Finder->addMatcher(
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits