#include "SimplifyBooleanExprCheck.h"
#include "clang/AST/Expr.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Basic/DiagnosticIDs.h"
#include "clang/Lex/Lexer.h"
#include "llvm/Support/SaveAndRestore.h"
#include <optional>
#include <string>
#include <utility>
usingnamespaceclang::ast_matchers;
namespace clang::tidy::readability {
namespace {
StringRef getText(const ASTContext &Context, SourceRange Range) { … }
template <typename T> StringRef getText(const ASTContext &Context, T &Node) { … }
}
static constexpr char SimplifyOperatorDiagnostic[] = …;
static constexpr char SimplifyConditionDiagnostic[] = …;
static constexpr char SimplifyConditionalReturnDiagnostic[] = …;
static bool needsParensAfterUnaryNegation(const Expr *E) { … }
static std::pair<BinaryOperatorKind, BinaryOperatorKind> Opposites[] = …;
static StringRef negatedOperator(const BinaryOperator *BinOp) { … }
static std::pair<OverloadedOperatorKind, StringRef> OperatorNames[] = …;
static StringRef getOperatorName(OverloadedOperatorKind OpKind) { … }
static std::pair<OverloadedOperatorKind, OverloadedOperatorKind>
OppositeOverloads[] = …;
static StringRef negatedOperator(const CXXOperatorCallExpr *OpCall) { … }
static std::string asBool(StringRef Text, bool NeedsStaticCast) { … }
static bool needsNullPtrComparison(const Expr *E) { … }
static bool needsZeroComparison(const Expr *E) { … }
static bool needsStaticCast(const Expr *E) { … }
static std::string compareExpressionToConstant(const ASTContext &Context,
const Expr *E, bool Negated,
const char *Constant) { … }
static std::string compareExpressionToNullPtr(const ASTContext &Context,
const Expr *E, bool Negated) { … }
static std::string compareExpressionToZero(const ASTContext &Context,
const Expr *E, bool Negated) { … }
static std::string replacementExpression(const ASTContext &Context,
bool Negated, const Expr *E) { … }
static bool containsDiscardedTokens(const ASTContext &Context,
CharSourceRange CharRange) { … }
class SimplifyBooleanExprCheck::Visitor : public RecursiveASTVisitor<Visitor> { … };
SimplifyBooleanExprCheck::SimplifyBooleanExprCheck(StringRef Name,
ClangTidyContext *Context)
: … { … }
static bool containsBoolLiteral(const Expr *E) { … }
void SimplifyBooleanExprCheck::reportBinOp(const ASTContext &Context,
const BinaryOperator *Op) { … }
void SimplifyBooleanExprCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { … }
void SimplifyBooleanExprCheck::registerMatchers(MatchFinder *Finder) { … }
void SimplifyBooleanExprCheck::check(const MatchFinder::MatchResult &Result) { … }
bool SimplifyBooleanExprCheck::canBeBypassed(const Stmt *S) const { … }
bool SimplifyBooleanExprCheck::issueDiag(const ASTContext &Context,
SourceLocation Loc,
StringRef Description,
SourceRange ReplacementRange,
StringRef Replacement) { … }
void SimplifyBooleanExprCheck::replaceWithThenStatement(
const ASTContext &Context, const IfStmt *IfStatement,
const Expr *BoolLiteral) { … }
void SimplifyBooleanExprCheck::replaceWithElseStatement(
const ASTContext &Context, const IfStmt *IfStatement,
const Expr *BoolLiteral) { … }
void SimplifyBooleanExprCheck::replaceWithCondition(
const ASTContext &Context, const ConditionalOperator *Ternary,
bool Negated) { … }
void SimplifyBooleanExprCheck::replaceWithReturnCondition(
const ASTContext &Context, const IfStmt *If, const Expr *BoolLiteral,
bool Negated) { … }
void SimplifyBooleanExprCheck::replaceCompoundReturnWithCondition(
const ASTContext &Context, const ReturnStmt *Ret, bool Negated,
const IfStmt *If, const Expr *ThenReturn) { … }
void SimplifyBooleanExprCheck::replaceWithAssignment(const ASTContext &Context,
const IfStmt *IfAssign,
const Expr *Var,
SourceLocation Loc,
bool Negated) { … }
static bool flipDemorganOperator(llvm::SmallVectorImpl<FixItHint> &Output,
const BinaryOperator *BO) { … }
static BinaryOperatorKind getDemorganFlippedOperator(BinaryOperatorKind BO) { … }
static bool flipDemorganSide(SmallVectorImpl<FixItHint> &Fixes,
const ASTContext &Ctx, const Expr *E,
std::optional<BinaryOperatorKind> OuterBO);
static bool
flipDemorganBinaryOperator(SmallVectorImpl<FixItHint> &Fixes,
const ASTContext &Ctx, const BinaryOperator *BinOp,
std::optional<BinaryOperatorKind> OuterBO,
const ParenExpr *Parens = nullptr) { … }
static bool flipDemorganSide(SmallVectorImpl<FixItHint> &Fixes,
const ASTContext &Ctx, const Expr *E,
std::optional<BinaryOperatorKind> OuterBO) { … }
static bool shouldRemoveParens(const Stmt *Parent,
BinaryOperatorKind NewOuterBinary,
const ParenExpr *Parens) { … }
bool SimplifyBooleanExprCheck::reportDeMorgan(const ASTContext &Context,
const UnaryOperator *Outer,
const BinaryOperator *Inner,
bool TryOfferFix,
const Stmt *Parent,
const ParenExpr *Parens) { … }
}