1//===--- NonTrivialTypesLibcMemoryCallsCheck.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 "NonTrivialTypesLibcMemoryCallsCheck.h"
10#include "../utils/OptionsUtils.h"
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/SmallVector.h"
17#include "llvm/ADT/StringRef.h"
18#include "llvm/Support/raw_ostream.h"
19
20using namespace clang::ast_matchers;
21
22namespace clang::tidy::cert {
23
24namespace {
25AST_MATCHER(CXXRecordDecl, isTriviallyDefaultConstructible) {
26 return Node.hasTrivialDefaultConstructor();
27}
28AST_MATCHER(CXXRecordDecl, isTriviallyCopyable) {
29 return Node.hasTrivialCopyAssignment() && Node.hasTrivialCopyConstructor();
30}
31} // namespace
32
33static const char BuiltinMemSet[] = "::std::memset;"
34 "::memset;";
35static const char BuiltinMemCpy[] = "::std::memcpy;"
36 "::memcpy;"
37 "::std::memmove;"
38 "::memmove;"
39 "::std::strcpy;"
40 "::strcpy;"
41 "::memccpy;"
42 "::stpncpy;"
43 "::strncpy;";
44static const char BuiltinMemCmp[] = "::std::memcmp;"
45 "::memcmp;"
46 "::std::strcmp;"
47 "::strcmp;"
48 "::strncmp;";
49static constexpr llvm::StringRef ComparisonOperators[] = {
50 "operator==", "operator!=", "operator<",
51 "operator>", "operator<=", "operator>="};
52
53NonTrivialTypesLibcMemoryCallsCheck::NonTrivialTypesLibcMemoryCallsCheck(
54 StringRef Name, ClangTidyContext *Context)
55 : ClangTidyCheck(Name, Context),
56 MemSetNames(Options.get(LocalName: "MemSetNames", Default: "")),
57 MemCpyNames(Options.get(LocalName: "MemCpyNames", Default: "")),
58 MemCmpNames(Options.get(LocalName: "MemCmpNames", Default: "")) {}
59
60void NonTrivialTypesLibcMemoryCallsCheck::storeOptions(
61 ClangTidyOptions::OptionMap &Opts) {
62 Options.store(Options&: Opts, LocalName: "MemSetNames", Value: MemSetNames);
63 Options.store(Options&: Opts, LocalName: "MemCpyNames", Value: MemCpyNames);
64 Options.store(Options&: Opts, LocalName: "MemCmpNames", Value: MemCmpNames);
65}
66
67void NonTrivialTypesLibcMemoryCallsCheck::registerMatchers(
68 MatchFinder *Finder) {
69 using namespace ast_matchers::internal;
70 auto IsStructPointer = [](Matcher<CXXRecordDecl> Constraint = anything(),
71 bool Bind = false) {
72 return expr(unaryOperator(
73 hasOperatorName(Name: "&"),
74 hasUnaryOperand(InnerMatcher: declRefExpr(
75 hasType(InnerMatcher: cxxRecordDecl(Constraint)),
76 hasType(InnerMatcher: Bind ? qualType().bind(ID: "Record") : qualType())))));
77 };
78 auto IsRecordSizeOf =
79 expr(sizeOfExpr(InnerMatcher: hasArgumentOfType(InnerMatcher: equalsBoundNode(ID: "Record"))));
80 auto ArgChecker = [&](Matcher<CXXRecordDecl> RecordConstraint,
81 BindableMatcher<Stmt> SecondArg = expr()) {
82 return allOf(argumentCountIs(N: 3),
83 hasArgument(N: 0, InnerMatcher: IsStructPointer(RecordConstraint, true)),
84 hasArgument(N: 1, InnerMatcher: SecondArg), hasArgument(N: 2, InnerMatcher: IsRecordSizeOf));
85 };
86
87 Finder->addMatcher(
88 NodeMatch: callExpr(callee(InnerMatcher: namedDecl(hasAnyName(
89 utils::options::parseListPair(L: BuiltinMemSet, R: MemSetNames)))),
90 ArgChecker(unless(isTriviallyDefaultConstructible())))
91 .bind(ID: "lazyConstruct"),
92 Action: this);
93 Finder->addMatcher(
94 NodeMatch: callExpr(callee(InnerMatcher: namedDecl(hasAnyName(
95 utils::options::parseListPair(L: BuiltinMemCpy, R: MemCpyNames)))),
96 ArgChecker(unless(isTriviallyCopyable()), IsStructPointer()))
97 .bind(ID: "lazyCopy"),
98 Action: this);
99 Finder->addMatcher(
100 NodeMatch: callExpr(callee(InnerMatcher: namedDecl(hasAnyName(
101 utils::options::parseListPair(L: BuiltinMemCmp, R: MemCmpNames)))),
102 ArgChecker(hasMethod(InnerMatcher: hasAnyName(ComparisonOperators)),
103 IsStructPointer()))
104 .bind(ID: "lazyCompare"),
105 Action: this);
106}
107
108void NonTrivialTypesLibcMemoryCallsCheck::check(
109 const MatchFinder::MatchResult &Result) {
110 if (const auto *Caller = Result.Nodes.getNodeAs<CallExpr>(ID: "lazyConstruct")) {
111 diag(Loc: Caller->getBeginLoc(), Description: "calling %0 on a non-trivially default "
112 "constructible class is undefined")
113 << cast<NamedDecl>(Val: Caller->getCalleeDecl());
114 }
115 if (const auto *Caller = Result.Nodes.getNodeAs<CallExpr>(ID: "lazyCopy")) {
116 diag(Loc: Caller->getBeginLoc(),
117 Description: "calling %0 on a non-trivially copyable class is undefined")
118 << cast<NamedDecl>(Val: Caller->getCalleeDecl());
119 }
120 if (const auto *Caller = Result.Nodes.getNodeAs<CallExpr>(ID: "lazyCompare")) {
121 diag(Loc: Caller->getBeginLoc(),
122 Description: "consider using comparison operators instead of calling %0")
123 << cast<NamedDecl>(Val: Caller->getCalleeDecl());
124 }
125}
126
127} // namespace clang::tidy::cert
128

source code of clang-tools-extra/clang-tidy/cert/NonTrivialTypesLibcMemoryCallsCheck.cpp