[Sema] Migrate away from PointerUnion::{is,get} (NFC) (#117498)

Note that PointerUnion::{is,get} have been soft deprecated in
PointerUnion.h:

  // FIXME: Replace the uses of is(), get() and dyn_cast() with
  //        isa<T>, cast<T> and the llvm::dyn_cast<T>

I'm not touching PointerUnion::dyn_cast for now because it's a bit
complicated; we could blindly migrate it to dyn_cast_if_present, but
we should probably use dyn_cast when the operand is known to be
non-null.
This commit is contained in:
Kazu Hirata 2024-11-27 09:13:28 -08:00 committed by GitHub
parent 2f02b5af6e
commit 1e3e199ed9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 54 additions and 57 deletions

View File

@ -482,7 +482,7 @@ static void ProcessAPINotes(Sema &S, FunctionOrMethod AnyFunc,
Decl *D = FD;
ObjCMethodDecl *MD = nullptr;
if (!D) {
MD = AnyFunc.get<ObjCMethodDecl *>();
MD = cast<ObjCMethodDecl *>(AnyFunc);
D = MD;
}

View File

@ -131,8 +131,8 @@ private:
}
// Add the new element to the end of the vector.
DeclOrVector.get<DeclIndexPairVector *>()->push_back(
DeclIndexPair(ND, Index));
cast<DeclIndexPairVector *>(DeclOrVector)
->push_back(DeclIndexPair(ND, Index));
}
~ShadowMapEntry() {
@ -659,13 +659,13 @@ public:
: DeclOrIterator(Iterator), SingleDeclIndex(0) {}
iterator &operator++() {
if (DeclOrIterator.is<const NamedDecl *>()) {
if (isa<const NamedDecl *>(DeclOrIterator)) {
DeclOrIterator = (NamedDecl *)nullptr;
SingleDeclIndex = 0;
return *this;
}
const DeclIndexPair *I = DeclOrIterator.get<const DeclIndexPair *>();
const DeclIndexPair *I = cast<const DeclIndexPair *>(DeclOrIterator);
++I;
DeclOrIterator = I;
return *this;
@ -681,7 +681,7 @@ public:
if (const NamedDecl *ND = DeclOrIterator.dyn_cast<const NamedDecl *>())
return reference(ND, SingleDeclIndex);
return *DeclOrIterator.get<const DeclIndexPair *>();
return *cast<const DeclIndexPair *>(DeclOrIterator);
}
pointer operator->() const { return pointer(**this); }
@ -705,15 +705,15 @@ ResultBuilder::ShadowMapEntry::begin() const {
if (const NamedDecl *ND = DeclOrVector.dyn_cast<const NamedDecl *>())
return iterator(ND, SingleDeclIndex);
return iterator(DeclOrVector.get<DeclIndexPairVector *>()->begin());
return iterator(cast<DeclIndexPairVector *>(DeclOrVector)->begin());
}
ResultBuilder::ShadowMapEntry::iterator
ResultBuilder::ShadowMapEntry::end() const {
if (DeclOrVector.is<const NamedDecl *>() || DeclOrVector.isNull())
if (isa<const NamedDecl *>(DeclOrVector) || DeclOrVector.isNull())
return iterator();
return iterator(DeclOrVector.get<DeclIndexPairVector *>()->end());
return iterator(cast<DeclIndexPairVector *>(DeclOrVector)->end());
}
/// Compute the qualification required to get from the current context

View File

@ -1384,8 +1384,7 @@ static void diagnoseUnsatisfiedConstraintExpr(
return;
}
diagnoseWellFormedUnsatisfiedConstraintExpr(S,
Record.template get<Expr *>(), First);
diagnoseWellFormedUnsatisfiedConstraintExpr(S, cast<Expr *>(Record), First);
}
void
@ -1557,12 +1556,12 @@ NormalizedConstraint::NormalizedConstraint(ASTContext &C,
NormalizedConstraint &NormalizedConstraint::getLHS() const {
assert(isCompound() && "getLHS called on a non-compound constraint.");
return Constraint.get<CompoundConstraint>().getPointer()->LHS;
return cast<CompoundConstraint>(Constraint).getPointer()->LHS;
}
NormalizedConstraint &NormalizedConstraint::getRHS() const {
assert(isCompound() && "getRHS called on a non-compound constraint.");
return Constraint.get<CompoundConstraint>().getPointer()->RHS;
return cast<CompoundConstraint>(Constraint).getPointer()->RHS;
}
std::optional<NormalizedConstraint>

View File

@ -17276,7 +17276,7 @@ Sema::ActOnTag(Scope *S, unsigned TagSpec, TagUseKind TUK, SourceLocation KWLoc,
if (TypeSourceInfo *TI = EnumUnderlying.dyn_cast<TypeSourceInfo *>())
ED->setIntegerTypeSourceInfo(TI);
else
ED->setIntegerType(QualType(EnumUnderlying.get<const Type *>(), 0));
ED->setIntegerType(QualType(cast<const Type *>(EnumUnderlying), 0));
QualType EnumTy = ED->getIntegerType();
ED->setPromotionType(Context.isPromotableIntegerType(EnumTy)
? Context.getPromotedIntegerType(EnumTy)
@ -17909,7 +17909,7 @@ CreateNewDecl:
if (TypeSourceInfo *TI = EnumUnderlying.dyn_cast<TypeSourceInfo*>())
ED->setIntegerTypeSourceInfo(TI);
else
ED->setIntegerType(QualType(EnumUnderlying.get<const Type *>(), 0));
ED->setIntegerType(QualType(cast<const Type *>(EnumUnderlying), 0));
QualType EnumTy = ED->getIntegerType();
ED->setPromotionType(Context.isPromotableIntegerType(EnumTy)
? Context.getPromotedIntegerType(EnumTy)
@ -19925,7 +19925,7 @@ static void CheckForDuplicateEnumValues(Sema &S, ArrayRef<Decl *> Elements,
continue;
}
ECDVector *Vec = Entry.get<ECDVector*>();
ECDVector *Vec = cast<ECDVector *>(Entry);
// Make sure constants are not added more than once.
if (*Vec->begin() == ECD)
continue;

View File

@ -776,9 +776,9 @@ static void handleDiagnoseAsBuiltinAttr(Sema &S, Decl *D,
auto DiagnoseType = [&](unsigned Index, AttributeArgumentNType T) {
SourceLocation Loc = [&]() {
auto Union = AL.getArg(Index - 1);
if (Union.is<Expr *>())
return Union.get<Expr *>()->getBeginLoc();
return Union.get<IdentifierLoc *>()->Loc;
if (auto *E = dyn_cast<Expr *>(Union))
return E->getBeginLoc();
return cast<IdentifierLoc *>(Union)->Loc;
}();
S.Diag(Loc, diag::err_attribute_argument_n_type) << AL << Index << T;

View File

@ -9218,7 +9218,7 @@ struct SpecialMemberVisitor {
if (auto *B = Subobj.dyn_cast<CXXBaseSpecifier*>())
return B->getBaseTypeLoc();
else
return Subobj.get<FieldDecl*>()->getLocation();
return cast<FieldDecl *>(Subobj)->getLocation();
}
enum BasesToVisit {
@ -9369,7 +9369,7 @@ bool SpecialMemberDeletionInfo::shouldDeleteForSubobjectCall(
<< /*IsField*/ true << Field << DiagKind << IsDtorCallInCtor
<< /*IsObjCPtr*/ false;
} else {
CXXBaseSpecifier *Base = Subobj.get<CXXBaseSpecifier*>();
CXXBaseSpecifier *Base = cast<CXXBaseSpecifier *>(Subobj);
S.Diag(Base->getBeginLoc(),
diag::note_deleted_special_member_class_subobject)
<< llvm::to_underlying(getEffectiveCSM()) << MD->getParent()
@ -17493,7 +17493,7 @@ DeclResult Sema::ActOnTemplatedFriendTag(
if (getDepthAndIndex(U).first >= FriendDeclDepth) {
auto *ND = U.first.dyn_cast<NamedDecl *>();
if (!ND)
ND = U.first.get<const TemplateTypeParmType *>()->getDecl();
ND = cast<const TemplateTypeParmType *>(U.first)->getDecl();
Diag(U.second, diag::friend_template_decl_malformed_pack_expansion)
<< ND->getDeclName() << SourceRange(SS.getBeginLoc(), EllipsisLoc);
return true;

View File

@ -1587,7 +1587,7 @@ void SemaObjC::actOnObjCTypeArgsOrProtocolQualifiers(
if (auto *actualTypeDecl = typeDecl.dyn_cast<TypeDecl *>())
type = Context.getTypeDeclType(actualTypeDecl);
else
type = Context.getObjCInterfaceType(typeDecl.get<ObjCInterfaceDecl *>());
type = Context.getObjCInterfaceType(cast<ObjCInterfaceDecl *>(typeDecl));
TypeSourceInfo *parsedTSInfo = Context.getTrivialTypeSourceInfo(type, loc);
ParsedType parsedType = SemaRef.CreateParsedType(type, parsedTSInfo);
DS.SetTypeSpecType(DeclSpec::TST_typename, loc, prevSpec, diagID,

View File

@ -516,7 +516,7 @@ class Analyzer {
CompleteFunctionAnalysis *completedAnalysisForDecl(const Decl *D) const {
if (FuncAnalysisPtr AP = lookup(D);
isa_and_nonnull<CompleteFunctionAnalysis *>(AP))
return AP.get<CompleteFunctionAnalysis *>();
return cast<CompleteFunctionAnalysis *>(AP);
return nullptr;
}
@ -528,12 +528,10 @@ class Analyzer {
OS << item.first << " " << CI.getNameForDiagnostic(SemaRef) << " : ";
if (AP.isNull()) {
OS << "null\n";
} else if (isa<CompleteFunctionAnalysis *>(AP)) {
auto *CFA = AP.get<CompleteFunctionAnalysis *>();
} else if (auto *CFA = dyn_cast<CompleteFunctionAnalysis *>(AP)) {
OS << CFA << " ";
CFA->dump(OS);
} else if (isa<PendingFunctionAnalysis *>(AP)) {
auto *PFA = AP.get<PendingFunctionAnalysis *>();
} else if (auto *PFA = dyn_cast<PendingFunctionAnalysis *>(AP)) {
OS << PFA << " ";
PFA->dump(SemaRef, OS);
} else
@ -1376,10 +1374,10 @@ private:
Analyzer::AnalysisMap::~AnalysisMap() {
for (const auto &Item : *this) {
FuncAnalysisPtr AP = Item.second;
if (isa<PendingFunctionAnalysis *>(AP))
delete AP.get<PendingFunctionAnalysis *>();
if (auto *PFA = dyn_cast<PendingFunctionAnalysis *>(AP))
delete PFA;
else
delete AP.get<CompleteFunctionAnalysis *>();
delete cast<CompleteFunctionAnalysis *>(AP);
}
}

View File

@ -1611,10 +1611,10 @@ const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
continue;
const ReductionData &ReductionData = I->ReductionMap.lookup(D);
if (!ReductionData.ReductionOp ||
ReductionData.ReductionOp.is<const Expr *>())
isa<const Expr *>(ReductionData.ReductionOp))
return DSAVarData();
SR = ReductionData.ReductionRange;
BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>();
BOK = cast<ReductionData::BOKPtrType>(ReductionData.ReductionOp);
assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
"expression for the descriptor is not "
"set.");
@ -1638,10 +1638,10 @@ const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
continue;
const ReductionData &ReductionData = I->ReductionMap.lookup(D);
if (!ReductionData.ReductionOp ||
!ReductionData.ReductionOp.is<const Expr *>())
!isa<const Expr *>(ReductionData.ReductionOp))
return DSAVarData();
SR = ReductionData.ReductionRange;
ReductionRef = ReductionData.ReductionOp.get<const Expr *>();
ReductionRef = cast<const Expr *>(ReductionData.ReductionOp);
assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
"expression for the descriptor is not "
"set.");

View File

@ -198,7 +198,7 @@ HandleVarTemplateSpec(const VarTemplateSpecializationDecl *VarTemplSpec,
if (Partial->isMemberSpecialization())
return Response::Done();
} else {
VarTemplateDecl *Tmpl = Specialized.get<VarTemplateDecl *>();
VarTemplateDecl *Tmpl = cast<VarTemplateDecl *>(Specialized);
if (!SkipForSpecialization)
Result.addOuterTemplateArguments(
Tmpl, VarTemplSpec->getTemplateInstantiationArgs().asArray(),
@ -2458,7 +2458,7 @@ TemplateInstantiator::TransformFunctionParmPackRefExpr(DeclRefExpr *E,
TransformedDecl = (*Pack)[getSema().ArgumentPackSubstitutionIndex];
} else {
TransformedDecl = Found->get<Decl*>();
TransformedDecl = cast<Decl *>(*Found);
}
// We have either an unexpanded pack or a specific expansion.
@ -2827,7 +2827,7 @@ TemplateInstantiator::TransformExprRequirement(concepts::ExprRequirement *Req) {
return RebuildExprRequirement(E, Req->isSimple(), Req->getNoexceptLoc(),
std::move(*TransRetReq));
return RebuildExprRequirement(
TransExpr.get<concepts::Requirement::SubstitutionDiagnostic *>(),
cast<concepts::Requirement::SubstitutionDiagnostic *>(TransExpr),
Req->isSimple(), Req->getNoexceptLoc(), std::move(*TransRetReq));
}
@ -4053,7 +4053,7 @@ getPatternForClassTemplateSpecialization(
llvm::PointerUnion<ClassTemplateDecl *,
ClassTemplatePartialSpecializationDecl *>
Specialized = ClassTemplateSpec->getSpecializedTemplateOrPartial();
if (!Specialized.is<ClassTemplatePartialSpecializationDecl *>()) {
if (!isa<ClassTemplatePartialSpecializationDecl *>(Specialized)) {
// Find best matching specialization.
ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
@ -4664,14 +4664,14 @@ void LocalInstantiationScope::InstantiatedLocal(const Decl *D, Decl *Inst) {
} else if (DeclArgumentPack *Pack = Stored.dyn_cast<DeclArgumentPack *>()) {
Pack->push_back(cast<VarDecl>(Inst));
} else {
assert(Stored.get<Decl *>() == Inst && "Already instantiated this local");
assert(cast<Decl *>(Stored) == Inst && "Already instantiated this local");
}
}
void LocalInstantiationScope::InstantiatedLocalPackArg(const Decl *D,
VarDecl *Inst) {
D = getCanonicalParmVarDecl(D);
DeclArgumentPack *Pack = LocalDecls[D].get<DeclArgumentPack *>();
DeclArgumentPack *Pack = cast<DeclArgumentPack *>(LocalDecls[D]);
Pack->push_back(Inst);
}

View File

@ -3720,8 +3720,8 @@ Decl *TemplateDeclInstantiator::VisitOMPDeclareReductionDecl(
auto *PrevDeclInScope = D->getPrevDeclInScope();
if (PrevDeclInScope && !PrevDeclInScope->isInvalidDecl()) {
PrevDeclInScope = cast<OMPDeclareReductionDecl>(
SemaRef.CurrentInstantiationScope->findInstantiationOf(PrevDeclInScope)
->get<Decl *>());
cast<Decl *>(*SemaRef.CurrentInstantiationScope->findInstantiationOf(
PrevDeclInScope)));
}
auto DRD = SemaRef.OpenMP().ActOnOpenMPDeclareReductionDirectiveStart(
/*S=*/nullptr, Owner, D->getDeclName(), ReductionTypes, D->getAccess(),
@ -3807,8 +3807,8 @@ TemplateDeclInstantiator::VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D) {
auto *PrevDeclInScope = D->getPrevDeclInScope();
if (PrevDeclInScope && !PrevDeclInScope->isInvalidDecl()) {
PrevDeclInScope = cast<OMPDeclareMapperDecl>(
SemaRef.CurrentInstantiationScope->findInstantiationOf(PrevDeclInScope)
->get<Decl *>());
cast<Decl *>(*SemaRef.CurrentInstantiationScope->findInstantiationOf(
PrevDeclInScope)));
}
bool IsCorrect = true;
SmallVector<OMPClause *, 6> Clauses;
@ -6186,7 +6186,7 @@ NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
assert(PackIdx != -1 &&
"found declaration pack but not pack expanding");
typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
return cast<NamedDecl>((*Found->get<DeclArgumentPack *>())[PackIdx]);
return cast<NamedDecl>((*cast<DeclArgumentPack *>(*Found))[PackIdx]);
}
}

View File

@ -371,7 +371,7 @@ Sema::DiagnoseUnexpandedParameterPacks(SourceLocation Loc,
auto *TTPD = dyn_cast<TemplateTypeParmDecl>(LocalPack);
return TTPD && TTPD->getTypeForDecl() == TTPT;
}
return declaresSameEntity(Pack.first.get<NamedDecl *>(), LocalPack);
return declaresSameEntity(cast<NamedDecl *>(Pack.first), LocalPack);
};
if (llvm::any_of(CSI->LocalPacks, DeclaresThisPack))
ParamPackReferences.push_back(Pack);
@ -423,7 +423,7 @@ Sema::DiagnoseUnexpandedParameterPacks(SourceLocation Loc,
= Unexpanded[I].first.dyn_cast<const TemplateTypeParmType *>())
Name = TTP->getIdentifier();
else
Name = Unexpanded[I].first.get<NamedDecl *>()->getIdentifier();
Name = cast<NamedDecl *>(Unexpanded[I].first)->getIdentifier();
if (Name && NamesKnown.insert(Name).second)
Names.push_back(Name);
@ -770,7 +770,7 @@ bool Sema::CheckParameterPacksForExpansion(
Index = TTP->getIndex();
Name = TTP->getIdentifier();
} else {
NamedDecl *ND = ParmPack.first.get<NamedDecl *>();
NamedDecl *ND = cast<NamedDecl *>(ParmPack.first);
if (isa<VarDecl>(ND))
IsVarDeclPack = true;
else
@ -787,10 +787,10 @@ bool Sema::CheckParameterPacksForExpansion(
llvm::PointerUnion<Decl *, DeclArgumentPack *> *Instantiation =
CurrentInstantiationScope->findInstantiationOf(
ParmPack.first.get<NamedDecl *>());
if (Instantiation->is<DeclArgumentPack *>()) {
cast<NamedDecl *>(ParmPack.first));
if (isa<DeclArgumentPack *>(*Instantiation)) {
// We could expand this function parameter pack.
NewPackSize = Instantiation->get<DeclArgumentPack *>()->size();
NewPackSize = cast<DeclArgumentPack *>(*Instantiation)->size();
} else {
// We can't expand this function parameter pack, so we can't expand
// the pack expansion.
@ -895,20 +895,20 @@ std::optional<unsigned> Sema::getNumArgumentsInExpansionFromUnexpanded(
Depth = TTP->getDepth();
Index = TTP->getIndex();
} else {
NamedDecl *ND = Unexpanded[I].first.get<NamedDecl *>();
NamedDecl *ND = cast<NamedDecl *>(Unexpanded[I].first);
if (isa<VarDecl>(ND)) {
// Function parameter pack or init-capture pack.
typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
llvm::PointerUnion<Decl *, DeclArgumentPack *> *Instantiation =
CurrentInstantiationScope->findInstantiationOf(
Unexpanded[I].first.get<NamedDecl *>());
if (Instantiation->is<Decl *>())
cast<NamedDecl *>(Unexpanded[I].first));
if (isa<Decl *>(*Instantiation))
// The pattern refers to an unexpanded pack. We're not ready to expand
// this pack yet.
return std::nullopt;
unsigned Size = Instantiation->get<DeclArgumentPack *>()->size();
unsigned Size = cast<DeclArgumentPack *>(*Instantiation)->size();
assert((!Result || *Result == Size) && "inconsistent pack sizes");
Result = Size;
continue;

View File

@ -14454,7 +14454,7 @@ TreeTransform<Derived>::TransformExprRequirement(concepts::ExprRequirement *Req)
Req->getNoexceptLoc(),
std::move(*TransRetReq));
return getDerived().RebuildExprRequirement(
TransExpr.get<concepts::Requirement::SubstitutionDiagnostic *>(),
cast<concepts::Requirement::SubstitutionDiagnostic *>(TransExpr),
Req->isSimple(), Req->getNoexceptLoc(), std::move(*TransRetReq));
}