Skip to content

Commit d924248

Browse files
localspookLukacma
authored andcommitted
[clang-tidy][NFC] Clean up and slightly optimize modernize-use-integer-sign-comparison (llvm#163492)
1 parent 312997f commit d924248

File tree

1 file changed

+13
-22
lines changed

1 file changed

+13
-22
lines changed

clang-tools-extra/clang-tidy/modernize/UseIntegerSignComparisonCheck.cpp

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,12 @@ AST_MATCHER(clang::QualType, isActualChar) {
3434
} // namespace
3535

3636
static BindableMatcher<clang::Stmt>
37-
intCastExpression(bool IsSigned,
38-
const std::string &CastBindName = std::string()) {
37+
intCastExpression(bool IsSigned, StringRef CastBindName = {}) {
3938
// std::cmp_{} functions trigger a compile-time error if either LHS or RHS
4039
// is a non-integer type, char, enum or bool
4140
// (unsigned char/ signed char are Ok and can be used).
4241
auto IntTypeExpr = expr(hasType(hasCanonicalType(qualType(
43-
isInteger(), IsSigned ? isSignedInteger() : isUnsignedInteger(),
42+
IsSigned ? isSignedInteger() : isUnsignedInteger(),
4443
unless(isActualChar()), unless(booleanType()), unless(enumType())))));
4544

4645
const auto ImplicitCastExpr =
@@ -71,7 +70,7 @@ static StringRef parseOpCode(BinaryOperator::Opcode Code) {
7170
case BO_NE:
7271
return "cmp_not_equal";
7372
default:
74-
return "";
73+
llvm_unreachable("invalid opcode");
7574
}
7675
}
7776

@@ -119,23 +118,16 @@ void UseIntegerSignComparisonCheck::check(
119118
Expr::EvalResult EVResult;
120119
if (!SignedCastExpression->isValueDependent() &&
121120
SignedCastExpression->getSubExpr()->EvaluateAsInt(EVResult,
122-
*Result.Context)) {
123-
const llvm::APSInt SValue = EVResult.Val.getInt();
124-
if (SValue.isNonNegative())
125-
return;
126-
}
121+
*Result.Context) &&
122+
EVResult.Val.getInt().isNonNegative())
123+
return;
127124

128125
const auto *BinaryOp =
129126
Result.Nodes.getNodeAs<BinaryOperator>("intComparison");
130-
if (BinaryOp == nullptr)
131-
return;
132-
133-
const BinaryOperator::Opcode OpCode = BinaryOp->getOpcode();
127+
assert(BinaryOp);
134128

135129
const Expr *LHS = BinaryOp->getLHS()->IgnoreImpCasts();
136130
const Expr *RHS = BinaryOp->getRHS()->IgnoreImpCasts();
137-
if (LHS == nullptr || RHS == nullptr)
138-
return;
139131
const Expr *SubExprLHS = nullptr;
140132
const Expr *SubExprRHS = nullptr;
141133
SourceRange R1(LHS->getBeginLoc());
@@ -144,8 +136,7 @@ void UseIntegerSignComparisonCheck::check(
144136
RHS->getEndLoc(), 0, *Result.SourceManager, getLangOpts()));
145137
if (const auto *LHSCast = llvm::dyn_cast<ExplicitCastExpr>(LHS)) {
146138
SubExprLHS = LHSCast->getSubExpr();
147-
R1 = SourceRange(LHS->getBeginLoc(),
148-
SubExprLHS->getBeginLoc().getLocWithOffset(-1));
139+
R1.setEnd(SubExprLHS->getBeginLoc().getLocWithOffset(-1));
149140
R2.setBegin(Lexer::getLocForEndOfToken(
150141
SubExprLHS->getEndLoc(), 0, *Result.SourceManager, getLangOpts()));
151142
}
@@ -158,21 +149,21 @@ void UseIntegerSignComparisonCheck::check(
158149
DiagnosticBuilder Diag =
159150
diag(BinaryOp->getBeginLoc(),
160151
"comparison between 'signed' and 'unsigned' integers");
161-
std::string CmpNamespace;
162-
llvm::StringRef CmpHeader;
152+
StringRef CmpNamespace;
153+
StringRef CmpHeader;
163154

164155
if (getLangOpts().CPlusPlus20) {
165156
CmpHeader = "<utility>";
166-
CmpNamespace = llvm::Twine("std::" + parseOpCode(OpCode)).str();
157+
CmpNamespace = "std::";
167158
} else if (getLangOpts().CPlusPlus17 && EnableQtSupport) {
168159
CmpHeader = "<QtCore/q20utility.h>";
169-
CmpNamespace = llvm::Twine("q20::" + parseOpCode(OpCode)).str();
160+
CmpNamespace = "q20::";
170161
}
171162

172163
// Prefer modernize-use-integer-sign-comparison when C++20 is available!
173164
Diag << FixItHint::CreateReplacement(
174165
CharSourceRange(R1, SubExprLHS != nullptr),
175-
llvm::Twine(CmpNamespace + "(").str());
166+
Twine(CmpNamespace + parseOpCode(BinaryOp->getOpcode()) + "(").str());
176167
Diag << FixItHint::CreateReplacement(R2, ",");
177168
Diag << FixItHint::CreateReplacement(CharSourceRange::getCharRange(R3), ")");
178169

0 commit comments

Comments
 (0)