10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
20 WarnOnlyIfThisHasSuspiciousField(
21 Options.get(
"WarnOnlyIfThisHasSuspiciousField", true)) {}
26 WarnOnlyIfThisHasSuspiciousField);
31 const auto IsUserDefined = cxxMethodDecl(
32 isDefinition(), unless(anyOf(isDeleted(), isImplicit(), isDefaulted())));
36 const auto HasReferenceParam =
37 cxxMethodDecl(hasParameter(0, parmVarDecl(hasType(referenceType()))));
41 const auto HasNoSelfCheck = cxxMethodDecl(unless(hasDescendant(
42 binaryOperation(hasAnyOperatorName(
"==",
"!="),
43 hasEitherOperand(ignoringParenCasts(cxxThisExpr()))))));
48 const auto HasNonTemplateSelfCopy = cxxMethodDecl(
49 ofClass(cxxRecordDecl(unless(hasAncestor(classTemplateDecl())))),
51 hasDescendant(cxxConstructExpr(hasDeclaration(cxxConstructorDecl(
52 isCopyConstructor(), ofClass(equalsBoundNode(
"class"))))))));
57 const auto HasTemplateSelfCopy = cxxMethodDecl(
58 ofClass(cxxRecordDecl(hasAncestor(classTemplateDecl()))),
60 varDecl(hasType(cxxRecordDecl(equalsBoundNode(
"class"))),
61 hasDescendant(parenListExpr()))),
62 hasDescendant(cxxUnresolvedConstructExpr(hasDescendant(declRefExpr(
63 hasType(cxxRecordDecl(equalsBoundNode(
"class")))))))));
68 const auto HasNoNestedSelfAssign =
69 cxxMethodDecl(unless(hasDescendant(cxxMemberCallExpr(callee(cxxMethodDecl(
70 hasName(
"operator="), ofClass(equalsBoundNode(
"class"))))))));
72 DeclarationMatcher AdditionalMatcher = cxxMethodDecl();
73 if (WarnOnlyIfThisHasSuspiciousField) {
75 const auto SmartPointerType = qualType(hasUnqualifiedDesugaredType(
76 recordType(hasDeclaration(classTemplateSpecializationDecl(
77 hasAnyName(
"::std::shared_ptr",
"::std::unique_ptr",
78 "::std::weak_ptr",
"::std::auto_ptr"),
79 templateArgumentCountIs(1))))));
85 AdditionalMatcher = cxxMethodDecl(ofClass(cxxRecordDecl(
86 has(fieldDecl(anyOf(hasType(pointerType()), hasType(SmartPointerType),
87 hasType(arrayType())))))));
90 Finder->addMatcher(cxxMethodDecl(ofClass(cxxRecordDecl().bind(
"class")),
91 isCopyAssignmentOperator(), IsUserDefined,
92 HasReferenceParam, HasNoSelfCheck,
93 unless(HasNonTemplateSelfCopy),
94 unless(HasTemplateSelfCopy),
95 HasNoNestedSelfAssign, AdditionalMatcher)
96 .bind(
"copyAssignmentOperator"),
101 const MatchFinder::MatchResult &Result) {
102 const auto *MatchedDecl =
103 Result.Nodes.getNodeAs<CXXMethodDecl>(
"copyAssignmentOperator");
104 diag(MatchedDecl->getLocation(),
105 "operator=() does not handle self-assignment properly");
llvm::SmallString< 256U > Name
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 registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
UnhandledSelfAssignmentCheck(StringRef Name, ClangTidyContext *Context)
llvm::StringMap< ClangTidyValue > OptionMap