11 #include "clang/AST/ASTContext.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 21 AST_MATCHER(CXXRecordDecl, isNotTriviallyCopyable) {
23 return Node.hasDefinition() ? !Node.isTriviallyCopyable() :
false;
27 void UndefinedMemoryManipulationCheck::registerMatchers(MatchFinder *Finder) {
28 const auto NotTriviallyCopyableObject =
29 hasType(ast_matchers::hasCanonicalType(
30 pointsTo(cxxRecordDecl(isNotTriviallyCopyable()))));
34 Finder->addMatcher(callExpr(callee(functionDecl(hasAnyName(
35 "::memset",
"::memcpy",
"::memmove"))),
36 hasArgument(0, NotTriviallyCopyableObject))
43 callExpr(callee(functionDecl(hasAnyName(
"::memcpy",
"::memmove"))),
44 hasArgument(1, NotTriviallyCopyableObject))
49 void UndefinedMemoryManipulationCheck::check(
50 const MatchFinder::MatchResult &Result) {
51 if (
const auto *Call = Result.Nodes.getNodeAs<CallExpr>(
"dest")) {
52 QualType DestType = Call->getArg(0)->IgnoreImplicit()->getType();
53 if (!DestType->getPointeeType().isNull())
54 DestType = DestType->getPointeeType();
55 diag(Call->getLocStart(),
"undefined behavior, destination object type %0 " 56 "is not TriviallyCopyable")
59 if (
const auto *Call = Result.Nodes.getNodeAs<CallExpr>(
"src")) {
60 QualType SourceType = Call->getArg(1)->IgnoreImplicit()->getType();
61 if (!SourceType->getPointeeType().isNull())
62 SourceType = SourceType->getPointeeType();
63 diag(Call->getLocStart(),
64 "undefined behavior, source object type %0 is not TriviallyCopyable")
AST_MATCHER(VarDecl, isAsm)