Skip to content

Commit 3f3b19d

Browse files
authored
[clang][NFC] Clean up SemaChecking.cpp (#141041)
Make pointer parameters const, remove some unused parameters, fix coding style, etc.
1 parent 8f9549c commit 3f3b19d

File tree

4 files changed

+77
-81
lines changed

4 files changed

+77
-81
lines changed

clang/include/clang/Sema/Sema.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2604,8 +2604,8 @@ class Sema final : public SemaBase {
26042604
/// Check for comparisons of floating-point values using == and !=. Issue a
26052605
/// warning if the comparison is not likely to do what the programmer
26062606
/// intended.
2607-
void CheckFloatComparison(SourceLocation Loc, Expr *LHS, Expr *RHS,
2608-
BinaryOperatorKind Opcode);
2607+
void CheckFloatComparison(SourceLocation Loc, const Expr *LHS,
2608+
const Expr *RHS, BinaryOperatorKind Opcode);
26092609

26102610
/// Register a magic integral constant to be used as a type tag.
26112611
void RegisterTypeTagForDatatype(const IdentifierInfo *ArgumentKind,
@@ -3013,7 +3013,8 @@ class Sema final : public SemaBase {
30133013
// Warn on anti-patterns as the 'size' argument to strncat.
30143014
// The correct size argument should look like following:
30153015
// strncat(dst, src, sizeof(dst) - strlen(dest) - 1);
3016-
void CheckStrncatArguments(const CallExpr *Call, IdentifierInfo *FnName);
3016+
void CheckStrncatArguments(const CallExpr *Call,
3017+
const IdentifierInfo *FnName);
30173018

30183019
/// Alerts the user that they are attempting to free a non-malloc'd object.
30193020
void CheckFreeArguments(const CallExpr *E);

clang/include/clang/Sema/SemaObjC.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ class SemaObjC : public SemaBase {
170170
bool isSignedCharBool(QualType Ty);
171171

172172
void adornBoolConversionDiagWithTernaryFixit(
173-
Expr *SourceExpr, const Sema::SemaDiagnosticBuilder &Builder);
173+
const Expr *SourceExpr, const Sema::SemaDiagnosticBuilder &Builder);
174174

175175
/// Check an Objective-C dictionary literal being converted to the given
176176
/// target type.

clang/lib/Sema/SemaChecking.cpp

Lines changed: 70 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -9358,10 +9358,10 @@ void Sema::CheckMaxUnsignedZero(const CallExpr *Call,
93589358
///
93599359
/// This is to catch typos like `if (memcmp(&a, &b, sizeof(a) > 0))`.
93609360
static bool CheckMemorySizeofForComparison(Sema &S, const Expr *E,
9361-
IdentifierInfo *FnName,
9361+
const IdentifierInfo *FnName,
93629362
SourceLocation FnLoc,
93639363
SourceLocation RParenLoc) {
9364-
const BinaryOperator *Size = dyn_cast<BinaryOperator>(E);
9364+
const auto *Size = dyn_cast<BinaryOperator>(E);
93659365
if (!Size)
93669366
return false;
93679367

@@ -9955,7 +9955,7 @@ static const Expr *getStrlenExprArg(const Expr *E) {
99559955
}
99569956

99579957
void Sema::CheckStrncatArguments(const CallExpr *CE,
9958-
IdentifierInfo *FnName) {
9958+
const IdentifierInfo *FnName) {
99599959
// Don't crash if the user has the wrong number of arguments.
99609960
if (CE->getNumArgs() < 3)
99619961
return;
@@ -10050,7 +10050,7 @@ void CheckFreeArgumentsAddressof(Sema &S, const std::string &CalleeName,
1005010050
const UnaryOperator *UnaryExpr) {
1005110051
if (const auto *Lvalue = dyn_cast<DeclRefExpr>(UnaryExpr->getSubExpr())) {
1005210052
const Decl *D = Lvalue->getDecl();
10053-
if (auto *DD = dyn_cast<DeclaratorDecl>(D)) {
10053+
if (const auto *DD = dyn_cast<DeclaratorDecl>(D)) {
1005410054
if (!DD->getType()->isReferenceType())
1005510055
return CheckFreeArgumentsOnLvalue(S, CalleeName, UnaryExpr, D);
1005610056
}
@@ -10190,15 +10190,15 @@ Sema::CheckReturnValExpr(Expr *RetValExp, QualType lhsType,
1019010190
PPC().CheckPPCMMAType(RetValExp->getType(), ReturnLoc);
1019110191
}
1019210192

10193-
void Sema::CheckFloatComparison(SourceLocation Loc, Expr *LHS, Expr *RHS,
10194-
BinaryOperatorKind Opcode) {
10193+
void Sema::CheckFloatComparison(SourceLocation Loc, const Expr *LHS,
10194+
const Expr *RHS, BinaryOperatorKind Opcode) {
1019510195
if (!BinaryOperator::isEqualityOp(Opcode))
1019610196
return;
1019710197

1019810198
// Match and capture subexpressions such as "(float) X == 0.1".
10199-
FloatingLiteral *FPLiteral;
10200-
CastExpr *FPCast;
10201-
auto getCastAndLiteral = [&FPLiteral, &FPCast](Expr *L, Expr *R) {
10199+
const FloatingLiteral *FPLiteral;
10200+
const CastExpr *FPCast;
10201+
auto getCastAndLiteral = [&FPLiteral, &FPCast](const Expr *L, const Expr *R) {
1020210202
FPLiteral = dyn_cast<FloatingLiteral>(L->IgnoreParens());
1020310203
FPCast = dyn_cast<CastExpr>(R->IgnoreParens());
1020410204
return FPLiteral && FPCast;
@@ -10225,13 +10225,13 @@ void Sema::CheckFloatComparison(SourceLocation Loc, Expr *LHS, Expr *RHS,
1022510225
}
1022610226

1022710227
// Match a more general floating-point equality comparison (-Wfloat-equal).
10228-
Expr* LeftExprSansParen = LHS->IgnoreParenImpCasts();
10229-
Expr* RightExprSansParen = RHS->IgnoreParenImpCasts();
10228+
const Expr *LeftExprSansParen = LHS->IgnoreParenImpCasts();
10229+
const Expr *RightExprSansParen = RHS->IgnoreParenImpCasts();
1023010230

1023110231
// Special case: check for x == x (which is OK).
1023210232
// Do not emit warnings for such cases.
10233-
if (auto *DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen))
10234-
if (auto *DRR = dyn_cast<DeclRefExpr>(RightExprSansParen))
10233+
if (const auto *DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen))
10234+
if (const auto *DRR = dyn_cast<DeclRefExpr>(RightExprSansParen))
1023510235
if (DRL->getDecl() == DRR->getDecl())
1023610236
return;
1023710237

@@ -10240,22 +10240,21 @@ void Sema::CheckFloatComparison(SourceLocation Loc, Expr *LHS, Expr *RHS,
1024010240
// is a heuristic: often comparison against such literals are used to
1024110241
// detect if a value in a variable has not changed. This clearly can
1024210242
// lead to false negatives.
10243-
if (FloatingLiteral* FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) {
10243+
if (const auto *FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) {
1024410244
if (FLL->isExact())
1024510245
return;
10246-
} else
10247-
if (FloatingLiteral* FLR = dyn_cast<FloatingLiteral>(RightExprSansParen))
10248-
if (FLR->isExact())
10249-
return;
10246+
} else if (const auto *FLR = dyn_cast<FloatingLiteral>(RightExprSansParen))
10247+
if (FLR->isExact())
10248+
return;
1025010249

1025110250
// Check for comparisons with builtin types.
10252-
if (CallExpr* CL = dyn_cast<CallExpr>(LeftExprSansParen))
10253-
if (CL->getBuiltinCallee())
10254-
return;
10251+
if (const auto *CL = dyn_cast<CallExpr>(LeftExprSansParen);
10252+
CL && CL->getBuiltinCallee())
10253+
return;
1025510254

10256-
if (CallExpr* CR = dyn_cast<CallExpr>(RightExprSansParen))
10257-
if (CR->getBuiltinCallee())
10258-
return;
10255+
if (const auto *CR = dyn_cast<CallExpr>(RightExprSansParen);
10256+
CR && CR->getBuiltinCallee())
10257+
return;
1025910258

1026010259
// Emit the diagnostic.
1026110260
Diag(Loc, diag::warn_floatingpoint_eq)
@@ -10302,18 +10301,18 @@ struct IntRange {
1030210301
static IntRange forValueOfCanonicalType(ASTContext &C, const Type *T) {
1030310302
assert(T->isCanonicalUnqualified());
1030410303

10305-
if (const VectorType *VT = dyn_cast<VectorType>(T))
10304+
if (const auto *VT = dyn_cast<VectorType>(T))
1030610305
T = VT->getElementType().getTypePtr();
10307-
if (const ComplexType *CT = dyn_cast<ComplexType>(T))
10306+
if (const auto *CT = dyn_cast<ComplexType>(T))
1030810307
T = CT->getElementType().getTypePtr();
10309-
if (const AtomicType *AT = dyn_cast<AtomicType>(T))
10308+
if (const auto *AT = dyn_cast<AtomicType>(T))
1031010309
T = AT->getValueType().getTypePtr();
1031110310

1031210311
if (!C.getLangOpts().CPlusPlus) {
1031310312
// For enum types in C code, use the underlying datatype.
10314-
if (const EnumType *ET = dyn_cast<EnumType>(T))
10313+
if (const auto *ET = dyn_cast<EnumType>(T))
1031510314
T = ET->getDecl()->getIntegerType().getDesugaredType(C).getTypePtr();
10316-
} else if (const EnumType *ET = dyn_cast<EnumType>(T)) {
10315+
} else if (const auto *ET = dyn_cast<EnumType>(T)) {
1031710316
// For enum types in C++, use the known bit width of the enumerators.
1031810317
EnumDecl *Enum = ET->getDecl();
1031910318
// In C++11, enums can have a fixed underlying type. Use this type to
@@ -10432,8 +10431,7 @@ struct IntRange {
1043210431

1043310432
} // namespace
1043410433

10435-
static IntRange GetValueRange(ASTContext &C, llvm::APSInt &value,
10436-
unsigned MaxWidth) {
10434+
static IntRange GetValueRange(llvm::APSInt &value, unsigned MaxWidth) {
1043710435
if (value.isSigned() && value.isNegative())
1043810436
return IntRange(value.getSignificantBits(), false);
1043910437

@@ -10445,23 +10443,22 @@ static IntRange GetValueRange(ASTContext &C, llvm::APSInt &value,
1044510443
return IntRange(value.getActiveBits(), true);
1044610444
}
1044710445

10448-
static IntRange GetValueRange(ASTContext &C, APValue &result, QualType Ty,
10449-
unsigned MaxWidth) {
10446+
static IntRange GetValueRange(APValue &result, QualType Ty, unsigned MaxWidth) {
1045010447
if (result.isInt())
10451-
return GetValueRange(C, result.getInt(), MaxWidth);
10448+
return GetValueRange(result.getInt(), MaxWidth);
1045210449

1045310450
if (result.isVector()) {
10454-
IntRange R = GetValueRange(C, result.getVectorElt(0), Ty, MaxWidth);
10451+
IntRange R = GetValueRange(result.getVectorElt(0), Ty, MaxWidth);
1045510452
for (unsigned i = 1, e = result.getVectorLength(); i != e; ++i) {
10456-
IntRange El = GetValueRange(C, result.getVectorElt(i), Ty, MaxWidth);
10453+
IntRange El = GetValueRange(result.getVectorElt(i), Ty, MaxWidth);
1045710454
R = IntRange::join(R, El);
1045810455
}
1045910456
return R;
1046010457
}
1046110458

1046210459
if (result.isComplexInt()) {
10463-
IntRange R = GetValueRange(C, result.getComplexIntReal(), MaxWidth);
10464-
IntRange I = GetValueRange(C, result.getComplexIntImag(), MaxWidth);
10460+
IntRange R = GetValueRange(result.getComplexIntReal(), MaxWidth);
10461+
IntRange I = GetValueRange(result.getComplexIntImag(), MaxWidth);
1046510462
return IntRange::join(R, I);
1046610463
}
1046710464

@@ -10476,7 +10473,7 @@ static IntRange GetValueRange(ASTContext &C, APValue &result, QualType Ty,
1047610473

1047710474
static QualType GetExprType(const Expr *E) {
1047810475
QualType Ty = E->getType();
10479-
if (const AtomicType *AtomicRHS = Ty->getAs<AtomicType>())
10476+
if (const auto *AtomicRHS = Ty->getAs<AtomicType>())
1048010477
Ty = AtomicRHS->getValueType();
1048110478
return Ty;
1048210479
}
@@ -10503,7 +10500,7 @@ static std::optional<IntRange> TryGetExprRange(ASTContext &C, const Expr *E,
1050310500
// Try a full evaluation first.
1050410501
Expr::EvalResult result;
1050510502
if (E->EvaluateAsRValue(result, C, InConstantContext))
10506-
return GetValueRange(C, result.Val, GetExprType(E), MaxWidth);
10503+
return GetValueRange(result.Val, GetExprType(E), MaxWidth);
1050710504

1050810505
// I think we only want to look through implicit casts here; if the
1050910506
// user has an explicit widening cast, we should treat the value as
@@ -10856,10 +10853,9 @@ static bool IsSameFloatAfterCast(const APValue &value,
1085610853
static void AnalyzeImplicitConversions(Sema &S, Expr *E, SourceLocation CC,
1085710854
bool IsListInit = false);
1085810855

10859-
static bool IsEnumConstOrFromMacro(Sema &S, Expr *E) {
10856+
static bool IsEnumConstOrFromMacro(Sema &S, const Expr *E) {
1086010857
// Suppress cases where we are comparing against an enum constant.
10861-
if (const DeclRefExpr *DR =
10862-
dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts()))
10858+
if (const auto *DR = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts()))
1086310859
if (isa<EnumConstantDecl>(DR->getDecl()))
1086410860
return true;
1086510861

@@ -10877,7 +10873,7 @@ static bool IsEnumConstOrFromMacro(Sema &S, Expr *E) {
1087710873
return false;
1087810874
}
1087910875

10880-
static bool isKnownToHaveUnsignedValue(Expr *E) {
10876+
static bool isKnownToHaveUnsignedValue(const Expr *E) {
1088110877
return E->getType()->isIntegerType() &&
1088210878
(!E->getType()->isSignedIntegerType() ||
1088310879
!E->IgnoreParenImpCasts()->getType()->isSignedIntegerType());
@@ -11008,9 +11004,9 @@ struct PromotedRange {
1100811004
};
1100911005
}
1101011006

11011-
static bool HasEnumType(Expr *E) {
11007+
static bool HasEnumType(const Expr *E) {
1101211008
// Strip off implicit integral promotions.
11013-
while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
11009+
while (const auto *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1101411010
if (ICE->getCastKind() != CK_IntegralCast &&
1101511011
ICE->getCastKind() != CK_NoOp)
1101611012
break;
@@ -11128,7 +11124,7 @@ static bool CheckTautologicalComparison(Sema &S, BinaryOperator *E,
1112811124
// If this is a comparison to an enum constant, include that
1112911125
// constant in the diagnostic.
1113011126
const EnumConstantDecl *ED = nullptr;
11131-
if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Constant))
11127+
if (const auto *DR = dyn_cast<DeclRefExpr>(Constant))
1113211128
ED = dyn_cast<EnumConstantDecl>(DR->getDecl());
1113311129

1113411130
// Should be enough for uint128 (39 decimal digits)
@@ -11503,9 +11499,9 @@ static void AnalyzeAssignment(Sema &S, BinaryOperator *E) {
1150311499
}
1150411500

1150511501
/// Diagnose an implicit cast; purely a helper for CheckImplicitConversion.
11506-
static void DiagnoseImpCast(Sema &S, Expr *E, QualType SourceType, QualType T,
11507-
SourceLocation CContext, unsigned diag,
11508-
bool pruneControlFlow = false) {
11502+
static void DiagnoseImpCast(Sema &S, const Expr *E, QualType SourceType,
11503+
QualType T, SourceLocation CContext, unsigned diag,
11504+
bool PruneControlFlow = false) {
1150911505
// For languages like HLSL and OpenCL, implicit conversion diagnostics listing
1151011506
// address space annotations isn't really useful. The warnings aren't because
1151111507
// you're converting a `private int` to `unsigned int`, it is because you're
@@ -11514,7 +11510,7 @@ static void DiagnoseImpCast(Sema &S, Expr *E, QualType SourceType, QualType T,
1151411510
SourceType = S.getASTContext().removeAddrSpaceQualType(SourceType);
1151511511
if (T.hasAddressSpace())
1151611512
T = S.getASTContext().removeAddrSpaceQualType(T);
11517-
if (pruneControlFlow) {
11513+
if (PruneControlFlow) {
1151811514
S.DiagRuntimeBehavior(E->getExprLoc(), E,
1151911515
S.PDiag(diag)
1152011516
<< SourceType << T << E->getSourceRange()
@@ -11526,26 +11522,25 @@ static void DiagnoseImpCast(Sema &S, Expr *E, QualType SourceType, QualType T,
1152611522
}
1152711523

1152811524
/// Diagnose an implicit cast; purely a helper for CheckImplicitConversion.
11529-
static void DiagnoseImpCast(Sema &S, Expr *E, QualType T,
11530-
SourceLocation CContext,
11531-
unsigned diag, bool pruneControlFlow = false) {
11532-
DiagnoseImpCast(S, E, E->getType(), T, CContext, diag, pruneControlFlow);
11525+
static void DiagnoseImpCast(Sema &S, const Expr *E, QualType T,
11526+
SourceLocation CContext, unsigned diag,
11527+
bool PruneControlFlow = false) {
11528+
DiagnoseImpCast(S, E, E->getType(), T, CContext, diag, PruneControlFlow);
1153311529
}
1153411530

1153511531
/// Diagnose an implicit cast from a floating point value to an integer value.
11536-
static void DiagnoseFloatingImpCast(Sema &S, Expr *E, QualType T,
11532+
static void DiagnoseFloatingImpCast(Sema &S, const Expr *E, QualType T,
1153711533
SourceLocation CContext) {
11538-
const bool IsBool = T->isSpecificBuiltinType(BuiltinType::Bool);
11539-
const bool PruneWarnings = S.inTemplateInstantiation();
11534+
bool IsBool = T->isSpecificBuiltinType(BuiltinType::Bool);
11535+
bool PruneWarnings = S.inTemplateInstantiation();
1154011536

11541-
Expr *InnerE = E->IgnoreParenImpCasts();
11537+
const Expr *InnerE = E->IgnoreParenImpCasts();
1154211538
// We also want to warn on, e.g., "int i = -1.234"
11543-
if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(InnerE))
11539+
if (const auto *UOp = dyn_cast<UnaryOperator>(InnerE))
1154411540
if (UOp->getOpcode() == UO_Minus || UOp->getOpcode() == UO_Plus)
1154511541
InnerE = UOp->getSubExpr()->IgnoreParenImpCasts();
1154611542

11547-
const bool IsLiteral =
11548-
isa<FloatingLiteral>(E) || isa<FloatingLiteral>(InnerE);
11543+
bool IsLiteral = isa<FloatingLiteral>(E) || isa<FloatingLiteral>(InnerE);
1154911544

1155011545
llvm::APFloat Value(0.0);
1155111546
bool IsConstant =
@@ -11703,37 +11698,37 @@ static std::string PrettyPrintInRange(const llvm::APSInt &Value,
1170311698
return toString(ValueInRange, 10);
1170411699
}
1170511700

11706-
static bool IsImplicitBoolFloatConversion(Sema &S, Expr *Ex, bool ToBool) {
11701+
static bool IsImplicitBoolFloatConversion(Sema &S, const Expr *Ex,
11702+
bool ToBool) {
1170711703
if (!isa<ImplicitCastExpr>(Ex))
1170811704
return false;
1170911705

11710-
Expr *InnerE = Ex->IgnoreParenImpCasts();
11706+
const Expr *InnerE = Ex->IgnoreParenImpCasts();
1171111707
const Type *Target = S.Context.getCanonicalType(Ex->getType()).getTypePtr();
1171211708
const Type *Source =
1171311709
S.Context.getCanonicalType(InnerE->getType()).getTypePtr();
1171411710
if (Target->isDependentType())
1171511711
return false;
1171611712

11717-
const BuiltinType *FloatCandidateBT =
11718-
dyn_cast<BuiltinType>(ToBool ? Source : Target);
11713+
const auto *FloatCandidateBT =
11714+
dyn_cast<BuiltinType>(ToBool ? Source : Target);
1171911715
const Type *BoolCandidateType = ToBool ? Target : Source;
1172011716

1172111717
return (BoolCandidateType->isSpecificBuiltinType(BuiltinType::Bool) &&
1172211718
FloatCandidateBT && (FloatCandidateBT->isFloatingPoint()));
1172311719
}
1172411720

11725-
static void CheckImplicitArgumentConversions(Sema &S, CallExpr *TheCall,
11721+
static void CheckImplicitArgumentConversions(Sema &S, const CallExpr *TheCall,
1172611722
SourceLocation CC) {
11727-
unsigned NumArgs = TheCall->getNumArgs();
11728-
for (unsigned i = 0; i < NumArgs; ++i) {
11729-
Expr *CurrA = TheCall->getArg(i);
11723+
for (unsigned I = 0, N = TheCall->getNumArgs(); I < N; ++I) {
11724+
const Expr *CurrA = TheCall->getArg(I);
1173011725
if (!IsImplicitBoolFloatConversion(S, CurrA, true))
1173111726
continue;
1173211727

11733-
bool IsSwapped = ((i > 0) &&
11734-
IsImplicitBoolFloatConversion(S, TheCall->getArg(i - 1), false));
11735-
IsSwapped |= ((i < (NumArgs - 1)) &&
11736-
IsImplicitBoolFloatConversion(S, TheCall->getArg(i + 1), false));
11728+
bool IsSwapped = ((I > 0) && IsImplicitBoolFloatConversion(
11729+
S, TheCall->getArg(I - 1), false));
11730+
IsSwapped |= ((I < (N - 1)) && IsImplicitBoolFloatConversion(
11731+
S, TheCall->getArg(I + 1), false));
1173711732
if (IsSwapped) {
1173811733
// Warn on this floating-point to bool conversion.
1173911734
DiagnoseImpCast(S, CurrA->IgnoreParenImpCasts(),
@@ -12568,7 +12563,7 @@ static void AnalyzeImplicitConversions(
1256812563
}
1256912564

1257012565
// Check implicit argument conversions for function calls.
12571-
if (CallExpr *Call = dyn_cast<CallExpr>(SourceExpr))
12566+
if (const auto *Call = dyn_cast<CallExpr>(SourceExpr))
1257212567
CheckImplicitArgumentConversions(S, Call, CC);
1257312568

1257412569
// Go ahead and check any implicit conversions we might have skipped.

clang/lib/Sema/SemaObjC.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2314,8 +2314,8 @@ bool SemaObjC::isSignedCharBool(QualType Ty) {
23142314
}
23152315

23162316
void SemaObjC::adornBoolConversionDiagWithTernaryFixit(
2317-
Expr *SourceExpr, const Sema::SemaDiagnosticBuilder &Builder) {
2318-
Expr *Ignored = SourceExpr->IgnoreImplicit();
2317+
const Expr *SourceExpr, const Sema::SemaDiagnosticBuilder &Builder) {
2318+
const Expr *Ignored = SourceExpr->IgnoreImplicit();
23192319
if (const auto *OVE = dyn_cast<OpaqueValueExpr>(Ignored))
23202320
Ignored = OVE->getSourceExpr();
23212321
bool NeedsParens = isa<AbstractConditionalOperator>(Ignored) ||

0 commit comments

Comments
 (0)