clang-tools 20.0.0git
UniqueptrDeleteReleaseCheck.cpp
Go to the documentation of this file.
1//===--- UniqueptrDeleteReleaseCheck.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
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"
15
16using namespace clang::ast_matchers;
17
19
22 Options.store(Opts, "PreferResetCall", PreferResetCall);
23}
24
26 StringRef Name, ClangTidyContext *Context)
27 : ClangTidyCheck(Name, Context),
28 PreferResetCall(Options.get("PreferResetCall", false)) {}
29
31
32 auto UniquePtrWithDefaultDelete = classTemplateSpecializationDecl(
33 hasName("::std::unique_ptr"),
34 hasTemplateArgument(1, refersToType(hasDeclaration(cxxRecordDecl(
35 hasName("::std::default_delete"))))));
36
37 Finder->addMatcher(
38 cxxDeleteExpr(
39 unless(isInTemplateInstantiation()),
40 has(cxxMemberCallExpr(
41 callee(memberExpr(hasObjectExpression(anyOf(
42 hasType(UniquePtrWithDefaultDelete),
43 hasType(pointsTo(
44 UniquePtrWithDefaultDelete)))),
45 member(cxxMethodDecl(hasName("release"))))
46 .bind("release_expr")))
47 .bind("release_call")))
48 .bind("delete"),
49 this);
50}
51
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");
58
59 if (ReleaseExpr->getBeginLoc().isMacroID())
60 return;
61
62 auto D =
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");
71 } else {
72 if (ReleaseExpr->isArrow())
73 D << FixItHint::CreateInsertion(ReleaseExpr->getBase()->getBeginLoc(),
74 "*");
75 D << FixItHint::CreateReplacement(
76 CharSourceRange::getTokenRange(ReleaseExpr->getOperatorLoc(),
77 ReleaseCallExpr->getEndLoc()),
78 " = nullptr");
79 }
80}
81
82} // namespace clang::tidy::readability
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