xref: /llvm-project/clang-tools-extra/clang-tidy/bugprone/UndefinedMemoryManipulationCheck.cpp (revision 6780d53f41fbd76dbf3c4a89467856ea2c7ca1dc)
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 
9 #include "UndefinedMemoryManipulationCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 
13 using namespace clang::ast_matchers;
14 
15 namespace clang::tidy::bugprone {
16 
17 namespace {
AST_MATCHER(CXXRecordDecl,isNotTriviallyCopyable)18 AST_MATCHER(CXXRecordDecl, isNotTriviallyCopyable) {
19   // For incomplete types, assume they are TriviallyCopyable.
20   return Node.hasDefinition() ? !Node.isTriviallyCopyable() : false;
21 }
22 } // namespace
23 
registerMatchers(MatchFinder * Finder)24 void UndefinedMemoryManipulationCheck::registerMatchers(MatchFinder *Finder) {
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 
check(const MatchFinder::MatchResult & Result)51 void UndefinedMemoryManipulationCheck::check(
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
72