2016-12-31 20:45:59 +08:00
|
|
|
//===--- DeleteNullPointerCheck.cpp - clang-tidy---------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-12-31 20:45:59 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "DeleteNullPointerCheck.h"
|
2019-05-27 01:00:38 +08:00
|
|
|
#include "../utils/LexerUtils.h"
|
2016-12-31 20:45:59 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
#include "clang/Lex/Lexer.h"
|
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
|
|
|
namespace readability {
|
|
|
|
|
|
|
|
void DeleteNullPointerCheck::registerMatchers(MatchFinder *Finder) {
|
|
|
|
const auto DeleteExpr =
|
2020-12-29 21:44:18 +08:00
|
|
|
cxxDeleteExpr(
|
|
|
|
has(declRefExpr(to(decl(equalsBoundNode("deletedPointer"))))))
|
2016-12-31 20:45:59 +08:00
|
|
|
.bind("deleteExpr");
|
|
|
|
|
2017-02-13 04:09:59 +08:00
|
|
|
const auto DeleteMemberExpr =
|
2020-12-29 21:44:18 +08:00
|
|
|
cxxDeleteExpr(has(memberExpr(hasDeclaration(
|
|
|
|
fieldDecl(equalsBoundNode("deletedMemberPointer"))))))
|
2017-02-13 04:09:59 +08:00
|
|
|
.bind("deleteMemberExpr");
|
|
|
|
|
2020-12-29 21:44:18 +08:00
|
|
|
const auto PointerExpr = anyOf(
|
2017-02-13 04:09:59 +08:00
|
|
|
declRefExpr(to(decl().bind("deletedPointer"))),
|
2020-12-29 21:44:18 +08:00
|
|
|
memberExpr(hasDeclaration(fieldDecl().bind("deletedMemberPointer"))));
|
2017-02-13 04:09:59 +08:00
|
|
|
|
2020-12-29 21:44:18 +08:00
|
|
|
const auto BinaryPointerCheckCondition = binaryOperator(hasOperands(
|
|
|
|
anyOf(cxxNullPtrLiteralExpr(), integerLiteral(equals(0))), PointerExpr));
|
2016-12-31 20:45:59 +08:00
|
|
|
|
|
|
|
Finder->addMatcher(
|
2020-12-29 21:44:18 +08:00
|
|
|
ifStmt(hasCondition(anyOf(PointerExpr, BinaryPointerCheckCondition)),
|
|
|
|
hasThen(anyOf(
|
|
|
|
DeleteExpr, DeleteMemberExpr,
|
|
|
|
compoundStmt(anyOf(has(DeleteExpr), has(DeleteMemberExpr)),
|
|
|
|
statementCountIs(1))
|
|
|
|
.bind("compound"))))
|
|
|
|
.bind("ifWithDelete"),
|
2016-12-31 20:45:59 +08:00
|
|
|
this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeleteNullPointerCheck::check(const MatchFinder::MatchResult &Result) {
|
|
|
|
const auto *IfWithDelete = Result.Nodes.getNodeAs<IfStmt>("ifWithDelete");
|
|
|
|
const auto *Compound = Result.Nodes.getNodeAs<CompoundStmt>("compound");
|
|
|
|
|
|
|
|
auto Diag = diag(
|
2018-08-10 06:42:26 +08:00
|
|
|
IfWithDelete->getBeginLoc(),
|
2016-12-31 20:45:59 +08:00
|
|
|
"'if' statement is unnecessary; deleting null pointer has no effect");
|
|
|
|
if (IfWithDelete->getElse())
|
|
|
|
return;
|
|
|
|
// FIXME: generate fixit for this case.
|
|
|
|
|
|
|
|
Diag << FixItHint::CreateRemoval(CharSourceRange::getTokenRange(
|
2018-08-10 06:42:26 +08:00
|
|
|
IfWithDelete->getBeginLoc(),
|
2019-05-27 01:00:38 +08:00
|
|
|
utils::lexer::getPreviousToken(IfWithDelete->getThen()->getBeginLoc(),
|
|
|
|
*Result.SourceManager,
|
|
|
|
Result.Context->getLangOpts())
|
|
|
|
.getLocation()));
|
|
|
|
|
2016-12-31 20:45:59 +08:00
|
|
|
if (Compound) {
|
2017-03-21 06:15:19 +08:00
|
|
|
Diag << FixItHint::CreateRemoval(
|
|
|
|
CharSourceRange::getTokenRange(Compound->getLBracLoc()));
|
|
|
|
Diag << FixItHint::CreateRemoval(
|
|
|
|
CharSourceRange::getTokenRange(Compound->getRBracLoc()));
|
2016-12-31 20:45:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace readability
|
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|