Skip to content

Commit 563c7c5

Browse files
[clang] Migrate away from PointerUnion::dyn_cast (NFC) (#124425)
Note that PointerUnion::dyn_cast has 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> This patch migrates uses of PointerUnion::dyn_cast to dyn_cast_if_present (see the definition of PointerUnion::dyn_cast). Note that we cannot use dyn_cast in any of the migrations in this patch; placing assert(!X.isNull()); just before any of dyn_cast_if_present in this patch triggers some failure in check-clang.
1 parent 1395cd0 commit 563c7c5

File tree

17 files changed

+83
-70
lines changed

17 files changed

+83
-70
lines changed

clang/include/clang/AST/APValue.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,9 @@ class APValue {
161161

162162
template <class T> T get() const { return cast<T>(Ptr); }
163163

164-
template <class T>
165-
T dyn_cast() const { return Ptr.dyn_cast<T>(); }
164+
template <class T> T dyn_cast() const {
165+
return dyn_cast_if_present<T>(Ptr);
166+
}
166167

167168
void *getOpaqueValue() const;
168169

clang/include/clang/AST/ASTContext.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ class ASTContext : public RefCountedBase<ASTContext> {
769769
/// pool.
770770
DeclListNode *AllocateDeclListNode(clang::NamedDecl *ND) {
771771
if (DeclListNode *Alloc = ListNodeFreeList) {
772-
ListNodeFreeList = Alloc->Rest.dyn_cast<DeclListNode*>();
772+
ListNodeFreeList = dyn_cast_if_present<DeclListNode *>(Alloc->Rest);
773773
Alloc->D = ND;
774774
Alloc->Rest = nullptr;
775775
return Alloc;

clang/include/clang/AST/Decl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4035,7 +4035,7 @@ class EnumDecl : public TagDecl {
40354035
/// Return the type source info for the underlying integer type,
40364036
/// if no type source info exists, return 0.
40374037
TypeSourceInfo *getIntegerTypeSourceInfo() const {
4038-
return IntegerType.dyn_cast<TypeSourceInfo*>();
4038+
return dyn_cast_if_present<TypeSourceInfo *>(IntegerType);
40394039
}
40404040

40414041
/// Retrieve the source range that covers the underlying type if

clang/include/clang/AST/DeclBase.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1391,7 +1391,7 @@ class DeclContextLookupResult {
13911391
const_iterator end() const { return iterator(); }
13921392

13931393
bool empty() const { return Result.isNull(); }
1394-
bool isSingleResult() const { return Result.dyn_cast<NamedDecl*>(); }
1394+
bool isSingleResult() const { return isa_and_present<NamedDecl *>(Result); }
13951395
reference front() const { return *begin(); }
13961396

13971397
// Find the first declaration of the given type in the list. Note that this

clang/include/clang/AST/DeclTemplate.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2009,7 +2009,8 @@ class ClassTemplateSpecializationDecl : public CXXRecordDecl,
20092009
/// Retrieve the template argument list as written in the sources,
20102010
/// if any.
20112011
const ASTTemplateArgumentListInfo *getTemplateArgsAsWritten() const {
2012-
if (auto *Info = ExplicitInfo.dyn_cast<ExplicitInstantiationInfo *>())
2012+
if (auto *Info =
2013+
dyn_cast_if_present<ExplicitInstantiationInfo *>(ExplicitInfo))
20132014
return Info->TemplateArgsAsWritten;
20142015
return cast<const ASTTemplateArgumentListInfo *>(ExplicitInfo);
20152016
}
@@ -2041,7 +2042,8 @@ class ClassTemplateSpecializationDecl : public CXXRecordDecl,
20412042

20422043
/// Gets the location of the template keyword, if present.
20432044
SourceLocation getTemplateKeywordLoc() const {
2044-
if (auto *Info = ExplicitInfo.dyn_cast<ExplicitInstantiationInfo *>())
2045+
if (auto *Info =
2046+
dyn_cast_if_present<ExplicitInstantiationInfo *>(ExplicitInfo))
20452047
return Info->TemplateKeywordLoc;
20462048
return SourceLocation();
20472049
}
@@ -2786,7 +2788,8 @@ class VarTemplateSpecializationDecl : public VarDecl,
27862788
/// Set the template argument list as written in the sources.
27872789
void
27882790
setTemplateArgsAsWritten(const ASTTemplateArgumentListInfo *ArgsWritten) {
2789-
if (auto *Info = ExplicitInfo.dyn_cast<ExplicitInstantiationInfo *>())
2791+
if (auto *Info =
2792+
dyn_cast_if_present<ExplicitInstantiationInfo *>(ExplicitInfo))
27902793
Info->TemplateArgsAsWritten = ArgsWritten;
27912794
else
27922795
ExplicitInfo = ArgsWritten;

clang/include/clang/AST/Expr.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5180,7 +5180,7 @@ class InitListExpr : public Expr {
51805180
/// than there are initializers in the list, specifies an expression to be
51815181
/// used for value initialization of the rest of the elements.
51825182
Expr *getArrayFiller() {
5183-
return ArrayFillerOrUnionFieldInit.dyn_cast<Expr *>();
5183+
return dyn_cast_if_present<Expr *>(ArrayFillerOrUnionFieldInit);
51845184
}
51855185
const Expr *getArrayFiller() const {
51865186
return const_cast<InitListExpr *>(this)->getArrayFiller();
@@ -5205,7 +5205,7 @@ class InitListExpr : public Expr {
52055205
/// union. However, a designated initializer can specify the
52065206
/// initialization of a different field within the union.
52075207
FieldDecl *getInitializedFieldInUnion() {
5208-
return ArrayFillerOrUnionFieldInit.dyn_cast<FieldDecl *>();
5208+
return dyn_cast_if_present<FieldDecl *>(ArrayFillerOrUnionFieldInit);
52095209
}
52105210
const FieldDecl *getInitializedFieldInUnion() const {
52115211
return const_cast<InitListExpr *>(this)->getInitializedFieldInUnion();

clang/include/clang/AST/ExprCXX.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5026,19 +5026,19 @@ class CXXParenListInitExpr final
50265026
void setArrayFiller(Expr *E) { ArrayFillerOrUnionFieldInit = E; }
50275027

50285028
Expr *getArrayFiller() {
5029-
return ArrayFillerOrUnionFieldInit.dyn_cast<Expr *>();
5029+
return dyn_cast_if_present<Expr *>(ArrayFillerOrUnionFieldInit);
50305030
}
50315031

50325032
const Expr *getArrayFiller() const {
5033-
return ArrayFillerOrUnionFieldInit.dyn_cast<Expr *>();
5033+
return dyn_cast_if_present<Expr *>(ArrayFillerOrUnionFieldInit);
50345034
}
50355035

50365036
void setInitializedFieldInUnion(FieldDecl *FD) {
50375037
ArrayFillerOrUnionFieldInit = FD;
50385038
}
50395039

50405040
FieldDecl *getInitializedFieldInUnion() {
5041-
return ArrayFillerOrUnionFieldInit.dyn_cast<FieldDecl *>();
5041+
return dyn_cast_if_present<FieldDecl *>(ArrayFillerOrUnionFieldInit);
50425042
}
50435043

50445044
const FieldDecl *getInitializedFieldInUnion() const {

clang/include/clang/Basic/IdentifierTable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,7 @@ class Selector {
10081008
}
10091009

10101010
const IdentifierInfo *getAsIdentifierInfo() const {
1011-
return InfoPtr.getPointer().dyn_cast<const IdentifierInfo *>();
1011+
return dyn_cast_if_present<const IdentifierInfo *>(InfoPtr.getPointer());
10121012
}
10131013

10141014
MultiKeywordSelector *getMultiKeywordSelector() const {

clang/include/clang/Lex/Preprocessor.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ class Preprocessor {
856856
!PP.CurSubmoduleState->VisibleModules.getGeneration())
857857
return nullptr;
858858

859-
auto *Info = State.dyn_cast<ModuleMacroInfo*>();
859+
auto *Info = dyn_cast_if_present<ModuleMacroInfo *>(State);
860860
if (!Info) {
861861
Info = new (PP.getPreprocessorAllocator())
862862
ModuleMacroInfo(cast<MacroDirective *>(State));
@@ -885,18 +885,18 @@ class Preprocessor {
885885
}
886886

887887
~MacroState() {
888-
if (auto *Info = State.dyn_cast<ModuleMacroInfo*>())
888+
if (auto *Info = dyn_cast_if_present<ModuleMacroInfo *>(State))
889889
Info->~ModuleMacroInfo();
890890
}
891891

892892
MacroDirective *getLatest() const {
893-
if (auto *Info = State.dyn_cast<ModuleMacroInfo*>())
893+
if (auto *Info = dyn_cast_if_present<ModuleMacroInfo *>(State))
894894
return Info->MD;
895895
return cast<MacroDirective *>(State);
896896
}
897897

898898
void setLatest(MacroDirective *MD) {
899-
if (auto *Info = State.dyn_cast<ModuleMacroInfo*>())
899+
if (auto *Info = dyn_cast_if_present<ModuleMacroInfo *>(State))
900900
Info->MD = MD;
901901
else
902902
State = MD;
@@ -940,7 +940,7 @@ class Preprocessor {
940940

941941
void setOverriddenMacros(Preprocessor &PP,
942942
ArrayRef<ModuleMacro *> Overrides) {
943-
auto *Info = State.dyn_cast<ModuleMacroInfo*>();
943+
auto *Info = dyn_cast_if_present<ModuleMacroInfo *>(State);
944944
if (!Info) {
945945
if (Overrides.empty())
946946
return;

clang/lib/APINotes/APINotesManager.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ APINotesManager::APINotesManager(SourceManager &SM, const LangOptions &LangOpts)
5656
APINotesManager::~APINotesManager() {
5757
// Free the API notes readers.
5858
for (const auto &Entry : Readers) {
59-
if (auto Reader = Entry.second.dyn_cast<APINotesReader *>())
59+
if (auto Reader = dyn_cast_if_present<APINotesReader *>(Entry.second))
6060
delete Reader;
6161
}
6262

@@ -381,7 +381,7 @@ APINotesManager::findAPINotes(SourceLocation Loc) {
381381
}
382382

383383
// We have the answer.
384-
if (auto Reader = Known->second.dyn_cast<APINotesReader *>())
384+
if (auto Reader = dyn_cast_if_present<APINotesReader *>(Known->second))
385385
Results.push_back(Reader);
386386
break;
387387
}

0 commit comments

Comments
 (0)