clang-tools 20.0.0git
UndefinedMemoryManipulationCheck.cpp
Go to the documentation of this file.
1//===--- UndefinedMemoryManipulationCheck.cpp - clang-tidy-----------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12
13using namespace clang::ast_matchers;
14
15namespace clang::tidy::bugprone {
16
17namespace {
18AST_MATCHER(CXXRecordDecl, isNotTriviallyCopyable) {
19 // For incomplete types, assume they are TriviallyCopyable.
20 return Node.hasDefinition() ? !Node.isTriviallyCopyable() : false;
21}
22} // namespace
23
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)));
33
34 // Check whether destination object is not TriviallyCopyable.
35 // Applicable to all three memory manipulation functions.
36 Finder->addMatcher(callExpr(callee(functionDecl(hasAnyName(
37 "::memset", "::memcpy", "::memmove"))),
38 hasArgument(0, NotTriviallyCopyableObject))
39 .bind("dest"),
40 this);
41
42 // Check whether source object is not TriviallyCopyable.
43 // Only applicable to memcpy() and memmove().
44 Finder->addMatcher(
45 callExpr(callee(functionDecl(hasAnyName("::memcpy", "::memmove"))),
46 hasArgument(1, NotTriviallyCopyableObject))
47 .bind("src"),
48 this);
49}
50
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")
59 << DestType;
60 }
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")
67 << SourceType;
68 }
69}
70
71} // namespace clang::tidy::bugprone
::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)