clang-tools  14.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 
16 using namespace clang::ast_matchers;
17 
18 namespace clang {
19 namespace tidy {
20 namespace readability {
21 
22 void UniqueptrDeleteReleaseCheck::storeOptions(
24  Options.store(Opts, "PreferResetCall", PreferResetCall);
25 }
26 
27 UniqueptrDeleteReleaseCheck::UniqueptrDeleteReleaseCheck(
28  StringRef Name, ClangTidyContext *Context)
29  : ClangTidyCheck(Name, Context),
30  PreferResetCall(Options.get("PreferResetCall", false)) {}
31 
33 
34  auto UniquePtrWithDefaultDelete = classTemplateSpecializationDecl(
35  hasName("::std::unique_ptr"),
36  hasTemplateArgument(1, refersToType(hasDeclaration(cxxRecordDecl(
37  hasName("::std::default_delete"))))));
38 
39  Finder->addMatcher(
40  cxxDeleteExpr(
41  unless(isInTemplateInstantiation()),
42  has(cxxMemberCallExpr(
43  callee(memberExpr(hasObjectExpression(anyOf(
44  hasType(UniquePtrWithDefaultDelete),
45  hasType(pointsTo(
46  UniquePtrWithDefaultDelete)))),
47  member(cxxMethodDecl(hasName("release"))))
48  .bind("release_expr")))
49  .bind("release_call")))
50  .bind("delete"),
51  this);
52 }
53 
55  const MatchFinder::MatchResult &Result) {
56  const auto *DeleteExpr = Result.Nodes.getNodeAs<CXXDeleteExpr>("delete");
57  const auto *ReleaseExpr = Result.Nodes.getNodeAs<MemberExpr>("release_expr");
58  const auto *ReleaseCallExpr =
59  Result.Nodes.getNodeAs<CXXMemberCallExpr>("release_call");
60 
61  if (ReleaseExpr->getBeginLoc().isMacroID())
62  return;
63 
64  auto D =
65  diag(DeleteExpr->getBeginLoc(), "prefer '%select{= nullptr|reset()}0' "
66  "to reset 'unique_ptr<>' objects");
67  D << PreferResetCall << DeleteExpr->getSourceRange()
68  << FixItHint::CreateRemoval(CharSourceRange::getCharRange(
69  DeleteExpr->getBeginLoc(),
70  DeleteExpr->getArgument()->getBeginLoc()));
71  if (PreferResetCall) {
72  D << FixItHint::CreateReplacement(ReleaseExpr->getMemberLoc(), "reset");
73  } else {
74  if (ReleaseExpr->isArrow())
75  D << FixItHint::CreateInsertion(ReleaseExpr->getBase()->getBeginLoc(),
76  "*");
77  D << FixItHint::CreateReplacement(
78  CharSourceRange::getTokenRange(ReleaseExpr->getOperatorLoc(),
79  ReleaseCallExpr->getEndLoc()),
80  " = nullptr");
81  }
82 }
83 
84 } // namespace readability
85 } // namespace tidy
86 } // namespace clang
clang::tidy::ClangTidyOptions::OptionMap
llvm::StringMap< ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:115
clang::tidy::readability::UniqueptrDeleteReleaseCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: UniqueptrDeleteReleaseCheck.cpp:54
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:54
clang::ast_matchers
Definition: AbseilMatcher.h:14
ns1::ns2::D
@ D
Definition: CategoricalFeature.h:3
UniqueptrDeleteReleaseCheck.h
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:72
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:28
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Definition: ClangTidyCheck.cpp:25
clang::tidy::readability::UniqueptrDeleteReleaseCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: UniqueptrDeleteReleaseCheck.cpp:32
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27