10#include "clang/AST/RecursiveASTVisitor.h"
11#include "clang/Lex/Lexer.h"
12#include "llvm/Support/SaveAndRestore.h"
24StringRef getText(
const ASTContext &Context, SourceRange Range) {
25 return Lexer::getSourceText(CharSourceRange::getTokenRange(Range),
26 Context.getSourceManager(),
27 Context.getLangOpts());
30template <
typename T> StringRef getText(
const ASTContext &Context, T &Node) {
31 return getText(Context,
Node.getSourceRange());
37 "redundant boolean literal supplied to boolean operator";
39 "redundant boolean literal in if statement condition";
41 "redundant boolean literal in conditional return statement";
44 E =
E->IgnoreImpCasts();
45 if (isa<BinaryOperator>(
E) || isa<ConditionalOperator>(
E))
48 if (
const auto *Op = dyn_cast<CXXOperatorCallExpr>(
E))
49 return Op->getNumArgs() == 2 && Op->getOperator() != OO_Call &&
50 Op->getOperator() != OO_Subscript;
55static std::pair<BinaryOperatorKind, BinaryOperatorKind>
Opposites[] = {
56 {BO_LT, BO_GE}, {BO_GT, BO_LE}, {BO_EQ, BO_NE}};
59 const BinaryOperatorKind Opcode = BinOp->getOpcode();
61 if (Opcode == NegatableOp.first)
62 return BinaryOperator::getOpcodeStr(NegatableOp.second);
63 if (Opcode == NegatableOp.second)
64 return BinaryOperator::getOpcodeStr(NegatableOp.first);
70 {OO_EqualEqual,
"=="}, {OO_ExclaimEqual,
"!="}, {OO_Less,
"<"},
71 {OO_GreaterEqual,
">="}, {OO_Greater,
">"}, {OO_LessEqual,
"<="}};
75 if (
Name.first == OpKind)
82static std::pair<OverloadedOperatorKind, OverloadedOperatorKind>
84 {OO_Less, OO_GreaterEqual},
85 {OO_Greater, OO_LessEqual}};
88 const OverloadedOperatorKind Opcode = OpCall->getOperator();
90 if (Opcode == NegatableOp.first)
92 if (Opcode == NegatableOp.second)
98static std::string
asBool(StringRef
Text,
bool NeedsStaticCast) {
100 return (
"static_cast<bool>(" +
Text +
")").str();
102 return std::string(
Text);
106 if (
const auto *ImpCast = dyn_cast<ImplicitCastExpr>(
E))
107 return ImpCast->getCastKind() == CK_PointerToBoolean ||
108 ImpCast->getCastKind() == CK_MemberPointerToBoolean;
114 if (
const auto *ImpCast = dyn_cast<ImplicitCastExpr>(
E))
115 return ImpCast->getCastKind() == CK_IntegralToBoolean;
121 if (
const auto *ImpCast = dyn_cast<ImplicitCastExpr>(
E)) {
122 if (ImpCast->getCastKind() == CK_UserDefinedConversion &&
123 ImpCast->getSubExpr()->getType()->isBooleanType()) {
124 if (
const auto *MemCall =
125 dyn_cast<CXXMemberCallExpr>(ImpCast->getSubExpr())) {
126 if (
const auto *MemDecl =
127 dyn_cast<CXXConversionDecl>(MemCall->getMethodDecl())) {
128 if (MemDecl->isExplicit())
135 E =
E->IgnoreImpCasts();
136 return !
E->getType()->isBooleanType();
140 const Expr *
E,
bool Negated,
141 const char *Constant) {
142 E =
E->IgnoreImpCasts();
143 const std::string ExprText =
144 (isa<BinaryOperator>(
E) ? (
"(" + getText(Context, *
E) +
")")
145 : getText(Context, *
E))
147 return ExprText +
" " + (Negated ?
"!=" :
"==") +
" " + Constant;
151 const Expr *
E,
bool Negated) {
152 const char *NullPtr = Context.getLangOpts().CPlusPlus11 ?
"nullptr" :
"NULL";
157 const Expr *
E,
bool Negated) {
162 bool Negated,
const Expr *
E) {
163 E =
E->IgnoreParenBaseCasts();
164 if (
const auto *EC = dyn_cast<ExprWithCleanups>(
E))
165 E = EC->getSubExpr();
169 if (
const auto *UnOp = dyn_cast<UnaryOperator>(
E)) {
170 if (UnOp->getOpcode() == UO_LNot) {
187 StringRef NegatedOperator;
188 const Expr *LHS =
nullptr;
189 const Expr *RHS =
nullptr;
190 if (
const auto *BinOp = dyn_cast<BinaryOperator>(
E)) {
192 LHS = BinOp->getLHS();
193 RHS = BinOp->getRHS();
194 }
else if (
const auto *OpExpr = dyn_cast<CXXOperatorCallExpr>(
E)) {
195 if (OpExpr->getNumArgs() == 2) {
197 LHS = OpExpr->getArg(0);
198 RHS = OpExpr->getArg(1);
201 if (!NegatedOperator.empty() && LHS && RHS)
202 return (
asBool((getText(Context, *LHS) +
" " + NegatedOperator +
" " +
203 getText(Context, *RHS))
207 StringRef
Text = getText(Context, *
E);
209 return (
"!(" +
Text +
")").str();
220 if (
const auto *UnOp = dyn_cast<UnaryOperator>(
E)) {
221 if (UnOp->getOpcode() == UO_LNot) {
236 return asBool(getText(Context, *
E), NeedsStaticCast);
240 CharSourceRange CharRange) {
241 std::string ReplacementText =
242 Lexer::getSourceText(CharRange, Context.getSourceManager(),
243 Context.getLangOpts())
245 Lexer Lex(CharRange.getBegin(), Context.getLangOpts(), ReplacementText.data(),
246 ReplacementText.data(),
247 ReplacementText.data() + ReplacementText.size());
248 Lex.SetCommentRetentionState(
true);
251 while (!Lex.LexFromRawLexer(Tok)) {
252 if (Tok.is(tok::TokenKind::comment) || Tok.is(tok::TokenKind::hash))
260 using Base = RecursiveASTVisitor<Visitor>;
264 : Check(Check), Context(Context) {}
269 switch (S->getStmtClass()) {
270 case Stmt::ImplicitCastExprClass:
271 case Stmt::MaterializeTemporaryExprClass:
272 case Stmt::CXXBindTemporaryExprClass:
281 StmtStack.push_back(S);
287 assert(StmtStack.back() == S);
288 StmtStack.pop_back();
294 Check->reportBinOp(Context, Op);
300 if (
const auto *Bool = dyn_cast<CXXBoolLiteralExpr>(
E)) {
301 if (FilterMacro && Bool->getBeginLoc().isMacroID())
303 return Bool->getValue();
305 if (
const auto *UnaryOp = dyn_cast<UnaryOperator>(
E)) {
306 if (FilterMacro && UnaryOp->getBeginLoc().isMacroID())
308 if (UnaryOp->getOpcode() == UO_LNot)
310 UnaryOp->getSubExpr()->IgnoreImplicit(), FilterMacro))
320 operator bool()
const {
return Item !=
nullptr; }
328 const auto *RS = dyn_cast<ReturnStmt>(S);
329 if (!RS || !RS->getRetValue())
331 if (std::optional<bool> Ret =
333 return {RS->getRetValue(), *Ret};
341 template <
typename Functor>
343 if (
auto *CS = dyn_cast<CompoundStmt>(S)) {
345 return F(CS->body_front());
352 return StmtStack.size() < 2 ? nullptr : StmtStack[StmtStack.size() - 2];
358 if (If->hasInitStorage() || If->hasVarStorage() || If->isConsteval())
365 Expr *Cond = If->getCond()->IgnoreImplicit();
368 Check->replaceWithThenStatement(Context, If, Cond);
370 Check->replaceWithElseStatement(Context, If, Cond);
382 if (ElseReturnBool && ThenReturnBool.
Bool != ElseReturnBool.
Bool) {
383 if (Check->ChainedConditionalReturn ||
384 !isa_and_nonnull<IfStmt>(
parent())) {
385 Check->replaceWithReturnCondition(Context, If, ThenReturnBool.Item,
386 ElseReturnBool.
Bool);
396 auto VarBoolAssignmentMatcher = [&Var,
398 const auto *BO = dyn_cast<BinaryOperator>(S);
399 if (!BO || BO->getOpcode() != BO_Assign)
401 std::optional<bool> RightasBool =
405 Expr *IgnImp = BO->getLHS()->IgnoreImplicit();
408 Loc = BO->getRHS()->getBeginLoc();
411 if (
auto *DRE = dyn_cast<DeclRefExpr>(IgnImp))
412 return {DRE->getDecl(), *RightasBool};
413 if (
auto *ME = dyn_cast<MemberExpr>(IgnImp))
414 return {ME->getMemberDecl(), *RightasBool};
421 if (ElseAssignment.
Item == ThenAssignment.Item &&
422 ElseAssignment.
Bool != ThenAssignment.Bool) {
423 if (Check->ChainedConditionalAssignment ||
424 !isa_and_nonnull<IfStmt>(
parent())) {
425 Check->replaceWithAssignment(Context, If, Var,
Loc,
426 ElseAssignment.
Bool);
440 if (std::optional<bool> Then =
442 if (std::optional<bool> Else =
445 Check->replaceWithCondition(Context, Cond, *Else);
454 bool CurIf =
false, PrevIf =
false;
455 for (
auto First = CS->body_begin(), Second = std::next(First),
456 End = CS->body_end();
457 Second != End; ++Second, ++First) {
459 CurIf = isa<IfStmt>(*First);
461 if (!TrailingReturnBool)
469 auto *If = cast<IfStmt>(*First);
470 if (!If->hasInitStorage() && !If->hasVarStorage() &&
471 !If->isConsteval()) {
474 if (ThenReturnBool &&
475 ThenReturnBool.
Bool != TrailingReturnBool.
Bool) {
476 if ((Check->ChainedConditionalReturn || !PrevIf) &&
477 If->getElse() ==
nullptr) {
478 Check->replaceCompoundReturnWithCondition(
479 Context, cast<ReturnStmt>(*Second), TrailingReturnBool.
Bool,
480 If, ThenReturnBool.
Item);
484 }
else if (isa<LabelStmt, CaseStmt, DefaultStmt>(*First)) {
490 isa<LabelStmt>(*First) ? cast<LabelStmt>(*First)->getSubStmt()
491 : isa<CaseStmt>(*First) ? cast<CaseStmt>(*First)->getSubStmt()
492 : cast<DefaultStmt>(*First)->getSubStmt();
493 auto *SubIf = dyn_cast<IfStmt>(SubStmt);
494 if (SubIf && !SubIf->getElse() && !SubIf->hasInitStorage() &&
495 !SubIf->hasVarStorage() && !SubIf->isConsteval()) {
498 if (ThenReturnBool &&
499 ThenReturnBool.
Bool != TrailingReturnBool.
Bool) {
500 Check->replaceCompoundReturnWithCondition(
501 Context, cast<ReturnStmt>(*Second), TrailingReturnBool.
Bool,
502 SubIf, ThenReturnBool.
Item);
511 return isa<UnaryOperator>(
E) &&
512 cast<UnaryOperator>(
E)->getOpcode() == UO_LNot;
515 template <
typename Functor>
517 return Func(BO->getLHS()) || Func(BO->getRHS());
521 const auto *BO = dyn_cast<BinaryOperator>(
E->IgnoreUnlessSpelledInSource());
524 if (!BO->getType()->isBooleanType())
526 switch (BO->getOpcode()) {
551 if (!Check->SimplifyDeMorgan || Op->getOpcode() != UO_LNot)
552 return Base::TraverseUnaryOperator(Op);
553 Expr *SubImp = Op->getSubExpr()->IgnoreImplicit();
554 auto *Parens = dyn_cast<ParenExpr>(SubImp);
557 ? dyn_cast<BinaryOperator>(Parens->getSubExpr()->IgnoreImplicit())
558 : dyn_cast<BinaryOperator>(SubImp);
559 if (!BinaryOp || !BinaryOp->isLogicalOp() ||
560 !BinaryOp->getType()->isBooleanType())
561 return Base::TraverseUnaryOperator(Op);
562 if (Check->SimplifyDeMorganRelaxed ||
566 if (Check->reportDeMorgan(Context, Op, BinaryOp, !IsProcessing,
parent(),
569 llvm::SaveAndRestore RAII(IsProcessing,
true);
570 return Base::TraverseUnaryOperator(Op);
573 return Base::TraverseUnaryOperator(Op);
577 bool IsProcessing =
false;
579 SmallVector<Stmt *, 32> StmtStack;
586 ChainedConditionalReturn(Options.get(
"ChainedConditionalReturn", false)),
587 ChainedConditionalAssignment(
588 Options.get(
"ChainedConditionalAssignment", false)),
589 SimplifyDeMorgan(Options.get(
"SimplifyDeMorgan", true)),
590 SimplifyDeMorganRelaxed(Options.get(
"SimplifyDeMorganRelaxed", false)) {
591 if (SimplifyDeMorganRelaxed && !SimplifyDeMorgan)
593 "without 'SimplifyDeMorgan' enabled")
600 E =
E->IgnoreParenImpCasts();
601 if (isa<CXXBoolLiteralExpr>(
E))
603 if (
const auto *BinOp = dyn_cast<BinaryOperator>(
E))
606 if (
const auto *UnaryOp = dyn_cast<UnaryOperator>(
E))
611void SimplifyBooleanExprCheck::reportBinOp(
const ASTContext &Context,
612 const BinaryOperator *Op) {
613 const auto *LHS = Op->getLHS()->IgnoreParenImpCasts();
614 const auto *RHS = Op->getRHS()->IgnoreParenImpCasts();
616 const CXXBoolLiteralExpr *Bool =
nullptr;
617 const Expr *Other =
nullptr;
618 if ((Bool = dyn_cast<CXXBoolLiteralExpr>(LHS)) !=
nullptr)
620 else if ((Bool = dyn_cast<CXXBoolLiteralExpr>(RHS)) !=
nullptr)
625 if (Bool->getBeginLoc().isMacroID())
632 bool BoolValue = Bool->getValue();
634 auto ReplaceWithExpression = [
this, &Context, LHS, RHS,
635 Bool](
const Expr *ReplaceWith,
bool Negated) {
636 std::string Replacement =
638 SourceRange
Range(LHS->getBeginLoc(), RHS->getEndLoc());
643 switch (Op->getOpcode()) {
647 ReplaceWithExpression(Other,
false);
650 ReplaceWithExpression(Bool,
false);
655 ReplaceWithExpression(Bool,
false);
658 ReplaceWithExpression(Other,
false);
662 ReplaceWithExpression(Other, !BoolValue);
666 ReplaceWithExpression(Other, BoolValue);
674 Options.
store(Opts,
"ChainedConditionalReturn", ChainedConditionalReturn);
676 ChainedConditionalAssignment);
677 Options.
store(Opts,
"SimplifyDeMorgan", SimplifyDeMorgan);
678 Options.
store(Opts,
"SimplifyDeMorganRelaxed", SimplifyDeMorganRelaxed);
682 Finder->addMatcher(translationUnitDecl(),
this);
689void SimplifyBooleanExprCheck::issueDiag(
const ASTContext &Context,
691 StringRef Description,
692 SourceRange ReplacementRange,
693 StringRef Replacement) {
694 CharSourceRange CharRange =
695 Lexer::makeFileCharRange(CharSourceRange::getTokenRange(ReplacementRange),
698 DiagnosticBuilder Diag =
diag(
Loc, Description);
700 Diag << FixItHint::CreateReplacement(CharRange, Replacement);
703void SimplifyBooleanExprCheck::replaceWithThenStatement(
704 const ASTContext &Context,
const IfStmt *IfStatement,
705 const Expr *BoolLiteral) {
707 IfStatement->getSourceRange(),
708 getText(Context, *IfStatement->getThen()));
711void SimplifyBooleanExprCheck::replaceWithElseStatement(
712 const ASTContext &Context,
const IfStmt *IfStatement,
713 const Expr *BoolLiteral) {
714 const Stmt *ElseStatement = IfStatement->getElse();
716 IfStatement->getSourceRange(),
717 ElseStatement ? getText(Context, *ElseStatement) :
"");
720void SimplifyBooleanExprCheck::replaceWithCondition(
721 const ASTContext &Context,
const ConditionalOperator *Ternary,
723 std::string Replacement =
725 issueDiag(Context, Ternary->getTrueExpr()->getBeginLoc(),
726 "redundant boolean literal in ternary expression result",
727 Ternary->getSourceRange(), Replacement);
730void SimplifyBooleanExprCheck::replaceWithReturnCondition(
731 const ASTContext &Context,
const IfStmt *If,
const Expr *BoolLiteral,
733 StringRef Terminator = isa<CompoundStmt>(If->getElse()) ?
";" :
"";
736 std::string Replacement = (
"return " +
Condition + Terminator).str();
737 SourceLocation Start = BoolLiteral->getBeginLoc();
739 If->getSourceRange(), Replacement);
742void SimplifyBooleanExprCheck::replaceCompoundReturnWithCondition(
743 const ASTContext &Context,
const ReturnStmt *Ret,
bool Negated,
744 const IfStmt *If,
const Expr *ThenReturn) {
745 const std::string Replacement =
747 issueDiag(Context, ThenReturn->getBeginLoc(),
749 SourceRange(If->getBeginLoc(), Ret->getEndLoc()), Replacement);
752void SimplifyBooleanExprCheck::replaceWithAssignment(
const ASTContext &Context,
753 const IfStmt *IfAssign,
757 SourceRange
Range = IfAssign->getSourceRange();
758 StringRef VariableName = getText(Context, *Var);
759 StringRef Terminator = isa<CompoundStmt>(IfAssign->getElse()) ?
";" :
"";
762 std::string Replacement =
763 (VariableName +
" = " +
Condition + Terminator).str();
764 issueDiag(Context,
Loc,
"redundant boolean literal in conditional assignment",
770 const BinaryOperator *BO) {
771 assert(BO->isLogicalOp());
772 if (BO->getOperatorLoc().isMacroID())
774 Output.push_back(FixItHint::CreateReplacement(
775 BO->getOperatorLoc(), BO->getOpcode() == BO_LAnd ?
"||" :
"&&"));
780 assert(BinaryOperator::isLogicalOp(BO));
781 return BO == BO_LAnd ? BO_LOr : BO_LAnd;
785 const ASTContext &Ctx,
const Expr *
E,
786 std::optional<BinaryOperatorKind> OuterBO);
793 const ASTContext &Ctx,
const BinaryOperator *BinOp,
794 std::optional<BinaryOperatorKind> OuterBO,
795 const ParenExpr *Parens =
nullptr) {
796 switch (BinOp->getOpcode()) {
810 constexpr bool LogicalOpParentheses =
true;
811 if (((*OuterBO == NewOp) || (!LogicalOpParentheses &&
812 (*OuterBO == BO_LOr && NewOp == BO_LAnd))) &&
814 if (!Parens->getLParen().isMacroID() &&
815 !Parens->getRParen().isMacroID()) {
816 Fixes.push_back(FixItHint::CreateRemoval(Parens->getLParen()));
817 Fixes.push_back(FixItHint::CreateRemoval(Parens->getRParen()));
820 if (*OuterBO == BO_LAnd && NewOp == BO_LOr && !Parens) {
821 Fixes.push_back(FixItHint::CreateInsertion(BinOp->getBeginLoc(),
"("));
822 Fixes.push_back(FixItHint::CreateInsertion(
823 Lexer::getLocForEndOfToken(BinOp->getEndLoc(), 0,
824 Ctx.getSourceManager(),
841 if (BinOp->getOperatorLoc().isMacroID())
843 Fixes.push_back(FixItHint::CreateReplacement(
844 BinOp->getOperatorLoc(),
845 BinaryOperator::getOpcodeStr(
846 BinaryOperator::negateComparisonOp(BinOp->getOpcode()))));
852 if (Parens->getBeginLoc().isMacroID())
854 Fixes.push_back(FixItHint::CreateInsertion(Parens->getBeginLoc(),
"!"));
856 if (BinOp->getBeginLoc().isMacroID() || BinOp->getEndLoc().isMacroID())
858 Fixes.append({FixItHint::CreateInsertion(BinOp->getBeginLoc(),
"!("),
859 FixItHint::CreateInsertion(
860 Lexer::getLocForEndOfToken(BinOp->getEndLoc(), 0,
861 Ctx.getSourceManager(),
871 const ASTContext &Ctx,
const Expr *
E,
872 std::optional<BinaryOperatorKind> OuterBO) {
873 if (isa<UnaryOperator>(
E) && cast<UnaryOperator>(
E)->getOpcode() == UO_LNot) {
875 if (cast<UnaryOperator>(
E)->getOperatorLoc().
isMacroID())
878 FixItHint::CreateRemoval(cast<UnaryOperator>(
E)->getOperatorLoc()));
881 if (
const auto *BinOp = dyn_cast<BinaryOperator>(
E)) {
884 if (
const auto *Paren = dyn_cast<ParenExpr>(
E)) {
885 if (
const auto *BinOp = dyn_cast<BinaryOperator>(Paren->getSubExpr())) {
890 if (
E->getBeginLoc().isMacroID())
892 Fixes.push_back(FixItHint::CreateInsertion(
E->getBeginLoc(),
"!"));
897 BinaryOperatorKind NewOuterBinary,
898 const ParenExpr *Parens) {
903 switch (
Parent->getStmtClass()) {
904 case Stmt::BinaryOperatorClass: {
905 const auto *BO = cast<BinaryOperator>(
Parent);
906 if (BO->isAssignmentOp())
910 if (BO->getOpcode() == NewOuterBinary)
914 case Stmt::UnaryOperatorClass:
915 case Stmt::CXXRewrittenBinaryOperatorClass:
922bool SimplifyBooleanExprCheck::reportDeMorgan(
const ASTContext &Context,
923 const UnaryOperator *Outer,
924 const BinaryOperator *
Inner,
927 const ParenExpr *Parens) {
930 assert(
Inner->isLogicalOp());
933 diag(Outer->getBeginLoc(),
934 "boolean expression can be simplified by DeMorgan's theorem");
935 Diag << Outer->getSourceRange();
939 if (Outer->getOperatorLoc().isMacroID())
941 SmallVector<FixItHint> Fixes;
944 Fixes.push_back(FixItHint::CreateRemoval(
945 SourceRange(Outer->getOperatorLoc(), Parens->getLParen())));
946 Fixes.push_back(FixItHint::CreateRemoval(Parens->getRParen()));
948 Fixes.push_back(FixItHint::CreateRemoval(Outer->getOperatorLoc()));
std::pair< Context, Canceler > Inner
CharSourceRange Range
SourceRange for the file name.
std::string Condition
Condition used after the preprocessor directive.
::clang::DynTypedNode Node
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
DiagnosticBuilder configurationDiag(StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning) const
Adds a diagnostic to report errors in the check's configuration.
bool areDiagsSelfContained() const
Returns true when the check is run in a use case when only 1 fix will be applied at a time.
const LangOptions & getLangOpts() const
Returns the language options from the context.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
bool TraverseUnaryOperator(UnaryOperator *Op)
Visitor(SimplifyBooleanExprCheck *Check, ASTContext &Context)
static bool isUnaryLNot(const Expr *E)
bool VisitConditionalOperator(ConditionalOperator *Cond)
static bool shouldIgnore(Stmt *S)
static std::optional< bool > getAsBoolLiteral(const Expr *E, bool FilterMacro)
bool dataTraverseStmtPost(Stmt *S)
static bool nestedDemorgan(const Expr *E, unsigned NestingLevel)
bool VisitCompoundStmt(CompoundStmt *CS)
bool dataTraverseStmtPre(Stmt *S)
static bool checkEitherSide(const BinaryOperator *BO, Functor Func)
static auto checkSingleStatement(Stmt *S, Functor F) -> decltype(F(S))
If S is not a CompoundStmt, applies F on S, otherwise if there is only 1 statement in the CompoundStm...
bool VisitBinaryOperator(const BinaryOperator *Op) const
static ExprAndBool parseReturnLiteralBool(const Stmt *S)
Detect's return (true|false|!true|!false);.
bool VisitIfStmt(IfStmt *If)
Looks for boolean expressions involving boolean constants and simplifies them to use the appropriate ...
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
SimplifyBooleanExprCheck(StringRef Name, ClangTidyContext *Context)
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
static std::string replacementExpression(const ASTContext &Context, bool Negated, const Expr *E)
static bool needsZeroComparison(const Expr *E)
static bool containsBoolLiteral(const Expr *E)
static StringRef negatedOperator(const BinaryOperator *BinOp)
static bool shouldRemoveParens(const Stmt *Parent, BinaryOperatorKind NewOuterBinary, const ParenExpr *Parens)
static std::string compareExpressionToConstant(const ASTContext &Context, const Expr *E, bool Negated, const char *Constant)
static std::pair< BinaryOperatorKind, BinaryOperatorKind > Opposites[]
static bool needsParensAfterUnaryNegation(const Expr *E)
static bool containsDiscardedTokens(const ASTContext &Context, CharSourceRange CharRange)
static StringRef getOperatorName(OverloadedOperatorKind OpKind)
static constexpr char SimplifyConditionDiagnostic[]
static bool flipDemorganOperator(llvm::SmallVectorImpl< FixItHint > &Output, const BinaryOperator *BO)
Swaps a BinaryOperator opcode from && to || or vice-versa.
static bool needsNullPtrComparison(const Expr *E)
static constexpr char SimplifyConditionalReturnDiagnostic[]
static bool flipDemorganBinaryOperator(SmallVectorImpl< FixItHint > &Fixes, const ASTContext &Ctx, const BinaryOperator *BinOp, std::optional< BinaryOperatorKind > OuterBO, const ParenExpr *Parens=nullptr)
Inverts BinOp, Removing Parens if they exist and are safe to remove.
static constexpr char SimplifyOperatorDiagnostic[]
static bool flipDemorganSide(SmallVectorImpl< FixItHint > &Fixes, const ASTContext &Ctx, const Expr *E, std::optional< BinaryOperatorKind > OuterBO)
static BinaryOperatorKind getDemorganFlippedOperator(BinaryOperatorKind BO)
static std::string asBool(StringRef Text, bool NeedsStaticCast)
static std::pair< OverloadedOperatorKind, OverloadedOperatorKind > OppositeOverloads[]
static std::string compareExpressionToZero(const ASTContext &Context, const Expr *E, bool Negated)
static bool isMacroID(SourceRange R)
static std::string compareExpressionToNullPtr(const ASTContext &Context, const Expr *E, bool Negated)
static std::pair< OverloadedOperatorKind, StringRef > OperatorNames[]
static bool needsStaticCast(const Expr *E)
llvm::StringMap< ClangTidyValue > OptionMap