10 #include "clang/AST/ASTContext.h" 11 #include "clang/ASTMatchers/ASTMatchFinder.h" 19 UnhandledSelfAssignmentCheck::UnhandledSelfAssignmentCheck(
22 WarnOnlyIfThisHasSuspiciousField(
23 Options.get(
"WarnOnlyIfThisHasSuspiciousField", true)) {}
28 WarnOnlyIfThisHasSuspiciousField);
36 const auto IsUserDefined = cxxMethodDecl(
37 isDefinition(), unless(anyOf(isDeleted(), isImplicit(), isDefaulted())));
41 const auto HasReferenceParam =
42 cxxMethodDecl(hasParameter(0, parmVarDecl(hasType(referenceType()))));
46 const auto HasNoSelfCheck = cxxMethodDecl(unless(hasDescendant(
47 binaryOperator(anyOf(hasOperatorName(
"=="), hasOperatorName(
"!=")),
48 has(ignoringParenCasts(cxxThisExpr()))))));
53 const auto HasNonTemplateSelfCopy = cxxMethodDecl(
54 ofClass(cxxRecordDecl(unless(hasAncestor(classTemplateDecl())))),
55 hasDescendant(cxxConstructExpr(hasDeclaration(cxxConstructorDecl(
56 isCopyConstructor(), ofClass(equalsBoundNode(
"class")))))));
61 const auto HasTemplateSelfCopy = cxxMethodDecl(
62 ofClass(cxxRecordDecl(hasAncestor(classTemplateDecl()))),
64 varDecl(hasType(cxxRecordDecl(equalsBoundNode(
"class"))),
65 hasDescendant(parenListExpr()))),
66 hasDescendant(cxxUnresolvedConstructExpr(hasDescendant(declRefExpr(
67 hasType(cxxRecordDecl(equalsBoundNode(
"class")))))))));
72 const auto HasNoNestedSelfAssign =
73 cxxMethodDecl(unless(hasDescendant(cxxMemberCallExpr(callee(cxxMethodDecl(
74 hasName(
"operator="), ofClass(equalsBoundNode(
"class"))))))));
76 DeclarationMatcher AdditionalMatcher = cxxMethodDecl();
77 if (WarnOnlyIfThisHasSuspiciousField) {
79 const auto SmartPointerType = qualType(hasUnqualifiedDesugaredType(
80 recordType(hasDeclaration(classTemplateSpecializationDecl(
81 hasAnyName(
"::std::shared_ptr",
"::std::unique_ptr",
82 "::std::weak_ptr",
"::std::auto_ptr"),
83 templateArgumentCountIs(1))))));
89 AdditionalMatcher = cxxMethodDecl(ofClass(cxxRecordDecl(
90 has(fieldDecl(anyOf(hasType(pointerType()), hasType(SmartPointerType),
91 hasType(arrayType())))))));
94 Finder->addMatcher(cxxMethodDecl(ofClass(cxxRecordDecl().bind(
"class")),
95 isCopyAssignmentOperator(), IsUserDefined,
96 HasReferenceParam, HasNoSelfCheck,
97 unless(HasNonTemplateSelfCopy),
98 unless(HasTemplateSelfCopy),
99 HasNoNestedSelfAssign, AdditionalMatcher)
100 .bind(
"copyAssignmentOperator"),
105 const MatchFinder::MatchResult &Result) {
106 const auto *MatchedDecl =
107 Result.Nodes.getNodeAs<CXXMethodDecl>(
"copyAssignmentOperator");
108 diag(MatchedDecl->getLocation(),
109 "operator=() does not handle self-assignment properly");
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.
Base class for all clang-tidy checks.
const LangOptions & getLangOpts() const
Returns the language options from the context.
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.
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.