clang-tools 22.0.0git
RawMemoryCallOnNonTrivialTypeCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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
11#include "clang/AST/Decl.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/ASTMatchers/ASTMatchers.h"
14#include "clang/ASTMatchers/ASTMatchersInternal.h"
15#include "clang/ASTMatchers/ASTMatchersMacros.h"
16#include "llvm/ADT/StringRef.h"
17
18using namespace clang::ast_matchers;
19
20namespace clang::tidy::bugprone {
21
22namespace {
23AST_MATCHER(CXXRecordDecl, isTriviallyDefaultConstructible) {
24 return Node.hasTrivialDefaultConstructor();
25}
26AST_MATCHER(CXXRecordDecl, isTriviallyCopyable) {
27 return Node.hasTrivialCopyAssignment() && Node.hasTrivialCopyConstructor();
28}
29} // namespace
30
31static const char BuiltinMemSet[] = "::std::memset;"
32 "::memset;";
33static const char BuiltinMemCpy[] = "::std::memcpy;"
34 "::memcpy;"
35 "::std::memmove;"
36 "::memmove;"
37 "::std::strcpy;"
38 "::strcpy;"
39 "::memccpy;"
40 "::stpncpy;"
41 "::strncpy;";
42static const char BuiltinMemCmp[] = "::std::memcmp;"
43 "::memcmp;"
44 "::std::strcmp;"
45 "::strcmp;"
46 "::strncmp;";
47static constexpr llvm::StringRef ComparisonOperators[] = {
48 "operator==", "operator!=", "operator<",
49 "operator>", "operator<=", "operator>="};
50
52 StringRef Name, ClangTidyContext *Context)
53 : ClangTidyCheck(Name, Context),
54 MemSetNames(Options.get("MemSetNames", "")),
55 MemCpyNames(Options.get("MemCpyNames", "")),
56 MemCmpNames(Options.get("MemCmpNames", "")) {}
57
60 Options.store(Opts, "MemSetNames", MemSetNames);
61 Options.store(Opts, "MemCpyNames", MemCpyNames);
62 Options.store(Opts, "MemCmpNames", MemCmpNames);
63}
64
66 using namespace ast_matchers::internal;
67 auto IsStructPointer = [](Matcher<CXXRecordDecl> Constraint = anything(),
68 bool Bind = false) {
69 return expr(unaryOperator(
70 hasOperatorName("&"),
71 hasUnaryOperand(declRefExpr(
72 hasType(cxxRecordDecl(Constraint)),
73 hasType(Bind ? qualType().bind("Record") : qualType())))));
74 };
75 auto IsRecordSizeOf =
76 expr(sizeOfExpr(hasArgumentOfType(equalsBoundNode("Record"))));
77 auto ArgChecker = [&](Matcher<CXXRecordDecl> RecordConstraint,
78 BindableMatcher<Stmt> SecondArg = expr()) {
79 return allOf(argumentCountIs(3),
80 hasArgument(0, IsStructPointer(RecordConstraint, true)),
81 hasArgument(1, SecondArg), hasArgument(2, IsRecordSizeOf));
82 };
83
84 Finder->addMatcher(
85 callExpr(callee(namedDecl(hasAnyName(
87 ArgChecker(unless(isTriviallyDefaultConstructible())))
88 .bind("lazyConstruct"),
89 this);
90 Finder->addMatcher(
91 callExpr(callee(namedDecl(hasAnyName(
93 ArgChecker(unless(isTriviallyCopyable()), IsStructPointer()))
94 .bind("lazyCopy"),
95 this);
96 Finder->addMatcher(
97 callExpr(callee(namedDecl(hasAnyName(
99 ArgChecker(hasMethod(hasAnyName(ComparisonOperators)),
100 IsStructPointer()))
101 .bind("lazyCompare"),
102 this);
103}
104
106 const MatchFinder::MatchResult &Result) {
107 if (const auto *Caller = Result.Nodes.getNodeAs<CallExpr>("lazyConstruct")) {
108 diag(Caller->getBeginLoc(), "calling %0 on a non-trivially default "
109 "constructible class is undefined")
110 << cast<NamedDecl>(Caller->getCalleeDecl());
111 }
112 if (const auto *Caller = Result.Nodes.getNodeAs<CallExpr>("lazyCopy")) {
113 diag(Caller->getBeginLoc(),
114 "calling %0 on a non-trivially copyable class is undefined")
115 << cast<NamedDecl>(Caller->getCalleeDecl());
116 }
117 if (const auto *Caller = Result.Nodes.getNodeAs<CallExpr>("lazyCompare")) {
118 diag(Caller->getBeginLoc(),
119 "consider using comparison operators instead of calling %0")
120 << cast<NamedDecl>(Caller->getCalleeDecl());
121 }
122}
123
124} // namespace clang::tidy::bugprone
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
static constexpr llvm::StringRef ComparisonOperators[]
AST_MATCHER(BinaryOperator, isRelationalOperator)
std::vector< StringRef > parseListPair(StringRef L, StringRef R)
llvm::StringMap< ClangTidyValue > OptionMap