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"
25AST_MATCHER(CXXRecordDecl, isTriviallyDefaultConstructible) {
26 return Node.hasTrivialDefaultConstructor();
29 return Node.hasTrivialCopyAssignment() &&
Node.hasTrivialCopyConstructor();
50 "operator==",
"operator!=",
"operator<",
51 "operator>",
"operator<=",
"operator>="};
56 MemSetNames(Options.get(
"MemSetNames",
"")),
57 MemCpyNames(Options.get(
"MemCpyNames",
"")),
58 MemCmpNames(Options.get(
"MemCmpNames",
"")) {}
68 MatchFinder *Finder) {
69 using namespace ast_matchers::internal;
70 auto IsStructPointer = [](Matcher<CXXRecordDecl> Constraint = anything(),
72 return expr(unaryOperator(
74 hasUnaryOperand(declRefExpr(
75 hasType(cxxRecordDecl(Constraint)),
76 hasType(Bind ? qualType().bind(
"Record") : qualType())))));
79 expr(sizeOfExpr(hasArgumentOfType(equalsBoundNode(
"Record"))));
80 auto ArgChecker = [&](Matcher<CXXRecordDecl> RecordConstraint,
81 BindableMatcher<Stmt> SecondArg = expr()) {
82 return allOf(argumentCountIs(3),
83 hasArgument(0, IsStructPointer(RecordConstraint,
true)),
84 hasArgument(1, SecondArg), hasArgument(2, IsRecordSizeOf));
88 callExpr(callee(namedDecl(hasAnyName(
90 ArgChecker(unless(isTriviallyDefaultConstructible())))
91 .bind(
"lazyConstruct"),
94 callExpr(callee(namedDecl(hasAnyName(
96 ArgChecker(unless(isTriviallyCopyable()), IsStructPointer()))
100 callExpr(callee(namedDecl(hasAnyName(
104 .bind(
"lazyCompare"),
109 const MatchFinder::MatchResult &Result) {
110 if (
const auto *Caller = Result.Nodes.getNodeAs<CallExpr>(
"lazyConstruct")) {
111 diag(Caller->getBeginLoc(),
"calling %0 on a non-trivially default "
112 "constructible class is undefined")
113 << cast<NamedDecl>(Caller->getCalleeDecl());
115 if (
const auto *Caller = Result.Nodes.getNodeAs<CallExpr>(
"lazyCopy")) {
116 diag(Caller->getBeginLoc(),
117 "calling %0 on a non-trivially copyable class is undefined")
118 << cast<NamedDecl>(Caller->getCalleeDecl());
120 if (
const auto *Caller = Result.Nodes.getNodeAs<CallExpr>(
"lazyCompare")) {
121 diag(Caller->getBeginLoc(),
122 "consider using comparison operators instead of calling %0")
123 << cast<NamedDecl>(Caller->getCalleeDecl());
llvm::SmallString< 256U > Name
::clang::DynTypedNode Node
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
NonTrivialTypesLibcMemoryCallsCheck(StringRef Name, ClangTidyContext *Context)
AST_MATCHER(Expr, isMacroID)
static const char BuiltinMemCpy[]
static const char BuiltinMemCmp[]
static constexpr llvm::StringRef ComparisonOperators[]
static const char BuiltinMemSet[]
std::vector< StringRef > parseListPair(StringRef L, StringRef R)
llvm::StringMap< ClangTidyValue > OptionMap