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" |
Kirill Bobyrev | acb6b35 | 2016-09-13 08:58:11 +0000 | [diff] [blame] | 10 | #include "../utils/Matchers.h" |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 11 | #include "clang/AST/ASTContext.h" |
| 12 | #include "clang/ASTMatchers/ASTMatchers.h" |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 13 | #include "clang/Lex/Lexer.h" |
Chandler Carruth | f766278 | 2015-02-13 09:07:58 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/StringRef.h" |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 15 | |
| 16 | using namespace clang::ast_matchers; |
| 17 | |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 18 | namespace clang { |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 19 | namespace tidy { |
| 20 | namespace readability { |
| 21 | |
| 22 | ContainerSizeEmptyCheck::ContainerSizeEmptyCheck(StringRef Name, |
| 23 | ClangTidyContext *Context) |
| 24 | : ClangTidyCheck(Name, Context) {} |
| 25 | |
| 26 | void ContainerSizeEmptyCheck::registerMatchers(MatchFinder *Finder) { |
Aaron Ballman | 1f1b067 | 2015-09-02 16:05:21 +0000 | [diff] [blame] | 27 | // Only register the matchers for C++; the functionality currently does not |
| 28 | // provide any benefit to other languages, despite being benign. |
| 29 | if (!getLangOpts().CPlusPlus) |
| 30 | return; |
| 31 | |
Kirill Bobyrev | acb6b35 | 2016-09-13 08:58:11 +0000 | [diff] [blame] | 32 | const auto validContainer = cxxRecordDecl(isSameOrDerivedFrom( |
| 33 | namedDecl( |
| 34 | has(cxxMethodDecl( |
| 35 | isConst(), parameterCountIs(0), isPublic(), hasName("size"), |
| 36 | returns(qualType(isInteger(), unless(booleanType())))) |
| 37 | .bind("size")), |
| 38 | has(cxxMethodDecl(isConst(), parameterCountIs(0), isPublic(), |
| 39 | hasName("empty"), returns(booleanType())) |
| 40 | .bind("empty"))) |
| 41 | .bind("container"))); |
Etienne Bergeron | 9d26599 | 2016-04-21 16:57:56 +0000 | [diff] [blame] | 42 | |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 43 | const auto WrongUse = anyOf( |
Gabor Horvath | 533c01d | 2016-04-19 13:29:05 +0000 | [diff] [blame] | 44 | hasParent(binaryOperator( |
Etienne Bergeron | 9d26599 | 2016-04-21 16:57:56 +0000 | [diff] [blame] | 45 | matchers::isComparisonOperator(), |
Gabor Horvath | 533c01d | 2016-04-19 13:29:05 +0000 | [diff] [blame] | 46 | hasEitherOperand(ignoringImpCasts(anyOf( |
| 47 | integerLiteral(equals(1)), integerLiteral(equals(0)))))) |
| 48 | .bind("SizeBinaryOp")), |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 49 | hasParent(implicitCastExpr( |
Gabor Horvath | a4fd3be | 2016-02-09 09:26:11 +0000 | [diff] [blame] | 50 | hasImplicitDestinationType(booleanType()), |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 51 | anyOf( |
| 52 | hasParent(unaryOperator(hasOperatorName("!")).bind("NegOnSize")), |
| 53 | anything()))), |
Gabor Horvath | a4fd3be | 2016-02-09 09:26:11 +0000 | [diff] [blame] | 54 | hasParent(explicitCastExpr(hasDestinationType(booleanType())))); |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 55 | |
| 56 | Finder->addMatcher( |
Kirill Bobyrev | acb6b35 | 2016-09-13 08:58:11 +0000 | [diff] [blame] | 57 | cxxMemberCallExpr(on(expr(anyOf(hasType(validContainer), |
| 58 | hasType(pointsTo(validContainer)), |
| 59 | hasType(references(validContainer)))) |
| 60 | .bind("STLObject")), |
| 61 | callee(cxxMethodDecl(hasName("size"))), WrongUse) |
Aaron Ballman | b9ea09c | 2015-09-17 13:31:25 +0000 | [diff] [blame] | 62 | .bind("SizeCallExpr"), |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 63 | this); |
| 64 | } |
| 65 | |
| 66 | void ContainerSizeEmptyCheck::check(const MatchFinder::MatchResult &Result) { |
| 67 | const auto *MemberCall = |
| 68 | Result.Nodes.getNodeAs<CXXMemberCallExpr>("SizeCallExpr"); |
| 69 | const auto *BinaryOp = Result.Nodes.getNodeAs<BinaryOperator>("SizeBinaryOp"); |
| 70 | const auto *E = Result.Nodes.getNodeAs<Expr>("STLObject"); |
| 71 | FixItHint Hint; |
Alexander Kornienko | 96e7b8b | 2015-01-22 12:40:47 +0000 | [diff] [blame] | 72 | std::string ReplacementText = Lexer::getSourceText( |
| 73 | CharSourceRange::getTokenRange(E->getSourceRange()), |
| 74 | *Result.SourceManager, Result.Context->getLangOpts()); |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 75 | if (E->getType()->isPointerType()) |
| 76 | ReplacementText += "->empty()"; |
| 77 | else |
| 78 | ReplacementText += ".empty()"; |
| 79 | |
| 80 | if (BinaryOp) { // Determine the correct transformation. |
| 81 | bool Negation = false; |
Gabor Horvath | a4e35ec | 2015-12-12 11:31:25 +0000 | [diff] [blame] | 82 | const bool ContainerIsLHS = |
| 83 | !llvm::isa<IntegerLiteral>(BinaryOp->getLHS()->IgnoreImpCasts()); |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 84 | const auto OpCode = BinaryOp->getOpcode(); |
| 85 | uint64_t Value = 0; |
| 86 | if (ContainerIsLHS) { |
Gabor Horvath | a4e35ec | 2015-12-12 11:31:25 +0000 | [diff] [blame] | 87 | if (const auto *Literal = llvm::dyn_cast<IntegerLiteral>( |
| 88 | BinaryOp->getRHS()->IgnoreImpCasts())) |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 89 | Value = Literal->getValue().getLimitedValue(); |
| 90 | else |
| 91 | return; |
| 92 | } else { |
Gabor Horvath | a4e35ec | 2015-12-12 11:31:25 +0000 | [diff] [blame] | 93 | Value = |
| 94 | llvm::dyn_cast<IntegerLiteral>(BinaryOp->getLHS()->IgnoreImpCasts()) |
| 95 | ->getValue() |
| 96 | .getLimitedValue(); |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | // Constant that is not handled. |
| 100 | if (Value > 1) |
| 101 | return; |
| 102 | |
Gabor Horvath | 533c01d | 2016-04-19 13:29:05 +0000 | [diff] [blame] | 103 | if (Value == 1 && (OpCode == BinaryOperatorKind::BO_EQ || |
| 104 | OpCode == BinaryOperatorKind::BO_NE)) |
| 105 | return; |
| 106 | |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 107 | // Always true, no warnings for that. |
| 108 | if ((OpCode == BinaryOperatorKind::BO_GE && Value == 0 && ContainerIsLHS) || |
| 109 | (OpCode == BinaryOperatorKind::BO_LE && Value == 0 && !ContainerIsLHS)) |
| 110 | return; |
| 111 | |
Gabor Horvath | 1f30cf6 | 2015-12-28 17:20:33 +0000 | [diff] [blame] | 112 | // Do not warn for size > 1, 1 < size, size <= 1, 1 >= size. |
| 113 | if (Value == 1) { |
| 114 | if ((OpCode == BinaryOperatorKind::BO_GT && ContainerIsLHS) || |
| 115 | (OpCode == BinaryOperatorKind::BO_LT && !ContainerIsLHS)) |
| 116 | return; |
| 117 | if ((OpCode == BinaryOperatorKind::BO_LE && ContainerIsLHS) || |
| 118 | (OpCode == BinaryOperatorKind::BO_GE && !ContainerIsLHS)) |
| 119 | return; |
| 120 | } |
Gabor Horvath | c6ff9c3 | 2015-12-21 09:43:52 +0000 | [diff] [blame] | 121 | |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 122 | if (OpCode == BinaryOperatorKind::BO_NE && Value == 0) |
| 123 | Negation = true; |
| 124 | if ((OpCode == BinaryOperatorKind::BO_GT || |
| 125 | OpCode == BinaryOperatorKind::BO_GE) && |
| 126 | ContainerIsLHS) |
| 127 | Negation = true; |
| 128 | if ((OpCode == BinaryOperatorKind::BO_LT || |
| 129 | OpCode == BinaryOperatorKind::BO_LE) && |
| 130 | !ContainerIsLHS) |
| 131 | Negation = true; |
| 132 | |
| 133 | if (Negation) |
| 134 | ReplacementText = "!" + ReplacementText; |
| 135 | Hint = FixItHint::CreateReplacement(BinaryOp->getSourceRange(), |
| 136 | ReplacementText); |
| 137 | |
| 138 | } else { |
| 139 | // If there is a conversion above the size call to bool, it is safe to just |
| 140 | // replace size with empty. |
| 141 | if (const auto *UnaryOp = |
| 142 | Result.Nodes.getNodeAs<UnaryOperator>("NegOnSize")) |
| 143 | Hint = FixItHint::CreateReplacement(UnaryOp->getSourceRange(), |
| 144 | ReplacementText); |
| 145 | else |
| 146 | Hint = FixItHint::CreateReplacement(MemberCall->getSourceRange(), |
| 147 | "!" + ReplacementText); |
| 148 | } |
Kirill Bobyrev | acb6b35 | 2016-09-13 08:58:11 +0000 | [diff] [blame] | 149 | |
Alexander Kornienko | 301130e | 2015-11-09 15:53:28 +0000 | [diff] [blame] | 150 | diag(MemberCall->getLocStart(), "the 'empty' method should be used to check " |
| 151 | "for emptiness instead of 'size'") |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 152 | << Hint; |
Kirill Bobyrev | acb6b35 | 2016-09-13 08:58:11 +0000 | [diff] [blame] | 153 | |
| 154 | const auto *Container = Result.Nodes.getNodeAs<NamedDecl>("container"); |
| 155 | const auto *Empty = Result.Nodes.getNodeAs<FunctionDecl>("empty"); |
| 156 | |
| 157 | diag(Empty->getLocation(), "method %0::empty() defined here", |
| 158 | DiagnosticIDs::Note) |
| 159 | << Container; |
Alexander Kornienko | 4babd68 | 2015-01-15 15:46:58 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | } // namespace readability |
| 163 | } // namespace tidy |
| 164 | } // namespace clang |