clang-tools 22.0.0git
RedundantExpressionCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "../utils/Matchers.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/Basic/LLVM.h"
14#include "clang/Basic/SourceLocation.h"
15#include "clang/Basic/SourceManager.h"
16#include "clang/Lex/Lexer.h"
17#include "llvm/ADT/APInt.h"
18#include "llvm/ADT/APSInt.h"
19#include "llvm/ADT/FoldingSet.h"
20#include "llvm/ADT/SmallBitVector.h"
21#include "llvm/Support/FormatVariadic.h"
22#include <algorithm>
23#include <cassert>
24#include <cstdint>
25#include <optional>
26#include <string>
27
28using namespace clang::ast_matchers;
29using namespace clang::tidy::matchers;
30
31namespace clang::tidy::misc {
32namespace {
33using llvm::APSInt;
34
35static constexpr llvm::StringLiteral KnownBannedMacroNames[] = {
36 "EAGAIN",
37 "EWOULDBLOCK",
38 "SIGCLD",
39 "SIGCHLD",
40};
41
42static bool incrementWithoutOverflow(const APSInt &Value, APSInt &Result) {
43 Result = Value;
44 ++Result;
45 return Value < Result;
46}
47
48static bool areEquivalentExpr(const Expr *Left, const Expr *Right) {
49 if (!Left || !Right)
50 return !Left && !Right;
51
52 Left = Left->IgnoreParens();
53 Right = Right->IgnoreParens();
54
55 // Compare classes.
56 if (Left->getStmtClass() != Right->getStmtClass())
57 return false;
58
59 // Compare children.
60 Expr::const_child_iterator LeftIter = Left->child_begin();
61 Expr::const_child_iterator RightIter = Right->child_begin();
62 while (LeftIter != Left->child_end() && RightIter != Right->child_end()) {
63 if (!areEquivalentExpr(dyn_cast_or_null<Expr>(*LeftIter),
64 dyn_cast_or_null<Expr>(*RightIter)))
65 return false;
66 ++LeftIter;
67 ++RightIter;
68 }
69 if (LeftIter != Left->child_end() || RightIter != Right->child_end())
70 return false;
71
72 // Perform extra checks.
73 switch (Left->getStmtClass()) {
74 default:
75 return false;
76
77 case Stmt::CharacterLiteralClass:
78 return cast<CharacterLiteral>(Left)->getValue() ==
79 cast<CharacterLiteral>(Right)->getValue();
80 case Stmt::IntegerLiteralClass: {
81 llvm::APInt LeftLit = cast<IntegerLiteral>(Left)->getValue();
82 llvm::APInt RightLit = cast<IntegerLiteral>(Right)->getValue();
83 return LeftLit.getBitWidth() == RightLit.getBitWidth() &&
84 LeftLit == RightLit;
85 }
86 case Stmt::FloatingLiteralClass:
87 return cast<FloatingLiteral>(Left)->getValue().bitwiseIsEqual(
88 cast<FloatingLiteral>(Right)->getValue());
89 case Stmt::StringLiteralClass:
90 return cast<StringLiteral>(Left)->getBytes() ==
91 cast<StringLiteral>(Right)->getBytes();
92 case Stmt::CXXOperatorCallExprClass:
93 return cast<CXXOperatorCallExpr>(Left)->getOperator() ==
94 cast<CXXOperatorCallExpr>(Right)->getOperator();
95 case Stmt::DependentScopeDeclRefExprClass:
96 if (cast<DependentScopeDeclRefExpr>(Left)->getDeclName() !=
97 cast<DependentScopeDeclRefExpr>(Right)->getDeclName())
98 return false;
99 return cast<DependentScopeDeclRefExpr>(Left)->getQualifier() ==
100 cast<DependentScopeDeclRefExpr>(Right)->getQualifier();
101 case Stmt::DeclRefExprClass:
102 return cast<DeclRefExpr>(Left)->getDecl() ==
103 cast<DeclRefExpr>(Right)->getDecl();
104 case Stmt::MemberExprClass:
105 return cast<MemberExpr>(Left)->getMemberDecl() ==
106 cast<MemberExpr>(Right)->getMemberDecl();
107 case Stmt::CXXFoldExprClass:
108 return cast<CXXFoldExpr>(Left)->getOperator() ==
109 cast<CXXFoldExpr>(Right)->getOperator();
110 case Stmt::CXXFunctionalCastExprClass:
111 case Stmt::CStyleCastExprClass:
112 return cast<ExplicitCastExpr>(Left)->getTypeAsWritten() ==
113 cast<ExplicitCastExpr>(Right)->getTypeAsWritten();
114 case Stmt::CallExprClass:
115 case Stmt::ImplicitCastExprClass:
116 case Stmt::ArraySubscriptExprClass:
117 return true;
118 case Stmt::UnaryOperatorClass:
119 if (cast<UnaryOperator>(Left)->isIncrementDecrementOp())
120 return false;
121 return cast<UnaryOperator>(Left)->getOpcode() ==
122 cast<UnaryOperator>(Right)->getOpcode();
123 case Stmt::BinaryOperatorClass:
124 if (cast<BinaryOperator>(Left)->isAssignmentOp())
125 return false;
126 return cast<BinaryOperator>(Left)->getOpcode() ==
127 cast<BinaryOperator>(Right)->getOpcode();
128 case Stmt::UnaryExprOrTypeTraitExprClass:
129 const auto *LeftUnaryExpr = cast<UnaryExprOrTypeTraitExpr>(Left);
130 const auto *RightUnaryExpr = cast<UnaryExprOrTypeTraitExpr>(Right);
131 if (LeftUnaryExpr->isArgumentType() && RightUnaryExpr->isArgumentType())
132 return LeftUnaryExpr->getKind() == RightUnaryExpr->getKind() &&
133 LeftUnaryExpr->getArgumentType() ==
134 RightUnaryExpr->getArgumentType();
135 if (!LeftUnaryExpr->isArgumentType() && !RightUnaryExpr->isArgumentType())
136 return areEquivalentExpr(LeftUnaryExpr->getArgumentExpr(),
137 RightUnaryExpr->getArgumentExpr());
138
139 return false;
140 }
141}
142
143// For a given expression 'x', returns whether the ranges covered by the
144// relational operators are equivalent (i.e. x <= 4 is equivalent to x < 5).
145static bool areEquivalentRanges(BinaryOperatorKind OpcodeLHS,
146 const APSInt &ValueLHS,
147 BinaryOperatorKind OpcodeRHS,
148 const APSInt &ValueRHS) {
149 assert(APSInt::compareValues(ValueLHS, ValueRHS) <= 0 &&
150 "Values must be ordered");
151 // Handle the case where constants are the same: x <= 4 <==> x <= 4.
152 if (APSInt::compareValues(ValueLHS, ValueRHS) == 0)
153 return OpcodeLHS == OpcodeRHS;
154
155 // Handle the case where constants are off by one: x <= 4 <==> x < 5.
156 APSInt ValueLhsPlus1;
157 return ((OpcodeLHS == BO_LE && OpcodeRHS == BO_LT) ||
158 (OpcodeLHS == BO_GT && OpcodeRHS == BO_GE)) &&
159 incrementWithoutOverflow(ValueLHS, ValueLhsPlus1) &&
160 APSInt::compareValues(ValueLhsPlus1, ValueRHS) == 0;
161}
162
163// For a given expression 'x', returns whether the ranges covered by the
164// relational operators are fully disjoint (i.e. x < 4 and x > 7).
165static bool areExclusiveRanges(BinaryOperatorKind OpcodeLHS,
166 const APSInt &ValueLHS,
167 BinaryOperatorKind OpcodeRHS,
168 const APSInt &ValueRHS) {
169 assert(APSInt::compareValues(ValueLHS, ValueRHS) <= 0 &&
170 "Values must be ordered");
171
172 // Handle cases where the constants are the same.
173 if (APSInt::compareValues(ValueLHS, ValueRHS) == 0) {
174 switch (OpcodeLHS) {
175 case BO_EQ:
176 return OpcodeRHS == BO_NE || OpcodeRHS == BO_GT || OpcodeRHS == BO_LT;
177 case BO_NE:
178 return OpcodeRHS == BO_EQ;
179 case BO_LE:
180 return OpcodeRHS == BO_GT;
181 case BO_GE:
182 return OpcodeRHS == BO_LT;
183 case BO_LT:
184 return OpcodeRHS == BO_EQ || OpcodeRHS == BO_GT || OpcodeRHS == BO_GE;
185 case BO_GT:
186 return OpcodeRHS == BO_EQ || OpcodeRHS == BO_LT || OpcodeRHS == BO_LE;
187 default:
188 return false;
189 }
190 }
191
192 // Handle cases where the constants are different.
193 if ((OpcodeLHS == BO_EQ || OpcodeLHS == BO_LT || OpcodeLHS == BO_LE) &&
194 (OpcodeRHS == BO_EQ || OpcodeRHS == BO_GT || OpcodeRHS == BO_GE))
195 return true;
196
197 // Handle the case where constants are off by one: x > 5 && x < 6.
198 APSInt ValueLhsPlus1;
199 if (OpcodeLHS == BO_GT && OpcodeRHS == BO_LT &&
200 incrementWithoutOverflow(ValueLHS, ValueLhsPlus1) &&
201 APSInt::compareValues(ValueLhsPlus1, ValueRHS) == 0)
202 return true;
203
204 return false;
205}
206
207// Returns whether the ranges covered by the union of both relational
208// expressions cover the whole domain (i.e. x < 10 and x > 0).
209static bool rangesFullyCoverDomain(BinaryOperatorKind OpcodeLHS,
210 const APSInt &ValueLHS,
211 BinaryOperatorKind OpcodeRHS,
212 const APSInt &ValueRHS) {
213 assert(APSInt::compareValues(ValueLHS, ValueRHS) <= 0 &&
214 "Values must be ordered");
215
216 // Handle cases where the constants are the same: x < 5 || x >= 5.
217 if (APSInt::compareValues(ValueLHS, ValueRHS) == 0) {
218 switch (OpcodeLHS) {
219 case BO_EQ:
220 return OpcodeRHS == BO_NE;
221 case BO_NE:
222 return OpcodeRHS == BO_EQ;
223 case BO_LE:
224 return OpcodeRHS == BO_GT || OpcodeRHS == BO_GE;
225 case BO_LT:
226 return OpcodeRHS == BO_GE;
227 case BO_GE:
228 return OpcodeRHS == BO_LT || OpcodeRHS == BO_LE;
229 case BO_GT:
230 return OpcodeRHS == BO_LE;
231 default:
232 return false;
233 }
234 }
235
236 // Handle the case where constants are off by one: x <= 4 || x >= 5.
237 APSInt ValueLhsPlus1;
238 if (OpcodeLHS == BO_LE && OpcodeRHS == BO_GE &&
239 incrementWithoutOverflow(ValueLHS, ValueLhsPlus1) &&
240 APSInt::compareValues(ValueLhsPlus1, ValueRHS) == 0)
241 return true;
242
243 // Handle cases where the constants are different: x > 4 || x <= 7.
244 if ((OpcodeLHS == BO_GT || OpcodeLHS == BO_GE) &&
245 (OpcodeRHS == BO_LT || OpcodeRHS == BO_LE))
246 return true;
247
248 // Handle cases where constants are different but both ops are !=, like:
249 // x != 5 || x != 10
250 if (OpcodeLHS == BO_NE && OpcodeRHS == BO_NE)
251 return true;
252
253 return false;
254}
255
256static bool rangeSubsumesRange(BinaryOperatorKind OpcodeLHS,
257 const APSInt &ValueLHS,
258 BinaryOperatorKind OpcodeRHS,
259 const APSInt &ValueRHS) {
260 int Comparison = APSInt::compareValues(ValueLHS, ValueRHS);
261 switch (OpcodeLHS) {
262 case BO_EQ:
263 return OpcodeRHS == BO_EQ && Comparison == 0;
264 case BO_NE:
265 return (OpcodeRHS == BO_NE && Comparison == 0) ||
266 (OpcodeRHS == BO_EQ && Comparison != 0) ||
267 (OpcodeRHS == BO_LT && Comparison >= 0) ||
268 (OpcodeRHS == BO_LE && Comparison > 0) ||
269 (OpcodeRHS == BO_GT && Comparison <= 0) ||
270 (OpcodeRHS == BO_GE && Comparison < 0);
271
272 case BO_LT:
273 return ((OpcodeRHS == BO_LT && Comparison >= 0) ||
274 (OpcodeRHS == BO_LE && Comparison > 0) ||
275 (OpcodeRHS == BO_EQ && Comparison > 0));
276 case BO_GT:
277 return ((OpcodeRHS == BO_GT && Comparison <= 0) ||
278 (OpcodeRHS == BO_GE && Comparison < 0) ||
279 (OpcodeRHS == BO_EQ && Comparison < 0));
280 case BO_LE:
281 return (OpcodeRHS == BO_LT || OpcodeRHS == BO_LE || OpcodeRHS == BO_EQ) &&
282 Comparison >= 0;
283 case BO_GE:
284 return (OpcodeRHS == BO_GT || OpcodeRHS == BO_GE || OpcodeRHS == BO_EQ) &&
285 Comparison <= 0;
286 default:
287 return false;
288 }
289}
290
291static void transformSubToCanonicalAddExpr(BinaryOperatorKind &Opcode,
292 APSInt &Value) {
293 if (Opcode == BO_Sub) {
294 Opcode = BO_Add;
295 Value = -Value;
296 }
297}
298
299// to use in the template below
300static OverloadedOperatorKind getOp(const BinaryOperator *Op) {
301 return BinaryOperator::getOverloadedOperator(Op->getOpcode());
302}
303
304static OverloadedOperatorKind getOp(const CXXOperatorCallExpr *Op) {
305 if (Op->getNumArgs() != 2)
306 return OO_None;
307 return Op->getOperator();
308}
309
310static std::pair<const Expr *, const Expr *>
311getOperands(const BinaryOperator *Op) {
312 return {Op->getLHS()->IgnoreParenImpCasts(),
313 Op->getRHS()->IgnoreParenImpCasts()};
314}
315
316static std::pair<const Expr *, const Expr *>
317getOperands(const CXXOperatorCallExpr *Op) {
318 return {Op->getArg(0)->IgnoreParenImpCasts(),
319 Op->getArg(1)->IgnoreParenImpCasts()};
320}
321
322template <typename TExpr>
323static const TExpr *checkOpKind(const Expr *TheExpr,
324 OverloadedOperatorKind OpKind) {
325 const auto *AsTExpr = dyn_cast_or_null<TExpr>(TheExpr);
326 if (AsTExpr && getOp(AsTExpr) == OpKind)
327 return AsTExpr;
328
329 return nullptr;
330}
331
332// returns true if a subexpression has two directly equivalent operands and
333// is already handled by operands/parametersAreEquivalent
334template <typename TExpr, unsigned N>
335static bool collectOperands(const Expr *Part,
336 SmallVector<const Expr *, N> &AllOperands,
337 OverloadedOperatorKind OpKind) {
338 if (const auto *BinOp = checkOpKind<TExpr>(Part, OpKind)) {
339 const std::pair<const Expr *, const Expr *> Operands = getOperands(BinOp);
340 if (areEquivalentExpr(Operands.first, Operands.second))
341 return true;
342 return collectOperands<TExpr>(Operands.first, AllOperands, OpKind) ||
343 collectOperands<TExpr>(Operands.second, AllOperands, OpKind);
344 }
345
346 AllOperands.push_back(Part);
347 return false;
348}
349
350template <typename TExpr>
351static bool hasSameOperatorParent(const Expr *TheExpr,
352 OverloadedOperatorKind OpKind,
353 ASTContext &Context) {
354 // IgnoreParenImpCasts logic in reverse: skip surrounding uninteresting nodes
355 const DynTypedNodeList Parents = Context.getParents(*TheExpr);
356 for (DynTypedNode DynParent : Parents) {
357 if (const auto *Parent = DynParent.get<Expr>()) {
358 bool Skip = isa<ParenExpr>(Parent) || isa<ImplicitCastExpr>(Parent) ||
359 isa<FullExpr>(Parent) ||
360 isa<MaterializeTemporaryExpr>(Parent);
361 if (Skip && hasSameOperatorParent<TExpr>(Parent, OpKind, Context))
362 return true;
363 if (checkOpKind<TExpr>(Parent, OpKind))
364 return true;
365 }
366 }
367
368 return false;
369}
370
371template <typename TExpr>
372static bool
373markDuplicateOperands(const TExpr *TheExpr,
374 ast_matchers::internal::BoundNodesTreeBuilder *Builder,
375 ASTContext &Context) {
376 const OverloadedOperatorKind OpKind = getOp(TheExpr);
377 if (OpKind == OO_None)
378 return false;
379 // if there are no nested operators of the same kind, it's handled by
380 // operands/parametersAreEquivalent
381 const std::pair<const Expr *, const Expr *> Operands = getOperands(TheExpr);
382 if (!(checkOpKind<TExpr>(Operands.first, OpKind) ||
383 checkOpKind<TExpr>(Operands.second, OpKind)))
384 return false;
385
386 // if parent is the same kind of operator, it's handled by a previous call to
387 // markDuplicateOperands
388 if (hasSameOperatorParent<TExpr>(TheExpr, OpKind, Context))
389 return false;
390
391 SmallVector<const Expr *, 4> AllOperands;
392 if (collectOperands<TExpr>(Operands.first, AllOperands, OpKind))
393 return false;
394 if (collectOperands<TExpr>(Operands.second, AllOperands, OpKind))
395 return false;
396 size_t NumOperands = AllOperands.size();
397 llvm::SmallBitVector Duplicates(NumOperands);
398 for (size_t I = 0; I < NumOperands; I++) {
399 if (Duplicates[I])
400 continue;
401 bool FoundDuplicates = false;
402
403 for (size_t J = I + 1; J < NumOperands; J++) {
404 if (AllOperands[J]->HasSideEffects(Context))
405 break;
406
407 if (areEquivalentExpr(AllOperands[I], AllOperands[J])) {
408 FoundDuplicates = true;
409 Duplicates.set(J);
410 Builder->setBinding(SmallString<11>(llvm::formatv("duplicate{0}", J)),
411 DynTypedNode::create(*AllOperands[J]));
412 }
413 }
414
415 if (FoundDuplicates)
416 Builder->setBinding(SmallString<11>(llvm::formatv("duplicate{0}", I)),
417 DynTypedNode::create(*AllOperands[I]));
418 }
419
420 return Duplicates.any();
421}
422
423AST_MATCHER(Expr, isIntegerConstantExpr) {
424 if (Node.isInstantiationDependent())
425 return false;
426 return Node.isIntegerConstantExpr(Finder->getASTContext());
427}
428
429AST_MATCHER(BinaryOperator, operandsAreEquivalent) {
430 return areEquivalentExpr(Node.getLHS(), Node.getRHS());
431}
432
433AST_MATCHER(BinaryOperator, nestedOperandsAreEquivalent) {
434 return markDuplicateOperands(&Node, Builder, Finder->getASTContext());
435}
436
437AST_MATCHER(ConditionalOperator, expressionsAreEquivalent) {
438 return areEquivalentExpr(Node.getTrueExpr(), Node.getFalseExpr());
439}
440
441AST_MATCHER(CallExpr, parametersAreEquivalent) {
442 return Node.getNumArgs() == 2 &&
443 areEquivalentExpr(Node.getArg(0), Node.getArg(1));
444}
445
446AST_MATCHER(CXXOperatorCallExpr, nestedParametersAreEquivalent) {
447 return markDuplicateOperands(&Node, Builder, Finder->getASTContext());
448}
449
450AST_MATCHER(BinaryOperator, binaryOperatorIsInMacro) {
451 return Node.getOperatorLoc().isMacroID();
452}
453
454AST_MATCHER(ConditionalOperator, conditionalOperatorIsInMacro) {
455 return Node.getQuestionLoc().isMacroID() || Node.getColonLoc().isMacroID();
456}
457
458AST_MATCHER(Expr, isMacro) { return Node.getExprLoc().isMacroID(); }
459
460AST_MATCHER_P(Expr, expandedByMacro, ArrayRef<llvm::StringLiteral>, Names) {
461 const SourceManager &SM = Finder->getASTContext().getSourceManager();
462 const LangOptions &LO = Finder->getASTContext().getLangOpts();
463 SourceLocation Loc = Node.getExprLoc();
464 while (Loc.isMacroID()) {
465 StringRef MacroName = Lexer::getImmediateMacroName(Loc, SM, LO);
466 if (llvm::is_contained(Names, MacroName))
467 return true;
468 Loc = SM.getImmediateMacroCallerLoc(Loc);
469 }
470 return false;
471}
472
473// Returns a matcher for integer constant expressions.
474static ast_matchers::internal::Matcher<Expr>
475matchIntegerConstantExpr(StringRef Id) {
476 std::string CstId = (Id + "-const").str();
477 return expr(isIntegerConstantExpr()).bind(CstId);
478}
479
480// Retrieves the integer expression matched by 'matchIntegerConstantExpr' with
481// name 'Id' and stores it into 'ConstExpr', the value of the expression is
482// stored into `Value`.
483static bool retrieveIntegerConstantExpr(const MatchFinder::MatchResult &Result,
484 StringRef Id, APSInt &Value,
485 const Expr *&ConstExpr) {
486 std::string CstId = (Id + "-const").str();
487 ConstExpr = Result.Nodes.getNodeAs<Expr>(CstId);
488 if (!ConstExpr)
489 return false;
490 std::optional<llvm::APSInt> R =
491 ConstExpr->getIntegerConstantExpr(*Result.Context);
492 if (!R)
493 return false;
494 Value = *R;
495 return true;
496}
497
498// Overloaded `retrieveIntegerConstantExpr` for compatibility.
499static bool retrieveIntegerConstantExpr(const MatchFinder::MatchResult &Result,
500 StringRef Id, APSInt &Value) {
501 const Expr *ConstExpr = nullptr;
502 return retrieveIntegerConstantExpr(Result, Id, Value, ConstExpr);
503}
504
505// Returns a matcher for symbolic expressions (matches every expression except
506// ingeter constant expressions).
507static ast_matchers::internal::Matcher<Expr> matchSymbolicExpr(StringRef Id) {
508 std::string SymId = (Id + "-sym").str();
509 return ignoringParenImpCasts(
510 expr(unless(isIntegerConstantExpr())).bind(SymId));
511}
512
513// Retrieves the expression matched by 'matchSymbolicExpr' with name 'Id' and
514// stores it into 'SymExpr'.
515static bool retrieveSymbolicExpr(const MatchFinder::MatchResult &Result,
516 StringRef Id, const Expr *&SymExpr) {
517 std::string SymId = (Id + "-sym").str();
518 if (const auto *Node = Result.Nodes.getNodeAs<Expr>(SymId)) {
519 SymExpr = Node;
520 return true;
521 }
522 return false;
523}
524
525// Match a binary operator between a symbolic expression and an integer constant
526// expression.
527static ast_matchers::internal::Matcher<Expr>
528matchBinOpIntegerConstantExpr(StringRef Id) {
529 const auto BinOpCstExpr =
530 expr(anyOf(binaryOperator(hasAnyOperatorName("+", "|", "&"),
531 hasOperands(matchSymbolicExpr(Id),
532 matchIntegerConstantExpr(Id))),
533 binaryOperator(hasOperatorName("-"),
534 hasLHS(matchSymbolicExpr(Id)),
535 hasRHS(matchIntegerConstantExpr(Id)))))
536 .bind(Id);
537 return ignoringParenImpCasts(BinOpCstExpr);
538}
539
540// Retrieves sub-expressions matched by 'matchBinOpIntegerConstantExpr' with
541// name 'Id'.
542static bool
543retrieveBinOpIntegerConstantExpr(const MatchFinder::MatchResult &Result,
544 StringRef Id, BinaryOperatorKind &Opcode,
545 const Expr *&Symbol, APSInt &Value) {
546 if (const auto *BinExpr = Result.Nodes.getNodeAs<BinaryOperator>(Id)) {
547 Opcode = BinExpr->getOpcode();
548 return retrieveSymbolicExpr(Result, Id, Symbol) &&
549 retrieveIntegerConstantExpr(Result, Id, Value);
550 }
551 return false;
552}
553
554// Matches relational expressions: 'Expr <op> k' (i.e. x < 2, x != 3, 12 <= x).
555static ast_matchers::internal::Matcher<Expr>
556matchRelationalIntegerConstantExpr(StringRef Id) {
557 std::string CastId = (Id + "-cast").str();
558 std::string SwapId = (Id + "-swap").str();
559 std::string NegateId = (Id + "-negate").str();
560 std::string OverloadId = (Id + "-overload").str();
561 std::string ConstId = (Id + "-const").str();
562
563 const auto RelationalExpr = ignoringParenImpCasts(binaryOperator(
564 isComparisonOperator(), expr().bind(Id),
565 anyOf(allOf(hasLHS(matchSymbolicExpr(Id)),
566 hasRHS(matchIntegerConstantExpr(Id))),
567 allOf(hasLHS(matchIntegerConstantExpr(Id)),
568 hasRHS(matchSymbolicExpr(Id)), expr().bind(SwapId)))));
569
570 // A cast can be matched as a comparator to zero. (i.e. if (x) is equivalent
571 // to if (x != 0)).
572 const auto CastExpr =
573 implicitCastExpr(hasCastKind(CK_IntegralToBoolean),
574 hasSourceExpression(matchSymbolicExpr(Id)))
575 .bind(CastId);
576
577 const auto NegateRelationalExpr =
578 unaryOperator(hasOperatorName("!"),
579 hasUnaryOperand(anyOf(CastExpr, RelationalExpr)))
580 .bind(NegateId);
581
582 // Do not bind to double negation.
583 const auto NegateNegateRelationalExpr =
584 unaryOperator(hasOperatorName("!"),
585 hasUnaryOperand(unaryOperator(
586 hasOperatorName("!"),
587 hasUnaryOperand(anyOf(CastExpr, RelationalExpr)))));
588
589 const auto OverloadedOperatorExpr =
590 cxxOperatorCallExpr(
591 hasAnyOverloadedOperatorName("==", "!=", "<", "<=", ">", ">="),
592 // Filter noisy false positives.
593 unless(isMacro()), unless(isInTemplateInstantiation()),
594 anyOf(hasLHS(ignoringParenImpCasts(integerLiteral().bind(ConstId))),
595 hasRHS(ignoringParenImpCasts(integerLiteral().bind(ConstId)))))
596 .bind(OverloadId);
597
598 return anyOf(RelationalExpr, CastExpr, NegateRelationalExpr,
599 NegateNegateRelationalExpr, OverloadedOperatorExpr);
600}
601
602// Checks whether a function param is non constant reference type, and may
603// be modified in the function.
604static bool isNonConstReferenceType(QualType ParamType) {
605 return ParamType->isReferenceType() &&
606 !ParamType.getNonReferenceType().isConstQualified();
607}
608
609// Checks whether the arguments of an overloaded operator can be modified in the
610// function.
611// For operators that take an instance and a constant as arguments, only the
612// first argument (the instance) needs to be checked, since the constant itself
613// is a temporary expression. Whether the second parameter is checked is
614// controlled by the parameter `ParamsToCheckCount`.
615static bool
616canOverloadedOperatorArgsBeModified(const CXXOperatorCallExpr *OperatorCall,
617 bool CheckSecondParam) {
618 const auto *OperatorDecl =
619 dyn_cast_or_null<FunctionDecl>(OperatorCall->getCalleeDecl());
620 // if we can't find the declaration, conservatively assume it can modify
621 // arguments
622 if (!OperatorDecl)
623 return true;
624
625 unsigned ParamCount = OperatorDecl->getNumParams();
626
627 // Overloaded operators declared inside a class have only one param.
628 // These functions must be declared const in order to not be able to modify
629 // the instance of the class they are called through.
630 if (ParamCount == 1 &&
631 !OperatorDecl->getType()->castAs<FunctionType>()->isConst())
632 return true;
633
634 if (isNonConstReferenceType(OperatorDecl->getParamDecl(0)->getType()))
635 return true;
636
637 return CheckSecondParam && ParamCount == 2 &&
638 isNonConstReferenceType(OperatorDecl->getParamDecl(1)->getType());
639}
640
641// Retrieves sub-expressions matched by 'matchRelationalIntegerConstantExpr'
642// with name 'Id'.
643static bool retrieveRelationalIntegerConstantExpr(
644 const MatchFinder::MatchResult &Result, StringRef Id,
645 const Expr *&OperandExpr, BinaryOperatorKind &Opcode, const Expr *&Symbol,
646 APSInt &Value, const Expr *&ConstExpr) {
647 std::string CastId = (Id + "-cast").str();
648 std::string SwapId = (Id + "-swap").str();
649 std::string NegateId = (Id + "-negate").str();
650 std::string OverloadId = (Id + "-overload").str();
651
652 if (const auto *Bin = Result.Nodes.getNodeAs<BinaryOperator>(Id)) {
653 // Operand received with explicit comparator.
654 Opcode = Bin->getOpcode();
655 OperandExpr = Bin;
656
657 if (!retrieveIntegerConstantExpr(Result, Id, Value, ConstExpr))
658 return false;
659 } else if (const auto *Cast = Result.Nodes.getNodeAs<CastExpr>(CastId)) {
660 // Operand received with implicit comparator (cast).
661 Opcode = BO_NE;
662 OperandExpr = Cast;
663 Value = APSInt(32, false);
664 } else if (const auto *OverloadedOperatorExpr =
665 Result.Nodes.getNodeAs<CXXOperatorCallExpr>(OverloadId)) {
666 if (canOverloadedOperatorArgsBeModified(OverloadedOperatorExpr, false))
667 return false;
668
669 bool IntegerConstantIsFirstArg = false;
670
671 if (const auto *Arg = OverloadedOperatorExpr->getArg(1)) {
672 if (!Arg->isValueDependent() &&
673 !Arg->isIntegerConstantExpr(*Result.Context)) {
674 IntegerConstantIsFirstArg = true;
675 if (const auto *Arg = OverloadedOperatorExpr->getArg(0)) {
676 if (!Arg->isValueDependent() &&
677 !Arg->isIntegerConstantExpr(*Result.Context))
678 return false;
679 } else
680 return false;
681 }
682 } else
683 return false;
684
685 Symbol = OverloadedOperatorExpr->getArg(IntegerConstantIsFirstArg ? 1 : 0);
686 OperandExpr = OverloadedOperatorExpr;
687 Opcode = BinaryOperator::getOverloadedOpcode(
688 OverloadedOperatorExpr->getOperator());
689
690 if (!retrieveIntegerConstantExpr(Result, Id, Value, ConstExpr))
691 return false;
692
693 if (!BinaryOperator::isComparisonOp(Opcode))
694 return false;
695
696 // The call site of this function expects the constant on the RHS,
697 // so change the opcode accordingly.
698 if (IntegerConstantIsFirstArg)
699 Opcode = BinaryOperator::reverseComparisonOp(Opcode);
700
701 return true;
702 } else {
703 return false;
704 }
705
706 if (!retrieveSymbolicExpr(Result, Id, Symbol))
707 return false;
708
709 if (Result.Nodes.getNodeAs<Expr>(SwapId))
710 Opcode = BinaryOperator::reverseComparisonOp(Opcode);
711 if (Result.Nodes.getNodeAs<Expr>(NegateId))
712 Opcode = BinaryOperator::negateComparisonOp(Opcode);
713 return true;
714}
715
716// Checks for expressions like (X == 4) && (Y != 9)
717static bool areSidesBinaryConstExpressions(const BinaryOperator *&BinOp,
718 const ASTContext *AstCtx) {
719 const auto *LhsBinOp = dyn_cast<BinaryOperator>(BinOp->getLHS());
720 const auto *RhsBinOp = dyn_cast<BinaryOperator>(BinOp->getRHS());
721
722 if (!LhsBinOp || !RhsBinOp)
723 return false;
724
725 auto IsIntegerConstantExpr = [AstCtx](const Expr *E) {
726 return !E->isValueDependent() && E->isIntegerConstantExpr(*AstCtx);
727 };
728
729 if ((IsIntegerConstantExpr(LhsBinOp->getLHS()) ||
730 IsIntegerConstantExpr(LhsBinOp->getRHS())) &&
731 (IsIntegerConstantExpr(RhsBinOp->getLHS()) ||
732 IsIntegerConstantExpr(RhsBinOp->getRHS())))
733 return true;
734 return false;
735}
736
737static bool areSidesBinaryConstExpressionsOrDefinesOrIntegerConstant(
738 const BinaryOperator *&BinOp, const ASTContext *AstCtx) {
739 if (areSidesBinaryConstExpressions(BinOp, AstCtx))
740 return true;
741
742 const Expr *Lhs = BinOp->getLHS();
743 const Expr *Rhs = BinOp->getRHS();
744
745 if (!Lhs || !Rhs)
746 return false;
747
748 auto IsDefineExpr = [AstCtx](const Expr *E) {
749 const SourceRange Lsr = E->getSourceRange();
750 if (!Lsr.getBegin().isMacroID() || E->isValueDependent() ||
751 !E->isIntegerConstantExpr(*AstCtx))
752 return false;
753 return true;
754 };
755
756 return IsDefineExpr(Lhs) || IsDefineExpr(Rhs);
757}
758
759// Retrieves integer constant subexpressions from binary operator expressions
760// that have two equivalent sides.
761// E.g.: from (X == 5) && (X == 5) retrieves 5 and 5.
762static bool retrieveConstExprFromBothSides(const BinaryOperator *&BinOp,
763 BinaryOperatorKind &MainOpcode,
764 BinaryOperatorKind &SideOpcode,
765 const Expr *&LhsConst,
766 const Expr *&RhsConst,
767 const ASTContext *AstCtx) {
768 assert(areSidesBinaryConstExpressions(BinOp, AstCtx) &&
769 "Both sides of binary operator must be constant expressions!");
770
771 MainOpcode = BinOp->getOpcode();
772
773 const auto *BinOpLhs = cast<BinaryOperator>(BinOp->getLHS());
774 const auto *BinOpRhs = cast<BinaryOperator>(BinOp->getRHS());
775
776 auto IsIntegerConstantExpr = [AstCtx](const Expr *E) {
777 return !E->isValueDependent() && E->isIntegerConstantExpr(*AstCtx);
778 };
779
780 LhsConst = IsIntegerConstantExpr(BinOpLhs->getLHS()) ? BinOpLhs->getLHS()
781 : BinOpLhs->getRHS();
782 RhsConst = IsIntegerConstantExpr(BinOpRhs->getLHS()) ? BinOpRhs->getLHS()
783 : BinOpRhs->getRHS();
784
785 if (!LhsConst || !RhsConst)
786 return false;
787
788 assert(BinOpLhs->getOpcode() == BinOpRhs->getOpcode() &&
789 "Sides of the binary operator must be equivalent expressions!");
790
791 SideOpcode = BinOpLhs->getOpcode();
792
793 return true;
794}
795
796static bool isSameRawIdentifierToken(const Token &T1, const Token &T2,
797 const SourceManager &SM) {
798 if (T1.getKind() != T2.getKind())
799 return false;
800 if (T1.isNot(tok::raw_identifier))
801 return true;
802 if (T1.getLength() != T2.getLength())
803 return false;
804 return StringRef(SM.getCharacterData(T1.getLocation()), T1.getLength()) ==
805 StringRef(SM.getCharacterData(T2.getLocation()), T2.getLength());
806}
807
808bool isTokAtEndOfExpr(SourceRange ExprSR, Token T, const SourceManager &SM) {
809 return SM.getExpansionLoc(ExprSR.getEnd()) == T.getLocation();
810}
811
812/// Returns true if both LhsExpr and RhsExpr are
813/// macro expressions and they are expanded
814/// from different macros.
815static bool areExprsFromDifferentMacros(const Expr *LhsExpr,
816 const Expr *RhsExpr,
817 const ASTContext *AstCtx) {
818 if (!LhsExpr || !RhsExpr)
819 return false;
820 const SourceRange Lsr = LhsExpr->getSourceRange();
821 const SourceRange Rsr = RhsExpr->getSourceRange();
822 if (!Lsr.getBegin().isMacroID() || !Rsr.getBegin().isMacroID())
823 return false;
824
825 const SourceManager &SM = AstCtx->getSourceManager();
826 const LangOptions &LO = AstCtx->getLangOpts();
827
828 std::pair<FileID, unsigned> LsrLocInfo =
829 SM.getDecomposedLoc(SM.getExpansionLoc(Lsr.getBegin()));
830 std::pair<FileID, unsigned> RsrLocInfo =
831 SM.getDecomposedLoc(SM.getExpansionLoc(Rsr.getBegin()));
832 llvm::MemoryBufferRef MB = SM.getBufferOrFake(LsrLocInfo.first);
833
834 const char *LTokenPos = MB.getBufferStart() + LsrLocInfo.second;
835 const char *RTokenPos = MB.getBufferStart() + RsrLocInfo.second;
836 Lexer LRawLex(SM.getLocForStartOfFile(LsrLocInfo.first), LO,
837 MB.getBufferStart(), LTokenPos, MB.getBufferEnd());
838 Lexer RRawLex(SM.getLocForStartOfFile(RsrLocInfo.first), LO,
839 MB.getBufferStart(), RTokenPos, MB.getBufferEnd());
840
841 Token LTok, RTok;
842 do { // Compare the expressions token-by-token.
843 LRawLex.LexFromRawLexer(LTok);
844 RRawLex.LexFromRawLexer(RTok);
845 } while (!LTok.is(tok::eof) && !RTok.is(tok::eof) &&
846 isSameRawIdentifierToken(LTok, RTok, SM) &&
847 !isTokAtEndOfExpr(Lsr, LTok, SM) &&
848 !isTokAtEndOfExpr(Rsr, RTok, SM));
849 return (!isTokAtEndOfExpr(Lsr, LTok, SM) ||
850 !isTokAtEndOfExpr(Rsr, RTok, SM)) ||
851 !isSameRawIdentifierToken(LTok, RTok, SM);
852}
853
854static bool areExprsMacroAndNonMacro(const Expr *&LhsExpr,
855 const Expr *&RhsExpr) {
856 if (!LhsExpr || !RhsExpr)
857 return false;
858
859 const SourceLocation LhsLoc = LhsExpr->getExprLoc();
860 const SourceLocation RhsLoc = RhsExpr->getExprLoc();
861
862 return LhsLoc.isMacroID() != RhsLoc.isMacroID();
863}
864
865static bool areStringsSameIgnoreSpaces(const llvm::StringRef Left,
866 const llvm::StringRef Right) {
867 if (Left == Right)
868 return true;
869
870 // Do running comparison ignoring spaces
871 llvm::StringRef L = Left.trim();
872 llvm::StringRef R = Right.trim();
873 while (!L.empty() && !R.empty()) {
874 L = L.ltrim();
875 R = R.ltrim();
876 if (L.empty() && R.empty())
877 return true;
878 // If symbol compared are different ==> strings are not the same
879 if (L.front() != R.front())
880 return false;
881 L = L.drop_front();
882 R = R.drop_front();
883 }
884 return L.empty() && R.empty();
885}
886
887static bool areExprsSameMacroOrLiteral(const BinaryOperator *BinOp,
888 const ASTContext *Context) {
889
890 if (!BinOp)
891 return false;
892
893 const Expr *Lhs = BinOp->getLHS();
894 const Expr *Rhs = BinOp->getRHS();
895 const SourceManager &SM = Context->getSourceManager();
896
897 const SourceRange Lsr = Lhs->getSourceRange();
898 const SourceRange Rsr = Rhs->getSourceRange();
899 if (Lsr.getBegin().isMacroID()) {
900 // Left is macro so right macro too
901 if (Rsr.getBegin().isMacroID()) {
902 // Both sides are macros so they are same macro or literal
903 const llvm::StringRef L = Lexer::getSourceText(
904 CharSourceRange::getTokenRange(Lsr), SM, Context->getLangOpts());
905 const llvm::StringRef R = Lexer::getSourceText(
906 CharSourceRange::getTokenRange(Rsr), SM, Context->getLangOpts());
907 return areStringsSameIgnoreSpaces(L, R);
908 }
909 // Left is macro but right is not so they are not same macro or literal
910 return false;
911 }
912 const auto *Lil = dyn_cast<IntegerLiteral>(Lhs);
913 const auto *Ril = dyn_cast<IntegerLiteral>(Rhs);
914 if (Lil && Ril)
915 return Lil->getValue() == Ril->getValue();
916
917 const auto *Lbl = dyn_cast<CXXBoolLiteralExpr>(Lhs);
918 const auto *Rbl = dyn_cast<CXXBoolLiteralExpr>(Rhs);
919 if (Lbl && Rbl)
920 return Lbl->getValue() == Rbl->getValue();
921
922 return false;
923}
924} // namespace
925
927 const auto BannedIntegerLiteral =
928 integerLiteral(expandedByMacro(KnownBannedMacroNames));
929 const auto IsInUnevaluatedContext = expr(anyOf(
930 hasAncestor(expr(hasUnevaluatedContext())), hasAncestor(typeLoc())));
931
932 // Binary with equivalent operands, like (X != 2 && X != 2).
933 Finder->addMatcher(
934 traverse(TK_AsIs,
935 binaryOperator(anyOf(isComparisonOperator(),
936 hasAnyOperatorName("-", "/", "%", "|", "&",
937 "^", "&&", "||", "=")),
938 operandsAreEquivalent(),
939 // Filter noisy false positives.
940 unless(isInTemplateInstantiation()),
941 unless(binaryOperatorIsInMacro()),
942 unless(hasAncestor(arraySubscriptExpr())),
943 unless(hasDescendant(BannedIntegerLiteral)),
944 unless(IsInUnevaluatedContext))
945 .bind("binary")),
946 this);
947
948 // Logical or bitwise operator with equivalent nested operands, like (X && Y
949 // && X) or (X && (Y && X))
950 Finder->addMatcher(
951 binaryOperator(hasAnyOperatorName("|", "&", "||", "&&", "^"),
952 nestedOperandsAreEquivalent(),
953 // Filter noisy false positives.
954 unless(isInTemplateInstantiation()),
955 unless(binaryOperatorIsInMacro()),
956 // TODO: if the banned macros are themselves duplicated
957 unless(hasDescendant(BannedIntegerLiteral)),
958 unless(IsInUnevaluatedContext))
959 .bind("nested-duplicates"),
960 this);
961
962 // Conditional (ternary) operator with equivalent operands, like (Y ? X : X).
963 Finder->addMatcher(
964 traverse(TK_AsIs,
965 conditionalOperator(expressionsAreEquivalent(),
966 // Filter noisy false positives.
967 unless(conditionalOperatorIsInMacro()),
968 unless(isInTemplateInstantiation()),
969 unless(IsInUnevaluatedContext))
970 .bind("cond")),
971 this);
972
973 // Overloaded operators with equivalent operands.
974 Finder->addMatcher(
975 traverse(TK_AsIs,
976 cxxOperatorCallExpr(
977 hasAnyOverloadedOperatorName("-", "/", "%", "|", "&", "^",
978 "==", "!=", "<", "<=", ">",
979 ">=", "&&", "||", "="),
980 parametersAreEquivalent(),
981 // Filter noisy false positives.
982 unless(isMacro()), unless(isInTemplateInstantiation()),
983 unless(IsInUnevaluatedContext))
984 .bind("call")),
985 this);
986
987 // Overloaded operators with equivalent operands.
988 Finder->addMatcher(
989 cxxOperatorCallExpr(
990 hasAnyOverloadedOperatorName("|", "&", "||", "&&", "^"),
991 nestedParametersAreEquivalent(), argumentCountIs(2),
992 // Filter noisy false positives.
993 unless(isMacro()), unless(isInTemplateInstantiation()),
994 unless(IsInUnevaluatedContext))
995 .bind("nested-duplicates"),
996 this);
997
998 // Match expressions like: !(1 | 2 | 3)
999 Finder->addMatcher(
1000 traverse(TK_AsIs,
1001 implicitCastExpr(
1002 hasImplicitDestinationType(isInteger()),
1003 has(unaryOperator(
1004 hasOperatorName("!"),
1005 hasUnaryOperand(ignoringParenImpCasts(binaryOperator(
1006 hasAnyOperatorName("|", "&"),
1007 hasLHS(anyOf(
1008 binaryOperator(hasAnyOperatorName("|", "&")),
1009 integerLiteral())),
1010 hasRHS(integerLiteral())))))
1011 .bind("logical-bitwise-confusion")),
1012 unless(IsInUnevaluatedContext))),
1013 this);
1014
1015 // Match expressions like: (X << 8) & 0xFF
1016 Finder->addMatcher(
1017 traverse(TK_AsIs,
1018 binaryOperator(
1019 hasOperatorName("&"),
1020 hasOperands(ignoringParenImpCasts(binaryOperator(
1021 hasOperatorName("<<"),
1022 hasRHS(ignoringParenImpCasts(
1023 integerLiteral().bind("shift-const"))))),
1024 ignoringParenImpCasts(
1025 integerLiteral().bind("and-const"))),
1026 unless(IsInUnevaluatedContext))
1027 .bind("left-right-shift-confusion")),
1028 this);
1029
1030 // Match common expressions and apply more checks to find redundant
1031 // sub-expressions.
1032 // a) Expr <op> K1 == K2
1033 // b) Expr <op> K1 == Expr
1034 // c) Expr <op> K1 == Expr <op> K2
1035 // see: 'checkArithmeticExpr' and 'checkBitwiseExpr'
1036 const auto BinOpCstLeft = matchBinOpIntegerConstantExpr("lhs");
1037 const auto BinOpCstRight = matchBinOpIntegerConstantExpr("rhs");
1038 const auto CstRight = matchIntegerConstantExpr("rhs");
1039 const auto SymRight = matchSymbolicExpr("rhs");
1040
1041 // Match expressions like: x <op> 0xFF == 0xF00.
1042 Finder->addMatcher(
1043 traverse(TK_AsIs, binaryOperator(isComparisonOperator(),
1044 hasOperands(BinOpCstLeft, CstRight),
1045 unless(IsInUnevaluatedContext))
1046 .bind("binop-const-compare-to-const")),
1047 this);
1048
1049 // Match expressions like: x <op> 0xFF == x.
1050 Finder->addMatcher(
1051 traverse(
1052 TK_AsIs,
1053 binaryOperator(isComparisonOperator(),
1054 anyOf(allOf(hasLHS(BinOpCstLeft), hasRHS(SymRight)),
1055 allOf(hasLHS(SymRight), hasRHS(BinOpCstLeft))),
1056 unless(IsInUnevaluatedContext))
1057 .bind("binop-const-compare-to-sym")),
1058 this);
1059
1060 // Match expressions like: x <op> 10 == x <op> 12.
1061 Finder->addMatcher(
1062 traverse(TK_AsIs,
1063 binaryOperator(isComparisonOperator(), hasLHS(BinOpCstLeft),
1064 hasRHS(BinOpCstRight),
1065 // Already reported as redundant.
1066 unless(operandsAreEquivalent()),
1067 unless(IsInUnevaluatedContext))
1068 .bind("binop-const-compare-to-binop-const")),
1069 this);
1070
1071 // Match relational expressions combined with logical operators and find
1072 // redundant sub-expressions.
1073 // see: 'checkRelationalExpr'
1074
1075 // Match expressions like: x < 2 && x > 2.
1076 const auto ComparisonLeft = matchRelationalIntegerConstantExpr("lhs");
1077 const auto ComparisonRight = matchRelationalIntegerConstantExpr("rhs");
1078 Finder->addMatcher(
1079 traverse(TK_AsIs,
1080 binaryOperator(hasAnyOperatorName("||", "&&"),
1081 hasLHS(ComparisonLeft), hasRHS(ComparisonRight),
1082 // Already reported as redundant.
1083 unless(operandsAreEquivalent()),
1084 unless(IsInUnevaluatedContext))
1085 .bind("comparisons-of-symbol-and-const")),
1086 this);
1087}
1088
1089void RedundantExpressionCheck::checkArithmeticExpr(
1090 const MatchFinder::MatchResult &Result) {
1091 APSInt LhsValue, RhsValue;
1092 const Expr *LhsSymbol = nullptr, *RhsSymbol = nullptr;
1093 BinaryOperatorKind LhsOpcode{}, RhsOpcode{};
1094
1095 if (const auto *ComparisonOperator = Result.Nodes.getNodeAs<BinaryOperator>(
1096 "binop-const-compare-to-sym")) {
1097 BinaryOperatorKind Opcode = ComparisonOperator->getOpcode();
1098 if (!retrieveBinOpIntegerConstantExpr(Result, "lhs", LhsOpcode, LhsSymbol,
1099 LhsValue) ||
1100 !retrieveSymbolicExpr(Result, "rhs", RhsSymbol) ||
1101 !areEquivalentExpr(LhsSymbol, RhsSymbol))
1102 return;
1103
1104 // Check expressions: x + k == x or x - k == x.
1105 if (LhsOpcode == BO_Add || LhsOpcode == BO_Sub) {
1106 if ((LhsValue != 0 && Opcode == BO_EQ) ||
1107 (LhsValue == 0 && Opcode == BO_NE))
1108 diag(ComparisonOperator->getOperatorLoc(),
1109 "logical expression is always false");
1110 else if ((LhsValue == 0 && Opcode == BO_EQ) ||
1111 (LhsValue != 0 && Opcode == BO_NE))
1112 diag(ComparisonOperator->getOperatorLoc(),
1113 "logical expression is always true");
1114 }
1115 } else if (const auto *ComparisonOperator =
1116 Result.Nodes.getNodeAs<BinaryOperator>(
1117 "binop-const-compare-to-binop-const")) {
1118 BinaryOperatorKind Opcode = ComparisonOperator->getOpcode();
1119
1120 if (!retrieveBinOpIntegerConstantExpr(Result, "lhs", LhsOpcode, LhsSymbol,
1121 LhsValue) ||
1122 !retrieveBinOpIntegerConstantExpr(Result, "rhs", RhsOpcode, RhsSymbol,
1123 RhsValue) ||
1124 !areEquivalentExpr(LhsSymbol, RhsSymbol))
1125 return;
1126
1127 transformSubToCanonicalAddExpr(LhsOpcode, LhsValue);
1128 transformSubToCanonicalAddExpr(RhsOpcode, RhsValue);
1129
1130 // Check expressions: x + 1 == x + 2 or x + 1 != x + 2.
1131 if (LhsOpcode == BO_Add && RhsOpcode == BO_Add) {
1132 if ((Opcode == BO_EQ && APSInt::compareValues(LhsValue, RhsValue) == 0) ||
1133 (Opcode == BO_NE && APSInt::compareValues(LhsValue, RhsValue) != 0)) {
1134 diag(ComparisonOperator->getOperatorLoc(),
1135 "logical expression is always true");
1136 } else if ((Opcode == BO_EQ &&
1137 APSInt::compareValues(LhsValue, RhsValue) != 0) ||
1138 (Opcode == BO_NE &&
1139 APSInt::compareValues(LhsValue, RhsValue) == 0)) {
1140 diag(ComparisonOperator->getOperatorLoc(),
1141 "logical expression is always false");
1142 }
1143 }
1144 }
1145}
1146
1147static bool exprEvaluatesToZero(BinaryOperatorKind Opcode, APSInt Value) {
1148 return (Opcode == BO_And || Opcode == BO_AndAssign) && Value == 0;
1149}
1150
1151static bool exprEvaluatesToBitwiseNegatedZero(BinaryOperatorKind Opcode,
1152 APSInt Value) {
1153 return (Opcode == BO_Or || Opcode == BO_OrAssign) && ~Value == 0;
1154}
1155
1156static bool exprEvaluatesToSymbolic(BinaryOperatorKind Opcode, APSInt Value) {
1157 return ((Opcode == BO_Or || Opcode == BO_OrAssign) && Value == 0) ||
1158 ((Opcode == BO_And || Opcode == BO_AndAssign) && ~Value == 0);
1159}
1160
1161void RedundantExpressionCheck::checkBitwiseExpr(
1162 const MatchFinder::MatchResult &Result) {
1163 if (const auto *ComparisonOperator = Result.Nodes.getNodeAs<BinaryOperator>(
1164 "binop-const-compare-to-const")) {
1165 BinaryOperatorKind Opcode = ComparisonOperator->getOpcode();
1166
1167 APSInt LhsValue, RhsValue;
1168 const Expr *LhsSymbol = nullptr;
1169 BinaryOperatorKind LhsOpcode{};
1170 if (!retrieveBinOpIntegerConstantExpr(Result, "lhs", LhsOpcode, LhsSymbol,
1171 LhsValue) ||
1172 !retrieveIntegerConstantExpr(Result, "rhs", RhsValue))
1173 return;
1174
1175 uint64_t LhsConstant = LhsValue.getZExtValue();
1176 uint64_t RhsConstant = RhsValue.getZExtValue();
1177 SourceLocation Loc = ComparisonOperator->getOperatorLoc();
1178
1179 // Check expression: x & k1 == k2 (i.e. x & 0xFF == 0xF00)
1180 if (LhsOpcode == BO_And && (LhsConstant & RhsConstant) != RhsConstant) {
1181 if (Opcode == BO_EQ)
1182 diag(Loc, "logical expression is always false");
1183 else if (Opcode == BO_NE)
1184 diag(Loc, "logical expression is always true");
1185 }
1186
1187 // Check expression: x | k1 == k2 (i.e. x | 0xFF == 0xF00)
1188 if (LhsOpcode == BO_Or && (LhsConstant | RhsConstant) != RhsConstant) {
1189 if (Opcode == BO_EQ)
1190 diag(Loc, "logical expression is always false");
1191 else if (Opcode == BO_NE)
1192 diag(Loc, "logical expression is always true");
1193 }
1194 } else if (const auto *IneffectiveOperator =
1195 Result.Nodes.getNodeAs<BinaryOperator>(
1196 "ineffective-bitwise")) {
1197 APSInt Value;
1198 const Expr *Sym = nullptr, *ConstExpr = nullptr;
1199
1200 if (!retrieveSymbolicExpr(Result, "ineffective-bitwise", Sym) ||
1201 !retrieveIntegerConstantExpr(Result, "ineffective-bitwise", Value,
1202 ConstExpr))
1203 return;
1204
1205 if ((Value != 0 && ~Value != 0) || Sym->getExprLoc().isMacroID())
1206 return;
1207
1208 SourceLocation Loc = IneffectiveOperator->getOperatorLoc();
1209
1210 BinaryOperatorKind Opcode = IneffectiveOperator->getOpcode();
1211 if (exprEvaluatesToZero(Opcode, Value)) {
1212 diag(Loc, "expression always evaluates to 0");
1213 } else if (exprEvaluatesToBitwiseNegatedZero(Opcode, Value)) {
1214 SourceRange ConstExprRange(ConstExpr->getBeginLoc(),
1215 ConstExpr->getEndLoc());
1216 StringRef ConstExprText = Lexer::getSourceText(
1217 CharSourceRange::getTokenRange(ConstExprRange), *Result.SourceManager,
1218 Result.Context->getLangOpts());
1219
1220 diag(Loc, "expression always evaluates to '%0'") << ConstExprText;
1221
1222 } else if (exprEvaluatesToSymbolic(Opcode, Value)) {
1223 SourceRange SymExprRange(Sym->getBeginLoc(), Sym->getEndLoc());
1224
1225 StringRef ExprText = Lexer::getSourceText(
1226 CharSourceRange::getTokenRange(SymExprRange), *Result.SourceManager,
1227 Result.Context->getLangOpts());
1228
1229 diag(Loc, "expression always evaluates to '%0'") << ExprText;
1230 }
1231 }
1232}
1233
1234void RedundantExpressionCheck::checkRelationalExpr(
1235 const MatchFinder::MatchResult &Result) {
1236 if (const auto *ComparisonOperator = Result.Nodes.getNodeAs<BinaryOperator>(
1237 "comparisons-of-symbol-and-const")) {
1238 // Matched expressions are: (x <op> k1) <REL> (x <op> k2).
1239 // E.g.: (X < 2) && (X > 4)
1240 BinaryOperatorKind Opcode = ComparisonOperator->getOpcode();
1241
1242 const Expr *LhsExpr = nullptr, *RhsExpr = nullptr;
1243 const Expr *LhsSymbol = nullptr, *RhsSymbol = nullptr;
1244 const Expr *LhsConst = nullptr, *RhsConst = nullptr;
1245 BinaryOperatorKind LhsOpcode{}, RhsOpcode{};
1246 APSInt LhsValue, RhsValue;
1247
1248 if (!retrieveRelationalIntegerConstantExpr(
1249 Result, "lhs", LhsExpr, LhsOpcode, LhsSymbol, LhsValue, LhsConst) ||
1250 !retrieveRelationalIntegerConstantExpr(
1251 Result, "rhs", RhsExpr, RhsOpcode, RhsSymbol, RhsValue, RhsConst) ||
1252 !areEquivalentExpr(LhsSymbol, RhsSymbol))
1253 return;
1254
1255 // Bring expr to a canonical form: smallest constant must be on the left.
1256 if (APSInt::compareValues(LhsValue, RhsValue) > 0) {
1257 std::swap(LhsExpr, RhsExpr);
1258 std::swap(LhsValue, RhsValue);
1259 std::swap(LhsSymbol, RhsSymbol);
1260 std::swap(LhsOpcode, RhsOpcode);
1261 }
1262
1263 // Constants come from two different macros, or one of them is a macro.
1264 if (areExprsFromDifferentMacros(LhsConst, RhsConst, Result.Context) ||
1265 areExprsMacroAndNonMacro(LhsConst, RhsConst))
1266 return;
1267
1268 if ((Opcode == BO_LAnd || Opcode == BO_LOr) &&
1269 areEquivalentRanges(LhsOpcode, LhsValue, RhsOpcode, RhsValue)) {
1270 diag(ComparisonOperator->getOperatorLoc(),
1271 "equivalent expression on both sides of logical operator");
1272 return;
1273 }
1274
1275 if (Opcode == BO_LAnd) {
1276 if (areExclusiveRanges(LhsOpcode, LhsValue, RhsOpcode, RhsValue)) {
1277 diag(ComparisonOperator->getOperatorLoc(),
1278 "logical expression is always false");
1279 } else if (rangeSubsumesRange(LhsOpcode, LhsValue, RhsOpcode, RhsValue)) {
1280 diag(LhsExpr->getExprLoc(), "expression is redundant");
1281 } else if (rangeSubsumesRange(RhsOpcode, RhsValue, LhsOpcode, LhsValue)) {
1282 diag(RhsExpr->getExprLoc(), "expression is redundant");
1283 }
1284 }
1285
1286 if (Opcode == BO_LOr) {
1287 if (rangesFullyCoverDomain(LhsOpcode, LhsValue, RhsOpcode, RhsValue)) {
1288 diag(ComparisonOperator->getOperatorLoc(),
1289 "logical expression is always true");
1290 } else if (rangeSubsumesRange(LhsOpcode, LhsValue, RhsOpcode, RhsValue)) {
1291 diag(RhsExpr->getExprLoc(), "expression is redundant");
1292 } else if (rangeSubsumesRange(RhsOpcode, RhsValue, LhsOpcode, LhsValue)) {
1293 diag(LhsExpr->getExprLoc(), "expression is redundant");
1294 }
1295 }
1296 }
1297}
1298
1299void RedundantExpressionCheck::check(const MatchFinder::MatchResult &Result) {
1300 if (const auto *BinOp = Result.Nodes.getNodeAs<BinaryOperator>("binary")) {
1301 // If the expression's constants are macros, check whether they are
1302 // intentional.
1303
1304 //
1305 // Special case for floating-point representation.
1306 //
1307 // If expressions on both sides of comparison operator are of type float,
1308 // then for some comparison operators no warning shall be
1309 // reported even if the expressions are identical from a symbolic point of
1310 // view. Comparison between expressions, declared variables and literals
1311 // are treated differently.
1312 //
1313 // != and == between float literals that have the same value should NOT
1314 // warn. < > between float literals that have the same value SHOULD warn.
1315 //
1316 // != and == between the same float declaration should NOT warn.
1317 // < > between the same float declaration SHOULD warn.
1318 //
1319 // != and == between eq. expressions that evaluates into float
1320 // should NOT warn.
1321 // < > between eq. expressions that evaluates into float
1322 // should NOT warn.
1323 //
1324 const Expr *LHS = BinOp->getLHS()->IgnoreParenImpCasts();
1325 const Expr *RHS = BinOp->getRHS()->IgnoreParenImpCasts();
1326 const BinaryOperator::Opcode Op = BinOp->getOpcode();
1327 const bool OpEqualEQorNE = ((Op == BO_EQ) || (Op == BO_NE));
1328
1329 const auto *DeclRef1 = dyn_cast<DeclRefExpr>(LHS);
1330 const auto *DeclRef2 = dyn_cast<DeclRefExpr>(RHS);
1331 const auto *FloatLit1 = dyn_cast<FloatingLiteral>(LHS);
1332 const auto *FloatLit2 = dyn_cast<FloatingLiteral>(RHS);
1333
1334 if (DeclRef1 && DeclRef2 &&
1335 DeclRef1->getType()->hasFloatingRepresentation() &&
1336 DeclRef2->getType()->hasFloatingRepresentation() &&
1337 (DeclRef1->getDecl() == DeclRef2->getDecl()) && OpEqualEQorNE) {
1338 return;
1339 }
1340
1341 if (FloatLit1 && FloatLit2 &&
1342 FloatLit1->getValue().bitwiseIsEqual(FloatLit2->getValue()) &&
1343 OpEqualEQorNE) {
1344 return;
1345 }
1346
1347 if (areSidesBinaryConstExpressionsOrDefinesOrIntegerConstant(
1348 BinOp, Result.Context)) {
1349 const Expr *LhsConst = nullptr, *RhsConst = nullptr;
1350 BinaryOperatorKind MainOpcode{}, SideOpcode{};
1351 if (areSidesBinaryConstExpressions(BinOp, Result.Context)) {
1352 if (!retrieveConstExprFromBothSides(BinOp, MainOpcode, SideOpcode,
1353 LhsConst, RhsConst, Result.Context))
1354 return;
1355
1356 if (areExprsFromDifferentMacros(LhsConst, RhsConst, Result.Context) ||
1357 areExprsMacroAndNonMacro(LhsConst, RhsConst))
1358 return;
1359 } else {
1360 if (!areExprsSameMacroOrLiteral(BinOp, Result.Context))
1361 return;
1362 }
1363 }
1364 diag(BinOp->getOperatorLoc(), "both sides of operator are equivalent");
1365 }
1366
1367 if (const auto *CondOp =
1368 Result.Nodes.getNodeAs<ConditionalOperator>("cond")) {
1369 const Expr *TrueExpr = CondOp->getTrueExpr();
1370 const Expr *FalseExpr = CondOp->getFalseExpr();
1371
1372 if (areExprsFromDifferentMacros(TrueExpr, FalseExpr, Result.Context) ||
1373 areExprsMacroAndNonMacro(TrueExpr, FalseExpr))
1374 return;
1375 diag(CondOp->getColonLoc(),
1376 "'true' and 'false' expressions are equivalent");
1377 }
1378
1379 if (const auto *Call = Result.Nodes.getNodeAs<CXXOperatorCallExpr>("call")) {
1380 if (canOverloadedOperatorArgsBeModified(Call, true))
1381 return;
1382
1383 diag(Call->getOperatorLoc(),
1384 "both sides of overloaded operator are equivalent");
1385 }
1386
1387 if (const auto *Op = Result.Nodes.getNodeAs<Expr>("nested-duplicates")) {
1388 const auto *Call = dyn_cast<CXXOperatorCallExpr>(Op);
1389 if (Call && canOverloadedOperatorArgsBeModified(Call, true))
1390 return;
1391
1392 StringRef Message =
1393 Call ? "overloaded operator has equivalent nested operands"
1394 : "operator has equivalent nested operands";
1395
1396 const auto Diag = diag(Op->getExprLoc(), Message);
1397 for (const auto &KeyValue : Result.Nodes.getMap()) {
1398 if (StringRef(KeyValue.first).starts_with("duplicate"))
1399 Diag << KeyValue.second.getSourceRange();
1400 }
1401 }
1402
1403 if (const auto *NegateOperator =
1404 Result.Nodes.getNodeAs<UnaryOperator>("logical-bitwise-confusion")) {
1405 SourceLocation OperatorLoc = NegateOperator->getOperatorLoc();
1406
1407 auto Diag =
1408 diag(OperatorLoc,
1409 "ineffective logical negation operator used; did you mean '~'?");
1410 SourceLocation LogicalNotLocation = OperatorLoc.getLocWithOffset(1);
1411
1412 if (!LogicalNotLocation.isMacroID())
1413 Diag << FixItHint::CreateReplacement(
1414 CharSourceRange::getCharRange(OperatorLoc, LogicalNotLocation), "~");
1415 }
1416
1417 if (const auto *BinaryAndExpr = Result.Nodes.getNodeAs<BinaryOperator>(
1418 "left-right-shift-confusion")) {
1419 const auto *ShiftingConst = Result.Nodes.getNodeAs<Expr>("shift-const");
1420 assert(ShiftingConst && "Expr* 'ShiftingConst' is nullptr!");
1421 std::optional<llvm::APSInt> ShiftingValue =
1422 ShiftingConst->getIntegerConstantExpr(*Result.Context);
1423
1424 if (!ShiftingValue)
1425 return;
1426
1427 const auto *AndConst = Result.Nodes.getNodeAs<Expr>("and-const");
1428 assert(AndConst && "Expr* 'AndCont' is nullptr!");
1429 std::optional<llvm::APSInt> AndValue =
1430 AndConst->getIntegerConstantExpr(*Result.Context);
1431 if (!AndValue)
1432 return;
1433
1434 // If ShiftingConst is shifted left with more bits than the position of the
1435 // leftmost 1 in the bit representation of AndValue, AndConstant is
1436 // ineffective.
1437 if (AndValue->getActiveBits() > *ShiftingValue)
1438 return;
1439
1440 auto Diag = diag(BinaryAndExpr->getOperatorLoc(),
1441 "ineffective bitwise and operation");
1442 }
1443
1444 // Check for the following bound expressions:
1445 // - "binop-const-compare-to-sym",
1446 // - "binop-const-compare-to-binop-const",
1447 // Produced message:
1448 // -> "logical expression is always false/true"
1449 checkArithmeticExpr(Result);
1450
1451 // Check for the following bound expression:
1452 // - "binop-const-compare-to-const",
1453 // - "ineffective-bitwise"
1454 // Produced message:
1455 // -> "logical expression is always false/true"
1456 // -> "expression always evaluates to ..."
1457 checkBitwiseExpr(Result);
1458
1459 // Check for te following bound expression:
1460 // - "comparisons-of-symbol-and-const",
1461 // Produced messages:
1462 // -> "equivalent expression on both sides of logical operator",
1463 // -> "logical expression is always false/true"
1464 // -> "expression is redundant"
1465 checkRelationalExpr(Result);
1466}
1467
1468} // namespace clang::tidy::misc
void registerMatchers(ast_matchers::MatchFinder *Finder) override
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
AST_MATCHER_P(Stmt, isStatementIdenticalToBoundNode, std::string, ID)
AST_MATCHER(BinaryOperator, isRelationalOperator)
static constexpr StringRef Message
static bool exprEvaluatesToSymbolic(BinaryOperatorKind Opcode, APSInt Value)
static bool exprEvaluatesToBitwiseNegatedZero(BinaryOperatorKind Opcode, APSInt Value)
static bool exprEvaluatesToZero(BinaryOperatorKind Opcode, APSInt Value)