blob: bb4cd6cfa4aa5ca0fb5acef69eda55dce923ba2e [file] [log] [blame]
Alexander Kornienko1b677db2015-03-09 12:18:39 +00001//===--- ContainerSizeEmptyCheck.cpp - clang-tidy -------------------------===//
Alexander Kornienko4babd682015-01-15 15:46:58 +00002//
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//===----------------------------------------------------------------------===//
Alexander Kornienko1b677db2015-03-09 12:18:39 +00009#include "ContainerSizeEmptyCheck.h"
Alexander Kornienko4babd682015-01-15 15:46:58 +000010#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchers.h"
Alexander Kornienko4babd682015-01-15 15:46:58 +000012#include "clang/Lex/Lexer.h"
Chandler Carruthf7662782015-02-13 09:07:58 +000013#include "llvm/ADT/StringRef.h"
Alexander Kornienko4babd682015-01-15 15:46:58 +000014
15using namespace clang::ast_matchers;
16
Benjamin Kramer73d27492015-04-17 13:52:08 +000017static bool isContainer(llvm::StringRef ClassName) {
Gabor Horvatha4e35ec2015-12-12 11:31:25 +000018 static const char *const ContainerNames[] = {"std::array",
19 "std::deque",
20 "std::forward_list",
21 "std::list",
22 "std::map",
23 "std::multimap",
24 "std::multiset",
25 "std::priority_queue",
26 "std::queue",
27 "std::set",
28 "std::stack",
29 "std::unordered_map",
30 "std::unordered_multimap",
31 "std::unordered_multiset",
32 "std::unordered_set",
33 "std::vector"};
Benjamin Kramer73d27492015-04-17 13:52:08 +000034 return std::binary_search(std::begin(ContainerNames),
35 std::end(ContainerNames), ClassName);
Alexander Kornienko4babd682015-01-15 15:46:58 +000036}
Alexander Kornienko4babd682015-01-15 15:46:58 +000037
38namespace clang {
Alexander Kornienko50d7f4612015-06-17 13:11:37 +000039namespace {
Alexander Kornienkofebfd3452015-01-22 12:27:09 +000040AST_MATCHER(QualType, isBoolType) { return Node->isBooleanType(); }
Alexander Kornienko4babd682015-01-15 15:46:58 +000041
42AST_MATCHER(NamedDecl, stlContainer) {
43 return isContainer(Node.getQualifiedNameAsString());
44}
Alexander Kornienko50d7f4612015-06-17 13:11:37 +000045} // namespace
Alexander Kornienko4babd682015-01-15 15:46:58 +000046
Alexander Kornienko4babd682015-01-15 15:46:58 +000047namespace tidy {
48namespace readability {
49
50ContainerSizeEmptyCheck::ContainerSizeEmptyCheck(StringRef Name,
51 ClangTidyContext *Context)
52 : ClangTidyCheck(Name, Context) {}
53
54void ContainerSizeEmptyCheck::registerMatchers(MatchFinder *Finder) {
Aaron Ballman1f1b0672015-09-02 16:05:21 +000055 // Only register the matchers for C++; the functionality currently does not
56 // provide any benefit to other languages, despite being benign.
57 if (!getLangOpts().CPlusPlus)
58 return;
59
Alexander Kornienko4babd682015-01-15 15:46:58 +000060 const auto WrongUse = anyOf(
61 hasParent(
62 binaryOperator(
63 anyOf(has(integerLiteral(equals(0))),
64 allOf(anyOf(hasOperatorName("<"), hasOperatorName(">="),
65 hasOperatorName(">"), hasOperatorName("<=")),
Gabor Horvatha4e35ec2015-12-12 11:31:25 +000066 hasEitherOperand(
67 ignoringImpCasts(integerLiteral(equals(1)))))))
Alexander Kornienko4babd682015-01-15 15:46:58 +000068 .bind("SizeBinaryOp")),
69 hasParent(implicitCastExpr(
Alexander Kornienkofebfd3452015-01-22 12:27:09 +000070 hasImplicitDestinationType(isBoolType()),
Alexander Kornienko4babd682015-01-15 15:46:58 +000071 anyOf(
72 hasParent(unaryOperator(hasOperatorName("!")).bind("NegOnSize")),
73 anything()))),
Alexander Kornienkofebfd3452015-01-22 12:27:09 +000074 hasParent(explicitCastExpr(hasDestinationType(isBoolType()))));
Alexander Kornienko4babd682015-01-15 15:46:58 +000075
76 Finder->addMatcher(
Aaron Ballmanb9ea09c2015-09-17 13:31:25 +000077 cxxMemberCallExpr(
Alexander Kornienko4babd682015-01-15 15:46:58 +000078 on(expr(anyOf(hasType(namedDecl(stlContainer())),
Alexander Kornienkofebfd3452015-01-22 12:27:09 +000079 hasType(pointsTo(namedDecl(stlContainer()))),
80 hasType(references(namedDecl(stlContainer())))))
81 .bind("STLObject")),
Aaron Ballmanb9ea09c2015-09-17 13:31:25 +000082 callee(cxxMethodDecl(hasName("size"))), WrongUse)
83 .bind("SizeCallExpr"),
Alexander Kornienko4babd682015-01-15 15:46:58 +000084 this);
85}
86
87void ContainerSizeEmptyCheck::check(const MatchFinder::MatchResult &Result) {
88 const auto *MemberCall =
89 Result.Nodes.getNodeAs<CXXMemberCallExpr>("SizeCallExpr");
90 const auto *BinaryOp = Result.Nodes.getNodeAs<BinaryOperator>("SizeBinaryOp");
91 const auto *E = Result.Nodes.getNodeAs<Expr>("STLObject");
92 FixItHint Hint;
Alexander Kornienko96e7b8b2015-01-22 12:40:47 +000093 std::string ReplacementText = Lexer::getSourceText(
94 CharSourceRange::getTokenRange(E->getSourceRange()),
95 *Result.SourceManager, Result.Context->getLangOpts());
Alexander Kornienko4babd682015-01-15 15:46:58 +000096 if (E->getType()->isPointerType())
97 ReplacementText += "->empty()";
98 else
99 ReplacementText += ".empty()";
100
101 if (BinaryOp) { // Determine the correct transformation.
102 bool Negation = false;
Gabor Horvatha4e35ec2015-12-12 11:31:25 +0000103 const bool ContainerIsLHS =
104 !llvm::isa<IntegerLiteral>(BinaryOp->getLHS()->IgnoreImpCasts());
Alexander Kornienko4babd682015-01-15 15:46:58 +0000105 const auto OpCode = BinaryOp->getOpcode();
106 uint64_t Value = 0;
107 if (ContainerIsLHS) {
Gabor Horvatha4e35ec2015-12-12 11:31:25 +0000108 if (const auto *Literal = llvm::dyn_cast<IntegerLiteral>(
109 BinaryOp->getRHS()->IgnoreImpCasts()))
Alexander Kornienko4babd682015-01-15 15:46:58 +0000110 Value = Literal->getValue().getLimitedValue();
111 else
112 return;
113 } else {
Gabor Horvatha4e35ec2015-12-12 11:31:25 +0000114 Value =
115 llvm::dyn_cast<IntegerLiteral>(BinaryOp->getLHS()->IgnoreImpCasts())
116 ->getValue()
117 .getLimitedValue();
Alexander Kornienko4babd682015-01-15 15:46:58 +0000118 }
119
120 // Constant that is not handled.
121 if (Value > 1)
122 return;
123
124 // Always true, no warnings for that.
125 if ((OpCode == BinaryOperatorKind::BO_GE && Value == 0 && ContainerIsLHS) ||
126 (OpCode == BinaryOperatorKind::BO_LE && Value == 0 && !ContainerIsLHS))
127 return;
128
Gabor Horvath1f30cf62015-12-28 17:20:33 +0000129 // Do not warn for size > 1, 1 < size, size <= 1, 1 >= size.
130 if (Value == 1) {
131 if ((OpCode == BinaryOperatorKind::BO_GT && ContainerIsLHS) ||
132 (OpCode == BinaryOperatorKind::BO_LT && !ContainerIsLHS))
133 return;
134 if ((OpCode == BinaryOperatorKind::BO_LE && ContainerIsLHS) ||
135 (OpCode == BinaryOperatorKind::BO_GE && !ContainerIsLHS))
136 return;
137 }
Gabor Horvathc6ff9c32015-12-21 09:43:52 +0000138
Alexander Kornienko4babd682015-01-15 15:46:58 +0000139 if (OpCode == BinaryOperatorKind::BO_NE && Value == 0)
140 Negation = true;
141 if ((OpCode == BinaryOperatorKind::BO_GT ||
142 OpCode == BinaryOperatorKind::BO_GE) &&
143 ContainerIsLHS)
144 Negation = true;
145 if ((OpCode == BinaryOperatorKind::BO_LT ||
146 OpCode == BinaryOperatorKind::BO_LE) &&
147 !ContainerIsLHS)
148 Negation = true;
149
150 if (Negation)
151 ReplacementText = "!" + ReplacementText;
152 Hint = FixItHint::CreateReplacement(BinaryOp->getSourceRange(),
153 ReplacementText);
154
155 } else {
156 // If there is a conversion above the size call to bool, it is safe to just
157 // replace size with empty.
158 if (const auto *UnaryOp =
159 Result.Nodes.getNodeAs<UnaryOperator>("NegOnSize"))
160 Hint = FixItHint::CreateReplacement(UnaryOp->getSourceRange(),
161 ReplacementText);
162 else
163 Hint = FixItHint::CreateReplacement(MemberCall->getSourceRange(),
164 "!" + ReplacementText);
165 }
Alexander Kornienko301130e2015-11-09 15:53:28 +0000166 diag(MemberCall->getLocStart(), "the 'empty' method should be used to check "
167 "for emptiness instead of 'size'")
Alexander Kornienko4babd682015-01-15 15:46:58 +0000168 << Hint;
169}
170
171} // namespace readability
172} // namespace tidy
173} // namespace clang