11 #include "clang/AST/ASTContext.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 13 #include "clang/Lex/Lexer.h" 19 namespace readability {
21 void DeleteNullPointerCheck::registerMatchers(MatchFinder *Finder) {
22 const auto DeleteExpr =
23 cxxDeleteExpr(has(castExpr(has(declRefExpr(
24 to(decl(equalsBoundNode(
"deletedPointer"))))))))
27 const auto DeleteMemberExpr =
28 cxxDeleteExpr(has(castExpr(has(memberExpr(hasDeclaration(
29 fieldDecl(equalsBoundNode(
"deletedMemberPointer"))))))))
30 .bind(
"deleteMemberExpr");
32 const auto PointerExpr = ignoringImpCasts(anyOf(
33 declRefExpr(to(decl().bind(
"deletedPointer"))),
34 memberExpr(hasDeclaration(fieldDecl().bind(
"deletedMemberPointer")))));
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));
43 ifStmt(hasCondition(anyOf(PointerCondition, BinaryPointerCheckCondition)),
45 DeleteExpr, DeleteMemberExpr,
46 compoundStmt(anyOf(has(DeleteExpr), has(DeleteMemberExpr)),
49 .bind(
"ifWithDelete"),
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");
58 IfWithDelete->getBeginLoc(),
59 "'if' statement is unnecessary; deleting null pointer has no effect");
60 if (IfWithDelete->getElse())
64 Diag << FixItHint::CreateRemoval(CharSourceRange::getTokenRange(
65 IfWithDelete->getBeginLoc(),
66 Lexer::getLocForEndOfToken(IfWithDelete->getCond()->getEndLoc(), 0,
67 *Result.SourceManager,
68 Result.Context->getLangOpts())));
70 Diag << FixItHint::CreateRemoval(
71 CharSourceRange::getTokenRange(Compound->getLBracLoc()));
72 Diag << FixItHint::CreateRemoval(
73 CharSourceRange::getTokenRange(Compound->getRBracLoc()));
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//