Alexander Kornienko | 1b677db | 2015-03-09 12:18:39 +0000 | [diff] [blame] | 1 | //===--- ContainerSizeEmptyCheck.cpp - clang-tidy -------------------------===// |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 2 | // |
| 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 Kornienko | 1b677db | 2015-03-09 12:18:39 +0000 | [diff] [blame] | 9 | #include "ContainerSizeEmptyCheck.h" |
Aaron Ballman | 72163a9 | 2017-04-24 14:57:09 +0000 | [diff] [blame] | 10 | #include "../utils/ASTUtils.h" |
Kirill Bobyrev | acb6b35 | 2016-09-13 08:58:11 +0000 | [diff] [blame] | 11 | #include "../utils/Matchers.h" |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 12 | #include "clang/AST/ASTContext.h" |
| 13 | #include "clang/ASTMatchers/ASTMatchers.h" |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 14 | #include "clang/Lex/Lexer.h" |
Chandler Carruth | f766278 | 2015-02-13 09:07:58 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/StringRef.h" |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 16 | |
| 17 | using namespace clang::ast_matchers; |
| 18 | |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 19 | namespace clang { |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 20 | namespace tidy { |
| 21 | namespace readability { |
| 22 | |
Aaron Ballman | 72163a9 | 2017-04-24 14:57:09 +0000 | [diff] [blame] | 23 | using utils::IsBinaryOrTernary; |
| 24 | |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 25 | ContainerSizeEmptyCheck::ContainerSizeEmptyCheck(StringRef Name, |
| 26 | ClangTidyContext *Context) |
| 27 | : ClangTidyCheck(Name, Context) {} |
| 28 | |
| 29 | void ContainerSizeEmptyCheck::registerMatchers(MatchFinder *Finder) { |
Aaron Ballman | 1f1b067 | 2015-09-02 16:05:21 +0000 | [diff] [blame] | 30 | // 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 Klimek | 7b9c117 | 2017-08-02 13:13:11 +0000 | [diff] [blame] | 35 | 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 Bergeron | 9d26599 | 2016-04-21 16:57:56 +0000 | [diff] [blame] | 47 | |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 48 | const auto WrongUse = anyOf( |
Gabor Horvath | 533c01d | 2016-04-19 13:29:05 +0000 | [diff] [blame] | 49 | hasParent(binaryOperator( |
Etienne Bergeron | 9d26599 | 2016-04-21 16:57:56 +0000 | [diff] [blame] | 50 | matchers::isComparisonOperator(), |
Gabor Horvath | 533c01d | 2016-04-19 13:29:05 +0000 | [diff] [blame] | 51 | hasEitherOperand(ignoringImpCasts(anyOf( |
| 52 | integerLiteral(equals(1)), integerLiteral(equals(0)))))) |
| 53 | .bind("SizeBinaryOp")), |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 54 | hasParent(implicitCastExpr( |
Gabor Horvath | a4fd3be | 2016-02-09 09:26:11 +0000 | [diff] [blame] | 55 | hasImplicitDestinationType(booleanType()), |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 56 | anyOf( |
| 57 | hasParent(unaryOperator(hasOperatorName("!")).bind("NegOnSize")), |
| 58 | anything()))), |
Gabor Horvath | a4fd3be | 2016-02-09 09:26:11 +0000 | [diff] [blame] | 59 | hasParent(explicitCastExpr(hasDestinationType(booleanType())))); |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 60 | |
| 61 | Finder->addMatcher( |
Kirill Bobyrev | 0d0bbfd | 2016-09-13 10:19:13 +0000 | [diff] [blame] | 62 | cxxMemberCallExpr(on(expr(anyOf(hasType(ValidContainer), |
| 63 | hasType(pointsTo(ValidContainer)), |
Alexander Kornienko | f6cd367 | 2017-03-20 22:15:27 +0000 | [diff] [blame] | 64 | hasType(references(ValidContainer))))), |
Alexander Kornienko | b5ca17f | 2016-12-21 23:44:23 +0000 | [diff] [blame] | 65 | callee(cxxMethodDecl(hasName("size"))), WrongUse, |
| 66 | unless(hasAncestor(cxxMethodDecl( |
| 67 | ofClass(equalsBoundNode("container")))))) |
Aaron Ballman | b9ea09c | 2015-09-17 13:31:25 +0000 | [diff] [blame] | 68 | .bind("SizeCallExpr"), |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 69 | this); |
Aaron Ballman | 72163a9 | 2017-04-24 14:57:09 +0000 | [diff] [blame] | 70 | |
| 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 Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void ContainerSizeEmptyCheck::check(const MatchFinder::MatchResult &Result) { |
| 105 | const auto *MemberCall = |
| 106 | Result.Nodes.getNodeAs<CXXMemberCallExpr>("SizeCallExpr"); |
Aaron Ballman | 72163a9 | 2017-04-24 14:57:09 +0000 | [diff] [blame] | 107 | const auto *BinCmp = Result.Nodes.getNodeAs<CXXOperatorCallExpr>("BinCmp"); |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 108 | const auto *BinaryOp = Result.Nodes.getNodeAs<BinaryOperator>("SizeBinaryOp"); |
Aaron Ballman | 72163a9 | 2017-04-24 14:57:09 +0000 | [diff] [blame] | 109 | 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 Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 114 | FixItHint Hint; |
Gabor Horvath | afad84c | 2016-09-24 02:13:45 +0000 | [diff] [blame] | 115 | std::string ReplacementText = |
| 116 | Lexer::getSourceText(CharSourceRange::getTokenRange(E->getSourceRange()), |
| 117 | *Result.SourceManager, getLangOpts()); |
Aaron Ballman | 72163a9 | 2017-04-24 14:57:09 +0000 | [diff] [blame] | 118 | if (BinCmp && IsBinaryOrTernary(E)) { |
| 119 | // Not just a DeclRefExpr, so parenthesize to be on the safe side. |
| 120 | ReplacementText = "(" + ReplacementText + ")"; |
| 121 | } |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 122 | if (E->getType()->isPointerType()) |
| 123 | ReplacementText += "->empty()"; |
| 124 | else |
| 125 | ReplacementText += ".empty()"; |
| 126 | |
Aaron Ballman | 72163a9 | 2017-04-24 14:57:09 +0000 | [diff] [blame] | 127 | 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 Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 134 | bool Negation = false; |
Gabor Horvath | a4e35ec | 2015-12-12 11:31:25 +0000 | [diff] [blame] | 135 | const bool ContainerIsLHS = |
| 136 | !llvm::isa<IntegerLiteral>(BinaryOp->getLHS()->IgnoreImpCasts()); |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 137 | const auto OpCode = BinaryOp->getOpcode(); |
| 138 | uint64_t Value = 0; |
| 139 | if (ContainerIsLHS) { |
Gabor Horvath | a4e35ec | 2015-12-12 11:31:25 +0000 | [diff] [blame] | 140 | if (const auto *Literal = llvm::dyn_cast<IntegerLiteral>( |
| 141 | BinaryOp->getRHS()->IgnoreImpCasts())) |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 142 | Value = Literal->getValue().getLimitedValue(); |
| 143 | else |
| 144 | return; |
| 145 | } else { |
Gabor Horvath | a4e35ec | 2015-12-12 11:31:25 +0000 | [diff] [blame] | 146 | Value = |
| 147 | llvm::dyn_cast<IntegerLiteral>(BinaryOp->getLHS()->IgnoreImpCasts()) |
| 148 | ->getValue() |
| 149 | .getLimitedValue(); |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | // Constant that is not handled. |
| 153 | if (Value > 1) |
| 154 | return; |
| 155 | |
Gabor Horvath | 533c01d | 2016-04-19 13:29:05 +0000 | [diff] [blame] | 156 | if (Value == 1 && (OpCode == BinaryOperatorKind::BO_EQ || |
| 157 | OpCode == BinaryOperatorKind::BO_NE)) |
| 158 | return; |
| 159 | |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 160 | // 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 Horvath | 1f30cf6 | 2015-12-28 17:20:33 +0000 | [diff] [blame] | 165 | // 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 Horvath | c6ff9c3 | 2015-12-21 09:43:52 +0000 | [diff] [blame] | 174 | |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 175 | 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 Bobyrev | acb6b35 | 2016-09-13 08:58:11 +0000 | [diff] [blame] | 202 | |
Aaron Ballman | 72163a9 | 2017-04-24 14:57:09 +0000 | [diff] [blame] | 203 | if (MemberCall) { |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 204 | diag(MemberCall->getBeginLoc(), |
Aaron Ballman | 72163a9 | 2017-04-24 14:57:09 +0000 | [diff] [blame] | 205 | "the 'empty' method should be used to check " |
| 206 | "for emptiness instead of 'size'") |
| 207 | << Hint; |
| 208 | } else { |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 209 | diag(BinCmp->getBeginLoc(), |
Aaron Ballman | 72163a9 | 2017-04-24 14:57:09 +0000 | [diff] [blame] | 210 | "the 'empty' method should be used to check " |
| 211 | "for emptiness instead of comparing to an empty object") |
| 212 | << Hint; |
| 213 | } |
Kirill Bobyrev | acb6b35 | 2016-09-13 08:58:11 +0000 | [diff] [blame] | 214 | |
| 215 | const auto *Container = Result.Nodes.getNodeAs<NamedDecl>("container"); |
Alexander Kornienko | 8577dcc | 2018-11-27 10:53:44 +0000 | [diff] [blame^] | 216 | 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 Bobyrev | acb6b35 | 2016-09-13 08:58:11 +0000 | [diff] [blame] | 224 | const auto *Empty = Result.Nodes.getNodeAs<FunctionDecl>("empty"); |
| 225 | |
| 226 | diag(Empty->getLocation(), "method %0::empty() defined here", |
| 227 | DiagnosticIDs::Note) |
| 228 | << Container; |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | } // namespace readability |
| 232 | } // namespace tidy |
| 233 | } // namespace clang |