clang-tools  5.0.0
UndefinedMemoryManipulationCheck.cpp
Go to the documentation of this file.
1 //===--- UndefinedMemoryManipulationCheck.cpp - clang-tidy-----------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 
14 using namespace clang::ast_matchers;
15 
16 namespace clang {
17 namespace tidy {
18 namespace bugprone {
19 
20 namespace {
21 AST_MATCHER(CXXRecordDecl, isNotTriviallyCopyable) {
22  return !Node.isTriviallyCopyable();
23 }
24 } // namespace
25 
26 void UndefinedMemoryManipulationCheck::registerMatchers(MatchFinder *Finder) {
27  const auto NotTriviallyCopyableObject =
28  hasType(pointsTo(cxxRecordDecl(isNotTriviallyCopyable())));
29 
30  // Check whether destination object is not TriviallyCopyable.
31  // Applicable to all three memory manipulation functions.
32  Finder->addMatcher(callExpr(callee(functionDecl(hasAnyName(
33  "::memset", "::memcpy", "::memmove"))),
34  hasArgument(0, NotTriviallyCopyableObject))
35  .bind("dest"),
36  this);
37 
38  // Check whether source object is not TriviallyCopyable.
39  // Only applicable to memcpy() and memmove().
40  Finder->addMatcher(
41  callExpr(callee(functionDecl(hasAnyName("::memcpy", "::memmove"))),
42  hasArgument(1, NotTriviallyCopyableObject))
43  .bind("src"),
44  this);
45 }
46 
47 void UndefinedMemoryManipulationCheck::check(
48  const MatchFinder::MatchResult &Result) {
49  if (const auto *Destination = Result.Nodes.getNodeAs<CallExpr>("dest")) {
50  diag(Destination->getLocStart(), "undefined behavior, destination "
51  "object is not TriviallyCopyable");
52  }
53  if (const auto *Source = Result.Nodes.getNodeAs<CallExpr>("src")) {
54  diag(Source->getLocStart(), "undefined behavior, source object is not "
55  "TriviallyCopyable");
56  }
57 }
58 
59 } // namespace bugprone
60 } // namespace tidy
61 } // namespace clang
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:275
AST_MATCHER(VarDecl, isAsm)