2015-03-09 20:18:39 +08:00
|
|
|
//===--- ContainerSizeEmptyCheck.cpp - clang-tidy -------------------------===//
|
2015-01-15 23:46:58 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2015-03-09 20:18:39 +08:00
|
|
|
#include "ContainerSizeEmptyCheck.h"
|
2015-01-15 23:46:58 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchers.h"
|
|
|
|
#include "clang/Lex/Lexer.h"
|
2015-02-13 17:07:58 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2016-04-22 00:57:56 +08:00
|
|
|
#include "../utils/Matchers.h"
|
2015-01-15 23:46:58 +08:00
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
|
|
|
namespace readability {
|
|
|
|
|
|
|
|
ContainerSizeEmptyCheck::ContainerSizeEmptyCheck(StringRef Name,
|
|
|
|
ClangTidyContext *Context)
|
|
|
|
: ClangTidyCheck(Name, Context) {}
|
|
|
|
|
|
|
|
void ContainerSizeEmptyCheck::registerMatchers(MatchFinder *Finder) {
|
2015-09-03 00:05:21 +08:00
|
|
|
// Only register the matchers for C++; the functionality currently does not
|
|
|
|
// provide any benefit to other languages, despite being benign.
|
|
|
|
if (!getLangOpts().CPlusPlus)
|
|
|
|
return;
|
|
|
|
|
2016-04-22 00:57:56 +08:00
|
|
|
const auto stlContainer = hasAnyName(
|
|
|
|
"array", "basic_string", "deque", "forward_list", "list", "map",
|
|
|
|
"multimap", "multiset", "priority_queue", "queue", "set", "stack",
|
|
|
|
"unordered_map", "unordered_multimap", "unordered_multiset",
|
|
|
|
"unordered_set", "vector");
|
|
|
|
|
2015-01-15 23:46:58 +08:00
|
|
|
const auto WrongUse = anyOf(
|
2016-04-19 21:29:05 +08:00
|
|
|
hasParent(binaryOperator(
|
2016-04-22 00:57:56 +08:00
|
|
|
matchers::isComparisonOperator(),
|
2016-04-19 21:29:05 +08:00
|
|
|
hasEitherOperand(ignoringImpCasts(anyOf(
|
|
|
|
integerLiteral(equals(1)), integerLiteral(equals(0))))))
|
|
|
|
.bind("SizeBinaryOp")),
|
2015-01-15 23:46:58 +08:00
|
|
|
hasParent(implicitCastExpr(
|
2016-02-09 17:26:11 +08:00
|
|
|
hasImplicitDestinationType(booleanType()),
|
2015-01-15 23:46:58 +08:00
|
|
|
anyOf(
|
|
|
|
hasParent(unaryOperator(hasOperatorName("!")).bind("NegOnSize")),
|
|
|
|
anything()))),
|
2016-02-09 17:26:11 +08:00
|
|
|
hasParent(explicitCastExpr(hasDestinationType(booleanType()))));
|
2015-01-15 23:46:58 +08:00
|
|
|
|
|
|
|
Finder->addMatcher(
|
2015-09-17 21:31:25 +08:00
|
|
|
cxxMemberCallExpr(
|
2016-04-22 00:57:56 +08:00
|
|
|
on(expr(anyOf(hasType(namedDecl(stlContainer)),
|
|
|
|
hasType(pointsTo(namedDecl(stlContainer))),
|
|
|
|
hasType(references(namedDecl(stlContainer)))))
|
2015-01-22 20:27:09 +08:00
|
|
|
.bind("STLObject")),
|
2015-09-17 21:31:25 +08:00
|
|
|
callee(cxxMethodDecl(hasName("size"))), WrongUse)
|
|
|
|
.bind("SizeCallExpr"),
|
2015-01-15 23:46:58 +08:00
|
|
|
this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ContainerSizeEmptyCheck::check(const MatchFinder::MatchResult &Result) {
|
|
|
|
const auto *MemberCall =
|
|
|
|
Result.Nodes.getNodeAs<CXXMemberCallExpr>("SizeCallExpr");
|
|
|
|
const auto *BinaryOp = Result.Nodes.getNodeAs<BinaryOperator>("SizeBinaryOp");
|
|
|
|
const auto *E = Result.Nodes.getNodeAs<Expr>("STLObject");
|
|
|
|
FixItHint Hint;
|
2015-01-22 20:40:47 +08:00
|
|
|
std::string ReplacementText = Lexer::getSourceText(
|
|
|
|
CharSourceRange::getTokenRange(E->getSourceRange()),
|
|
|
|
*Result.SourceManager, Result.Context->getLangOpts());
|
2015-01-15 23:46:58 +08:00
|
|
|
if (E->getType()->isPointerType())
|
|
|
|
ReplacementText += "->empty()";
|
|
|
|
else
|
|
|
|
ReplacementText += ".empty()";
|
|
|
|
|
|
|
|
if (BinaryOp) { // Determine the correct transformation.
|
|
|
|
bool Negation = false;
|
2015-12-12 19:31:25 +08:00
|
|
|
const bool ContainerIsLHS =
|
|
|
|
!llvm::isa<IntegerLiteral>(BinaryOp->getLHS()->IgnoreImpCasts());
|
2015-01-15 23:46:58 +08:00
|
|
|
const auto OpCode = BinaryOp->getOpcode();
|
|
|
|
uint64_t Value = 0;
|
|
|
|
if (ContainerIsLHS) {
|
2015-12-12 19:31:25 +08:00
|
|
|
if (const auto *Literal = llvm::dyn_cast<IntegerLiteral>(
|
|
|
|
BinaryOp->getRHS()->IgnoreImpCasts()))
|
2015-01-15 23:46:58 +08:00
|
|
|
Value = Literal->getValue().getLimitedValue();
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
} else {
|
2015-12-12 19:31:25 +08:00
|
|
|
Value =
|
|
|
|
llvm::dyn_cast<IntegerLiteral>(BinaryOp->getLHS()->IgnoreImpCasts())
|
|
|
|
->getValue()
|
|
|
|
.getLimitedValue();
|
2015-01-15 23:46:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Constant that is not handled.
|
|
|
|
if (Value > 1)
|
|
|
|
return;
|
|
|
|
|
2016-04-19 21:29:05 +08:00
|
|
|
if (Value == 1 && (OpCode == BinaryOperatorKind::BO_EQ ||
|
|
|
|
OpCode == BinaryOperatorKind::BO_NE))
|
|
|
|
return;
|
|
|
|
|
2015-01-15 23:46:58 +08:00
|
|
|
// Always true, no warnings for that.
|
|
|
|
if ((OpCode == BinaryOperatorKind::BO_GE && Value == 0 && ContainerIsLHS) ||
|
|
|
|
(OpCode == BinaryOperatorKind::BO_LE && Value == 0 && !ContainerIsLHS))
|
|
|
|
return;
|
|
|
|
|
2015-12-29 01:20:33 +08:00
|
|
|
// Do not warn for size > 1, 1 < size, size <= 1, 1 >= size.
|
|
|
|
if (Value == 1) {
|
|
|
|
if ((OpCode == BinaryOperatorKind::BO_GT && ContainerIsLHS) ||
|
|
|
|
(OpCode == BinaryOperatorKind::BO_LT && !ContainerIsLHS))
|
|
|
|
return;
|
|
|
|
if ((OpCode == BinaryOperatorKind::BO_LE && ContainerIsLHS) ||
|
|
|
|
(OpCode == BinaryOperatorKind::BO_GE && !ContainerIsLHS))
|
|
|
|
return;
|
|
|
|
}
|
2015-12-21 17:43:52 +08:00
|
|
|
|
2015-01-15 23:46:58 +08:00
|
|
|
if (OpCode == BinaryOperatorKind::BO_NE && Value == 0)
|
|
|
|
Negation = true;
|
|
|
|
if ((OpCode == BinaryOperatorKind::BO_GT ||
|
|
|
|
OpCode == BinaryOperatorKind::BO_GE) &&
|
|
|
|
ContainerIsLHS)
|
|
|
|
Negation = true;
|
|
|
|
if ((OpCode == BinaryOperatorKind::BO_LT ||
|
|
|
|
OpCode == BinaryOperatorKind::BO_LE) &&
|
|
|
|
!ContainerIsLHS)
|
|
|
|
Negation = true;
|
|
|
|
|
|
|
|
if (Negation)
|
|
|
|
ReplacementText = "!" + ReplacementText;
|
|
|
|
Hint = FixItHint::CreateReplacement(BinaryOp->getSourceRange(),
|
|
|
|
ReplacementText);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// If there is a conversion above the size call to bool, it is safe to just
|
|
|
|
// replace size with empty.
|
|
|
|
if (const auto *UnaryOp =
|
|
|
|
Result.Nodes.getNodeAs<UnaryOperator>("NegOnSize"))
|
|
|
|
Hint = FixItHint::CreateReplacement(UnaryOp->getSourceRange(),
|
|
|
|
ReplacementText);
|
|
|
|
else
|
|
|
|
Hint = FixItHint::CreateReplacement(MemberCall->getSourceRange(),
|
|
|
|
"!" + ReplacementText);
|
|
|
|
}
|
2015-11-09 23:53:28 +08:00
|
|
|
diag(MemberCall->getLocStart(), "the 'empty' method should be used to check "
|
|
|
|
"for emptiness instead of 'size'")
|
2015-01-15 23:46:58 +08:00
|
|
|
<< Hint;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace readability
|
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|