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