Skip to content

Commit bf5f745

Browse files
committed
review
1 parent 3872a69 commit bf5f745

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "clang/ASTMatchers/ASTMatchFinder.h"
1111
#include "clang/Lex/Lexer.h"
1212
#include "llvm/ADT/ScopeExit.h"
13+
#include "llvm/ADT/Twine.h"
1314
#include <array>
1415
#include <optional>
1516
#include <utility>
@@ -18,12 +19,14 @@ using namespace clang::ast_matchers;
1819

1920
namespace clang::tidy::performance {
2021

21-
static std::string tryPrintVariable(const BinaryOperator *E) {
22-
if (E->isCompoundAssignmentOp()) {
22+
static std::string tryPrintVariable(const BinaryOperator *BO) {
23+
if (BO->isCompoundAssignmentOp()) {
2324
const auto *DelcRefLHS =
24-
dyn_cast<DeclRefExpr>(E->getLHS()->IgnoreImpCasts());
25+
dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreImpCasts());
2526
if (DelcRefLHS)
26-
return "variable '" + DelcRefLHS->getDecl()->getNameAsString() + "'";
27+
return ("variable '" +
28+
llvm::Twine(DelcRefLHS->getDecl()->getNameAsString()) + "'")
29+
.str();
2730
}
2831
return "values";
2932
}
@@ -36,8 +39,8 @@ static bool hasExplicitParentheses(const Expr *E, const SourceManager &SM,
3639
const SourceLocation Start = E->getBeginLoc();
3740
const SourceLocation End = E->getEndLoc();
3841

39-
if (Start.isMacroID() || End.isMacroID() || !Start.isValid() ||
40-
!End.isValid())
42+
if (Start.isMacroID() || End.isMacroID() || Start.isInvalid() ||
43+
End.isInvalid())
4144
return false;
4245

4346
const std::optional<Token> PrevTok =
@@ -52,7 +55,7 @@ static bool hasExplicitParentheses(const Expr *E, const SourceManager &SM,
5255
template <typename AstNode>
5356
static bool isInTemplateFunction(const AstNode *AN, ASTContext &Context) {
5457
DynTypedNodeList Parents = Context.getParents(*AN);
55-
for (const auto &Parent : Parents) {
58+
for (const DynTypedNode &Parent : Parents) {
5659
if (const auto *FD = Parent.template get<FunctionDecl>())
5760
return FD->isTemplateInstantiation() ||
5861
FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate;
@@ -72,10 +75,10 @@ constexpr std::array<std::pair<llvm::StringRef, llvm::StringRef>, 8U>
7275
{"bitor", "or"},
7376
{"or_eq", "or"}}};
7477

75-
static llvm::StringRef translate(llvm::StringRef Value) {
78+
static std::string translate(llvm::StringRef Value) {
7679
for (const auto &[Bitwise, Logical] : OperatorsTransformation) {
7780
if (Value == Bitwise)
78-
return Logical;
81+
return Logical.str();
7982
}
8083

8184
return {};
@@ -155,14 +158,12 @@ void BoolBitwiseOperationCheck::check(const MatchFinder::MatchResult &Result) {
155158

156159
StringRef Spelling = Lexer::getSourceText(TokenRange, *Result.SourceManager,
157160
Result.Context->getLangOpts());
158-
StringRef TranslatedSpelling = translate(Spelling);
161+
const std::string FixSpelling = translate(Spelling);
159162

160-
if (TranslatedSpelling.empty())
163+
if (FixSpelling.empty())
161164
return;
162165

163-
const std::string FixSpelling = TranslatedSpelling.str();
164-
165-
FixItHint InsertEqual, ReplaceOperator, InsertBrace1, InsertBrace2;
166+
FixItHint InsertEqual;
166167
if (MatchedExpr->isCompoundAssignmentOp()) {
167168
const auto *DelcRefLHS =
168169
dyn_cast<DeclRefExpr>(MatchedExpr->getLHS()->IgnoreImpCasts());
@@ -184,7 +185,8 @@ void BoolBitwiseOperationCheck::check(const MatchFinder::MatchResult &Result) {
184185
InsertEqual = FixItHint::CreateInsertion(
185186
InsertLoc, " = " + DelcRefLHS->getDecl()->getNameAsString());
186187
}
187-
ReplaceOperator = FixItHint::CreateReplacement(TokenRange, FixSpelling);
188+
189+
auto ReplaceOperator = FixItHint::CreateReplacement(TokenRange, FixSpelling);
188190

189191
std::optional<BinaryOperatorKind> ParentOpcode;
190192
if (const auto *Parent = Result.Nodes.getNodeAs<BinaryOperator>("p");
@@ -215,6 +217,7 @@ void BoolBitwiseOperationCheck::check(const MatchFinder::MatchResult &Result) {
215217
Result.Context->getLangOpts()))
216218
SurroundedExpr = nullptr;
217219

220+
FixItHint InsertBrace1, InsertBrace2;
218221
if (SurroundedExpr) {
219222
const SourceLocation InsertFirstLoc = SurroundedExpr->getBeginLoc();
220223
const SourceLocation InsertSecondLoc = clang::Lexer::getLocForEndOfToken(

0 commit comments

Comments
 (0)