blob: 97f9eb714bea06d5bd7d662d3281ad38fb074176 [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"
Aaron Ballman72163a92017-04-24 14:57:09 +000010#include "../utils/ASTUtils.h"
Kirill Bobyrevacb6b352016-09-13 08:58:11 +000011#include "../utils/Matchers.h"
Alexander Kornienko4babd682015-01-15 15:46:58 +000012#include "clang/AST/ASTContext.h"
13#include "clang/ASTMatchers/ASTMatchers.h"
Alexander Kornienko4babd682015-01-15 15:46:58 +000014#include "clang/Lex/Lexer.h"
Chandler Carruthf7662782015-02-13 09:07:58 +000015#include "llvm/ADT/StringRef.h"
Alexander Kornienko4babd682015-01-15 15:46:58 +000016
17using namespace clang::ast_matchers;
18
Alexander Kornienko4babd682015-01-15 15:46:58 +000019namespace clang {
Alexander Kornienko4babd682015-01-15 15:46:58 +000020namespace tidy {
21namespace readability {
22
Aaron Ballman72163a92017-04-24 14:57:09 +000023using utils::IsBinaryOrTernary;
24
Alexander Kornienko4babd682015-01-15 15:46:58 +000025ContainerSizeEmptyCheck::ContainerSizeEmptyCheck(StringRef Name,
26 ClangTidyContext *Context)
27 : ClangTidyCheck(Name, Context) {}
28
29void ContainerSizeEmptyCheck::registerMatchers(MatchFinder *Finder) {
Aaron Ballman1f1b0672015-09-02 16:05:21 +000030 // Only register the matchers for C++; the functionality currently does not
31 // provide any benefit to other languages, despite being benign.
32 if (!getLangOpts().CPlusPlus)
33 return;
34
Manuel Klimek7b9c1172017-08-02 13:13:11 +000035 const auto ValidContainer = qualType(hasUnqualifiedDesugaredType(
36 recordType(hasDeclaration(cxxRecordDecl(isSameOrDerivedFrom(
37 namedDecl(
38 has(cxxMethodDecl(
39 isConst(), parameterCountIs(0), isPublic(),
40 hasName("size"),
41 returns(qualType(isInteger(), unless(booleanType()))))
42 .bind("size")),
43 has(cxxMethodDecl(isConst(), parameterCountIs(0), isPublic(),
44 hasName("empty"), returns(booleanType()))
45 .bind("empty")))
46 .bind("container")))))));
Etienne Bergeron9d265992016-04-21 16:57:56 +000047
Alexander Kornienko4babd682015-01-15 15:46:58 +000048 const auto WrongUse = anyOf(
Gabor Horvath533c01d2016-04-19 13:29:05 +000049 hasParent(binaryOperator(
Etienne Bergeron9d265992016-04-21 16:57:56 +000050 matchers::isComparisonOperator(),
Gabor Horvath533c01d2016-04-19 13:29:05 +000051 hasEitherOperand(ignoringImpCasts(anyOf(
52 integerLiteral(equals(1)), integerLiteral(equals(0))))))
53 .bind("SizeBinaryOp")),
Alexander Kornienko4babd682015-01-15 15:46:58 +000054 hasParent(implicitCastExpr(
Gabor Horvatha4fd3be2016-02-09 09:26:11 +000055 hasImplicitDestinationType(booleanType()),
Alexander Kornienko4babd682015-01-15 15:46:58 +000056 anyOf(
57 hasParent(unaryOperator(hasOperatorName("!")).bind("NegOnSize")),
58 anything()))),
Gabor Horvatha4fd3be2016-02-09 09:26:11 +000059 hasParent(explicitCastExpr(hasDestinationType(booleanType()))));
Alexander Kornienko4babd682015-01-15 15:46:58 +000060
61 Finder->addMatcher(
Kirill Bobyrev0d0bbfd2016-09-13 10:19:13 +000062 cxxMemberCallExpr(on(expr(anyOf(hasType(ValidContainer),
63 hasType(pointsTo(ValidContainer)),
Alexander Kornienkof6cd3672017-03-20 22:15:27 +000064 hasType(references(ValidContainer))))),
Alexander Kornienkob5ca17f2016-12-21 23:44:23 +000065 callee(cxxMethodDecl(hasName("size"))), WrongUse,
66 unless(hasAncestor(cxxMethodDecl(
67 ofClass(equalsBoundNode("container"))))))
Aaron Ballmanb9ea09c2015-09-17 13:31:25 +000068 .bind("SizeCallExpr"),
Alexander Kornienko4babd682015-01-15 15:46:58 +000069 this);
Aaron Ballman72163a92017-04-24 14:57:09 +000070
71 // Empty constructor matcher.
72 const auto DefaultConstructor = cxxConstructExpr(
73 hasDeclaration(cxxConstructorDecl(isDefaultConstructor())));
74 // Comparison to empty string or empty constructor.
75 const auto WrongComparend = anyOf(
76 ignoringImpCasts(stringLiteral(hasSize(0))),
77 ignoringImpCasts(cxxBindTemporaryExpr(has(DefaultConstructor))),
78 ignoringImplicit(DefaultConstructor),
79 cxxConstructExpr(
80 hasDeclaration(cxxConstructorDecl(isCopyConstructor())),
81 has(expr(ignoringImpCasts(DefaultConstructor)))),
82 cxxConstructExpr(
83 hasDeclaration(cxxConstructorDecl(isMoveConstructor())),
84 has(expr(ignoringImpCasts(DefaultConstructor)))));
85 // Match the object being compared.
86 const auto STLArg =
87 anyOf(unaryOperator(
88 hasOperatorName("*"),
89 hasUnaryOperand(
90 expr(hasType(pointsTo(ValidContainer))).bind("Pointee"))),
91 expr(hasType(ValidContainer)).bind("STLObject"));
92 Finder->addMatcher(
93 cxxOperatorCallExpr(
94 anyOf(hasOverloadedOperatorName("=="),
95 hasOverloadedOperatorName("!=")),
96 anyOf(allOf(hasArgument(0, WrongComparend), hasArgument(1, STLArg)),
97 allOf(hasArgument(0, STLArg), hasArgument(1, WrongComparend))),
98 unless(hasAncestor(
99 cxxMethodDecl(ofClass(equalsBoundNode("container"))))))
100 .bind("BinCmp"),
101 this);
Alexander Kornienko4babd682015-01-15 15:46:58 +0000102}
103
104void ContainerSizeEmptyCheck::check(const MatchFinder::MatchResult &Result) {
105 const auto *MemberCall =
106 Result.Nodes.getNodeAs<CXXMemberCallExpr>("SizeCallExpr");
Aaron Ballman72163a92017-04-24 14:57:09 +0000107 const auto *BinCmp = Result.Nodes.getNodeAs<CXXOperatorCallExpr>("BinCmp");
Alexander Kornienko4babd682015-01-15 15:46:58 +0000108 const auto *BinaryOp = Result.Nodes.getNodeAs<BinaryOperator>("SizeBinaryOp");
Aaron Ballman72163a92017-04-24 14:57:09 +0000109 const auto *Pointee = Result.Nodes.getNodeAs<Expr>("Pointee");
110 const auto *E =
111 MemberCall
112 ? MemberCall->getImplicitObjectArgument()
113 : (Pointee ? Pointee : Result.Nodes.getNodeAs<Expr>("STLObject"));
Alexander Kornienko4babd682015-01-15 15:46:58 +0000114 FixItHint Hint;
Gabor Horvathafad84c2016-09-24 02:13:45 +0000115 std::string ReplacementText =
116 Lexer::getSourceText(CharSourceRange::getTokenRange(E->getSourceRange()),
117 *Result.SourceManager, getLangOpts());
Aaron Ballman72163a92017-04-24 14:57:09 +0000118 if (BinCmp && IsBinaryOrTernary(E)) {
119 // Not just a DeclRefExpr, so parenthesize to be on the safe side.
120 ReplacementText = "(" + ReplacementText + ")";
121 }
Alexander Kornienko4babd682015-01-15 15:46:58 +0000122 if (E->getType()->isPointerType())
123 ReplacementText += "->empty()";
124 else
125 ReplacementText += ".empty()";
126
Aaron Ballman72163a92017-04-24 14:57:09 +0000127 if (BinCmp) {
128 if (BinCmp->getOperator() == OO_ExclaimEqual) {
129 ReplacementText = "!" + ReplacementText;
130 }
131 Hint =
132 FixItHint::CreateReplacement(BinCmp->getSourceRange(), ReplacementText);
133 } else if (BinaryOp) { // Determine the correct transformation.
Alexander Kornienko4babd682015-01-15 15:46:58 +0000134 bool Negation = false;
Gabor Horvatha4e35ec2015-12-12 11:31:25 +0000135 const bool ContainerIsLHS =
136 !llvm::isa<IntegerLiteral>(BinaryOp->getLHS()->IgnoreImpCasts());
Alexander Kornienko4babd682015-01-15 15:46:58 +0000137 const auto OpCode = BinaryOp->getOpcode();
138 uint64_t Value = 0;
139 if (ContainerIsLHS) {
Gabor Horvatha4e35ec2015-12-12 11:31:25 +0000140 if (const auto *Literal = llvm::dyn_cast<IntegerLiteral>(
141 BinaryOp->getRHS()->IgnoreImpCasts()))
Alexander Kornienko4babd682015-01-15 15:46:58 +0000142 Value = Literal->getValue().getLimitedValue();
143 else
144 return;
145 } else {
Gabor Horvatha4e35ec2015-12-12 11:31:25 +0000146 Value =
147 llvm::dyn_cast<IntegerLiteral>(BinaryOp->getLHS()->IgnoreImpCasts())
148 ->getValue()
149 .getLimitedValue();
Alexander Kornienko4babd682015-01-15 15:46:58 +0000150 }
151
152 // Constant that is not handled.
153 if (Value > 1)
154 return;
155
Gabor Horvath533c01d2016-04-19 13:29:05 +0000156 if (Value == 1 && (OpCode == BinaryOperatorKind::BO_EQ ||
157 OpCode == BinaryOperatorKind::BO_NE))
158 return;
159
Alexander Kornienko4babd682015-01-15 15:46:58 +0000160 // Always true, no warnings for that.
161 if ((OpCode == BinaryOperatorKind::BO_GE && Value == 0 && ContainerIsLHS) ||
162 (OpCode == BinaryOperatorKind::BO_LE && Value == 0 && !ContainerIsLHS))
163 return;
164
Gabor Horvath1f30cf62015-12-28 17:20:33 +0000165 // Do not warn for size > 1, 1 < size, size <= 1, 1 >= size.
166 if (Value == 1) {
167 if ((OpCode == BinaryOperatorKind::BO_GT && ContainerIsLHS) ||
168 (OpCode == BinaryOperatorKind::BO_LT && !ContainerIsLHS))
169 return;
170 if ((OpCode == BinaryOperatorKind::BO_LE && ContainerIsLHS) ||
171 (OpCode == BinaryOperatorKind::BO_GE && !ContainerIsLHS))
172 return;
173 }
Gabor Horvathc6ff9c32015-12-21 09:43:52 +0000174
Alexander Kornienko4babd682015-01-15 15:46:58 +0000175 if (OpCode == BinaryOperatorKind::BO_NE && Value == 0)
176 Negation = true;
177 if ((OpCode == BinaryOperatorKind::BO_GT ||
178 OpCode == BinaryOperatorKind::BO_GE) &&
179 ContainerIsLHS)
180 Negation = true;
181 if ((OpCode == BinaryOperatorKind::BO_LT ||
182 OpCode == BinaryOperatorKind::BO_LE) &&
183 !ContainerIsLHS)
184 Negation = true;
185
186 if (Negation)
187 ReplacementText = "!" + ReplacementText;
188 Hint = FixItHint::CreateReplacement(BinaryOp->getSourceRange(),
189 ReplacementText);
190
191 } else {
192 // If there is a conversion above the size call to bool, it is safe to just
193 // replace size with empty.
194 if (const auto *UnaryOp =
195 Result.Nodes.getNodeAs<UnaryOperator>("NegOnSize"))
196 Hint = FixItHint::CreateReplacement(UnaryOp->getSourceRange(),
197 ReplacementText);
198 else
199 Hint = FixItHint::CreateReplacement(MemberCall->getSourceRange(),
200 "!" + ReplacementText);
201 }
Kirill Bobyrevacb6b352016-09-13 08:58:11 +0000202
Aaron Ballman72163a92017-04-24 14:57:09 +0000203 if (MemberCall) {
Stephen Kelly43465bf2018-08-09 22:42:26 +0000204 diag(MemberCall->getBeginLoc(),
Aaron Ballman72163a92017-04-24 14:57:09 +0000205 "the 'empty' method should be used to check "
206 "for emptiness instead of 'size'")
207 << Hint;
208 } else {
Stephen Kelly43465bf2018-08-09 22:42:26 +0000209 diag(BinCmp->getBeginLoc(),
Aaron Ballman72163a92017-04-24 14:57:09 +0000210 "the 'empty' method should be used to check "
211 "for emptiness instead of comparing to an empty object")
212 << Hint;
213 }
Kirill Bobyrevacb6b352016-09-13 08:58:11 +0000214
215 const auto *Container = Result.Nodes.getNodeAs<NamedDecl>("container");
Alexander Kornienko8577dcc2018-11-27 10:53:44 +0000216 if (const auto *CTS = dyn_cast<ClassTemplateSpecializationDecl>(Container)) {
217 // The definition of the empty() method is the same for all implicit
218 // instantiations. In order to avoid duplicate or inconsistent warnings
219 // (depending on how deduplication is done), we use the same class name
220 // for all implicit instantiations of a template.
221 if (CTS->getSpecializationKind() == TSK_ImplicitInstantiation)
222 Container = CTS->getSpecializedTemplate();
223 }
Kirill Bobyrevacb6b352016-09-13 08:58:11 +0000224 const auto *Empty = Result.Nodes.getNodeAs<FunctionDecl>("empty");
225
226 diag(Empty->getLocation(), "method %0::empty() defined here",
227 DiagnosticIDs::Note)
228 << Container;
Alexander Kornienko4babd682015-01-15 15:46:58 +0000229}
230
231} // namespace readability
232} // namespace tidy
233} // namespace clang