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