10#include "../utils/Matchers.h"
11#include "../utils/OptionsUtils.h"
12#include "clang/AST/ASTContext.h"
13#include "clang/AST/ExprConcepts.h"
14#include "clang/ASTMatchers/ASTMatchFinder.h"
15#include "clang/Basic/LLVM.h"
16#include "clang/Basic/SourceLocation.h"
17#include "clang/Basic/SourceManager.h"
18#include "clang/Lex/Lexer.h"
19#include "llvm/ADT/APInt.h"
20#include "llvm/ADT/APSInt.h"
21#include "llvm/ADT/FoldingSet.h"
22#include "llvm/ADT/SmallBitVector.h"
23#include "llvm/Support/Casting.h"
24#include "llvm/Support/FormatVariadic.h"
39static constexpr llvm::StringLiteral KnownBannedMacroNames[] = {
46static bool incrementWithoutOverflow(
const APSInt &
Value, APSInt &Result) {
49 return Value < Result;
52static bool areEquivalentNameSpecifier(
const NestedNameSpecifier *Left,
53 const NestedNameSpecifier *Right) {
54 llvm::FoldingSetNodeID LeftID, RightID;
55 Left->Profile(LeftID);
56 Right->Profile(RightID);
57 return LeftID == RightID;
60static bool areEquivalentExpr(
const Expr *Left,
const Expr *Right) {
68 if (
Left->getStmtClass() !=
Right->getStmtClass())
72 Expr::const_child_iterator LeftIter =
Left->child_begin();
73 Expr::const_child_iterator RightIter =
Right->child_begin();
74 while (LeftIter !=
Left->child_end() && RightIter !=
Right->child_end()) {
75 if (!areEquivalentExpr(dyn_cast_or_null<Expr>(*LeftIter),
76 dyn_cast_or_null<Expr>(*RightIter)))
81 if (LeftIter !=
Left->child_end() || RightIter !=
Right->child_end())
85 switch (
Left->getStmtClass()) {
89 case Stmt::CharacterLiteralClass:
90 return cast<CharacterLiteral>(Left)->getValue() ==
91 cast<CharacterLiteral>(Right)->getValue();
92 case Stmt::IntegerLiteralClass: {
93 llvm::APInt LeftLit = cast<IntegerLiteral>(Left)->getValue();
94 llvm::APInt RightLit = cast<IntegerLiteral>(Right)->getValue();
95 return LeftLit.getBitWidth() == RightLit.getBitWidth() &&
98 case Stmt::FloatingLiteralClass:
99 return cast<FloatingLiteral>(Left)->getValue().bitwiseIsEqual(
100 cast<FloatingLiteral>(Right)->getValue());
101 case Stmt::StringLiteralClass:
102 return cast<StringLiteral>(Left)->getBytes() ==
103 cast<StringLiteral>(Right)->getBytes();
104 case Stmt::CXXOperatorCallExprClass:
105 return cast<CXXOperatorCallExpr>(Left)->getOperator() ==
106 cast<CXXOperatorCallExpr>(Right)->getOperator();
107 case Stmt::DependentScopeDeclRefExprClass:
108 if (cast<DependentScopeDeclRefExpr>(Left)->getDeclName() !=
109 cast<DependentScopeDeclRefExpr>(Right)->getDeclName())
111 return areEquivalentNameSpecifier(
112 cast<DependentScopeDeclRefExpr>(Left)->getQualifier(),
113 cast<DependentScopeDeclRefExpr>(Right)->getQualifier());
114 case Stmt::DeclRefExprClass:
115 return cast<DeclRefExpr>(Left)->getDecl() ==
116 cast<DeclRefExpr>(Right)->getDecl();
117 case Stmt::MemberExprClass:
118 return cast<MemberExpr>(Left)->getMemberDecl() ==
119 cast<MemberExpr>(Right)->getMemberDecl();
120 case Stmt::CXXFoldExprClass:
121 return cast<CXXFoldExpr>(Left)->getOperator() ==
122 cast<CXXFoldExpr>(Right)->getOperator();
123 case Stmt::CXXFunctionalCastExprClass:
124 case Stmt::CStyleCastExprClass:
125 return cast<ExplicitCastExpr>(Left)->getTypeAsWritten() ==
126 cast<ExplicitCastExpr>(Right)->getTypeAsWritten();
127 case Stmt::CallExprClass:
128 case Stmt::ImplicitCastExprClass:
129 case Stmt::ArraySubscriptExprClass:
131 case Stmt::UnaryOperatorClass:
132 if (cast<UnaryOperator>(Left)->isIncrementDecrementOp())
134 return cast<UnaryOperator>(Left)->getOpcode() ==
135 cast<UnaryOperator>(Right)->getOpcode();
136 case Stmt::BinaryOperatorClass:
137 if (cast<BinaryOperator>(Left)->isAssignmentOp())
139 return cast<BinaryOperator>(Left)->getOpcode() ==
140 cast<BinaryOperator>(Right)->getOpcode();
141 case Stmt::UnaryExprOrTypeTraitExprClass:
142 const auto *LeftUnaryExpr =
143 cast<UnaryExprOrTypeTraitExpr>(Left);
144 const auto *RightUnaryExpr =
145 cast<UnaryExprOrTypeTraitExpr>(Right);
146 if (LeftUnaryExpr->isArgumentType() && RightUnaryExpr->isArgumentType())
147 return LeftUnaryExpr->getKind() == RightUnaryExpr->getKind() &&
148 LeftUnaryExpr->getArgumentType() ==
149 RightUnaryExpr->getArgumentType();
150 if (!LeftUnaryExpr->isArgumentType() && !RightUnaryExpr->isArgumentType())
151 return areEquivalentExpr(LeftUnaryExpr->getArgumentExpr(),
152 RightUnaryExpr->getArgumentExpr());
160static bool areEquivalentRanges(BinaryOperatorKind OpcodeLHS,
161 const APSInt &ValueLHS,
162 BinaryOperatorKind OpcodeRHS,
163 const APSInt &ValueRHS) {
164 assert(APSInt::compareValues(ValueLHS, ValueRHS) <= 0 &&
165 "Values must be ordered");
167 if (APSInt::compareValues(ValueLHS, ValueRHS) == 0)
168 return OpcodeLHS == OpcodeRHS;
171 APSInt ValueLhsPlus1;
172 return ((OpcodeLHS == BO_LE && OpcodeRHS == BO_LT) ||
173 (OpcodeLHS == BO_GT && OpcodeRHS == BO_GE)) &&
174 incrementWithoutOverflow(ValueLHS, ValueLhsPlus1) &&
175 APSInt::compareValues(ValueLhsPlus1, ValueRHS) == 0;
180static bool areExclusiveRanges(BinaryOperatorKind OpcodeLHS,
181 const APSInt &ValueLHS,
182 BinaryOperatorKind OpcodeRHS,
183 const APSInt &ValueRHS) {
184 assert(APSInt::compareValues(ValueLHS, ValueRHS) <= 0 &&
185 "Values must be ordered");
188 if (APSInt::compareValues(ValueLHS, ValueRHS) == 0) {
191 return OpcodeRHS == BO_NE || OpcodeRHS == BO_GT || OpcodeRHS == BO_LT;
193 return OpcodeRHS == BO_EQ;
195 return OpcodeRHS == BO_GT;
197 return OpcodeRHS == BO_LT;
199 return OpcodeRHS == BO_EQ || OpcodeRHS == BO_GT || OpcodeRHS == BO_GE;
201 return OpcodeRHS == BO_EQ || OpcodeRHS == BO_LT || OpcodeRHS == BO_LE;
208 if ((OpcodeLHS == BO_EQ || OpcodeLHS == BO_LT || OpcodeLHS == BO_LE) &&
209 (OpcodeRHS == BO_EQ || OpcodeRHS == BO_GT || OpcodeRHS == BO_GE))
213 APSInt ValueLhsPlus1;
214 if (OpcodeLHS == BO_GT && OpcodeRHS == BO_LT &&
215 incrementWithoutOverflow(ValueLHS, ValueLhsPlus1) &&
216 APSInt::compareValues(ValueLhsPlus1, ValueRHS) == 0)
224static bool rangesFullyCoverDomain(BinaryOperatorKind OpcodeLHS,
225 const APSInt &ValueLHS,
226 BinaryOperatorKind OpcodeRHS,
227 const APSInt &ValueRHS) {
228 assert(APSInt::compareValues(ValueLHS, ValueRHS) <= 0 &&
229 "Values must be ordered");
232 if (APSInt::compareValues(ValueLHS, ValueRHS) == 0) {
235 return OpcodeRHS == BO_NE;
237 return OpcodeRHS == BO_EQ;
239 return OpcodeRHS == BO_GT || OpcodeRHS == BO_GE;
241 return OpcodeRHS == BO_GE;
243 return OpcodeRHS == BO_LT || OpcodeRHS == BO_LE;
245 return OpcodeRHS == BO_LE;
252 APSInt ValueLhsPlus1;
253 if (OpcodeLHS == BO_LE && OpcodeRHS == BO_GE &&
254 incrementWithoutOverflow(ValueLHS, ValueLhsPlus1) &&
255 APSInt::compareValues(ValueLhsPlus1, ValueRHS) == 0)
259 if ((OpcodeLHS == BO_GT || OpcodeLHS == BO_GE) &&
260 (OpcodeRHS == BO_LT || OpcodeRHS == BO_LE))
265 if (OpcodeLHS == BO_NE && OpcodeRHS == BO_NE)
271static bool rangeSubsumesRange(BinaryOperatorKind OpcodeLHS,
272 const APSInt &ValueLHS,
273 BinaryOperatorKind OpcodeRHS,
274 const APSInt &ValueRHS) {
275 int Comparison = APSInt::compareValues(ValueLHS, ValueRHS);
278 return OpcodeRHS == BO_EQ && Comparison == 0;
280 return (OpcodeRHS == BO_NE && Comparison == 0) ||
281 (OpcodeRHS == BO_EQ && Comparison != 0) ||
282 (OpcodeRHS == BO_LT && Comparison >= 0) ||
283 (OpcodeRHS == BO_LE && Comparison > 0) ||
284 (OpcodeRHS == BO_GT && Comparison <= 0) ||
285 (OpcodeRHS == BO_GE && Comparison < 0);
288 return ((OpcodeRHS == BO_LT && Comparison >= 0) ||
289 (OpcodeRHS == BO_LE && Comparison > 0) ||
290 (OpcodeRHS == BO_EQ && Comparison > 0));
292 return ((OpcodeRHS == BO_GT && Comparison <= 0) ||
293 (OpcodeRHS == BO_GE && Comparison < 0) ||
294 (OpcodeRHS == BO_EQ && Comparison < 0));
296 return (OpcodeRHS == BO_LT || OpcodeRHS == BO_LE || OpcodeRHS == BO_EQ) &&
299 return (OpcodeRHS == BO_GT || OpcodeRHS == BO_GE || OpcodeRHS == BO_EQ) &&
306static void transformSubToCanonicalAddExpr(BinaryOperatorKind &Opcode,
308 if (Opcode == BO_Sub) {
315static OverloadedOperatorKind getOp(
const BinaryOperator *Op) {
316 return BinaryOperator::getOverloadedOperator(Op->getOpcode());
319static OverloadedOperatorKind getOp(
const CXXOperatorCallExpr *Op) {
320 if (Op->getNumArgs() != 2)
322 return Op->getOperator();
325static std::pair<const Expr *, const Expr *>
326getOperands(
const BinaryOperator *Op) {
327 return {Op->getLHS()->IgnoreParenImpCasts(),
328 Op->getRHS()->IgnoreParenImpCasts()};
331static std::pair<const Expr *, const Expr *>
332getOperands(
const CXXOperatorCallExpr *Op) {
333 return {Op->getArg(0)->IgnoreParenImpCasts(),
334 Op->getArg(1)->IgnoreParenImpCasts()};
337template <
typename TExpr>
338static const TExpr *checkOpKind(
const Expr *
TheExpr,
339 OverloadedOperatorKind OpKind) {
340 const auto *AsTExpr = dyn_cast_or_null<TExpr>(
TheExpr);
341 if (AsTExpr && getOp(AsTExpr) == OpKind)
349template <
typename TExpr,
unsigned N>
350static bool collectOperands(
const Expr *Part,
351 SmallVector<const Expr *, N> &AllOperands,
352 OverloadedOperatorKind OpKind) {
353 if (
const auto *BinOp = checkOpKind<TExpr>(Part, OpKind)) {
354 const std::pair<const Expr *, const Expr *>
Operands = getOperands(BinOp);
357 return collectOperands<TExpr>(
Operands.first, AllOperands, OpKind) ||
358 collectOperands<TExpr>(
Operands.second, AllOperands, OpKind);
361 AllOperands.push_back(Part);
365template <
typename TExpr>
366static bool hasSameOperatorParent(
const Expr *
TheExpr,
367 OverloadedOperatorKind OpKind,
368 ASTContext &Context) {
371 for (DynTypedNode DynParent : Parents) {
372 if (
const auto *
Parent = DynParent.get<Expr>()) {
373 bool Skip = isa<ParenExpr>(
Parent) || isa<ImplicitCastExpr>(
Parent) ||
375 isa<MaterializeTemporaryExpr>(
Parent);
376 if (Skip && hasSameOperatorParent<TExpr>(
Parent, OpKind, Context))
378 if (checkOpKind<TExpr>(
Parent, OpKind))
386template <
typename TExpr>
388markDuplicateOperands(
const TExpr *
TheExpr,
389 ast_matchers::internal::BoundNodesTreeBuilder *
Builder,
390 ASTContext &Context) {
391 const OverloadedOperatorKind OpKind = getOp(
TheExpr);
392 if (OpKind == OO_None)
396 const std::pair<const Expr *, const Expr *>
Operands = getOperands(
TheExpr);
397 if (!(checkOpKind<TExpr>(
Operands.first, OpKind) ||
398 checkOpKind<TExpr>(
Operands.second, OpKind)))
403 if (hasSameOperatorParent<TExpr>(
TheExpr, OpKind, Context))
406 SmallVector<const Expr *, 4> AllOperands;
407 if (collectOperands<TExpr>(
Operands.first, AllOperands, OpKind))
409 if (collectOperands<TExpr>(
Operands.second, AllOperands, OpKind))
411 size_t NumOperands = AllOperands.size();
412 llvm::SmallBitVector Duplicates(NumOperands);
413 for (
size_t I = 0; I < NumOperands; I++) {
416 bool FoundDuplicates =
false;
418 for (
size_t J = I + 1; J < NumOperands; J++) {
419 if (AllOperands[J]->HasSideEffects(Context))
422 if (areEquivalentExpr(AllOperands[I], AllOperands[J])) {
423 FoundDuplicates =
true;
425 Builder->setBinding(SmallString<11>(llvm::formatv(
"duplicate{0}", J)),
426 DynTypedNode::create(*AllOperands[J]));
431 Builder->setBinding(SmallString<11>(llvm::formatv(
"duplicate{0}", I)),
432 DynTypedNode::create(*AllOperands[I]));
435 return Duplicates.any();
439 if (
Node.isInstantiationDependent())
441 return Node.isIntegerConstantExpr(Finder->getASTContext());
444AST_MATCHER(BinaryOperator, operandsAreEquivalent) {
445 return areEquivalentExpr(
Node.getLHS(),
Node.getRHS());
448AST_MATCHER(BinaryOperator, nestedOperandsAreEquivalent) {
449 return markDuplicateOperands(&Node,
Builder, Finder->getASTContext());
452AST_MATCHER(ConditionalOperator, expressionsAreEquivalent) {
453 return areEquivalentExpr(
Node.getTrueExpr(),
Node.getFalseExpr());
457 return Node.getNumArgs() == 2 &&
458 areEquivalentExpr(
Node.getArg(0),
Node.getArg(1));
461AST_MATCHER(CXXOperatorCallExpr, nestedParametersAreEquivalent) {
462 return markDuplicateOperands(&Node,
Builder, Finder->getASTContext());
465AST_MATCHER(BinaryOperator, binaryOperatorIsInMacro) {
466 return Node.getOperatorLoc().isMacroID();
469AST_MATCHER(ConditionalOperator, conditionalOperatorIsInMacro) {
470 return Node.getQuestionLoc().isMacroID() ||
Node.getColonLoc().isMacroID();
475AST_MATCHER_P(Expr, expandedByMacro, ArrayRef<llvm::StringLiteral>, Names) {
476 const SourceManager &SM = Finder->getASTContext().getSourceManager();
477 const LangOptions &LO = Finder->getASTContext().getLangOpts();
478 SourceLocation
Loc =
Node.getExprLoc();
479 while (
Loc.isMacroID()) {
480 StringRef
MacroName = Lexer::getImmediateMacroName(
Loc, SM, LO);
481 if (llvm::is_contained(Names,
MacroName))
483 Loc = SM.getImmediateMacroCallerLoc(
Loc);
489static ast_matchers::internal::Matcher<Expr>
490matchIntegerConstantExpr(StringRef
Id) {
491 std::string CstId = (
Id +
"-const").str();
492 return expr(isIntegerConstantExpr()).bind(CstId);
498static bool retrieveIntegerConstantExpr(
const MatchFinder::MatchResult &Result,
500 const Expr *&ConstExpr) {
501 std::string CstId = (
Id +
"-const").str();
502 ConstExpr = Result.Nodes.getNodeAs<Expr>(CstId);
505 std::optional<llvm::APSInt> R =
506 ConstExpr->getIntegerConstantExpr(*Result.Context);
514static bool retrieveIntegerConstantExpr(
const MatchFinder::MatchResult &Result,
516 const Expr *ConstExpr =
nullptr;
517 return retrieveIntegerConstantExpr(Result,
Id,
Value, ConstExpr);
522static ast_matchers::internal::Matcher<Expr> matchSymbolicExpr(StringRef
Id) {
523 std::string SymId = (
Id +
"-sym").str();
524 return ignoringParenImpCasts(
525 expr(unless(isIntegerConstantExpr())).bind(SymId));
530static bool retrieveSymbolicExpr(
const MatchFinder::MatchResult &Result,
531 StringRef
Id,
const Expr *&SymExpr) {
532 std::string SymId = (
Id +
"-sym").str();
533 if (
const auto *Node = Result.Nodes.getNodeAs<Expr>(SymId)) {
542static ast_matchers::internal::Matcher<Expr>
543matchBinOpIntegerConstantExpr(StringRef
Id) {
544 const auto BinOpCstExpr =
545 expr(anyOf(binaryOperator(hasAnyOperatorName(
"+",
"|",
"&"),
546 hasOperands(matchSymbolicExpr(
Id),
547 matchIntegerConstantExpr(
Id))),
548 binaryOperator(hasOperatorName(
"-"),
549 hasLHS(matchSymbolicExpr(
Id)),
550 hasRHS(matchIntegerConstantExpr(
Id)))))
552 return ignoringParenImpCasts(BinOpCstExpr);
558retrieveBinOpIntegerConstantExpr(
const MatchFinder::MatchResult &Result,
559 StringRef
Id, BinaryOperatorKind &Opcode,
560 const Expr *&Symbol, APSInt &
Value) {
561 if (
const auto *BinExpr = Result.Nodes.getNodeAs<BinaryOperator>(
Id)) {
562 Opcode = BinExpr->getOpcode();
563 return retrieveSymbolicExpr(Result,
Id, Symbol) &&
564 retrieveIntegerConstantExpr(Result,
Id,
Value);
570static ast_matchers::internal::Matcher<Expr>
571matchRelationalIntegerConstantExpr(StringRef
Id) {
572 std::string CastId = (
Id +
"-cast").str();
573 std::string SwapId = (
Id +
"-swap").str();
574 std::string NegateId = (
Id +
"-negate").str();
575 std::string OverloadId = (
Id +
"-overload").str();
576 std::string ConstId = (
Id +
"-const").str();
578 const auto RelationalExpr = ignoringParenImpCasts(binaryOperator(
579 isComparisonOperator(), expr().bind(
Id),
580 anyOf(allOf(hasLHS(matchSymbolicExpr(
Id)),
581 hasRHS(matchIntegerConstantExpr(
Id))),
582 allOf(hasLHS(matchIntegerConstantExpr(
Id)),
583 hasRHS(matchSymbolicExpr(
Id)), expr().bind(SwapId)))));
587 const auto CastExpr =
588 implicitCastExpr(hasCastKind(CK_IntegralToBoolean),
589 hasSourceExpression(matchSymbolicExpr(
Id)))
592 const auto NegateRelationalExpr =
593 unaryOperator(hasOperatorName(
"!"),
594 hasUnaryOperand(anyOf(CastExpr, RelationalExpr)))
598 const auto NegateNegateRelationalExpr =
599 unaryOperator(hasOperatorName(
"!"),
600 hasUnaryOperand(unaryOperator(
601 hasOperatorName(
"!"),
602 hasUnaryOperand(anyOf(CastExpr, RelationalExpr)))));
604 const auto OverloadedOperatorExpr =
606 hasAnyOverloadedOperatorName(
"==",
"!=",
"<",
"<=",
">",
">="),
608 unless(isMacro()), unless(isInTemplateInstantiation()),
609 anyOf(hasLHS(ignoringParenImpCasts(integerLiteral().bind(ConstId))),
610 hasRHS(ignoringParenImpCasts(integerLiteral().bind(ConstId)))))
613 return anyOf(RelationalExpr, CastExpr, NegateRelationalExpr,
614 NegateNegateRelationalExpr, OverloadedOperatorExpr);
619static bool isNonConstReferenceType(QualType ParamType) {
620 return ParamType->isReferenceType() &&
621 !ParamType.getNonReferenceType().isConstQualified();
631canOverloadedOperatorArgsBeModified(
const CXXOperatorCallExpr *OperatorCall,
632 bool CheckSecondParam) {
633 const auto *OperatorDecl =
634 dyn_cast_or_null<FunctionDecl>(OperatorCall->getCalleeDecl());
640 unsigned ParamCount = OperatorDecl->getNumParams();
645 if (ParamCount == 1 &&
646 !OperatorDecl->getType()->castAs<FunctionType>()->isConst())
649 if (isNonConstReferenceType(OperatorDecl->getParamDecl(0)->getType()))
652 return CheckSecondParam && ParamCount == 2 &&
653 isNonConstReferenceType(OperatorDecl->getParamDecl(1)->getType());
658static bool retrieveRelationalIntegerConstantExpr(
659 const MatchFinder::MatchResult &Result, StringRef
Id,
660 const Expr *&OperandExpr, BinaryOperatorKind &Opcode,
const Expr *&Symbol,
661 APSInt &
Value,
const Expr *&ConstExpr) {
662 std::string CastId = (
Id +
"-cast").str();
663 std::string SwapId = (
Id +
"-swap").str();
664 std::string NegateId = (
Id +
"-negate").str();
665 std::string OverloadId = (
Id +
"-overload").str();
667 if (
const auto *Bin = Result.Nodes.getNodeAs<BinaryOperator>(
Id)) {
669 Opcode = Bin->getOpcode();
672 if (!retrieveIntegerConstantExpr(Result,
Id,
Value, ConstExpr))
674 }
else if (
const auto *Cast = Result.Nodes.getNodeAs<CastExpr>(CastId)) {
678 Value = APSInt(32,
false);
679 }
else if (
const auto *OverloadedOperatorExpr =
680 Result.Nodes.getNodeAs<CXXOperatorCallExpr>(OverloadId)) {
681 if (canOverloadedOperatorArgsBeModified(OverloadedOperatorExpr,
false))
684 bool IntegerConstantIsFirstArg =
false;
686 if (
const auto *Arg = OverloadedOperatorExpr->getArg(1)) {
687 if (!Arg->isValueDependent() &&
688 !Arg->isIntegerConstantExpr(*Result.Context)) {
689 IntegerConstantIsFirstArg =
true;
690 if (
const auto *Arg = OverloadedOperatorExpr->getArg(0)) {
691 if (!Arg->isValueDependent() &&
692 !Arg->isIntegerConstantExpr(*Result.Context))
700 Symbol = OverloadedOperatorExpr->getArg(IntegerConstantIsFirstArg ? 1 : 0);
701 OperandExpr = OverloadedOperatorExpr;
702 Opcode = BinaryOperator::getOverloadedOpcode(OverloadedOperatorExpr->getOperator());
704 if (!retrieveIntegerConstantExpr(Result,
Id,
Value, ConstExpr))
707 if (!BinaryOperator::isComparisonOp(Opcode))
712 if (IntegerConstantIsFirstArg)
713 Opcode = BinaryOperator::reverseComparisonOp(Opcode);
720 if (!retrieveSymbolicExpr(Result,
Id, Symbol))
723 if (Result.Nodes.getNodeAs<Expr>(SwapId))
724 Opcode = BinaryOperator::reverseComparisonOp(Opcode);
725 if (Result.Nodes.getNodeAs<Expr>(NegateId))
726 Opcode = BinaryOperator::negateComparisonOp(Opcode);
731static bool areSidesBinaryConstExpressions(
const BinaryOperator *&BinOp,
const ASTContext *AstCtx) {
732 const auto *LhsBinOp = dyn_cast<BinaryOperator>(BinOp->getLHS());
733 const auto *RhsBinOp = dyn_cast<BinaryOperator>(BinOp->getRHS());
735 if (!LhsBinOp || !RhsBinOp)
738 auto IsIntegerConstantExpr = [AstCtx](
const Expr *
E) {
739 return !
E->isValueDependent() &&
E->isIntegerConstantExpr(*AstCtx);
742 if ((IsIntegerConstantExpr(LhsBinOp->getLHS()) ||
743 IsIntegerConstantExpr(LhsBinOp->getRHS())) &&
744 (IsIntegerConstantExpr(RhsBinOp->getLHS()) ||
745 IsIntegerConstantExpr(RhsBinOp->getRHS())))
753static bool retrieveConstExprFromBothSides(
const BinaryOperator *&BinOp,
754 BinaryOperatorKind &MainOpcode,
755 BinaryOperatorKind &SideOpcode,
756 const Expr *&LhsConst,
757 const Expr *&RhsConst,
758 const ASTContext *AstCtx) {
759 assert(areSidesBinaryConstExpressions(BinOp, AstCtx) &&
760 "Both sides of binary operator must be constant expressions!");
762 MainOpcode = BinOp->getOpcode();
764 const auto *BinOpLhs = cast<BinaryOperator>(BinOp->getLHS());
765 const auto *BinOpRhs = cast<BinaryOperator>(BinOp->getRHS());
767 auto IsIntegerConstantExpr = [AstCtx](
const Expr *
E) {
768 return !
E->isValueDependent() &&
E->isIntegerConstantExpr(*AstCtx);
771 LhsConst = IsIntegerConstantExpr(BinOpLhs->getLHS()) ? BinOpLhs->getLHS()
772 : BinOpLhs->getRHS();
773 RhsConst = IsIntegerConstantExpr(BinOpRhs->getLHS()) ? BinOpRhs->getLHS()
774 : BinOpRhs->getRHS();
776 if (!LhsConst || !RhsConst)
779 assert(BinOpLhs->getOpcode() == BinOpRhs->getOpcode() &&
780 "Sides of the binary operator must be equivalent expressions!");
782 SideOpcode = BinOpLhs->getOpcode();
787static bool isSameRawIdentifierToken(
const Token &T1,
const Token &T2,
788 const SourceManager &SM) {
789 if (T1.getKind() != T2.getKind())
791 if (T1.isNot(tok::raw_identifier))
793 if (T1.getLength() != T2.getLength())
795 return StringRef(SM.getCharacterData(T1.getLocation()), T1.getLength()) ==
796 StringRef(SM.getCharacterData(T2.getLocation()), T2.getLength());
799bool isTokAtEndOfExpr(SourceRange ExprSR, Token T,
const SourceManager &SM) {
800 return SM.getExpansionLoc(ExprSR.getEnd()) ==
T.getLocation();
806static bool areExprsFromDifferentMacros(
const Expr *LhsExpr,
808 const ASTContext *AstCtx) {
809 if (!LhsExpr || !RhsExpr)
811 SourceRange Lsr = LhsExpr->getSourceRange();
812 SourceRange Rsr = RhsExpr->getSourceRange();
813 if (!Lsr.getBegin().isMacroID() || !Rsr.getBegin().isMacroID())
816 const SourceManager &SM = AstCtx->getSourceManager();
817 const LangOptions &LO = AstCtx->getLangOpts();
819 std::pair<FileID, unsigned> LsrLocInfo =
820 SM.getDecomposedLoc(SM.getExpansionLoc(Lsr.getBegin()));
821 std::pair<FileID, unsigned> RsrLocInfo =
822 SM.getDecomposedLoc(SM.getExpansionLoc(Rsr.getBegin()));
823 llvm::MemoryBufferRef MB = SM.getBufferOrFake(LsrLocInfo.first);
825 const char *LTokenPos = MB.getBufferStart() + LsrLocInfo.second;
826 const char *RTokenPos = MB.getBufferStart() + RsrLocInfo.second;
827 Lexer LRawLex(SM.getLocForStartOfFile(LsrLocInfo.first), LO,
828 MB.getBufferStart(), LTokenPos, MB.getBufferEnd());
829 Lexer RRawLex(SM.getLocForStartOfFile(RsrLocInfo.first), LO,
830 MB.getBufferStart(), RTokenPos, MB.getBufferEnd());
834 LRawLex.LexFromRawLexer(LTok);
835 RRawLex.LexFromRawLexer(RTok);
836 }
while (!LTok.is(tok::eof) && !RTok.is(tok::eof) &&
837 isSameRawIdentifierToken(LTok, RTok, SM) &&
838 !isTokAtEndOfExpr(Lsr, LTok, SM) &&
839 !isTokAtEndOfExpr(Rsr, RTok, SM));
840 return (!isTokAtEndOfExpr(Lsr, LTok, SM) ||
841 !isTokAtEndOfExpr(Rsr, RTok, SM)) ||
842 !isSameRawIdentifierToken(LTok, RTok, SM);
845static bool areExprsMacroAndNonMacro(
const Expr *&LhsExpr,
846 const Expr *&RhsExpr) {
847 if (!LhsExpr || !RhsExpr)
850 SourceLocation LhsLoc = LhsExpr->getExprLoc();
851 SourceLocation RhsLoc = RhsExpr->getExprLoc();
853 return LhsLoc.isMacroID() != RhsLoc.isMacroID();
858 const auto BannedIntegerLiteral =
859 integerLiteral(expandedByMacro(KnownBannedMacroNames));
860 const auto IsInUnevaluatedContext = expr(anyOf(
861 hasAncestor(expr(hasUnevaluatedContext())), hasAncestor(typeLoc())));
866 binaryOperator(anyOf(isComparisonOperator(),
867 hasAnyOperatorName(
"-",
"/",
"%",
"|",
"&",
868 "^",
"&&",
"||",
"=")),
869 operandsAreEquivalent(),
871 unless(isInTemplateInstantiation()),
872 unless(binaryOperatorIsInMacro()),
873 unless(hasAncestor(arraySubscriptExpr())),
874 unless(hasDescendant(BannedIntegerLiteral)),
875 unless(IsInUnevaluatedContext))
882 binaryOperator(hasAnyOperatorName(
"|",
"&",
"||",
"&&",
"^"),
883 nestedOperandsAreEquivalent(),
885 unless(isInTemplateInstantiation()),
886 unless(binaryOperatorIsInMacro()),
888 unless(hasDescendant(BannedIntegerLiteral)),
889 unless(IsInUnevaluatedContext))
890 .bind(
"nested-duplicates"),
896 conditionalOperator(expressionsAreEquivalent(),
898 unless(conditionalOperatorIsInMacro()),
899 unless(isInTemplateInstantiation()),
900 unless(IsInUnevaluatedContext))
908 hasAnyOverloadedOperatorName(
"-",
"/",
"%",
"|",
"&",
"^",
909 "==",
"!=",
"<",
"<=",
">",
910 ">=",
"&&",
"||",
"="),
911 parametersAreEquivalent(),
913 unless(isMacro()), unless(isInTemplateInstantiation()),
914 unless(IsInUnevaluatedContext))
921 hasAnyOverloadedOperatorName(
"|",
"&",
"||",
"&&",
"^"),
922 nestedParametersAreEquivalent(), argumentCountIs(2),
924 unless(isMacro()), unless(isInTemplateInstantiation()),
925 unless(IsInUnevaluatedContext))
926 .bind(
"nested-duplicates"),
933 hasImplicitDestinationType(isInteger()),
935 hasOperatorName(
"!"),
936 hasUnaryOperand(ignoringParenImpCasts(binaryOperator(
937 hasAnyOperatorName(
"|",
"&"),
939 binaryOperator(hasAnyOperatorName(
"|",
"&")),
941 hasRHS(integerLiteral())))))
942 .bind(
"logical-bitwise-confusion")),
943 unless(IsInUnevaluatedContext))),
950 hasOperatorName(
"&"),
951 hasOperands(ignoringParenImpCasts(binaryOperator(
952 hasOperatorName(
"<<"),
953 hasRHS(ignoringParenImpCasts(
954 integerLiteral().bind(
"shift-const"))))),
955 ignoringParenImpCasts(
956 integerLiteral().bind(
"and-const"))),
957 unless(IsInUnevaluatedContext))
958 .bind(
"left-right-shift-confusion")),
967 const auto BinOpCstLeft = matchBinOpIntegerConstantExpr(
"lhs");
968 const auto BinOpCstRight = matchBinOpIntegerConstantExpr(
"rhs");
969 const auto CstRight = matchIntegerConstantExpr(
"rhs");
970 const auto SymRight = matchSymbolicExpr(
"rhs");
974 traverse(TK_AsIs, binaryOperator(isComparisonOperator(),
975 hasOperands(BinOpCstLeft, CstRight),
976 unless(IsInUnevaluatedContext))
977 .bind(
"binop-const-compare-to-const")),
984 binaryOperator(isComparisonOperator(),
985 anyOf(allOf(hasLHS(BinOpCstLeft), hasRHS(SymRight)),
986 allOf(hasLHS(SymRight), hasRHS(BinOpCstLeft))),
987 unless(IsInUnevaluatedContext))
988 .bind(
"binop-const-compare-to-sym")),
994 binaryOperator(isComparisonOperator(), hasLHS(BinOpCstLeft),
995 hasRHS(BinOpCstRight),
997 unless(operandsAreEquivalent()),
998 unless(IsInUnevaluatedContext))
999 .bind(
"binop-const-compare-to-binop-const")),
1007 const auto ComparisonLeft = matchRelationalIntegerConstantExpr(
"lhs");
1008 const auto ComparisonRight = matchRelationalIntegerConstantExpr(
"rhs");
1011 binaryOperator(hasAnyOperatorName(
"||",
"&&"),
1012 hasLHS(ComparisonLeft), hasRHS(ComparisonRight),
1014 unless(operandsAreEquivalent()),
1015 unless(IsInUnevaluatedContext))
1016 .bind(
"comparisons-of-symbol-and-const")),
1020void RedundantExpressionCheck::checkArithmeticExpr(
1021 const MatchFinder::MatchResult &Result) {
1022 APSInt LhsValue, RhsValue;
1023 const Expr *LhsSymbol =
nullptr, *RhsSymbol =
nullptr;
1024 BinaryOperatorKind LhsOpcode{}, RhsOpcode{};
1026 if (
const auto *ComparisonOperator = Result.Nodes.getNodeAs<BinaryOperator>(
1027 "binop-const-compare-to-sym")) {
1028 BinaryOperatorKind Opcode = ComparisonOperator->getOpcode();
1029 if (!retrieveBinOpIntegerConstantExpr(Result,
"lhs", LhsOpcode, LhsSymbol,
1031 !retrieveSymbolicExpr(Result,
"rhs", RhsSymbol) ||
1032 !areEquivalentExpr(LhsSymbol, RhsSymbol))
1036 if (LhsOpcode == BO_Add || LhsOpcode == BO_Sub) {
1037 if ((LhsValue != 0 && Opcode == BO_EQ) ||
1038 (LhsValue == 0 && Opcode == BO_NE))
1039 diag(ComparisonOperator->getOperatorLoc(),
1040 "logical expression is always false");
1041 else if ((LhsValue == 0 && Opcode == BO_EQ) ||
1042 (LhsValue != 0 && Opcode == BO_NE))
1043 diag(ComparisonOperator->getOperatorLoc(),
1044 "logical expression is always true");
1046 }
else if (
const auto *ComparisonOperator =
1047 Result.Nodes.getNodeAs<BinaryOperator>(
1048 "binop-const-compare-to-binop-const")) {
1049 BinaryOperatorKind Opcode = ComparisonOperator->getOpcode();
1051 if (!retrieveBinOpIntegerConstantExpr(Result,
"lhs", LhsOpcode, LhsSymbol,
1053 !retrieveBinOpIntegerConstantExpr(Result,
"rhs", RhsOpcode, RhsSymbol,
1055 !areEquivalentExpr(LhsSymbol, RhsSymbol))
1058 transformSubToCanonicalAddExpr(LhsOpcode, LhsValue);
1059 transformSubToCanonicalAddExpr(RhsOpcode, RhsValue);
1062 if (LhsOpcode == BO_Add && RhsOpcode == BO_Add) {
1063 if ((Opcode == BO_EQ && APSInt::compareValues(LhsValue, RhsValue) == 0) ||
1064 (Opcode == BO_NE && APSInt::compareValues(LhsValue, RhsValue) != 0)) {
1065 diag(ComparisonOperator->getOperatorLoc(),
1066 "logical expression is always true");
1067 }
else if ((Opcode == BO_EQ &&
1068 APSInt::compareValues(LhsValue, RhsValue) != 0) ||
1070 APSInt::compareValues(LhsValue, RhsValue) == 0)) {
1071 diag(ComparisonOperator->getOperatorLoc(),
1072 "logical expression is always false");
1079 return (Opcode == BO_And || Opcode == BO_AndAssign) &&
Value == 0;
1084 return (Opcode == BO_Or || Opcode == BO_OrAssign) && ~Value == 0;
1088 return ((Opcode == BO_Or || Opcode == BO_OrAssign) &&
Value == 0) ||
1089 ((Opcode == BO_And || Opcode == BO_AndAssign) && ~
Value == 0);
1093void RedundantExpressionCheck::checkBitwiseExpr(
1094 const MatchFinder::MatchResult &Result) {
1095 if (
const auto *ComparisonOperator = Result.Nodes.getNodeAs<BinaryOperator>(
1096 "binop-const-compare-to-const")) {
1097 BinaryOperatorKind Opcode = ComparisonOperator->getOpcode();
1099 APSInt LhsValue, RhsValue;
1100 const Expr *LhsSymbol =
nullptr;
1101 BinaryOperatorKind LhsOpcode{};
1102 if (!retrieveBinOpIntegerConstantExpr(Result,
"lhs", LhsOpcode, LhsSymbol,
1104 !retrieveIntegerConstantExpr(Result,
"rhs", RhsValue))
1107 uint64_t LhsConstant = LhsValue.getZExtValue();
1108 uint64_t RhsConstant = RhsValue.getZExtValue();
1109 SourceLocation
Loc = ComparisonOperator->getOperatorLoc();
1112 if (LhsOpcode == BO_And && (LhsConstant & RhsConstant) != RhsConstant) {
1113 if (Opcode == BO_EQ)
1114 diag(
Loc,
"logical expression is always false");
1115 else if (Opcode == BO_NE)
1116 diag(
Loc,
"logical expression is always true");
1120 if (LhsOpcode == BO_Or && (LhsConstant | RhsConstant) != RhsConstant) {
1121 if (Opcode == BO_EQ)
1122 diag(
Loc,
"logical expression is always false");
1123 else if (Opcode == BO_NE)
1124 diag(
Loc,
"logical expression is always true");
1126 }
else if (
const auto *IneffectiveOperator =
1127 Result.Nodes.getNodeAs<BinaryOperator>(
1128 "ineffective-bitwise")) {
1130 const Expr *Sym =
nullptr, *ConstExpr =
nullptr;
1132 if (!retrieveSymbolicExpr(Result,
"ineffective-bitwise", Sym) ||
1133 !retrieveIntegerConstantExpr(Result,
"ineffective-bitwise",
Value,
1137 if((
Value != 0 && ~
Value != 0) || Sym->getExprLoc().isMacroID())
1140 SourceLocation
Loc = IneffectiveOperator->getOperatorLoc();
1142 BinaryOperatorKind Opcode = IneffectiveOperator->getOpcode();
1144 diag(
Loc,
"expression always evaluates to 0");
1146 SourceRange ConstExprRange(ConstExpr->getBeginLoc(),
1147 ConstExpr->getEndLoc());
1148 StringRef ConstExprText = Lexer::getSourceText(
1149 CharSourceRange::getTokenRange(ConstExprRange), *Result.SourceManager,
1150 Result.Context->getLangOpts());
1152 diag(
Loc,
"expression always evaluates to '%0'") << ConstExprText;
1155 SourceRange SymExprRange(Sym->getBeginLoc(), Sym->getEndLoc());
1157 StringRef ExprText = Lexer::getSourceText(
1158 CharSourceRange::getTokenRange(SymExprRange), *Result.SourceManager,
1159 Result.Context->getLangOpts());
1161 diag(
Loc,
"expression always evaluates to '%0'") << ExprText;
1166void RedundantExpressionCheck::checkRelationalExpr(
1167 const MatchFinder::MatchResult &Result) {
1168 if (
const auto *ComparisonOperator = Result.Nodes.getNodeAs<BinaryOperator>(
1169 "comparisons-of-symbol-and-const")) {
1172 BinaryOperatorKind Opcode = ComparisonOperator->getOpcode();
1174 const Expr *LhsExpr =
nullptr, *RhsExpr =
nullptr;
1175 const Expr *LhsSymbol =
nullptr, *RhsSymbol =
nullptr;
1176 const Expr *LhsConst =
nullptr, *RhsConst =
nullptr;
1177 BinaryOperatorKind LhsOpcode{}, RhsOpcode{};
1178 APSInt LhsValue, RhsValue;
1180 if (!retrieveRelationalIntegerConstantExpr(
1181 Result,
"lhs", LhsExpr, LhsOpcode, LhsSymbol, LhsValue, LhsConst) ||
1182 !retrieveRelationalIntegerConstantExpr(
1183 Result,
"rhs", RhsExpr, RhsOpcode, RhsSymbol, RhsValue, RhsConst) ||
1184 !areEquivalentExpr(LhsSymbol, RhsSymbol))
1188 if (APSInt::compareValues(LhsValue, RhsValue) > 0) {
1189 std::swap(LhsExpr, RhsExpr);
1190 std::swap(LhsValue, RhsValue);
1191 std::swap(LhsSymbol, RhsSymbol);
1192 std::swap(LhsOpcode, RhsOpcode);
1196 if (areExprsFromDifferentMacros(LhsConst, RhsConst, Result.Context) ||
1197 areExprsMacroAndNonMacro(LhsConst, RhsConst))
1200 if ((Opcode == BO_LAnd || Opcode == BO_LOr) &&
1201 areEquivalentRanges(LhsOpcode, LhsValue, RhsOpcode, RhsValue)) {
1202 diag(ComparisonOperator->getOperatorLoc(),
1203 "equivalent expression on both sides of logical operator");
1207 if (Opcode == BO_LAnd) {
1208 if (areExclusiveRanges(LhsOpcode, LhsValue, RhsOpcode, RhsValue)) {
1209 diag(ComparisonOperator->getOperatorLoc(),
1210 "logical expression is always false");
1211 }
else if (rangeSubsumesRange(LhsOpcode, LhsValue, RhsOpcode, RhsValue)) {
1212 diag(LhsExpr->getExprLoc(),
"expression is redundant");
1213 }
else if (rangeSubsumesRange(RhsOpcode, RhsValue, LhsOpcode, LhsValue)) {
1214 diag(RhsExpr->getExprLoc(),
"expression is redundant");
1218 if (Opcode == BO_LOr) {
1219 if (rangesFullyCoverDomain(LhsOpcode, LhsValue, RhsOpcode, RhsValue)) {
1220 diag(ComparisonOperator->getOperatorLoc(),
1221 "logical expression is always true");
1222 }
else if (rangeSubsumesRange(LhsOpcode, LhsValue, RhsOpcode, RhsValue)) {
1223 diag(RhsExpr->getExprLoc(),
"expression is redundant");
1224 }
else if (rangeSubsumesRange(RhsOpcode, RhsValue, LhsOpcode, LhsValue)) {
1225 diag(LhsExpr->getExprLoc(),
"expression is redundant");
1232 if (
const auto *BinOp = Result.Nodes.getNodeAs<BinaryOperator>(
"binary")) {
1256 const Expr *LHS = BinOp->getLHS()->IgnoreParenImpCasts();
1257 const Expr *RHS = BinOp->getRHS()->IgnoreParenImpCasts();
1258 const BinaryOperator::Opcode Op = BinOp->getOpcode();
1259 const bool OpEqualEQorNE = ((Op == BO_EQ) || (Op == BO_NE));
1261 const auto *DeclRef1 = dyn_cast<DeclRefExpr>(LHS);
1262 const auto *DeclRef2 = dyn_cast<DeclRefExpr>(RHS);
1263 const auto *FloatLit1 = dyn_cast<FloatingLiteral>(LHS);
1264 const auto *FloatLit2 = dyn_cast<FloatingLiteral>(RHS);
1266 if (DeclRef1 && DeclRef2 &&
1267 DeclRef1->getType()->hasFloatingRepresentation() &&
1268 DeclRef2->getType()->hasFloatingRepresentation() &&
1269 (DeclRef1->getDecl() == DeclRef2->getDecl()) && OpEqualEQorNE) {
1273 if (FloatLit1 && FloatLit2 &&
1274 FloatLit1->getValue().bitwiseIsEqual(FloatLit2->getValue()) &&
1279 if (areSidesBinaryConstExpressions(BinOp, Result.Context)) {
1280 const Expr *LhsConst =
nullptr, *RhsConst =
nullptr;
1281 BinaryOperatorKind MainOpcode{}, SideOpcode{};
1283 if (!retrieveConstExprFromBothSides(BinOp, MainOpcode, SideOpcode,
1284 LhsConst, RhsConst, Result.Context))
1287 if (areExprsFromDifferentMacros(LhsConst, RhsConst, Result.Context) ||
1288 areExprsMacroAndNonMacro(LhsConst, RhsConst))
1292 diag(BinOp->getOperatorLoc(),
"both sides of operator are equivalent");
1295 if (
const auto *CondOp =
1296 Result.Nodes.getNodeAs<ConditionalOperator>(
"cond")) {
1297 const Expr *TrueExpr = CondOp->getTrueExpr();
1298 const Expr *FalseExpr = CondOp->getFalseExpr();
1300 if (areExprsFromDifferentMacros(TrueExpr, FalseExpr, Result.Context) ||
1301 areExprsMacroAndNonMacro(TrueExpr, FalseExpr))
1303 diag(CondOp->getColonLoc(),
1304 "'true' and 'false' expressions are equivalent");
1307 if (
const auto *Call = Result.Nodes.getNodeAs<CXXOperatorCallExpr>(
"call")) {
1308 if (canOverloadedOperatorArgsBeModified(Call,
true))
1311 diag(Call->getOperatorLoc(),
1312 "both sides of overloaded operator are equivalent");
1315 if (
const auto *Op = Result.Nodes.getNodeAs<Expr>(
"nested-duplicates")) {
1316 const auto *Call = dyn_cast<CXXOperatorCallExpr>(Op);
1317 if (Call && canOverloadedOperatorArgsBeModified(Call,
true))
1321 Call ?
"overloaded operator has equivalent nested operands"
1322 :
"operator has equivalent nested operands";
1324 const auto Diag =
diag(Op->getExprLoc(),
Message);
1325 for (
const auto &KeyValue : Result.Nodes.getMap()) {
1326 if (StringRef(KeyValue.first).starts_with(
"duplicate"))
1327 Diag << KeyValue.second.getSourceRange();
1331 if (
const auto *NegateOperator =
1332 Result.Nodes.getNodeAs<UnaryOperator>(
"logical-bitwise-confusion")) {
1333 SourceLocation OperatorLoc = NegateOperator->getOperatorLoc();
1337 "ineffective logical negation operator used; did you mean '~'?");
1338 SourceLocation LogicalNotLocation = OperatorLoc.getLocWithOffset(1);
1340 if (!LogicalNotLocation.isMacroID())
1341 Diag << FixItHint::CreateReplacement(
1342 CharSourceRange::getCharRange(OperatorLoc, LogicalNotLocation),
"~");
1345 if (
const auto *BinaryAndExpr = Result.Nodes.getNodeAs<BinaryOperator>(
1346 "left-right-shift-confusion")) {
1347 const auto *ShiftingConst = Result.Nodes.getNodeAs<Expr>(
"shift-const");
1348 assert(ShiftingConst &&
"Expr* 'ShiftingConst' is nullptr!");
1349 std::optional<llvm::APSInt> ShiftingValue =
1350 ShiftingConst->getIntegerConstantExpr(*Result.Context);
1355 const auto *AndConst = Result.Nodes.getNodeAs<Expr>(
"and-const");
1356 assert(AndConst &&
"Expr* 'AndCont' is nullptr!");
1357 std::optional<llvm::APSInt> AndValue =
1358 AndConst->getIntegerConstantExpr(*Result.Context);
1365 if (AndValue->getActiveBits() > *ShiftingValue)
1368 auto Diag =
diag(BinaryAndExpr->getOperatorLoc(),
1369 "ineffective bitwise and operation");
1377 checkArithmeticExpr(Result);
1385 checkBitwiseExpr(Result);
1393 checkRelationalExpr(Result);
llvm::SmallVector< const Expr *, 32U > Operands
CodeCompletionBuilder Builder
const clang::Expr * TheExpr
The Expr object that represents the closest evaluable expression.
::clang::DynTypedNode Node
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
AST_MATCHER(Expr, isMacroID)
AST_MATCHER_P(UserDefinedLiteral, hasLiteral, clang::ast_matchers::internal::Matcher< Expr >, InnerMatcher)
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)