1 //===--- DeleteNullPointerCheck.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 
9 #include "DeleteNullPointerCheck.h"
10 #include "../utils/LexerUtils.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 namespace readability {
20 
registerMatchers(MatchFinder * Finder)21 void DeleteNullPointerCheck::registerMatchers(MatchFinder *Finder) {
22   const auto DeleteExpr =
23       cxxDeleteExpr(has(castExpr(has(declRefExpr(
24                         to(decl(equalsBoundNode("deletedPointer"))))))))
25           .bind("deleteExpr");
26 
27   const auto DeleteMemberExpr =
28       cxxDeleteExpr(has(castExpr(has(memberExpr(hasDeclaration(
29                         fieldDecl(equalsBoundNode("deletedMemberPointer"))))))))
30           .bind("deleteMemberExpr");
31 
32   const auto PointerExpr = ignoringImpCasts(anyOf(
33       declRefExpr(to(decl().bind("deletedPointer"))),
34       memberExpr(hasDeclaration(fieldDecl().bind("deletedMemberPointer")))));
35 
36   const auto PointerCondition = castExpr(hasCastKind(CK_PointerToBoolean),
37                                          hasSourceExpression(PointerExpr));
38   const auto BinaryPointerCheckCondition =
39       binaryOperator(hasEitherOperand(castExpr(hasCastKind(CK_NullToPointer))),
40                      hasEitherOperand(PointerExpr));
41 
42   Finder->addMatcher(
43       ifStmt(hasCondition(anyOf(PointerCondition, BinaryPointerCheckCondition)),
44              hasThen(anyOf(
45                  DeleteExpr, DeleteMemberExpr,
46                  compoundStmt(anyOf(has(DeleteExpr), has(DeleteMemberExpr)),
47                               statementCountIs(1))
48                      .bind("compound"))))
49           .bind("ifWithDelete"),
50       this);
51 }
52 
check(const MatchFinder::MatchResult & Result)53 void DeleteNullPointerCheck::check(const MatchFinder::MatchResult &Result) {
54   const auto *IfWithDelete = Result.Nodes.getNodeAs<IfStmt>("ifWithDelete");
55   const auto *Compound = Result.Nodes.getNodeAs<CompoundStmt>("compound");
56 
57   auto Diag = diag(
58       IfWithDelete->getBeginLoc(),
59       "'if' statement is unnecessary; deleting null pointer has no effect");
60   if (IfWithDelete->getElse())
61     return;
62   // FIXME: generate fixit for this case.
63 
64   Diag << FixItHint::CreateRemoval(CharSourceRange::getTokenRange(
65       IfWithDelete->getBeginLoc(),
66       utils::lexer::getPreviousToken(IfWithDelete->getThen()->getBeginLoc(),
67                                      *Result.SourceManager,
68                                      Result.Context->getLangOpts())
69           .getLocation()));
70 
71   if (Compound) {
72     Diag << FixItHint::CreateRemoval(
73         CharSourceRange::getTokenRange(Compound->getLBracLoc()));
74     Diag << FixItHint::CreateRemoval(
75         CharSourceRange::getTokenRange(Compound->getRBracLoc()));
76   }
77 }
78 
79 } // namespace readability
80 } // namespace tidy
81 } // namespace clang
82