Etienne Bergeron | bda187d | 2016-04-26 17:30:30 +0000 | [diff] [blame] | 1 | //===--- RedundantExpressionCheck.cpp - clang-tidy-------------------------===// |
| 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 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "RedundantExpressionCheck.h" |
| 11 | #include "../utils/Matchers.h" |
Etienne Bergeron | c87599f | 2016-05-12 04:32:47 +0000 | [diff] [blame^] | 12 | #include "../utils/OptionsUtils.h" |
Etienne Bergeron | bda187d | 2016-04-26 17:30:30 +0000 | [diff] [blame] | 13 | #include "clang/AST/ASTContext.h" |
| 14 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
Etienne Bergeron | c87599f | 2016-05-12 04:32:47 +0000 | [diff] [blame^] | 15 | #include "clang/Lex/Lexer.h" |
Etienne Bergeron | bda187d | 2016-04-26 17:30:30 +0000 | [diff] [blame] | 16 | |
| 17 | using namespace clang::ast_matchers; |
| 18 | |
| 19 | namespace clang { |
| 20 | namespace tidy { |
| 21 | namespace misc { |
| 22 | |
Etienne Bergeron | c87599f | 2016-05-12 04:32:47 +0000 | [diff] [blame^] | 23 | static const char KnownBannedMacroNames[] = |
| 24 | "EAGAIN;EWOULDBLOCK;SIGCLD;SIGCHLD;"; |
| 25 | |
| 26 | static bool areEquivalentNameSpecifier(const NestedNameSpecifier *Left, |
| 27 | const NestedNameSpecifier *Right) { |
| 28 | llvm::FoldingSetNodeID LeftID, RightID; |
| 29 | Left->Profile(LeftID); |
| 30 | Right->Profile(RightID); |
| 31 | return LeftID == RightID; |
| 32 | } |
| 33 | |
| 34 | static bool areEquivalentExpr(const Expr *Left, const Expr *Right) { |
Etienne Bergeron | bda187d | 2016-04-26 17:30:30 +0000 | [diff] [blame] | 35 | if (!Left || !Right) |
| 36 | return !Left && !Right; |
| 37 | |
| 38 | Left = Left->IgnoreParens(); |
| 39 | Right = Right->IgnoreParens(); |
| 40 | |
| 41 | // Compare classes. |
| 42 | if (Left->getStmtClass() != Right->getStmtClass()) |
| 43 | return false; |
| 44 | |
| 45 | // Compare children. |
| 46 | Expr::const_child_iterator LeftIter = Left->child_begin(); |
| 47 | Expr::const_child_iterator RightIter = Right->child_begin(); |
| 48 | while (LeftIter != Left->child_end() && RightIter != Right->child_end()) { |
Etienne Bergeron | c87599f | 2016-05-12 04:32:47 +0000 | [diff] [blame^] | 49 | if (!areEquivalentExpr(dyn_cast<Expr>(*LeftIter), |
| 50 | dyn_cast<Expr>(*RightIter))) |
Etienne Bergeron | bda187d | 2016-04-26 17:30:30 +0000 | [diff] [blame] | 51 | return false; |
| 52 | ++LeftIter; |
| 53 | ++RightIter; |
| 54 | } |
| 55 | if (LeftIter != Left->child_end() || RightIter != Right->child_end()) |
| 56 | return false; |
| 57 | |
| 58 | // Perform extra checks. |
| 59 | switch (Left->getStmtClass()) { |
| 60 | default: |
| 61 | return false; |
| 62 | |
| 63 | case Stmt::CharacterLiteralClass: |
| 64 | return cast<CharacterLiteral>(Left)->getValue() == |
| 65 | cast<CharacterLiteral>(Right)->getValue(); |
| 66 | case Stmt::IntegerLiteralClass: { |
| 67 | llvm::APInt LeftLit = cast<IntegerLiteral>(Left)->getValue(); |
| 68 | llvm::APInt RightLit = cast<IntegerLiteral>(Right)->getValue(); |
Etienne Bergeron | c87599f | 2016-05-12 04:32:47 +0000 | [diff] [blame^] | 69 | return LeftLit.getBitWidth() == RightLit.getBitWidth() && |
| 70 | LeftLit == RightLit; |
Etienne Bergeron | bda187d | 2016-04-26 17:30:30 +0000 | [diff] [blame] | 71 | } |
| 72 | case Stmt::FloatingLiteralClass: |
| 73 | return cast<FloatingLiteral>(Left)->getValue().bitwiseIsEqual( |
| 74 | cast<FloatingLiteral>(Right)->getValue()); |
| 75 | case Stmt::StringLiteralClass: |
| 76 | return cast<StringLiteral>(Left)->getBytes() == |
| 77 | cast<StringLiteral>(Right)->getBytes(); |
| 78 | |
Etienne Bergeron | c87599f | 2016-05-12 04:32:47 +0000 | [diff] [blame^] | 79 | case Stmt::DependentScopeDeclRefExprClass: |
| 80 | if (cast<DependentScopeDeclRefExpr>(Left)->getDeclName() != |
| 81 | cast<DependentScopeDeclRefExpr>(Right)->getDeclName()) |
| 82 | return false; |
| 83 | return areEquivalentNameSpecifier( |
| 84 | cast<DependentScopeDeclRefExpr>(Left)->getQualifier(), |
| 85 | cast<DependentScopeDeclRefExpr>(Right)->getQualifier()); |
Etienne Bergeron | bda187d | 2016-04-26 17:30:30 +0000 | [diff] [blame] | 86 | case Stmt::DeclRefExprClass: |
| 87 | return cast<DeclRefExpr>(Left)->getDecl() == |
| 88 | cast<DeclRefExpr>(Right)->getDecl(); |
| 89 | case Stmt::MemberExprClass: |
| 90 | return cast<MemberExpr>(Left)->getMemberDecl() == |
| 91 | cast<MemberExpr>(Right)->getMemberDecl(); |
| 92 | |
| 93 | case Stmt::CStyleCastExprClass: |
| 94 | return cast<CStyleCastExpr>(Left)->getTypeAsWritten() == |
| 95 | cast<CStyleCastExpr>(Right)->getTypeAsWritten(); |
| 96 | |
| 97 | case Stmt::CallExprClass: |
| 98 | case Stmt::ImplicitCastExprClass: |
| 99 | case Stmt::ArraySubscriptExprClass: |
| 100 | return true; |
| 101 | |
| 102 | case Stmt::UnaryOperatorClass: |
| 103 | if (cast<UnaryOperator>(Left)->isIncrementDecrementOp()) |
| 104 | return false; |
| 105 | return cast<UnaryOperator>(Left)->getOpcode() == |
| 106 | cast<UnaryOperator>(Right)->getOpcode(); |
| 107 | case Stmt::BinaryOperatorClass: |
| 108 | return cast<BinaryOperator>(Left)->getOpcode() == |
| 109 | cast<BinaryOperator>(Right)->getOpcode(); |
| 110 | } |
| 111 | } |
| 112 | |
Etienne Bergeron | c87599f | 2016-05-12 04:32:47 +0000 | [diff] [blame^] | 113 | AST_MATCHER(BinaryOperator, operandsAreEquivalent) { |
| 114 | return areEquivalentExpr(Node.getLHS(), Node.getRHS()); |
Etienne Bergeron | bda187d | 2016-04-26 17:30:30 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Etienne Bergeron | c87599f | 2016-05-12 04:32:47 +0000 | [diff] [blame^] | 117 | AST_MATCHER(ConditionalOperator, expressionsAreEquivalent) { |
| 118 | return areEquivalentExpr(Node.getTrueExpr(), Node.getFalseExpr()); |
| 119 | } |
| 120 | |
| 121 | AST_MATCHER(CallExpr, parametersAreEquivalent) { |
| 122 | return Node.getNumArgs() == 2 && |
| 123 | areEquivalentExpr(Node.getArg(0), Node.getArg(1)); |
| 124 | } |
| 125 | |
| 126 | AST_MATCHER(BinaryOperator, binaryOperatorIsInMacro) { |
Etienne Bergeron | bda187d | 2016-04-26 17:30:30 +0000 | [diff] [blame] | 127 | return Node.getOperatorLoc().isMacroID(); |
| 128 | } |
| 129 | |
Etienne Bergeron | c87599f | 2016-05-12 04:32:47 +0000 | [diff] [blame^] | 130 | AST_MATCHER(ConditionalOperator, conditionalOperatorIsInMacro) { |
| 131 | return Node.getQuestionLoc().isMacroID() || Node.getColonLoc().isMacroID(); |
| 132 | } |
| 133 | |
| 134 | AST_MATCHER(Expr, isMacro) { return Node.getExprLoc().isMacroID(); } |
| 135 | |
| 136 | AST_MATCHER_P(Expr, expandedByMacro, std::set<std::string>, Names) { |
| 137 | const SourceManager &SM = Finder->getASTContext().getSourceManager(); |
| 138 | const LangOptions &LO = Finder->getASTContext().getLangOpts(); |
| 139 | SourceLocation Loc = Node.getExprLoc(); |
| 140 | while (Loc.isMacroID()) { |
| 141 | std::string MacroName = Lexer::getImmediateMacroName(Loc, SM, LO); |
| 142 | if (Names.find(MacroName) != Names.end()) |
| 143 | return true; |
| 144 | Loc = SM.getImmediateMacroCallerLoc(Loc); |
| 145 | } |
| 146 | return false; |
Etienne Bergeron | bda187d | 2016-04-26 17:30:30 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void RedundantExpressionCheck::registerMatchers(MatchFinder *Finder) { |
| 150 | const auto AnyLiteralExpr = ignoringParenImpCasts( |
| 151 | anyOf(cxxBoolLiteral(), characterLiteral(), integerLiteral())); |
| 152 | |
Etienne Bergeron | c87599f | 2016-05-12 04:32:47 +0000 | [diff] [blame^] | 153 | std::vector<std::string> MacroNames = |
| 154 | utils::options::parseStringList(KnownBannedMacroNames); |
| 155 | std::set<std::string> Names(MacroNames.begin(), MacroNames.end()); |
| 156 | |
| 157 | const auto BannedIntegerLiteral = integerLiteral(expandedByMacro(Names)); |
| 158 | |
Etienne Bergeron | bda187d | 2016-04-26 17:30:30 +0000 | [diff] [blame] | 159 | Finder->addMatcher( |
| 160 | binaryOperator(anyOf(hasOperatorName("-"), hasOperatorName("/"), |
| 161 | hasOperatorName("%"), hasOperatorName("|"), |
| 162 | hasOperatorName("&"), hasOperatorName("^"), |
| 163 | matchers::isComparisonOperator(), |
| 164 | hasOperatorName("&&"), hasOperatorName("||"), |
| 165 | hasOperatorName("=")), |
Etienne Bergeron | c87599f | 2016-05-12 04:32:47 +0000 | [diff] [blame^] | 166 | operandsAreEquivalent(), |
Etienne Bergeron | bda187d | 2016-04-26 17:30:30 +0000 | [diff] [blame] | 167 | // Filter noisy false positives. |
Etienne Bergeron | c87599f | 2016-05-12 04:32:47 +0000 | [diff] [blame^] | 168 | unless(isInTemplateInstantiation()), |
| 169 | unless(binaryOperatorIsInMacro()), |
Etienne Bergeron | bda187d | 2016-04-26 17:30:30 +0000 | [diff] [blame] | 170 | unless(hasType(realFloatingPointType())), |
| 171 | unless(hasEitherOperand(hasType(realFloatingPointType()))), |
Etienne Bergeron | c87599f | 2016-05-12 04:32:47 +0000 | [diff] [blame^] | 172 | unless(hasLHS(AnyLiteralExpr)), |
| 173 | unless(hasDescendant(BannedIntegerLiteral))) |
Etienne Bergeron | bda187d | 2016-04-26 17:30:30 +0000 | [diff] [blame] | 174 | .bind("binary"), |
| 175 | this); |
Etienne Bergeron | c87599f | 2016-05-12 04:32:47 +0000 | [diff] [blame^] | 176 | |
| 177 | Finder->addMatcher( |
| 178 | conditionalOperator(expressionsAreEquivalent(), |
| 179 | // Filter noisy false positives. |
| 180 | unless(conditionalOperatorIsInMacro()), |
| 181 | unless(hasTrueExpression(AnyLiteralExpr)), |
| 182 | unless(isInTemplateInstantiation())) |
| 183 | .bind("cond"), |
| 184 | this); |
| 185 | |
| 186 | Finder->addMatcher( |
| 187 | cxxOperatorCallExpr( |
| 188 | anyOf( |
| 189 | hasOverloadedOperatorName("-"), hasOverloadedOperatorName("/"), |
| 190 | hasOverloadedOperatorName("%"), hasOverloadedOperatorName("|"), |
| 191 | hasOverloadedOperatorName("&"), hasOverloadedOperatorName("^"), |
| 192 | hasOverloadedOperatorName("=="), hasOverloadedOperatorName("!="), |
| 193 | hasOverloadedOperatorName("<"), hasOverloadedOperatorName("<="), |
| 194 | hasOverloadedOperatorName(">"), hasOverloadedOperatorName(">="), |
| 195 | hasOverloadedOperatorName("&&"), hasOverloadedOperatorName("||"), |
| 196 | hasOverloadedOperatorName("=")), |
| 197 | parametersAreEquivalent(), |
| 198 | // Filter noisy false positives. |
| 199 | unless(isMacro()), unless(isInTemplateInstantiation())) |
| 200 | .bind("call"), |
| 201 | this); |
Etienne Bergeron | bda187d | 2016-04-26 17:30:30 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | void RedundantExpressionCheck::check(const MatchFinder::MatchResult &Result) { |
| 205 | if (const auto *BinOp = Result.Nodes.getNodeAs<BinaryOperator>("binary")) |
| 206 | diag(BinOp->getOperatorLoc(), "both side of operator are equivalent"); |
Etienne Bergeron | c87599f | 2016-05-12 04:32:47 +0000 | [diff] [blame^] | 207 | if (const auto *CondOp = Result.Nodes.getNodeAs<ConditionalOperator>("cond")) |
| 208 | diag(CondOp->getColonLoc(), "'true' and 'false' expression are equivalent"); |
| 209 | if (const auto *Call = Result.Nodes.getNodeAs<CXXOperatorCallExpr>("call")) |
| 210 | diag(Call->getOperatorLoc(), |
| 211 | "both side of overloaded operator are equivalent"); |
Etienne Bergeron | bda187d | 2016-04-26 17:30:30 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | } // namespace misc |
| 215 | } // namespace tidy |
| 216 | } // namespace clang |