clang-tools 23.0.0git
SimplifyBooleanExprCheck.h
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
9#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_SIMPLIFYBOOLEANEXPRCHECK_H
10#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_SIMPLIFYBOOLEANEXPRCHECK_H
11
12#include "../ClangTidyCheck.h"
13
15
16/// Looks for boolean expressions involving boolean constants and simplifies
17/// them to use the appropriate boolean expression directly.
18///
19/// For the user-facing documentation see:
20/// https://clang.llvm.org/extra/clang-tidy/checks/readability/simplify-boolean-expr.html
22public:
23 SimplifyBooleanExprCheck(StringRef Name, ClangTidyContext *Context);
24
25 bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
26 return LangOpts.CPlusPlus || LangOpts.C23;
27 }
28 void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
29 void registerMatchers(ast_matchers::MatchFinder *Finder) override;
30 void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
31 std::optional<TraversalKind> getCheckTraversalKind() const override {
32 return TK_IgnoreUnlessSpelledInSource;
33 }
34
35private:
36 class Visitor;
37
38 void reportBinOp(const ASTContext &Context, const BinaryOperator *Op);
39
40 void replaceWithThenStatement(const ASTContext &Context,
41 const IfStmt *IfStatement,
42 const Expr *BoolLiteral);
43
44 void replaceWithElseStatement(const ASTContext &Context,
45 const IfStmt *IfStatement,
46 const Expr *BoolLiteral);
47
48 void replaceWithCondition(const ASTContext &Context,
49 const ConditionalOperator *Ternary, bool Negated);
50
51 void replaceWithReturnCondition(const ASTContext &Context, const IfStmt *If,
52 const Expr *BoolLiteral, bool Negated);
53
54 void replaceWithAssignment(const ASTContext &Context, const IfStmt *If,
55 const Expr *Var, SourceLocation Loc, bool Negated);
56
57 void replaceCompoundReturnWithCondition(const ASTContext &Context,
58 const ReturnStmt *Ret, bool Negated,
59 const IfStmt *If,
60 const Expr *ThenReturn);
61
62 bool reportDeMorgan(const ASTContext &Context, const UnaryOperator *Outer,
63 const BinaryOperator *Inner, bool TryOfferFix,
64 const Stmt *Parent, const ParenExpr *Parens);
65
66 bool issueDiag(const ASTContext &Context, SourceLocation Loc,
67 StringRef Description, SourceRange ReplacementRange,
68 StringRef Replacement);
69
70 bool canBeBypassed(const Stmt *S) const;
71
72 const bool IgnoreMacros;
73 const bool ChainedConditionalReturn;
74 const bool ChainedConditionalAssignment;
75 const bool SimplifyDeMorgan;
76 const bool SimplifyDeMorganRelaxed;
77};
78
79} // namespace clang::tidy::readability
80
81#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_SIMPLIFYBOOLEANEXPRCHECK_H
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override
std::optional< TraversalKind > getCheckTraversalKind() const override
SimplifyBooleanExprCheck(StringRef Name, ClangTidyContext *Context)
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
llvm::StringMap< ClangTidyValue > OptionMap