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