blob: bb741e3795482fee8374aafa9c082a910dd14661 [file] [log] [blame]
Etienne Bergeronbda187d2016-04-26 17:30:30 +00001//===--- 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 Bergeronc87599f2016-05-12 04:32:47 +000012#include "../utils/OptionsUtils.h"
Etienne Bergeronbda187d2016-04-26 17:30:30 +000013#include "clang/AST/ASTContext.h"
14#include "clang/ASTMatchers/ASTMatchFinder.h"
Etienne Bergeronc87599f2016-05-12 04:32:47 +000015#include "clang/Lex/Lexer.h"
Etienne Bergeronbda187d2016-04-26 17:30:30 +000016
17using namespace clang::ast_matchers;
18
19namespace clang {
20namespace tidy {
21namespace misc {
22
Etienne Bergeronc87599f2016-05-12 04:32:47 +000023static const char KnownBannedMacroNames[] =
24 "EAGAIN;EWOULDBLOCK;SIGCLD;SIGCHLD;";
25
26static 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
34static bool areEquivalentExpr(const Expr *Left, const Expr *Right) {
Etienne Bergeronbda187d2016-04-26 17:30:30 +000035 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 Bergeronc87599f2016-05-12 04:32:47 +000049 if (!areEquivalentExpr(dyn_cast<Expr>(*LeftIter),
50 dyn_cast<Expr>(*RightIter)))
Etienne Bergeronbda187d2016-04-26 17:30:30 +000051 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 Bergeronc87599f2016-05-12 04:32:47 +000069 return LeftLit.getBitWidth() == RightLit.getBitWidth() &&
70 LeftLit == RightLit;
Etienne Bergeronbda187d2016-04-26 17:30:30 +000071 }
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 Bergeronc87599f2016-05-12 04:32:47 +000079 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 Bergeronbda187d2016-04-26 17:30:30 +000086 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 Bergeronc87599f2016-05-12 04:32:47 +0000113AST_MATCHER(BinaryOperator, operandsAreEquivalent) {
114 return areEquivalentExpr(Node.getLHS(), Node.getRHS());
Etienne Bergeronbda187d2016-04-26 17:30:30 +0000115}
116
Etienne Bergeronc87599f2016-05-12 04:32:47 +0000117AST_MATCHER(ConditionalOperator, expressionsAreEquivalent) {
118 return areEquivalentExpr(Node.getTrueExpr(), Node.getFalseExpr());
119}
120
121AST_MATCHER(CallExpr, parametersAreEquivalent) {
122 return Node.getNumArgs() == 2 &&
123 areEquivalentExpr(Node.getArg(0), Node.getArg(1));
124}
125
126AST_MATCHER(BinaryOperator, binaryOperatorIsInMacro) {
Etienne Bergeronbda187d2016-04-26 17:30:30 +0000127 return Node.getOperatorLoc().isMacroID();
128}
129
Etienne Bergeronc87599f2016-05-12 04:32:47 +0000130AST_MATCHER(ConditionalOperator, conditionalOperatorIsInMacro) {
131 return Node.getQuestionLoc().isMacroID() || Node.getColonLoc().isMacroID();
132}
133
134AST_MATCHER(Expr, isMacro) { return Node.getExprLoc().isMacroID(); }
135
136AST_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 Bergeronbda187d2016-04-26 17:30:30 +0000147}
148
149void RedundantExpressionCheck::registerMatchers(MatchFinder *Finder) {
150 const auto AnyLiteralExpr = ignoringParenImpCasts(
151 anyOf(cxxBoolLiteral(), characterLiteral(), integerLiteral()));
152
Etienne Bergeronc87599f2016-05-12 04:32:47 +0000153 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 Bergeronbda187d2016-04-26 17:30:30 +0000159 Finder->addMatcher(
160 binaryOperator(anyOf(hasOperatorName("-"), hasOperatorName("/"),
161 hasOperatorName("%"), hasOperatorName("|"),
162 hasOperatorName("&"), hasOperatorName("^"),
163 matchers::isComparisonOperator(),
164 hasOperatorName("&&"), hasOperatorName("||"),
165 hasOperatorName("=")),
Etienne Bergeronc87599f2016-05-12 04:32:47 +0000166 operandsAreEquivalent(),
Etienne Bergeronbda187d2016-04-26 17:30:30 +0000167 // Filter noisy false positives.
Etienne Bergeronc87599f2016-05-12 04:32:47 +0000168 unless(isInTemplateInstantiation()),
169 unless(binaryOperatorIsInMacro()),
Etienne Bergeronbda187d2016-04-26 17:30:30 +0000170 unless(hasType(realFloatingPointType())),
171 unless(hasEitherOperand(hasType(realFloatingPointType()))),
Etienne Bergeronc87599f2016-05-12 04:32:47 +0000172 unless(hasLHS(AnyLiteralExpr)),
173 unless(hasDescendant(BannedIntegerLiteral)))
Etienne Bergeronbda187d2016-04-26 17:30:30 +0000174 .bind("binary"),
175 this);
Etienne Bergeronc87599f2016-05-12 04:32:47 +0000176
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 Bergeronbda187d2016-04-26 17:30:30 +0000202}
203
204void 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 Bergeronc87599f2016-05-12 04:32:47 +0000207 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 Bergeronbda187d2016-04-26 17:30:30 +0000212}
213
214} // namespace misc
215} // namespace tidy
216} // namespace clang