11 #include "../utils/Matchers.h"
12 #include "../utils/OptionsUtils.h"
13 #include "clang/AST/ASTContext.h"
14 #include "clang/ASTMatchers/ASTMatchFinder.h"
15 #include "clang/Lex/Lexer.h"
17 using namespace clang::ast_matchers;
18 using namespace clang::tidy::matchers;
29 "EAGAIN;EWOULDBLOCK;SIGCLD;SIGCHLD;";
38 const NestedNameSpecifier *Right) {
39 llvm::FoldingSetNodeID LeftID, RightID;
40 Left->Profile(LeftID);
41 Right->Profile(RightID);
42 return LeftID == RightID;
47 return !Left && !Right;
49 Left = Left->IgnoreParens();
50 Right = Right->IgnoreParens();
53 if (Left->getStmtClass() != Right->getStmtClass())
57 Expr::const_child_iterator LeftIter = Left->child_begin();
58 Expr::const_child_iterator RightIter = Right->child_begin();
59 while (LeftIter != Left->child_end() && RightIter != Right->child_end()) {
61 dyn_cast<Expr>(*RightIter)))
66 if (LeftIter != Left->child_end() || RightIter != Right->child_end())
70 switch (Left->getStmtClass()) {
74 case Stmt::CharacterLiteralClass:
75 return cast<CharacterLiteral>(Left)->getValue() ==
76 cast<CharacterLiteral>(Right)->getValue();
77 case Stmt::IntegerLiteralClass: {
78 llvm::APInt LeftLit = cast<IntegerLiteral>(Left)->getValue();
79 llvm::APInt RightLit = cast<IntegerLiteral>(Right)->getValue();
80 return LeftLit.getBitWidth() == RightLit.getBitWidth() &&
83 case Stmt::FloatingLiteralClass:
84 return cast<FloatingLiteral>(Left)->getValue().bitwiseIsEqual(
85 cast<FloatingLiteral>(Right)->getValue());
86 case Stmt::StringLiteralClass:
87 return cast<StringLiteral>(Left)->getBytes() ==
88 cast<StringLiteral>(Right)->getBytes();
90 case Stmt::DependentScopeDeclRefExprClass:
91 if (cast<DependentScopeDeclRefExpr>(Left)->getDeclName() !=
92 cast<DependentScopeDeclRefExpr>(Right)->getDeclName())
95 cast<DependentScopeDeclRefExpr>(Left)->getQualifier(),
96 cast<DependentScopeDeclRefExpr>(Right)->getQualifier());
97 case Stmt::DeclRefExprClass:
98 return cast<DeclRefExpr>(Left)->getDecl() ==
99 cast<DeclRefExpr>(Right)->getDecl();
100 case Stmt::MemberExprClass:
101 return cast<MemberExpr>(Left)->getMemberDecl() ==
102 cast<MemberExpr>(Right)->getMemberDecl();
104 case Stmt::CStyleCastExprClass:
105 return cast<CStyleCastExpr>(Left)->getTypeAsWritten() ==
106 cast<CStyleCastExpr>(Right)->getTypeAsWritten();
108 case Stmt::CallExprClass:
109 case Stmt::ImplicitCastExprClass:
110 case Stmt::ArraySubscriptExprClass:
113 case Stmt::UnaryOperatorClass:
114 if (cast<UnaryOperator>(Left)->isIncrementDecrementOp())
116 return cast<UnaryOperator>(Left)->getOpcode() ==
117 cast<UnaryOperator>(Right)->getOpcode();
118 case Stmt::BinaryOperatorClass:
119 return cast<BinaryOperator>(Left)->getOpcode() ==
120 cast<BinaryOperator>(Right)->getOpcode();
127 const APSInt &ValueLHS,
128 BinaryOperatorKind OpcodeRHS,
129 const APSInt &ValueRHS) {
130 assert(APSInt::compareValues(ValueLHS, ValueRHS) <= 0 &&
131 "Values must be ordered");
133 if (APSInt::compareValues(ValueLHS, ValueRHS) == 0)
134 return OpcodeLHS == OpcodeRHS;
137 APSInt ValueLHS_plus1;
138 return ((OpcodeLHS == BO_LE && OpcodeRHS == BO_LT) ||
139 (OpcodeLHS == BO_GT && OpcodeRHS == BO_GE)) &&
141 APSInt::compareValues(ValueLHS_plus1, ValueRHS) == 0;
147 const APSInt &ValueLHS,
148 BinaryOperatorKind OpcodeRHS,
149 const APSInt &ValueRHS) {
150 assert(APSInt::compareValues(ValueLHS, ValueRHS) <= 0 &&
151 "Values must be ordered");
154 if (APSInt::compareValues(ValueLHS, ValueRHS) == 0) {
157 return OpcodeRHS == BO_NE || OpcodeRHS == BO_GT || OpcodeRHS == BO_LT;
159 return OpcodeRHS == BO_EQ;
161 return OpcodeRHS == BO_GT;
163 return OpcodeRHS == BO_LT;
165 return OpcodeRHS == BO_EQ || OpcodeRHS == BO_GT || OpcodeRHS == BO_GE;
167 return OpcodeRHS == BO_EQ || OpcodeRHS == BO_LT || OpcodeRHS == BO_LE;
174 if ((OpcodeLHS == BO_EQ || OpcodeLHS == BO_LE || OpcodeLHS == BO_LE) &&
175 (OpcodeRHS == BO_EQ || OpcodeRHS == BO_GT || OpcodeRHS == BO_GE))
179 APSInt ValueLHS_plus1;
180 if (OpcodeLHS == BO_GT && OpcodeRHS == BO_LT &&
182 APSInt::compareValues(ValueLHS_plus1, ValueRHS) == 0)
191 const APSInt &ValueLHS,
192 BinaryOperatorKind OpcodeRHS,
193 const APSInt &ValueRHS) {
194 assert(APSInt::compareValues(ValueLHS, ValueRHS) <= 0 &&
195 "Values must be ordered");
198 if (APSInt::compareValues(ValueLHS, ValueRHS) == 0) {
201 return OpcodeRHS == BO_NE;
203 return OpcodeRHS == BO_EQ;
205 return OpcodeRHS == BO_GT || OpcodeRHS == BO_GE;
207 return OpcodeRHS == BO_GE;
209 return OpcodeRHS == BO_LT || OpcodeRHS == BO_LE;
211 return OpcodeRHS == BO_LE;
218 APSInt ValueLHS_plus1;
219 if (OpcodeLHS == BO_LE && OpcodeRHS == BO_GE &&
221 APSInt::compareValues(ValueLHS_plus1, ValueRHS) == 0)
225 if ((OpcodeLHS == BO_GT || OpcodeLHS == BO_GE) &&
226 (OpcodeRHS == BO_LT || OpcodeRHS == BO_LE))
233 const APSInt &ValueLHS,
234 BinaryOperatorKind OpcodeRHS,
235 const APSInt &ValueRHS) {
236 int Comparison = APSInt::compareValues(ValueLHS, ValueRHS);
239 return OpcodeRHS == BO_EQ && Comparison == 0;
241 return (OpcodeRHS == BO_NE && Comparison == 0) ||
242 (OpcodeRHS == BO_EQ && Comparison != 0) ||
243 (OpcodeRHS == BO_LT && Comparison >= 0) ||
244 (OpcodeRHS == BO_LE && Comparison > 0) ||
245 (OpcodeRHS == BO_GT && Comparison <= 0) ||
246 (OpcodeRHS == BO_GE && Comparison < 0);
249 return ((OpcodeRHS == BO_LT && Comparison >= 0) ||
250 (OpcodeRHS == BO_LE && Comparison > 0) ||
251 (OpcodeRHS == BO_EQ && Comparison > 0));
253 return ((OpcodeRHS == BO_GT && Comparison <= 0) ||
254 (OpcodeRHS == BO_GE && Comparison < 0) ||
255 (OpcodeRHS == BO_EQ && Comparison < 0));
257 return (OpcodeRHS == BO_LT || OpcodeRHS == BO_LE || OpcodeRHS == BO_EQ) &&
260 return (OpcodeRHS == BO_GT || OpcodeRHS == BO_GE || OpcodeRHS == BO_EQ) &&
268 if (Opcode == BO_Sub) {
275 if (Node.isInstantiationDependent())
277 return Node.isIntegerConstantExpr(
Finder->getASTContext());
281 static ast_matchers::internal::Matcher<Expr>
283 std::string CstId = (Id +
"-const").str();
284 return expr(isIntegerConstantExpr()).bind(CstId);
290 StringRef Id, APSInt &Value) {
291 std::string CstId = (Id +
"-const").str();
292 const auto *CstExpr = Result.Nodes.getNodeAs<Expr>(CstId);
293 return CstExpr && CstExpr->isIntegerConstantExpr(Value, *Result.Context);
299 std::string SymId = (Id +
"-sym").str();
300 return ignoringParenImpCasts(
301 expr(unless(isIntegerConstantExpr())).bind(SymId));
307 StringRef Id,
const Expr *&SymExpr) {
308 std::string SymId = (Id +
"-sym").str();
309 if (
const auto *Node = Result.Nodes.getNodeAs<Expr>(SymId)) {
318 static ast_matchers::internal::Matcher<Expr>
320 const auto BinOpCstExpr =
322 anyOf(binaryOperator(anyOf(hasOperatorName(
"+"), hasOperatorName(
"|"),
323 hasOperatorName(
"&")),
326 binaryOperator(hasOperatorName(
"-"),
330 return ignoringParenImpCasts(BinOpCstExpr);
337 StringRef Id, BinaryOperatorKind &Opcode,
338 const Expr *&Symbol, APSInt &Value) {
339 if (
const auto *BinExpr = Result.Nodes.getNodeAs<BinaryOperator>(Id)) {
340 Opcode = BinExpr->getOpcode();
348 static ast_matchers::internal::Matcher<Expr>
350 std::string CastId = (Id +
"-cast").str();
351 std::string SwapId = (Id +
"-swap").str();
352 std::string NegateId = (Id +
"-negate").str();
354 const auto RelationalExpr = ignoringParenImpCasts(binaryOperator(
355 isComparisonOperator(), expr().bind(Id),
363 const auto CastExpr =
364 implicitCastExpr(hasCastKind(CK_IntegralToBoolean),
368 const auto NegateRelationalExpr =
369 unaryOperator(hasOperatorName(
"!"),
370 hasUnaryOperand(anyOf(CastExpr, RelationalExpr)))
373 const auto NegateNegateRelationalExpr =
374 unaryOperator(hasOperatorName(
"!"),
375 hasUnaryOperand(unaryOperator(
376 hasOperatorName(
"!"),
377 hasUnaryOperand(anyOf(CastExpr, RelationalExpr)))));
379 return anyOf(RelationalExpr, CastExpr, NegateRelationalExpr,
380 NegateNegateRelationalExpr);
387 StringRef Id,
const Expr *&OperandExpr,
388 BinaryOperatorKind &Opcode,
389 const Expr *&Symbol, APSInt &Value) {
390 std::string CastId = (Id +
"-cast").str();
391 std::string SwapId = (Id +
"-swap").str();
392 std::string NegateId = (Id +
"-negate").str();
394 if (
const auto *Bin = Result.Nodes.getNodeAs<BinaryOperator>(Id)) {
396 Opcode = Bin->getOpcode();
400 }
else if (
const auto *Cast = Result.Nodes.getNodeAs<CastExpr>(CastId)) {
404 Value = APSInt(32, 0);
412 if (Result.Nodes.getNodeAs<Expr>(SwapId))
413 Opcode = BinaryOperator::reverseComparisonOp(Opcode);
414 if (Result.Nodes.getNodeAs<Expr>(NegateId))
415 Opcode = BinaryOperator::negateComparisonOp(Opcode);
429 return Node.getNumArgs() == 2 &&
434 return Node.getOperatorLoc().isMacroID();
438 return Node.getQuestionLoc().isMacroID() || Node.getColonLoc().isMacroID();
441 AST_MATCHER(Expr, isMacro) {
return Node.getExprLoc().isMacroID(); }
444 const SourceManager &
SM =
Finder->getASTContext().getSourceManager();
445 const LangOptions &LO =
Finder->getASTContext().getLangOpts();
446 SourceLocation
Loc = Node.getExprLoc();
447 while (Loc.isMacroID()) {
448 std::string MacroName = Lexer::getImmediateMacroName(Loc, SM, LO);
449 if (Names.find(MacroName) != Names.end())
451 Loc = SM.getImmediateMacroCallerLoc(Loc);
456 void RedundantExpressionCheck::registerMatchers(MatchFinder *
Finder) {
457 const auto AnyLiteralExpr = ignoringParenImpCasts(
458 anyOf(cxxBoolLiteral(), characterLiteral(), integerLiteral()));
460 std::vector<std::string> MacroNames =
462 std::set<std::string> Names(MacroNames.begin(), MacroNames.end());
464 const auto BannedIntegerLiteral = integerLiteral(expandedByMacro(Names));
467 binaryOperator(anyOf(hasOperatorName(
"-"), hasOperatorName(
"/"),
468 hasOperatorName(
"%"), hasOperatorName(
"|"),
469 hasOperatorName(
"&"), hasOperatorName(
"^"),
470 matchers::isComparisonOperator(),
471 hasOperatorName(
"&&"), hasOperatorName(
"||"),
472 hasOperatorName(
"=")),
473 operandsAreEquivalent(),
475 unless(isInTemplateInstantiation()),
476 unless(binaryOperatorIsInMacro()),
477 unless(hasType(realFloatingPointType())),
478 unless(hasEitherOperand(hasType(realFloatingPointType()))),
479 unless(hasLHS(AnyLiteralExpr)),
480 unless(hasDescendant(BannedIntegerLiteral)))
485 conditionalOperator(expressionsAreEquivalent(),
487 unless(conditionalOperatorIsInMacro()),
488 unless(hasTrueExpression(AnyLiteralExpr)),
489 unless(isInTemplateInstantiation()))
496 hasOverloadedOperatorName(
"-"), hasOverloadedOperatorName(
"/"),
497 hasOverloadedOperatorName(
"%"), hasOverloadedOperatorName(
"|"),
498 hasOverloadedOperatorName(
"&"), hasOverloadedOperatorName(
"^"),
499 hasOverloadedOperatorName(
"=="), hasOverloadedOperatorName(
"!="),
500 hasOverloadedOperatorName(
"<"), hasOverloadedOperatorName(
"<="),
501 hasOverloadedOperatorName(
">"), hasOverloadedOperatorName(
">="),
502 hasOverloadedOperatorName(
"&&"), hasOverloadedOperatorName(
"||"),
503 hasOverloadedOperatorName(
"=")),
504 parametersAreEquivalent(),
506 unless(isMacro()), unless(isInTemplateInstantiation()))
522 Finder->addMatcher(binaryOperator(isComparisonOperator(),
523 hasEitherOperand(BinOpCstLeft),
524 hasEitherOperand(CstRight))
525 .bind(
"binop-const-compare-to-const"),
530 binaryOperator(isComparisonOperator(),
531 anyOf(allOf(hasLHS(BinOpCstLeft), hasRHS(SymRight)),
532 allOf(hasLHS(SymRight), hasRHS(BinOpCstLeft))))
533 .bind(
"binop-const-compare-to-sym"),
537 Finder->addMatcher(binaryOperator(isComparisonOperator(),
538 hasLHS(BinOpCstLeft), hasRHS(BinOpCstRight),
540 unless(operandsAreEquivalent()))
541 .bind(
"binop-const-compare-to-binop-const"),
552 binaryOperator(anyOf(hasOperatorName(
"||"), hasOperatorName(
"&&")),
553 hasLHS(ComparisonLeft), hasRHS(ComparisonRight),
555 unless(operandsAreEquivalent()))
556 .bind(
"comparisons-of-symbol-and-const"),
560 void RedundantExpressionCheck::checkArithmeticExpr(
561 const MatchFinder::MatchResult &
Result) {
562 APSInt LhsValue, RhsValue;
563 const Expr *LhsSymbol =
nullptr, *RhsSymbol =
nullptr;
564 BinaryOperatorKind LhsOpcode, RhsOpcode;
566 if (
const auto *ComparisonOperator = Result.Nodes.getNodeAs<BinaryOperator>(
567 "binop-const-compare-to-sym")) {
568 BinaryOperatorKind Opcode = ComparisonOperator->getOpcode();
576 if (LhsOpcode == BO_Add || LhsOpcode == BO_Sub) {
577 if ((LhsValue != 0 && Opcode == BO_EQ) ||
578 (LhsValue == 0 && Opcode == BO_NE))
579 diag(ComparisonOperator->getOperatorLoc(),
580 "logical expression is always false");
581 else if ((LhsValue == 0 && Opcode == BO_EQ) ||
582 (LhsValue != 0 && Opcode == BO_NE))
583 diag(ComparisonOperator->getOperatorLoc(),
584 "logical expression is always true");
586 }
else if (
const auto *ComparisonOperator =
587 Result.Nodes.getNodeAs<BinaryOperator>(
588 "binop-const-compare-to-binop-const")) {
589 BinaryOperatorKind Opcode = ComparisonOperator->getOpcode();
602 if (LhsOpcode == BO_Add && RhsOpcode == BO_Add) {
603 if ((Opcode == BO_EQ && APSInt::compareValues(LhsValue, RhsValue) == 0) ||
604 (Opcode == BO_NE && APSInt::compareValues(LhsValue, RhsValue) != 0)) {
605 diag(ComparisonOperator->getOperatorLoc(),
606 "logical expression is always true");
607 }
else if ((Opcode == BO_EQ &&
608 APSInt::compareValues(LhsValue, RhsValue) != 0) ||
610 APSInt::compareValues(LhsValue, RhsValue) == 0)) {
611 diag(ComparisonOperator->getOperatorLoc(),
612 "logical expression is always false");
618 void RedundantExpressionCheck::checkBitwiseExpr(
619 const MatchFinder::MatchResult &Result) {
620 if (
const auto *ComparisonOperator = Result.Nodes.getNodeAs<BinaryOperator>(
621 "binop-const-compare-to-const")) {
622 BinaryOperatorKind Opcode = ComparisonOperator->getOpcode();
624 APSInt LhsValue, RhsValue;
625 const Expr *LhsSymbol =
nullptr;
626 BinaryOperatorKind LhsOpcode;
632 uint64_t LhsConstant = LhsValue.getZExtValue();
633 uint64_t RhsConstant = RhsValue.getZExtValue();
634 SourceLocation
Loc = ComparisonOperator->getOperatorLoc();
637 if (LhsOpcode == BO_And && (LhsConstant & RhsConstant) != RhsConstant) {
639 diag(Loc,
"logical expression is always false");
640 else if (Opcode == BO_NE)
641 diag(Loc,
"logical expression is always true");
645 if (LhsOpcode == BO_Or && (LhsConstant | RhsConstant) != RhsConstant) {
647 diag(Loc,
"logical expression is always false");
648 else if (Opcode == BO_NE)
649 diag(Loc,
"logical expression is always true");
654 void RedundantExpressionCheck::checkRelationalExpr(
655 const MatchFinder::MatchResult &Result) {
656 if (
const auto *ComparisonOperator = Result.Nodes.getNodeAs<BinaryOperator>(
657 "comparisons-of-symbol-and-const")) {
659 BinaryOperatorKind Opcode = ComparisonOperator->getOpcode();
661 const Expr *LhsExpr =
nullptr, *RhsExpr =
nullptr;
662 APSInt LhsValue, RhsValue;
663 const Expr *LhsSymbol =
nullptr, *RhsSymbol =
nullptr;
664 BinaryOperatorKind LhsOpcode, RhsOpcode;
666 Result,
"lhs", LhsExpr, LhsOpcode, LhsSymbol, LhsValue) ||
668 Result,
"rhs", RhsExpr, RhsOpcode, RhsSymbol, RhsValue) ||
673 if (APSInt::compareValues(LhsValue, RhsValue) > 0) {
674 std::swap(LhsExpr, RhsExpr);
675 std::swap(LhsValue, RhsValue);
676 std::swap(LhsSymbol, RhsSymbol);
677 std::swap(LhsOpcode, RhsOpcode);
680 if ((Opcode == BO_LAnd || Opcode == BO_LOr) &&
682 diag(ComparisonOperator->getOperatorLoc(),
683 "equivalent expression on both side of logical operator");
687 if (Opcode == BO_LAnd) {
689 diag(ComparisonOperator->getOperatorLoc(),
690 "logical expression is always false");
692 diag(LhsExpr->getExprLoc(),
"expression is redundant");
694 diag(RhsExpr->getExprLoc(),
"expression is redundant");
698 if (Opcode == BO_LOr) {
700 diag(ComparisonOperator->getOperatorLoc(),
701 "logical expression is always true");
703 diag(RhsExpr->getExprLoc(),
"expression is redundant");
705 diag(LhsExpr->getExprLoc(),
"expression is redundant");
711 void RedundantExpressionCheck::check(
const MatchFinder::MatchResult &Result) {
712 if (
const auto *BinOp = Result.Nodes.getNodeAs<BinaryOperator>(
"binary"))
713 diag(BinOp->getOperatorLoc(),
"both side of operator are equivalent");
714 if (
const auto *CondOp = Result.Nodes.getNodeAs<ConditionalOperator>(
"cond"))
715 diag(CondOp->getColonLoc(),
"'true' and 'false' expression are equivalent");
716 if (
const auto *Call = Result.Nodes.getNodeAs<CXXOperatorCallExpr>(
"call"))
717 diag(Call->getOperatorLoc(),
718 "both side of overloaded operator are equivalent");
720 checkArithmeticExpr(Result);
721 checkBitwiseExpr(Result);
722 checkRelationalExpr(Result);
SourceLocation Loc
'#' location in the include directive
static ast_matchers::internal::Matcher< Expr > matchRelationalIntegerConstantExpr(StringRef Id)
static bool rangesFullyCoverDomain(BinaryOperatorKind OpcodeLHS, const APSInt &ValueLHS, BinaryOperatorKind OpcodeRHS, const APSInt &ValueRHS)
std::unique_ptr< ast_matchers::MatchFinder > Finder
static const char KnownBannedMacroNames[]
static bool retrieveIntegerConstantExpr(const MatchFinder::MatchResult &Result, StringRef Id, APSInt &Value)
static bool rangeSubsumesRange(BinaryOperatorKind OpcodeLHS, const APSInt &ValueLHS, BinaryOperatorKind OpcodeRHS, const APSInt &ValueRHS)
static bool areEquivalentNameSpecifier(const NestedNameSpecifier *Left, const NestedNameSpecifier *Right)
std::vector< std::string > parseStringList(StringRef Option)
Parse a semicolon separated list of strings.
static bool retrieveRelationalIntegerConstantExpr(const MatchFinder::MatchResult &Result, StringRef Id, const Expr *&OperandExpr, BinaryOperatorKind &Opcode, const Expr *&Symbol, APSInt &Value)
static bool incrementWithoutOverflow(const APSInt &Value, APSInt &Result)
AST_MATCHER(Expr, isMacro)
static bool areEquivalentRanges(BinaryOperatorKind OpcodeLHS, const APSInt &ValueLHS, BinaryOperatorKind OpcodeRHS, const APSInt &ValueRHS)
static ast_matchers::internal::Matcher< Expr > matchSymbolicExpr(StringRef Id)
static bool retrieveSymbolicExpr(const MatchFinder::MatchResult &Result, StringRef Id, const Expr *&SymExpr)
static bool areExclusiveRanges(BinaryOperatorKind OpcodeLHS, const APSInt &ValueLHS, BinaryOperatorKind OpcodeRHS, const APSInt &ValueRHS)
static bool areEquivalentExpr(const Expr *Left, const Expr *Right)
static bool retrieveBinOpIntegerConstantExpr(const MatchFinder::MatchResult &Result, StringRef Id, BinaryOperatorKind &Opcode, const Expr *&Symbol, APSInt &Value)
static ast_matchers::internal::Matcher< Expr > matchBinOpIntegerConstantExpr(StringRef Id)
static ast_matchers::internal::Matcher< Expr > matchIntegerConstantExpr(StringRef Id)
static void canonicalNegateExpr(BinaryOperatorKind &Opcode, APSInt &Value)
AST_MATCHER_P(Expr, expandedByMacro, std::set< std::string >, Names)