diff options
| author | Gabor Horvath <xazax.hun@gmail.com> | 2017-07-14 12:20:19 +0000 |
|---|---|---|
| committer | Gabor Horvath <xazax.hun@gmail.com> | 2017-07-14 12:20:19 +0000 |
| commit | 46a9db45c64ef98ad4cb51ac7ada2c650a48b755 (patch) | |
| tree | f8b529045488ed871fb213e6f84fbe01ed2b8d05 /clang-tools-extra/clang-tidy/bugprone/UndefinedMemoryManipulationCheck.cpp | |
| parent | 829e75a0377403cd94204d74c4f1e94fca68c96b (diff) | |
| download | bcm5719-llvm-46a9db45c64ef98ad4cb51ac7ada2c650a48b755.tar.gz bcm5719-llvm-46a9db45c64ef98ad4cb51ac7ada2c650a48b755.zip | |
[clang-tidy] Add bugprone-undefined-memory-manipulation check
Patch by: Reka Nikolett Kovacs
Differential Revision: https://reviews.llvm.org/D35051
llvm-svn: 308021
Diffstat (limited to 'clang-tools-extra/clang-tidy/bugprone/UndefinedMemoryManipulationCheck.cpp')
| -rw-r--r-- | clang-tools-extra/clang-tidy/bugprone/UndefinedMemoryManipulationCheck.cpp | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/clang-tools-extra/clang-tidy/bugprone/UndefinedMemoryManipulationCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/UndefinedMemoryManipulationCheck.cpp new file mode 100644 index 00000000000..cbc1f34cfca --- /dev/null +++ b/clang-tools-extra/clang-tidy/bugprone/UndefinedMemoryManipulationCheck.cpp @@ -0,0 +1,61 @@ +//===--- UndefinedMemoryManipulationCheck.cpp - clang-tidy-----------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "UndefinedMemoryManipulationCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace bugprone { + +namespace { +AST_MATCHER(CXXRecordDecl, isNotTriviallyCopyable) { + return !Node.isTriviallyCopyable(); +} +} // namespace + +void UndefinedMemoryManipulationCheck::registerMatchers(MatchFinder *Finder) { + const auto NotTriviallyCopyableObject = + hasType(pointsTo(cxxRecordDecl(isNotTriviallyCopyable()))); + + // Check whether destination object is not TriviallyCopyable. + // Applicable to all three memory manipulation functions. + Finder->addMatcher(callExpr(callee(functionDecl(hasAnyName( + "::memset", "::memcpy", "::memmove"))), + hasArgument(0, NotTriviallyCopyableObject)) + .bind("dest"), + this); + + // Check whether source object is not TriviallyCopyable. + // Only applicable to memcpy() and memmove(). + Finder->addMatcher( + callExpr(callee(functionDecl(hasAnyName("::memcpy", "::memmove"))), + hasArgument(1, NotTriviallyCopyableObject)) + .bind("src"), + this); +} + +void UndefinedMemoryManipulationCheck::check( + const MatchFinder::MatchResult &Result) { + if (const auto *Destination = Result.Nodes.getNodeAs<CallExpr>("dest")) { + diag(Destination->getLocStart(), "undefined behavior, destination " + "object is not TriviallyCopyable"); + } + if (const auto *Source = Result.Nodes.getNodeAs<CallExpr>("src")) { + diag(Source->getLocStart(), "undefined behavior, source object is not " + "TriviallyCopyable"); + } +} + +} // namespace bugprone +} // namespace tidy +} // namespace clang |

