Skip to content

Commit 5480be1

Browse files
committed
[clang][NFC] Refactor Sema::DiagnoseSentinelCalls
Fix indentation, naming and capitalization to match current style guides.
1 parent f5b378b commit 5480be1

File tree

2 files changed

+49
-45
lines changed

2 files changed

+49
-45
lines changed

clang/include/clang/Sema/Sema.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5452,7 +5452,7 @@ class Sema final {
54525452
bool DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *PD,
54535453
ObjCMethodDecl *Getter,
54545454
SourceLocation Loc);
5455-
void DiagnoseSentinelCalls(NamedDecl *D, SourceLocation Loc,
5455+
void DiagnoseSentinelCalls(const NamedDecl *D, SourceLocation Loc,
54565456
ArrayRef<Expr *> Args);
54575457

54585458
void PushExpressionEvaluationContext(

clang/lib/Sema/SemaExpr.cpp

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -414,80 +414,83 @@ bool Sema::DiagnoseUseOfDecl(NamedDecl *D, ArrayRef<SourceLocation> Locs,
414414
/// message-send is to a declaration with the sentinel attribute, and
415415
/// if so, it checks that the requirements of the sentinel are
416416
/// satisfied.
417-
void Sema::DiagnoseSentinelCalls(NamedDecl *D, SourceLocation Loc,
417+
void Sema::DiagnoseSentinelCalls(const NamedDecl *D, SourceLocation Loc,
418418
ArrayRef<Expr *> Args) {
419-
const SentinelAttr *attr = D->getAttr<SentinelAttr>();
420-
if (!attr)
419+
const SentinelAttr *Attr = D->getAttr<SentinelAttr>();
420+
if (!Attr)
421421
return;
422422

423423
// The number of formal parameters of the declaration.
424-
unsigned numFormalParams;
424+
unsigned NumFormalParams;
425425

426426
// The kind of declaration. This is also an index into a %select in
427427
// the diagnostic.
428-
enum CalleeType { CT_Function, CT_Method, CT_Block } calleeType;
429-
430-
if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
431-
numFormalParams = MD->param_size();
432-
calleeType = CT_Method;
433-
} else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
434-
numFormalParams = FD->param_size();
435-
calleeType = CT_Function;
436-
} else if (isa<VarDecl>(D)) {
437-
QualType type = cast<ValueDecl>(D)->getType();
438-
const FunctionType *fn = nullptr;
439-
if (const PointerType *ptr = type->getAs<PointerType>()) {
440-
fn = ptr->getPointeeType()->getAs<FunctionType>();
441-
if (!fn) return;
442-
calleeType = CT_Function;
443-
} else if (const BlockPointerType *ptr = type->getAs<BlockPointerType>()) {
444-
fn = ptr->getPointeeType()->castAs<FunctionType>();
445-
calleeType = CT_Block;
428+
enum { CK_Function, CK_Method, CK_Block } CalleeKind;
429+
430+
if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
431+
NumFormalParams = MD->param_size();
432+
CalleeKind = CK_Method;
433+
} else if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
434+
NumFormalParams = FD->param_size();
435+
CalleeKind = CK_Function;
436+
} else if (const auto *VD = dyn_cast<VarDecl>(D)) {
437+
QualType Ty = VD->getType();
438+
const FunctionType *Fn = nullptr;
439+
if (const auto *PtrTy = Ty->getAs<PointerType>()) {
440+
Fn = PtrTy->getPointeeType()->getAs<FunctionType>();
441+
if (!Fn)
442+
return;
443+
CalleeKind = CK_Function;
444+
} else if (const auto *PtrTy = Ty->getAs<BlockPointerType>()) {
445+
Fn = PtrTy->getPointeeType()->castAs<FunctionType>();
446+
CalleeKind = CK_Block;
446447
} else {
447448
return;
448449
}
449450

450-
if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(fn)) {
451-
numFormalParams = proto->getNumParams();
452-
} else {
453-
numFormalParams = 0;
454-
}
451+
if (const auto *proto = dyn_cast<FunctionProtoType>(Fn))
452+
NumFormalParams = proto->getNumParams();
453+
else
454+
NumFormalParams = 0;
455455
} else {
456456
return;
457457
}
458458

459-
// "nullPos" is the number of formal parameters at the end which
459+
// "NullPos" is the number of formal parameters at the end which
460460
// effectively count as part of the variadic arguments. This is
461461
// useful if you would prefer to not have *any* formal parameters,
462462
// but the language forces you to have at least one.
463-
unsigned nullPos = attr->getNullPos();
464-
assert((nullPos == 0 || nullPos == 1) && "invalid null position on sentinel");
465-
numFormalParams = (nullPos > numFormalParams ? 0 : numFormalParams - nullPos);
463+
unsigned NullPos = Attr->getNullPos();
464+
assert((NullPos == 0 || NullPos == 1) && "invalid null position on sentinel");
465+
NumFormalParams = (NullPos > NumFormalParams ? 0 : NumFormalParams - NullPos);
466466

467467
// The number of arguments which should follow the sentinel.
468-
unsigned numArgsAfterSentinel = attr->getSentinel();
468+
unsigned NumArgsAfterSentinel = Attr->getSentinel();
469469

470470
// If there aren't enough arguments for all the formal parameters,
471471
// the sentinel, and the args after the sentinel, complain.
472-
if (Args.size() < numFormalParams + numArgsAfterSentinel + 1) {
472+
if (Args.size() < NumFormalParams + NumArgsAfterSentinel + 1) {
473473
Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName();
474-
Diag(D->getLocation(), diag::note_sentinel_here) << int(calleeType);
474+
Diag(D->getLocation(), diag::note_sentinel_here) << int(CalleeKind);
475475
return;
476476
}
477477

478478
// Otherwise, find the sentinel expression.
479-
Expr *sentinelExpr = Args[Args.size() - numArgsAfterSentinel - 1];
480-
if (!sentinelExpr) return;
481-
if (sentinelExpr->isValueDependent()) return;
482-
if (Context.isSentinelNullExpr(sentinelExpr)) return;
479+
const Expr *SentinelExpr = Args[Args.size() - NumArgsAfterSentinel - 1];
480+
if (!SentinelExpr)
481+
return;
482+
if (SentinelExpr->isValueDependent())
483+
return;
484+
if (Context.isSentinelNullExpr(SentinelExpr))
485+
return;
483486

484487
// Pick a reasonable string to insert. Optimistically use 'nil', 'nullptr',
485488
// or 'NULL' if those are actually defined in the context. Only use
486489
// 'nil' for ObjC methods, where it's much more likely that the
487490
// variadic arguments form a list of object pointers.
488-
SourceLocation MissingNilLoc = getLocForEndOfToken(sentinelExpr->getEndLoc());
491+
SourceLocation MissingNilLoc = getLocForEndOfToken(SentinelExpr->getEndLoc());
489492
std::string NullValue;
490-
if (calleeType == CT_Method && PP.isMacroDefined("nil"))
493+
if (CalleeKind == CK_Method && PP.isMacroDefined("nil"))
491494
NullValue = "nil";
492495
else if (getLangOpts().CPlusPlus11)
493496
NullValue = "nullptr";
@@ -497,12 +500,13 @@ void Sema::DiagnoseSentinelCalls(NamedDecl *D, SourceLocation Loc,
497500
NullValue = "(void*) 0";
498501

499502
if (MissingNilLoc.isInvalid())
500-
Diag(Loc, diag::warn_missing_sentinel) << int(calleeType);
503+
Diag(Loc, diag::warn_missing_sentinel) << int(CalleeKind);
501504
else
502505
Diag(MissingNilLoc, diag::warn_missing_sentinel)
503-
<< int(calleeType)
504-
<< FixItHint::CreateInsertion(MissingNilLoc, ", " + NullValue);
505-
Diag(D->getLocation(), diag::note_sentinel_here) << int(calleeType);
506+
<< int(CalleeKind)
507+
<< FixItHint::CreateInsertion(MissingNilLoc, ", " + NullValue);
508+
Diag(D->getLocation(), diag::note_sentinel_here)
509+
<< int(CalleeKind) << Attr->getRange();
506510
}
507511

508512
SourceRange Sema::getExprRange(Expr *E) const {

0 commit comments

Comments
 (0)