clang-tools 3.8.0

- Main Page
- Namespaces
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

Public Member Functions | List of all members
clang::tidy::readability::SimplifyBooleanExprCheck Class Reference

Looks for boolean expressions involving boolean constants and simplifies them to use the appropriate boolean expression directly. More...

`#include <SimplifyBooleanExprCheck.h>`

Inheritance diagram for clang::tidy::readability::SimplifyBooleanExprCheck:
[legend]
Collaboration diagram for clang::tidy::readability::SimplifyBooleanExprCheck:
[legend]

## Public Member Functions
| SimplifyBooleanExprCheck (StringRef Name, ClangTidyContext *Context)
void | storeOptions (ClangTidyOptions::OptionMap &Options) override
| Should store all options supported by this check with their current values or default values for options that haven't been overridden. More...
void | registerMatchers (ast_matchers::MatchFinder *Finder) override
| Override this to register ASTMatchers with `Finder`. More...
void | check (const ast_matchers::MatchFinder::MatchResult &Result) override
| `ClangTidyChecks` that register ASTMatchers should do the actual work in here. More...
Public Member Functions inherited from clang::tidy::ClangTidyCheck
| ClangTidyCheck (StringRef CheckName, ClangTidyContext *Context)
| Initializes the check with `CheckName` and `Context`. More...
virtual void | registerPPCallbacks (CompilerInstance &Compiler)
| Override this to register `PPCallbacks` with `Compiler`. More...
DiagnosticBuilder | diag (SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
| Add a diagnostic with the check's name. More...

## Additional Inherited Members
Protected Member Functions inherited from clang::tidy::ClangTidyCheck
StringRef | getCurrentMainFile () const
| Returns the main file name of the current translation unit. More...
LangOptions | getLangOpts () const
| Returns the language options from the context. More...
Protected Attributes inherited from clang::tidy::ClangTidyCheck
OptionsView | Options

## Detailed Description

Looks for boolean expressions involving boolean constants and simplifies them to use the appropriate boolean expression directly.

Examples:

=========================================== ================ Initial expression Result

`if (b == true)` `if (b)` `if (b == false)` `if (!b)` `if (b && true)` `if (b)` `if (b && false)` `if (false)` `if (b || true)` `if (true)` `if (b || false)` `if (b)` `e ? true : false` `e` `e ? false : true` `!e` `if (true) t(); else f();` `t();` `if (false) t(); else f();` `f();` `if (e) return true; else return false;` `return e;` `if (e) return false; else return true;` `return !e;` `if (e) b = true; else b = false;` `b = e;` `if (e) b = false; else b = true;` `b = !e;` `if (e) return true; return false;` `return e;` `if (e) return false; return true;` `return !e;` =========================================== ================

The resulting expression `e` is modified as follows:

1. Unnecessary parentheses around the expression are removed.
2. Negated applications of `!` are eliminated.
3. Negated applications of comparison operators are changed to use the opposite condition.
4. Implicit conversions of pointer to `bool` are replaced with explicit comparisons to `nullptr`.
5. Implicit casts to `bool` are replaced with explicit casts to `bool`.
6. Object expressions with `explicit operator bool` conversion operators are replaced with explicit casts to `bool`.

Examples:

1. The ternary assignment `bool b = (i < 0) ? true : false;` has redundant parentheses and becomes `bool b = i < 0;`.
2. The conditional return `if (!b) return false; return true;` has an implied double negation and becomes `return b;`.

3. The conditional return `if (i < 0) return false; return true;` becomes `return i >= 0;`.

   The conditional return `if (i != 0) return false; return true;` becomes `return i == 0;`.

4. The conditional return `if (p) return true; return false;` has an implicit conversion of a pointer to `bool` and becomes `return p != nullptr;`.

   The ternary assignment `bool b = (i & 1) ? true : false;` has an implicit conversion of `i & 1` to `bool` and becomes `bool b = static_cast<bool>(i & 1);`.

5. The conditional return `if (i & 1) return true; else return false;` has an implicit conversion of an integer quantity `i & 1` to `bool` and becomes `return static_cast<bool>(i & 1);`
6. Given `struct X { explicit operator bool(); };`, and an instance `x` of `struct X`, the conditional return `if (x) return true; return false;` becomes `return static_cast<bool>(x);`

When a conditional boolean return or assignment appears at the end of a chain of `if`, `else if` statements, the conditional statement is left unchanged unless the option `ChainedConditionalReturn` or `ChainedConditionalAssignment`, respectively, is specified as non-zero. The default value for both options is zero.

Definition at line 91 of file SimplifyBooleanExprCheck.h.

## Constructor & Destructor Documentation

clang::tidy::readability::SimplifyBooleanExprCheck::SimplifyBooleanExprCheck | ( | StringRef | Name,
|  | ClangTidyContext * | Context
| ) |  |

Definition at line 263 of file SimplifyBooleanExprCheck.cpp.

## Member Function Documentation

void clang::tidy::readability::SimplifyBooleanExprCheck::check | ( | const ast_matchers::MatchFinder::MatchResult & | Result | ) |
| override virtual

`ClangTidyChecks` that register ASTMatchers should do the actual work in here.

Reimplemented from clang::tidy::ClangTidyCheck.

Definition at line 445 of file SimplifyBooleanExprCheck.cpp.

void clang::tidy::readability::SimplifyBooleanExprCheck::registerMatchers | ( | ast_matchers::MatchFinder * | Finder | ) |
| override virtual

Override this to register ASTMatchers with `Finder`.

This should be used by clang-tidy checks that analyze code properties that dependent on AST knowledge.

You can register as many matchers as necessary with `Finder`. Usually, "this" will be used as callback, but you can also specify other callback classes. Thereby, different matchers can trigger different callbacks.

If you need to merge information between the different matchers, you can store these as members of the derived class. However, note that all matches occur in the order of the AST traversal.

Reimplemented from clang::tidy::ClangTidyCheck.

Definition at line 408 of file SimplifyBooleanExprCheck.cpp.

void clang::tidy::readability::SimplifyBooleanExprCheck::storeOptions | ( | ClangTidyOptions::OptionMap & | Options | ) |
| override virtual

Should store all options supported by this check with their current values or default values for options that haven't been overridden.

The check should use `Options.store()` to store each option it supports whether it has the default value or it has been overridden.

Reimplemented from clang::tidy::ClangTidyCheck.

Definition at line 402 of file SimplifyBooleanExprCheck.cpp.

References clang::tidy::ClangTidyCheck::Options, and clang::tidy::OptionsView::store().

The documentation for this class was generated from the following files:

- SimplifyBooleanExprCheck.h
- SimplifyBooleanExprCheck.cpp