Skip to content

Commit 0ff272d

Browse files
committed
get rid of hasExplicitParentheses
1 parent a3bc1f7 commit 0ff272d

File tree

1 file changed

+11
-28
lines changed

1 file changed

+11
-28
lines changed

clang-tools-extra/clang-tidy/misc/BoolBitwiseOperationCheck.cpp

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,6 @@ getLHSNamedDeclIfCompoundAssign(const BinaryOperator *BO) {
2727
return nullptr;
2828
}
2929

30-
static bool hasExplicitParentheses(const Expr *E, const SourceManager &SM,
31-
const LangOptions &LangOpts) {
32-
if (!E)
33-
return false;
34-
35-
const SourceLocation Start = E->getBeginLoc();
36-
const SourceLocation End = E->getEndLoc();
37-
38-
if (Start.isMacroID() || End.isMacroID() || Start.isInvalid() ||
39-
End.isInvalid())
40-
return false;
41-
42-
const std::optional<Token> PrevTok =
43-
Lexer::findPreviousToken(Start, SM, LangOpts, /*IncludeComments=*/false);
44-
const std::optional<Token> NextTok =
45-
Lexer::findNextToken(End, SM, LangOpts, /*IncludeComments=*/false);
46-
47-
return (PrevTok && PrevTok->is(tok::l_paren)) &&
48-
(NextTok && NextTok->is(tok::r_paren));
49-
}
50-
5130
constexpr std::array<std::pair<llvm::StringRef, llvm::StringRef>, 8U>
5231
OperatorsTransformation{{{"|", "||"},
5332
{"|=", "||"},
@@ -155,8 +134,9 @@ void BoolBitwiseOperationCheck::check(const MatchFinder::MatchResult &Result) {
155134

156135
auto ReplaceOperator = FixItHint::CreateReplacement(TokenRange, FixSpelling);
157136

137+
const auto *Parent = Result.Nodes.getNodeAs<BinaryOperator>("p");
158138
std::optional<BinaryOperatorKind> ParentOpcode;
159-
if (const auto *Parent = Result.Nodes.getNodeAs<BinaryOperator>("p"); Parent)
139+
if (Parent)
160140
ParentOpcode = Parent->getOpcode();
161141

162142
const auto *RHS =
@@ -165,16 +145,19 @@ void BoolBitwiseOperationCheck::check(const MatchFinder::MatchResult &Result) {
165145
if (RHS)
166146
RHSOpcode = RHS->getOpcode();
167147

168-
const BinaryOperator *SurroundedExpr = nullptr;
148+
const Expr *SurroundedExpr = nullptr;
169149
if ((MatchedExpr->getOpcode() == BO_Or && ParentOpcode == BO_LAnd) ||
170150
(MatchedExpr->getOpcode() == BO_And &&
171-
llvm::is_contained({BO_Xor, BO_Or}, ParentOpcode)))
172-
SurroundedExpr = MatchedExpr;
173-
else if (MatchedExpr->getOpcode() == BO_AndAssign && RHSOpcode == BO_LOr)
151+
llvm::is_contained({BO_Xor, BO_Or}, ParentOpcode))) {
152+
const auto *Side = Parent->getLHS()->IgnoreParenImpCasts() == MatchedExpr
153+
? Parent->getLHS()
154+
: Parent->getRHS();
155+
SurroundedExpr = Side->IgnoreImpCasts();
156+
assert(SurroundedExpr->IgnoreParens() == MatchedExpr);
157+
} else if (MatchedExpr->getOpcode() == BO_AndAssign && RHSOpcode == BO_LOr)
174158
SurroundedExpr = RHS;
175159

176-
if (hasExplicitParentheses(SurroundedExpr, *Result.SourceManager,
177-
Result.Context->getLangOpts()))
160+
if (SurroundedExpr && isa<ParenExpr>(SurroundedExpr))
178161
SurroundedExpr = nullptr;
179162

180163
FixItHint InsertBrace1, InsertBrace2;

0 commit comments

Comments
 (0)