clang-tools
3.8.0
|
Looks for boolean expressions involving boolean constants and simplifies them to use the appropriate boolean expression directly. More...
#include <SimplifyBooleanExprCheck.h>
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... | |
![]() | |
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 | |
![]() | |
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... | |
![]() | |
OptionsView | Options |
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:
!
are eliminated.bool
are replaced with explicit comparisons to nullptr
.bool
are replaced with explicit casts to bool
.explicit operator bool
conversion operators are replaced with explicit casts to bool
.Examples:
bool b = (i < 0) ? true : false;
has redundant parentheses and becomes bool b = i < 0;
.if (!b) return false; return true;
has an implied double negation and becomes return b;
.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;
.
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);
.
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);
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.
clang::tidy::readability::SimplifyBooleanExprCheck::SimplifyBooleanExprCheck | ( | StringRef | Name, |
ClangTidyContext * | Context | ||
) |
Definition at line 263 of file SimplifyBooleanExprCheck.cpp.
|
overridevirtual |
ClangTidyChecks
that register ASTMatchers should do the actual work in here.
Reimplemented from clang::tidy::ClangTidyCheck.
Definition at line 445 of file SimplifyBooleanExprCheck.cpp.
|
overridevirtual |
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.
|
overridevirtual |
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().