10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
20 return Node.hasDefinition() ? !
Node.isTriviallyCopyable() :
false;
25 const auto HasNotTriviallyCopyableDecl =
26 hasDeclaration(cxxRecordDecl(isNotTriviallyCopyable()));
27 const auto ArrayOfNotTriviallyCopyable =
28 arrayType(hasElementType(HasNotTriviallyCopyableDecl));
29 const auto NotTriviallyCopyableObject = hasType(hasCanonicalType(
30 anyOf(pointsTo(qualType(anyOf(HasNotTriviallyCopyableDecl,
31 ArrayOfNotTriviallyCopyable))),
32 ArrayOfNotTriviallyCopyable)));
36 Finder->addMatcher(callExpr(callee(functionDecl(hasAnyName(
37 "::memset",
"::memcpy",
"::memmove"))),
38 hasArgument(0, NotTriviallyCopyableObject))
45 callExpr(callee(functionDecl(hasAnyName(
"::memcpy",
"::memmove"))),
46 hasArgument(1, NotTriviallyCopyableObject))
52 const MatchFinder::MatchResult &Result) {
53 if (
const auto *Call = Result.Nodes.getNodeAs<CallExpr>(
"dest")) {
54 QualType DestType = Call->getArg(0)->IgnoreImplicit()->getType();
55 if (!DestType->getPointeeType().isNull())
56 DestType = DestType->getPointeeType();
57 diag(Call->getBeginLoc(),
"undefined behavior, destination object type %0 "
58 "is not TriviallyCopyable")
61 if (
const auto *Call = Result.Nodes.getNodeAs<CallExpr>(
"src")) {
62 QualType SourceType = Call->getArg(1)->IgnoreImplicit()->getType();
63 if (!SourceType->getPointeeType().isNull())
64 SourceType = SourceType->getPointeeType();
65 diag(Call->getBeginLoc(),
66 "undefined behavior, source object type %0 is not TriviallyCopyable")
::clang::DynTypedNode Node
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
AST_MATCHER(clang::VarDecl, hasConstantDeclaration)