clang-tools 22.0.0git
NonTrivialTypesLibcMemoryCallsCheck.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::cert {
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 MatchFinder *Finder) {
67 using namespace ast_matchers::internal;
68 auto IsStructPointer = [](Matcher<CXXRecordDecl> Constraint = anything(),
69 bool Bind = false) {
70 return expr(unaryOperator(
71 hasOperatorName("&"),
72 hasUnaryOperand(declRefExpr(
73 hasType(cxxRecordDecl(Constraint)),
74 hasType(Bind ? qualType().bind("Record") : qualType())))));
75 };
76 auto IsRecordSizeOf =
77 expr(sizeOfExpr(hasArgumentOfType(equalsBoundNode("Record"))));
78 auto ArgChecker = [&](Matcher<CXXRecordDecl> RecordConstraint,
79 BindableMatcher<Stmt> SecondArg = expr()) {
80 return allOf(argumentCountIs(3),
81 hasArgument(0, IsStructPointer(RecordConstraint, true)),
82 hasArgument(1, SecondArg), hasArgument(2, IsRecordSizeOf));
83 };
84
85 Finder->addMatcher(
86 callExpr(callee(namedDecl(hasAnyName(
88 ArgChecker(unless(isTriviallyDefaultConstructible())))
89 .bind("lazyConstruct"),
90 this);
91 Finder->addMatcher(
92 callExpr(callee(namedDecl(hasAnyName(
94 ArgChecker(unless(isTriviallyCopyable()), IsStructPointer()))
95 .bind("lazyCopy"),
96 this);
97 Finder->addMatcher(
98 callExpr(callee(namedDecl(hasAnyName(
100 ArgChecker(hasMethod(hasAnyName(ComparisonOperators)),
101 IsStructPointer()))
102 .bind("lazyCompare"),
103 this);
104}
105
107 const MatchFinder::MatchResult &Result) {
108 if (const auto *Caller = Result.Nodes.getNodeAs<CallExpr>("lazyConstruct")) {
109 diag(Caller->getBeginLoc(), "calling %0 on a non-trivially default "
110 "constructible class is undefined")
111 << cast<NamedDecl>(Caller->getCalleeDecl());
112 }
113 if (const auto *Caller = Result.Nodes.getNodeAs<CallExpr>("lazyCopy")) {
114 diag(Caller->getBeginLoc(),
115 "calling %0 on a non-trivially copyable class is undefined")
116 << cast<NamedDecl>(Caller->getCalleeDecl());
117 }
118 if (const auto *Caller = Result.Nodes.getNodeAs<CallExpr>("lazyCompare")) {
119 diag(Caller->getBeginLoc(),
120 "consider using comparison operators instead of calling %0")
121 << cast<NamedDecl>(Caller->getCalleeDecl());
122 }
123}
124
125} // namespace clang::tidy::cert
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
NonTrivialTypesLibcMemoryCallsCheck(StringRef Name, ClangTidyContext *Context)
static constexpr llvm::StringRef ComparisonOperators[]
AST_MATCHER(BinaryOperator, isRelationalOperator)
std::vector< StringRef > parseListPair(StringRef L, StringRef R)
llvm::StringMap< ClangTidyValue > OptionMap