@@ -414,80 +414,83 @@ bool Sema::DiagnoseUseOfDecl(NamedDecl *D, ArrayRef<SourceLocation> Locs,
414
414
/// message-send is to a declaration with the sentinel attribute, and
415
415
/// if so, it checks that the requirements of the sentinel are
416
416
/// satisfied.
417
- void Sema::DiagnoseSentinelCalls(NamedDecl *D, SourceLocation Loc,
417
+ void Sema::DiagnoseSentinelCalls(const NamedDecl *D, SourceLocation Loc,
418
418
ArrayRef<Expr *> Args) {
419
- const SentinelAttr *attr = D->getAttr<SentinelAttr>();
420
- if (!attr )
419
+ const SentinelAttr *Attr = D->getAttr<SentinelAttr>();
420
+ if (!Attr )
421
421
return;
422
422
423
423
// The number of formal parameters of the declaration.
424
- unsigned numFormalParams ;
424
+ unsigned NumFormalParams ;
425
425
426
426
// The kind of declaration. This is also an index into a %select in
427
427
// 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;
446
447
} else {
447
448
return;
448
449
}
449
450
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;
455
455
} else {
456
456
return;
457
457
}
458
458
459
- // "nullPos " is the number of formal parameters at the end which
459
+ // "NullPos " is the number of formal parameters at the end which
460
460
// effectively count as part of the variadic arguments. This is
461
461
// useful if you would prefer to not have *any* formal parameters,
462
462
// 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 );
466
466
467
467
// The number of arguments which should follow the sentinel.
468
- unsigned numArgsAfterSentinel = attr ->getSentinel();
468
+ unsigned NumArgsAfterSentinel = Attr ->getSentinel();
469
469
470
470
// If there aren't enough arguments for all the formal parameters,
471
471
// the sentinel, and the args after the sentinel, complain.
472
- if (Args.size() < numFormalParams + numArgsAfterSentinel + 1) {
472
+ if (Args.size() < NumFormalParams + NumArgsAfterSentinel + 1) {
473
473
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 );
475
475
return;
476
476
}
477
477
478
478
// 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;
483
486
484
487
// Pick a reasonable string to insert. Optimistically use 'nil', 'nullptr',
485
488
// or 'NULL' if those are actually defined in the context. Only use
486
489
// 'nil' for ObjC methods, where it's much more likely that the
487
490
// variadic arguments form a list of object pointers.
488
- SourceLocation MissingNilLoc = getLocForEndOfToken(sentinelExpr ->getEndLoc());
491
+ SourceLocation MissingNilLoc = getLocForEndOfToken(SentinelExpr ->getEndLoc());
489
492
std::string NullValue;
490
- if (calleeType == CT_Method && PP.isMacroDefined("nil"))
493
+ if (CalleeKind == CK_Method && PP.isMacroDefined("nil"))
491
494
NullValue = "nil";
492
495
else if (getLangOpts().CPlusPlus11)
493
496
NullValue = "nullptr";
@@ -497,12 +500,13 @@ void Sema::DiagnoseSentinelCalls(NamedDecl *D, SourceLocation Loc,
497
500
NullValue = "(void*) 0";
498
501
499
502
if (MissingNilLoc.isInvalid())
500
- Diag(Loc, diag::warn_missing_sentinel) << int(calleeType );
503
+ Diag(Loc, diag::warn_missing_sentinel) << int(CalleeKind );
501
504
else
502
505
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();
506
510
}
507
511
508
512
SourceRange Sema::getExprRange(Expr *E) const {
0 commit comments