clang-tools  14.0.0git
SimplifyBooleanExprCheck.h
Go to the documentation of this file.
1 //===--- SimplifyBooleanExpr.h clang-tidy -----------------------*- C++ -*-===//
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_SIMPLIFY_BOOLEAN_EXPR_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_SIMPLIFY_BOOLEAN_EXPR_H
11 
12 #include "../ClangTidyCheck.h"
13 
14 namespace clang {
15 namespace tidy {
16 namespace readability {
17 
18 /// Looks for boolean expressions involving boolean constants and simplifies
19 /// them to use the appropriate boolean expression directly.
20 ///
21 /// For the user-facing documentation see:
22 /// http://clang.llvm.org/extra/clang-tidy/checks/readability-simplify-boolean-expr.html
24 public:
25  SimplifyBooleanExprCheck(StringRef Name, ClangTidyContext *Context);
26 
28  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
29  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
30  llvm::Optional<TraversalKind> getCheckTraversalKind() const override {
31  return TK_IgnoreUnlessSpelledInSource;
32  }
33 
34 private:
35  class Visitor;
36 
37  void reportBinOp(const ast_matchers::MatchFinder::MatchResult &Result,
38  const BinaryOperator *Op);
39 
40  void matchBoolCondition(ast_matchers::MatchFinder *Finder, bool Value,
41  StringRef BooleanId);
42 
43  void matchTernaryResult(ast_matchers::MatchFinder *Finder, bool Value,
44  StringRef TernaryId);
45 
46  void matchIfReturnsBool(ast_matchers::MatchFinder *Finder, bool Value,
47  StringRef Id);
48 
49  void matchIfAssignsBool(ast_matchers::MatchFinder *Finder, bool Value,
50  StringRef Id);
51 
52  void matchCompoundIfReturnsBool(ast_matchers::MatchFinder *Finder, bool Value,
53  StringRef Id);
54 
55  void
56  replaceWithThenStatement(const ast_matchers::MatchFinder::MatchResult &Result,
57  const Expr *BoolLiteral);
58 
59  void
60  replaceWithElseStatement(const ast_matchers::MatchFinder::MatchResult &Result,
61  const Expr *FalseConditionRemoved);
62 
63  void
64  replaceWithCondition(const ast_matchers::MatchFinder::MatchResult &Result,
65  const ConditionalOperator *Ternary,
66  bool Negated = false);
67 
68  void replaceWithReturnCondition(
69  const ast_matchers::MatchFinder::MatchResult &Result, const IfStmt *If,
70  bool Negated = false);
71 
72  void
73  replaceWithAssignment(const ast_matchers::MatchFinder::MatchResult &Result,
74  const IfStmt *If, bool Negated = false);
75 
76  void replaceCompoundReturnWithCondition(
77  const ast_matchers::MatchFinder::MatchResult &Result,
78  const CompoundStmt *Compound, bool Negated = false);
79 
80  void issueDiag(const ast_matchers::MatchFinder::MatchResult &Result,
81  SourceLocation Loc, StringRef Description,
82  SourceRange ReplacementRange, StringRef Replacement);
83 
84  const bool ChainedConditionalReturn;
85  const bool ChainedConditionalAssignment;
86 };
87 
88 } // namespace readability
89 } // namespace tidy
90 } // namespace clang
91 
92 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_SIMPLIFY_BOOLEAN_EXPR_H
Loc
SourceLocation Loc
Definition: KernelNameRestrictionCheck.cpp:45
clang::tidy::ClangTidyOptions::OptionMap
llvm::StringMap< ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:115
clang::tidy::readability::SimplifyBooleanExprCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: SimplifyBooleanExprCheck.cpp:540
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:54
clang::tidy::readability::SimplifyBooleanExprCheck::getCheckTraversalKind
llvm::Optional< TraversalKind > getCheckTraversalKind() const override
Definition: SimplifyBooleanExprCheck.h:30
clang::tidy::readability::SimplifyBooleanExprCheck::SimplifyBooleanExprCheck
SimplifyBooleanExprCheck(StringRef Name, ClangTidyContext *Context)
Definition: SimplifyBooleanExprCheck.cpp:357
clang::tidy::readability::SimplifyBooleanExprCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Options) override
Should store all options supported by this check with their current values or default values for opti...
Definition: SimplifyBooleanExprCheck.cpp:515
clang::tidy::readability::SimplifyBooleanExprCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: SimplifyBooleanExprCheck.cpp:521
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidyCheck.h:416
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:72
Description
const char * Description
Definition: Dexp.cpp:361
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:28
clang::tidy::readability::SimplifyBooleanExprCheck
Looks for boolean expressions involving boolean constants and simplifies them to use the appropriate ...
Definition: SimplifyBooleanExprCheck.h:23
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
Value
static constexpr bool Value
Definition: SuspiciousCallArgumentCheck.cpp:72