clang-tools 3.8.0

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

UniqueptrDeleteReleaseCheck.cpp
Go to the documentation of this file.
1 //===--- UniqueptrDeleteReleaseCheck.cpp - clang-tidy----------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "UniqueptrDeleteReleaseCheck.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/Lex/Lexer.h"
14
15 using namespace clang::ast_matchers;
16
17 namespace clang {
18 namespace tidy {
19
20 void UniqueptrDeleteReleaseCheck::registerMatchers(MatchFinder *Finder) {
21 auto IsSusbstituted = qualType(anyOf(
22 substTemplateTypeParmType(), hasDescendant(substTemplateTypeParmType())));
23
24 auto UniquePtrWithDefaultDelete = classTemplateSpecializationDecl(
25 hasName("std::unique_ptr"),
26 hasTemplateArgument(1, refersToType(qualType(hasDeclaration(cxxRecordDecl(
27 hasName("std::default_delete")))))));
28
29 Finder->addMatcher(
30 cxxDeleteExpr(
31 has(cxxMemberCallExpr(on(expr(hasType(UniquePtrWithDefaultDelete),
32 unless(hasType(IsSusbstituted)))
33 .bind("uptr")),
34 callee(cxxMethodDecl(hasName("release"))))))
35 .bind("delete"),
36 this);
37 }
38
39 void UniqueptrDeleteReleaseCheck::check(
40 const MatchFinder::MatchResult &Result) {
41 const auto *PtrExpr = Result.Nodes.getNodeAs<Expr>("uptr");
42 const auto *DeleteExpr = Result.Nodes.getNodeAs<Expr>("delete");
43
44 if (PtrExpr->getLocStart().isMacroID())
45 return;
46
47 // Ignore dependent types.
48 // It can give us false positives, so we go with false negatives instead to
49 // be safe.
50 if (PtrExpr->getType()->isDependentType())
51 return;
52
53 SourceLocation AfterPtr =
54 Lexer::getLocForEndOfToken(PtrExpr->getLocEnd(), 0, *Result.SourceManager,
55 Result.Context->getLangOpts());
56
57 diag(DeleteExpr->getLocStart(),
58 "prefer '= nullptr' to 'delete x.release()' to reset unique_ptr<> "
59 "objects")
60 << FixItHint::CreateRemoval(CharSourceRange::getCharRange(
61 DeleteExpr->getLocStart(), PtrExpr->getLocStart()))
62 << FixItHint::CreateReplacement(
63 CharSourceRange::getTokenRange(AfterPtr, DeleteExpr->getLocEnd()),
64 " = nullptr");
65 }
66
67 } // namespace tidy
68 } // namespace clang
69
Finder
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:188
UniqueptrDeleteReleaseCheck.h
Result
const NamedDecl * Result
Definition: USRFinder.cpp:121