@@ -75,44 +75,44 @@ static llvm::StringRef translate(llvm::StringRef Value) {
7575}
7676
7777static bool isBooleanBitwise (const BinaryOperator *BinOp, ASTContext *AC,
78- std::optional<bool > &rootAssignsToBoolean ) {
78+ std::optional<bool > &RootAssignsToBoolean ) {
7979 if (!BinOp)
8080 return false ;
8181
8282 for (const auto &[Bitwise, _] : OperatorsTransformation) {
8383 if (BinOp->getOpcodeStr () == Bitwise) {
84- bool lhsBoolean = BinOp->getLHS ()
85- ->IgnoreImpCasts ()
86- ->getType ()
87- .getDesugaredType (*AC)
88- ->isBooleanType ();
89- bool rhsBoolean = BinOp->getRHS ()
90- ->IgnoreImpCasts ()
91- ->getType ()
92- .getDesugaredType (*AC)
93- ->isBooleanType ();
84+ bool IsBooleanLHS = BinOp->getLHS ()
85+ ->IgnoreImpCasts ()
86+ ->getType ()
87+ .getDesugaredType (*AC)
88+ ->isBooleanType ();
89+ bool IsBooleanRHS = BinOp->getRHS ()
90+ ->IgnoreImpCasts ()
91+ ->getType ()
92+ .getDesugaredType (*AC)
93+ ->isBooleanType ();
9494 for (int i = 0 ; i < 2 ; ++i) {
95- if (lhsBoolean && rhsBoolean ) {
96- rootAssignsToBoolean = rootAssignsToBoolean .value_or (false );
95+ if (IsBooleanLHS && IsBooleanRHS ) {
96+ RootAssignsToBoolean = RootAssignsToBoolean .value_or (false );
9797 return true ;
9898 }
9999 if (assignsToBoolean (BinOp, AC) ||
100- rootAssignsToBoolean .value_or (false )) {
101- rootAssignsToBoolean = rootAssignsToBoolean .value_or (true );
100+ RootAssignsToBoolean .value_or (false )) {
101+ RootAssignsToBoolean = RootAssignsToBoolean .value_or (true );
102102 return true ;
103103 }
104- if (BinOp->isCompoundAssignmentOp () && lhsBoolean ) {
105- rootAssignsToBoolean = rootAssignsToBoolean .value_or (true );
104+ if (BinOp->isCompoundAssignmentOp () && IsBooleanLHS ) {
105+ RootAssignsToBoolean = RootAssignsToBoolean .value_or (true );
106106 return true ;
107107 }
108108 std::optional<bool > DummyFlag = false ;
109- lhsBoolean =
110- lhsBoolean ||
109+ IsBooleanLHS =
110+ IsBooleanLHS ||
111111 isBooleanBitwise (dyn_cast<BinaryOperator>(
112112 BinOp->getLHS ()->IgnoreParenImpCasts ()),
113113 AC, DummyFlag);
114- rhsBoolean =
115- rhsBoolean ||
114+ IsBooleanRHS =
115+ IsBooleanRHS ||
116116 isBooleanBitwise (dyn_cast<BinaryOperator>(
117117 BinOp->getRHS ()->IgnoreParenImpCasts ()),
118118 AC, DummyFlag);
@@ -122,12 +122,14 @@ static bool isBooleanBitwise(const BinaryOperator *BinOp, ASTContext *AC,
122122 return false ;
123123}
124124
125- static const Expr* getValidCompoundsLHS (const BinaryOperator* BinOp) {
125+ static const Expr * getValidCompoundsLHS (const BinaryOperator * BinOp) {
126126 assert (BinOp->isCompoundAssignmentOp ());
127-
128- if (const auto *DeclRefLHS = dyn_cast<DeclRefExpr>(BinOp->getLHS ()->IgnoreImpCasts ()))
127+
128+ if (const auto *DeclRefLHS =
129+ dyn_cast<DeclRefExpr>(BinOp->getLHS ()->IgnoreImpCasts ()))
129130 return DeclRefLHS;
130- else if (const auto *MemberLHS = dyn_cast<MemberExpr>(BinOp->getLHS ()->IgnoreImpCasts ()))
131+ else if (const auto *MemberLHS =
132+ dyn_cast<MemberExpr>(BinOp->getLHS ()->IgnoreImpCasts ()))
131133 return MemberLHS;
132134
133135 return nullptr ;
@@ -159,9 +161,9 @@ void BoolBitwiseOperationCheck::emitWarningAndChangeOperatorsIfPossible(
159161 const clang::SourceManager &SM, clang::ASTContext &Ctx) {
160162 auto DiagEmitter = [BinOp, this ] {
161163 return diag (BinOp->getOperatorLoc (),
162- " use logical operator '%0' for boolean semantics instead of bitwise operator '%1' " )
163- << translate (BinOp-> getOpcodeStr () )
164- << BinOp->getOpcodeStr ();
164+ " use logical operator '%0' for boolean semantics instead of "
165+ " bitwise operator '%1' " )
166+ << translate (BinOp-> getOpcodeStr ()) << BinOp->getOpcodeStr ();
165167 };
166168
167169 const bool HasVolatileOperand = llvm::any_of (
@@ -211,10 +213,10 @@ void BoolBitwiseOperationCheck::emitWarningAndChangeOperatorsIfPossible(
211213 if (InsertLoc.isInvalid () || InsertLoc.isMacroID ())
212214 return static_cast <void >(IgnoreMacros || DiagEmitter ());
213215 auto SourceText = static_cast <std::string>(Lexer::getSourceText (
214- CharSourceRange::getTokenRange (LHS->getSourceRange ()),
215- SM, Ctx.getLangOpts ()
216- ));
217- llvm::erase_if (SourceText, [](unsigned char ch) { return std::isspace (ch); });
216+ CharSourceRange::getTokenRange (LHS->getSourceRange ()), SM ,
217+ Ctx.getLangOpts ()));
218+ llvm::erase_if (SourceText,
219+ [](unsigned char ch) { return std::isspace (ch); });
218220 InsertEqual = FixItHint::CreateInsertion (InsertLoc, " = " + SourceText);
219221 }
220222
@@ -264,27 +266,27 @@ void BoolBitwiseOperationCheck::emitWarningAndChangeOperatorsIfPossible(
264266void BoolBitwiseOperationCheck::visitBinaryTreesNode (
265267 const BinaryOperator *BinOp, const BinaryOperator *ParentBinOp,
266268 const clang::SourceManager &SM, clang::ASTContext &Ctx,
267- std::optional<bool > &rootAssignsToBoolean ) {
269+ std::optional<bool > &RootAssignsToBoolean ) {
268270 if (!BinOp)
269271 return ;
270272
271- if (isBooleanBitwise (BinOp, &Ctx, rootAssignsToBoolean ))
273+ if (isBooleanBitwise (BinOp, &Ctx, RootAssignsToBoolean ))
272274 emitWarningAndChangeOperatorsIfPossible (BinOp, ParentBinOp, SM, Ctx);
273275
274276 visitBinaryTreesNode (
275277 dyn_cast<BinaryOperator>(BinOp->getLHS ()->IgnoreParenImpCasts ()), BinOp,
276- SM, Ctx, rootAssignsToBoolean );
278+ SM, Ctx, RootAssignsToBoolean );
277279 visitBinaryTreesNode (
278280 dyn_cast<BinaryOperator>(BinOp->getRHS ()->IgnoreParenImpCasts ()), BinOp,
279- SM, Ctx, rootAssignsToBoolean );
281+ SM, Ctx, RootAssignsToBoolean );
280282}
281283
282284void BoolBitwiseOperationCheck::check (const MatchFinder::MatchResult &Result) {
283285 const auto *binOpRoot = Result.Nodes .getNodeAs <BinaryOperator>(" binOpRoot" );
284286 const SourceManager &SM = *Result.SourceManager ;
285287 ASTContext &Ctx = *Result.Context ;
286- std::optional<bool > rootAssignsToBoolean = std::nullopt ;
287- visitBinaryTreesNode (binOpRoot, nullptr , SM, Ctx, rootAssignsToBoolean );
288+ std::optional<bool > RootAssignsToBoolean = std::nullopt ;
289+ visitBinaryTreesNode (binOpRoot, nullptr , SM, Ctx, RootAssignsToBoolean );
288290}
289291
290292} // namespace clang::tidy::misc
0 commit comments