10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/Basic/Diagnostic.h"
13#include "clang/Basic/SourceLocation.h"
14#include "clang/Lex/Lexer.h"
28 PreferResetCall(Options.get(
"PreferResetCall", false)) {}
32 auto UniquePtrWithDefaultDelete = classTemplateSpecializationDecl(
33 hasName(
"::std::unique_ptr"),
34 hasTemplateArgument(1, refersToType(hasDeclaration(cxxRecordDecl(
35 hasName(
"::std::default_delete"))))));
39 unless(isInTemplateInstantiation()),
40 has(cxxMemberCallExpr(
41 callee(memberExpr(hasObjectExpression(anyOf(
42 hasType(UniquePtrWithDefaultDelete),
44 UniquePtrWithDefaultDelete)))),
45 member(cxxMethodDecl(hasName(
"release"))))
46 .bind(
"release_expr")))
47 .bind(
"release_call")))
53 const MatchFinder::MatchResult &Result) {
54 const auto *DeleteExpr = Result.Nodes.getNodeAs<CXXDeleteExpr>(
"delete");
55 const auto *ReleaseExpr = Result.Nodes.getNodeAs<MemberExpr>(
"release_expr");
56 const auto *ReleaseCallExpr =
57 Result.Nodes.getNodeAs<CXXMemberCallExpr>(
"release_call");
59 if (ReleaseExpr->getBeginLoc().isMacroID())
63 diag(DeleteExpr->getBeginLoc(),
"prefer '%select{= nullptr|reset()}0' "
64 "to reset 'unique_ptr<>' objects");
65 D << PreferResetCall << DeleteExpr->getSourceRange()
66 << FixItHint::CreateRemoval(CharSourceRange::getCharRange(
67 DeleteExpr->getBeginLoc(),
68 DeleteExpr->getArgument()->getBeginLoc()));
69 if (PreferResetCall) {
70 D << FixItHint::CreateReplacement(ReleaseExpr->getMemberLoc(),
"reset");
72 if (ReleaseExpr->isArrow())
73 D << FixItHint::CreateInsertion(ReleaseExpr->getBase()->getBeginLoc(),
75 D << FixItHint::CreateReplacement(
76 CharSourceRange::getTokenRange(ReleaseExpr->getOperatorLoc(),
77 ReleaseCallExpr->getEndLoc()),
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.
UniqueptrDeleteReleaseCheck(StringRef Name, ClangTidyContext *Context)
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...
llvm::StringMap< ClangTidyValue > OptionMap