[clang] [clang] Migrate away from PointerUnion::dyn_cast (NFC) (PR #124425)
Kazu Hirata via cfe-commits
cfe-commits at lists.llvm.org
Sat Jan 25 12:52:13 PST 2025
https://github.com/kazutakahirata updated https://github.com/llvm/llvm-project/pull/124425
>From 8586c0eae876176888bba3f9f9b8f2a83eea9684 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sat, 25 Jan 2025 10:38:03 -0800
Subject: [PATCH 1/2] [clang] Migrate away from PointerUnion::dyn_cast (NFC)
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.
---
clang/include/clang/AST/APValue.h | 5 +-
clang/include/clang/AST/ASTContext.h | 2 +-
clang/include/clang/AST/Decl.h | 2 +-
clang/include/clang/AST/DeclBase.h | 4 +-
clang/include/clang/AST/DeclTemplate.h | 9 ++-
clang/include/clang/AST/Expr.h | 4 +-
clang/include/clang/AST/ExprCXX.h | 6 +-
clang/include/clang/Basic/IdentifierTable.h | 2 +-
clang/include/clang/Lex/Preprocessor.h | 10 +--
clang/lib/APINotes/APINotesManager.cpp | 4 +-
clang/lib/AST/Decl.cpp | 71 ++++++++++---------
clang/lib/AST/DeclCXX.cpp | 10 +--
clang/lib/AST/DeclTemplate.cpp | 4 +-
clang/lib/AST/TemplateName.cpp | 11 +--
.../Frontend/SerializedDiagnosticPrinter.cpp | 2 +-
clang/lib/Sema/SemaDecl.cpp | 6 +-
clang/tools/libclang/CIndexDiagnostic.cpp | 3 +-
17 files changed, 85 insertions(+), 70 deletions(-)
diff --git a/clang/include/clang/AST/APValue.h b/clang/include/clang/AST/APValue.h
index 833a78c77871d0..9999a30c51ade4 100644
--- a/clang/include/clang/AST/APValue.h
+++ b/clang/include/clang/AST/APValue.h
@@ -161,8 +161,9 @@ class APValue {
template <class T> T get() const { return cast<T>(Ptr); }
- template <class T>
- T dyn_cast() const { return Ptr.dyn_cast<T>(); }
+ template <class T> T dyn_cast() const {
+ return dyn_cast_if_present<T>(Ptr);
+ }
void *getOpaqueValue() const;
diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h
index 4e9b961688d559..65be782c1ba43e 100644
--- a/clang/include/clang/AST/ASTContext.h
+++ b/clang/include/clang/AST/ASTContext.h
@@ -769,7 +769,7 @@ class ASTContext : public RefCountedBase<ASTContext> {
/// pool.
DeclListNode *AllocateDeclListNode(clang::NamedDecl *ND) {
if (DeclListNode *Alloc = ListNodeFreeList) {
- ListNodeFreeList = Alloc->Rest.dyn_cast<DeclListNode*>();
+ ListNodeFreeList = dyn_cast_if_present<DeclListNode *>(Alloc->Rest);
Alloc->D = ND;
Alloc->Rest = nullptr;
return Alloc;
diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h
index d01681483a9189..16403774e72b31 100644
--- a/clang/include/clang/AST/Decl.h
+++ b/clang/include/clang/AST/Decl.h
@@ -4035,7 +4035,7 @@ class EnumDecl : public TagDecl {
/// Return the type source info for the underlying integer type,
/// if no type source info exists, return 0.
TypeSourceInfo *getIntegerTypeSourceInfo() const {
- return IntegerType.dyn_cast<TypeSourceInfo*>();
+ return dyn_cast_if_present<TypeSourceInfo *>(IntegerType);
}
/// Retrieve the source range that covers the underlying type if
diff --git a/clang/include/clang/AST/DeclBase.h b/clang/include/clang/AST/DeclBase.h
index 573b46a2321c5f..6074e0aee06161 100644
--- a/clang/include/clang/AST/DeclBase.h
+++ b/clang/include/clang/AST/DeclBase.h
@@ -1391,7 +1391,9 @@ class DeclContextLookupResult {
const_iterator end() const { return iterator(); }
bool empty() const { return Result.isNull(); }
- bool isSingleResult() const { return Result.dyn_cast<NamedDecl*>(); }
+ bool isSingleResult() const {
+ return dyn_cast_if_present<NamedDecl *>(Result);
+ }
reference front() const { return *begin(); }
// Find the first declaration of the given type in the list. Note that this
diff --git a/clang/include/clang/AST/DeclTemplate.h b/clang/include/clang/AST/DeclTemplate.h
index 8c2da97c07a3bf..caaa47d0a297cf 100644
--- a/clang/include/clang/AST/DeclTemplate.h
+++ b/clang/include/clang/AST/DeclTemplate.h
@@ -2009,7 +2009,8 @@ class ClassTemplateSpecializationDecl : public CXXRecordDecl,
/// Retrieve the template argument list as written in the sources,
/// if any.
const ASTTemplateArgumentListInfo *getTemplateArgsAsWritten() const {
- if (auto *Info = ExplicitInfo.dyn_cast<ExplicitInstantiationInfo *>())
+ if (auto *Info =
+ dyn_cast_if_present<ExplicitInstantiationInfo *>(ExplicitInfo))
return Info->TemplateArgsAsWritten;
return cast<const ASTTemplateArgumentListInfo *>(ExplicitInfo);
}
@@ -2041,7 +2042,8 @@ class ClassTemplateSpecializationDecl : public CXXRecordDecl,
/// Gets the location of the template keyword, if present.
SourceLocation getTemplateKeywordLoc() const {
- if (auto *Info = ExplicitInfo.dyn_cast<ExplicitInstantiationInfo *>())
+ if (auto *Info =
+ dyn_cast_if_present<ExplicitInstantiationInfo *>(ExplicitInfo))
return Info->TemplateKeywordLoc;
return SourceLocation();
}
@@ -2786,7 +2788,8 @@ class VarTemplateSpecializationDecl : public VarDecl,
/// Set the template argument list as written in the sources.
void
setTemplateArgsAsWritten(const ASTTemplateArgumentListInfo *ArgsWritten) {
- if (auto *Info = ExplicitInfo.dyn_cast<ExplicitInstantiationInfo *>())
+ if (auto *Info =
+ dyn_cast_if_present<ExplicitInstantiationInfo *>(ExplicitInfo))
Info->TemplateArgsAsWritten = ArgsWritten;
else
ExplicitInfo = ArgsWritten;
diff --git a/clang/include/clang/AST/Expr.h b/clang/include/clang/AST/Expr.h
index 708c8656decbe0..7be4022649329b 100644
--- a/clang/include/clang/AST/Expr.h
+++ b/clang/include/clang/AST/Expr.h
@@ -5180,7 +5180,7 @@ class InitListExpr : public Expr {
/// than there are initializers in the list, specifies an expression to be
/// used for value initialization of the rest of the elements.
Expr *getArrayFiller() {
- return ArrayFillerOrUnionFieldInit.dyn_cast<Expr *>();
+ return dyn_cast_if_present<Expr *>(ArrayFillerOrUnionFieldInit);
}
const Expr *getArrayFiller() const {
return const_cast<InitListExpr *>(this)->getArrayFiller();
@@ -5205,7 +5205,7 @@ class InitListExpr : public Expr {
/// union. However, a designated initializer can specify the
/// initialization of a different field within the union.
FieldDecl *getInitializedFieldInUnion() {
- return ArrayFillerOrUnionFieldInit.dyn_cast<FieldDecl *>();
+ return dyn_cast_if_present<FieldDecl *>(ArrayFillerOrUnionFieldInit);
}
const FieldDecl *getInitializedFieldInUnion() const {
return const_cast<InitListExpr *>(this)->getInitializedFieldInUnion();
diff --git a/clang/include/clang/AST/ExprCXX.h b/clang/include/clang/AST/ExprCXX.h
index 4cec89c979f775..aa10945addf78f 100644
--- a/clang/include/clang/AST/ExprCXX.h
+++ b/clang/include/clang/AST/ExprCXX.h
@@ -5026,11 +5026,11 @@ class CXXParenListInitExpr final
void setArrayFiller(Expr *E) { ArrayFillerOrUnionFieldInit = E; }
Expr *getArrayFiller() {
- return ArrayFillerOrUnionFieldInit.dyn_cast<Expr *>();
+ return dyn_cast_if_present<Expr *>(ArrayFillerOrUnionFieldInit);
}
const Expr *getArrayFiller() const {
- return ArrayFillerOrUnionFieldInit.dyn_cast<Expr *>();
+ return dyn_cast_if_present<Expr *>(ArrayFillerOrUnionFieldInit);
}
void setInitializedFieldInUnion(FieldDecl *FD) {
@@ -5038,7 +5038,7 @@ class CXXParenListInitExpr final
}
FieldDecl *getInitializedFieldInUnion() {
- return ArrayFillerOrUnionFieldInit.dyn_cast<FieldDecl *>();
+ return dyn_cast_if_present<FieldDecl *>(ArrayFillerOrUnionFieldInit);
}
const FieldDecl *getInitializedFieldInUnion() const {
diff --git a/clang/include/clang/Basic/IdentifierTable.h b/clang/include/clang/Basic/IdentifierTable.h
index 33d1cdb46f108b..e5e6be3c966003 100644
--- a/clang/include/clang/Basic/IdentifierTable.h
+++ b/clang/include/clang/Basic/IdentifierTable.h
@@ -1008,7 +1008,7 @@ class Selector {
}
const IdentifierInfo *getAsIdentifierInfo() const {
- return InfoPtr.getPointer().dyn_cast<const IdentifierInfo *>();
+ return dyn_cast_if_present<const IdentifierInfo *>(InfoPtr.getPointer());
}
MultiKeywordSelector *getMultiKeywordSelector() const {
diff --git a/clang/include/clang/Lex/Preprocessor.h b/clang/include/clang/Lex/Preprocessor.h
index 8ddc5b56eedbd4..416f403c298410 100644
--- a/clang/include/clang/Lex/Preprocessor.h
+++ b/clang/include/clang/Lex/Preprocessor.h
@@ -856,7 +856,7 @@ class Preprocessor {
!PP.CurSubmoduleState->VisibleModules.getGeneration())
return nullptr;
- auto *Info = State.dyn_cast<ModuleMacroInfo*>();
+ auto *Info = dyn_cast_if_present<ModuleMacroInfo *>(State);
if (!Info) {
Info = new (PP.getPreprocessorAllocator())
ModuleMacroInfo(cast<MacroDirective *>(State));
@@ -885,18 +885,18 @@ class Preprocessor {
}
~MacroState() {
- if (auto *Info = State.dyn_cast<ModuleMacroInfo*>())
+ if (auto *Info = dyn_cast_if_present<ModuleMacroInfo *>(State))
Info->~ModuleMacroInfo();
}
MacroDirective *getLatest() const {
- if (auto *Info = State.dyn_cast<ModuleMacroInfo*>())
+ if (auto *Info = dyn_cast_if_present<ModuleMacroInfo *>(State))
return Info->MD;
return cast<MacroDirective *>(State);
}
void setLatest(MacroDirective *MD) {
- if (auto *Info = State.dyn_cast<ModuleMacroInfo*>())
+ if (auto *Info = dyn_cast_if_present<ModuleMacroInfo *>(State))
Info->MD = MD;
else
State = MD;
@@ -940,7 +940,7 @@ class Preprocessor {
void setOverriddenMacros(Preprocessor &PP,
ArrayRef<ModuleMacro *> Overrides) {
- auto *Info = State.dyn_cast<ModuleMacroInfo*>();
+ auto *Info = dyn_cast_if_present<ModuleMacroInfo *>(State);
if (!Info) {
if (Overrides.empty())
return;
diff --git a/clang/lib/APINotes/APINotesManager.cpp b/clang/lib/APINotes/APINotesManager.cpp
index 70d96c735503fd..7f8a126ffaa03a 100644
--- a/clang/lib/APINotes/APINotesManager.cpp
+++ b/clang/lib/APINotes/APINotesManager.cpp
@@ -56,7 +56,7 @@ APINotesManager::APINotesManager(SourceManager &SM, const LangOptions &LangOpts)
APINotesManager::~APINotesManager() {
// Free the API notes readers.
for (const auto &Entry : Readers) {
- if (auto Reader = Entry.second.dyn_cast<APINotesReader *>())
+ if (auto Reader = dyn_cast_if_present<APINotesReader *>(Entry.second))
delete Reader;
}
@@ -381,7 +381,7 @@ APINotesManager::findAPINotes(SourceLocation Loc) {
}
// We have the answer.
- if (auto Reader = Known->second.dyn_cast<APINotesReader *>())
+ if (auto Reader = dyn_cast_if_present<APINotesReader *>(Known->second))
Results.push_back(Reader);
break;
}
diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp
index 5ce03ce20d2841..74bcb618f2950f 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -2447,7 +2447,7 @@ bool VarDecl::isOutOfLine() const {
}
void VarDecl::setInit(Expr *I) {
- if (auto *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
+ if (auto *Eval = dyn_cast_if_present<EvaluatedStmt *>(Init)) {
Eval->~EvaluatedStmt();
getASTContext().Deallocate(Eval);
}
@@ -2527,7 +2527,7 @@ bool VarDecl::isUsableInConstantExpressions(const ASTContext &Context) const {
/// form, which contains extra information on the evaluated value of the
/// initializer.
EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const {
- auto *Eval = Init.dyn_cast<EvaluatedStmt *>();
+ auto *Eval = dyn_cast_if_present<EvaluatedStmt *>(Init);
if (!Eval) {
// Note: EvaluatedStmt contains an APValue, which usually holds
// resources not allocated from the ASTContext. We need to do some
@@ -2541,7 +2541,7 @@ EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const {
}
EvaluatedStmt *VarDecl::getEvaluatedStmt() const {
- return Init.dyn_cast<EvaluatedStmt *>();
+ return dyn_cast_if_present<EvaluatedStmt *>(Init);
}
APValue *VarDecl::evaluateValue() const {
@@ -2784,8 +2784,8 @@ SourceLocation VarDecl::getPointOfInstantiation() const {
}
VarTemplateDecl *VarDecl::getDescribedVarTemplate() const {
- return getASTContext().getTemplateOrSpecializationInfo(this)
- .dyn_cast<VarTemplateDecl *>();
+ return dyn_cast_if_present<VarTemplateDecl *>(
+ getASTContext().getTemplateOrSpecializationInfo(this));
}
void VarDecl::setDescribedVarTemplate(VarTemplateDecl *Template) {
@@ -2875,8 +2875,8 @@ MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
if (isStaticDataMember())
// FIXME: Remove ?
// return getASTContext().getInstantiatedFromStaticDataMember(this);
- return getASTContext().getTemplateOrSpecializationInfo(this)
- .dyn_cast<MemberSpecializationInfo *>();
+ return dyn_cast_if_present<MemberSpecializationInfo *>(
+ getASTContext().getTemplateOrSpecializationInfo(this));
return nullptr;
}
@@ -4040,11 +4040,11 @@ FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
}
MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
- if (auto *MSI =
- TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>())
+ if (auto *MSI = dyn_cast_if_present<MemberSpecializationInfo *>(
+ TemplateOrSpecialization))
return MSI;
- if (auto *FTSI = TemplateOrSpecialization
- .dyn_cast<FunctionTemplateSpecializationInfo *>())
+ if (auto *FTSI = dyn_cast_if_present<FunctionTemplateSpecializationInfo *>(
+ TemplateOrSpecialization))
return FTSI->getMemberSpecializationInfo();
return nullptr;
}
@@ -4062,7 +4062,7 @@ FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
FunctionTemplateDecl *FunctionDecl::getDescribedFunctionTemplate() const {
return dyn_cast_if_present<FunctionTemplateDecl>(
- TemplateOrSpecialization.dyn_cast<NamedDecl *>());
+ dyn_cast_if_present<NamedDecl *>(TemplateOrSpecialization));
}
void FunctionDecl::setDescribedFunctionTemplate(
@@ -4181,9 +4181,9 @@ FunctionDecl::getTemplateInstantiationPattern(bool ForDefinition) const {
}
FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
- if (FunctionTemplateSpecializationInfo *Info
- = TemplateOrSpecialization
- .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
+ if (FunctionTemplateSpecializationInfo *Info =
+ dyn_cast_if_present<FunctionTemplateSpecializationInfo *>(
+ TemplateOrSpecialization)) {
return Info->getTemplate();
}
return nullptr;
@@ -4191,15 +4191,15 @@ FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
FunctionTemplateSpecializationInfo *
FunctionDecl::getTemplateSpecializationInfo() const {
- return TemplateOrSpecialization
- .dyn_cast<FunctionTemplateSpecializationInfo *>();
+ return dyn_cast_if_present<FunctionTemplateSpecializationInfo *>(
+ TemplateOrSpecialization);
}
const TemplateArgumentList *
FunctionDecl::getTemplateSpecializationArgs() const {
- if (FunctionTemplateSpecializationInfo *Info
- = TemplateOrSpecialization
- .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
+ if (FunctionTemplateSpecializationInfo *Info =
+ dyn_cast_if_present<FunctionTemplateSpecializationInfo *>(
+ TemplateOrSpecialization)) {
return Info->TemplateArguments;
}
return nullptr;
@@ -4207,14 +4207,14 @@ FunctionDecl::getTemplateSpecializationArgs() const {
const ASTTemplateArgumentListInfo *
FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
- if (FunctionTemplateSpecializationInfo *Info
- = TemplateOrSpecialization
- .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
+ if (FunctionTemplateSpecializationInfo *Info =
+ dyn_cast_if_present<FunctionTemplateSpecializationInfo *>(
+ TemplateOrSpecialization)) {
return Info->TemplateArgumentsAsWritten;
}
if (DependentFunctionTemplateSpecializationInfo *Info =
- TemplateOrSpecialization
- .dyn_cast<DependentFunctionTemplateSpecializationInfo *>()) {
+ dyn_cast_if_present<DependentFunctionTemplateSpecializationInfo *>(
+ TemplateOrSpecialization)) {
return Info->TemplateArgumentsAsWritten;
}
return nullptr;
@@ -4239,7 +4239,8 @@ void FunctionDecl::setFunctionTemplateSpecialization(
FunctionTemplateSpecializationInfo::Create(
C, this, Template, TSK, TemplateArgs, TemplateArgsAsWritten,
PointOfInstantiation,
- TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>());
+ dyn_cast_if_present<MemberSpecializationInfo *>(
+ TemplateOrSpecialization));
TemplateOrSpecialization = Info;
Template->addSpecialization(Info, InsertPos);
}
@@ -4256,8 +4257,8 @@ void FunctionDecl::setDependentTemplateSpecialization(
DependentFunctionTemplateSpecializationInfo *
FunctionDecl::getDependentSpecializationInfo() const {
- return TemplateOrSpecialization
- .dyn_cast<DependentFunctionTemplateSpecializationInfo *>();
+ return dyn_cast_if_present<DependentFunctionTemplateSpecializationInfo *>(
+ TemplateOrSpecialization);
}
DependentFunctionTemplateSpecializationInfo *
@@ -4288,12 +4289,13 @@ TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
// For a function template specialization, query the specialization
// information object.
if (FunctionTemplateSpecializationInfo *FTSInfo =
- TemplateOrSpecialization
- .dyn_cast<FunctionTemplateSpecializationInfo *>())
+ dyn_cast_if_present<FunctionTemplateSpecializationInfo *>(
+ TemplateOrSpecialization))
return FTSInfo->getTemplateSpecializationKind();
if (MemberSpecializationInfo *MSInfo =
- TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>())
+ dyn_cast_if_present<MemberSpecializationInfo *>(
+ TemplateOrSpecialization))
return MSInfo->getTemplateSpecializationKind();
// A dependent function template specialization is an explicit specialization,
@@ -4331,15 +4333,16 @@ FunctionDecl::getTemplateSpecializationKindForInstantiation() const {
// of A<int>::f, and that A<int>::f<int> should be implicitly instantiated
// from A::f<int> if a definition is needed.
if (FunctionTemplateSpecializationInfo *FTSInfo =
- TemplateOrSpecialization
- .dyn_cast<FunctionTemplateSpecializationInfo *>()) {
+ dyn_cast_if_present<FunctionTemplateSpecializationInfo *>(
+ TemplateOrSpecialization)) {
if (auto *MSInfo = FTSInfo->getMemberSpecializationInfo())
return MSInfo->getTemplateSpecializationKind();
return FTSInfo->getTemplateSpecializationKind();
}
if (MemberSpecializationInfo *MSInfo =
- TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>())
+ dyn_cast_if_present<MemberSpecializationInfo *>(
+ TemplateOrSpecialization))
return MSInfo->getTemplateSpecializationKind();
if (isa<DependentFunctionTemplateSpecializationInfo *>(
diff --git a/clang/lib/AST/DeclCXX.cpp b/clang/lib/AST/DeclCXX.cpp
index 44f45898fb483d..c0a4356dcb004f 100644
--- a/clang/lib/AST/DeclCXX.cpp
+++ b/clang/lib/AST/DeclCXX.cpp
@@ -1987,7 +1987,8 @@ CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
}
MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
- return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
+ return dyn_cast_if_present<MemberSpecializationInfo *>(
+ TemplateOrInstantiation);
}
void
@@ -2001,7 +2002,7 @@ CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
}
ClassTemplateDecl *CXXRecordDecl::getDescribedClassTemplate() const {
- return TemplateOrInstantiation.dyn_cast<ClassTemplateDecl *>();
+ return dyn_cast_if_present<ClassTemplateDecl *>(TemplateOrInstantiation);
}
void CXXRecordDecl::setDescribedClassTemplate(ClassTemplateDecl *Template) {
@@ -2045,7 +2046,7 @@ const CXXRecordDecl *CXXRecordDecl::getTemplateInstantiationPattern() const {
// specialization from which it was instantiated.
if (auto *TD = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
auto From = TD->getInstantiatedFrom();
- if (auto *CTD = From.dyn_cast<ClassTemplateDecl *>()) {
+ if (auto *CTD = dyn_cast_if_present<ClassTemplateDecl *>(From)) {
while (auto *NewCTD = CTD->getInstantiatedFromMemberTemplate()) {
if (NewCTD->isMemberSpecialization())
break;
@@ -2054,7 +2055,8 @@ const CXXRecordDecl *CXXRecordDecl::getTemplateInstantiationPattern() const {
return GetDefinitionOrSelf(CTD->getTemplatedDecl());
}
if (auto *CTPSD =
- From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) {
+ dyn_cast_if_present<ClassTemplatePartialSpecializationDecl *>(
+ From)) {
while (auto *NewCTPSD = CTPSD->getInstantiatedFromMember()) {
if (NewCTPSD->isMemberSpecialization())
break;
diff --git a/clang/lib/AST/DeclTemplate.cpp b/clang/lib/AST/DeclTemplate.cpp
index 2933ba7fb8a295..926b2b26dd381d 100644
--- a/clang/lib/AST/DeclTemplate.cpp
+++ b/clang/lib/AST/DeclTemplate.cpp
@@ -1077,7 +1077,7 @@ ClassTemplateSpecializationDecl::getSourceRange() const {
}
void ClassTemplateSpecializationDecl::setExternKeywordLoc(SourceLocation Loc) {
- auto *Info = ExplicitInfo.dyn_cast<ExplicitInstantiationInfo *>();
+ auto *Info = dyn_cast_if_present<ExplicitInstantiationInfo *>(ExplicitInfo);
if (!Info) {
// Don't allocate if the location is invalid.
if (Loc.isInvalid())
@@ -1091,7 +1091,7 @@ void ClassTemplateSpecializationDecl::setExternKeywordLoc(SourceLocation Loc) {
void ClassTemplateSpecializationDecl::setTemplateKeywordLoc(
SourceLocation Loc) {
- auto *Info = ExplicitInfo.dyn_cast<ExplicitInstantiationInfo *>();
+ auto *Info = dyn_cast_if_present<ExplicitInstantiationInfo *>(ExplicitInfo);
if (!Info) {
// Don't allocate if the location is invalid.
if (Loc.isInvalid())
diff --git a/clang/lib/AST/TemplateName.cpp b/clang/lib/AST/TemplateName.cpp
index 7d6275caedc4f5..3a1eb1ca12f453 100644
--- a/clang/lib/AST/TemplateName.cpp
+++ b/clang/lib/AST/TemplateName.cpp
@@ -182,7 +182,8 @@ TemplateDecl *TemplateName::getAsTemplateDecl(bool IgnoreDeduced) const {
"Unexpected canonical DeducedTemplateName; Did you mean to use "
"getTemplateDeclAndDefaultArgs instead?");
- return cast_if_present<TemplateDecl>(Name.Storage.dyn_cast<Decl *>());
+ return cast_if_present<TemplateDecl>(
+ dyn_cast_if_present<Decl *>(Name.Storage));
}
std::pair<TemplateDecl *, DefaultArguments>
@@ -208,7 +209,7 @@ TemplateName::getTemplateDeclAndDefaultArgs() const {
}
std::optional<TemplateName> TemplateName::desugar(bool IgnoreDeduced) const {
- if (Decl *D = Storage.dyn_cast<Decl *>()) {
+ if (Decl *D = dyn_cast_if_present<Decl *>(Storage)) {
if (auto *USD = dyn_cast<UsingShadowDecl>(D))
return TemplateName(USD->getTargetDecl());
return std::nullopt;
@@ -242,7 +243,7 @@ AssumedTemplateStorage *TemplateName::getAsAssumedTemplateName() const {
SubstTemplateTemplateParmStorage *
TemplateName::getAsSubstTemplateTemplateParm() const {
if (UncommonTemplateNameStorage *uncommon =
- Storage.dyn_cast<UncommonTemplateNameStorage *>())
+ dyn_cast_if_present<UncommonTemplateNameStorage *>(Storage))
return uncommon->getAsSubstTemplateTemplateParm();
return nullptr;
@@ -258,7 +259,7 @@ TemplateName::getAsSubstTemplateTemplateParmPack() const {
}
QualifiedTemplateName *TemplateName::getAsQualifiedTemplateName() const {
- return Storage.dyn_cast<QualifiedTemplateName *>();
+ return dyn_cast_if_present<QualifiedTemplateName *>(Storage);
}
DependentTemplateName *TemplateName::getAsDependentTemplateName() const {
@@ -276,7 +277,7 @@ UsingShadowDecl *TemplateName::getAsUsingShadowDecl() const {
DeducedTemplateStorage *TemplateName::getAsDeducedTemplateName() const {
if (UncommonTemplateNameStorage *Uncommon =
- Storage.dyn_cast<UncommonTemplateNameStorage *>())
+ dyn_cast_if_present<UncommonTemplateNameStorage *>(Storage))
return Uncommon->getAsDeducedTemplateName();
return nullptr;
diff --git a/clang/lib/Frontend/SerializedDiagnosticPrinter.cpp b/clang/lib/Frontend/SerializedDiagnosticPrinter.cpp
index 0887b5a504f05a..131334269aa75c 100644
--- a/clang/lib/Frontend/SerializedDiagnosticPrinter.cpp
+++ b/clang/lib/Frontend/SerializedDiagnosticPrinter.cpp
@@ -650,7 +650,7 @@ void SDiagsWriter::EmitDiagnosticMessage(FullSourceLoc Loc, PresumedLoc PLoc,
Record.push_back(getStableLevel(Level));
AddLocToRecord(Loc, PLoc, Record);
- if (const Diagnostic *Info = D.dyn_cast<const Diagnostic*>()) {
+ if (const Diagnostic *Info = dyn_cast_if_present<const Diagnostic *>(D)) {
// Emit the category string lazily and get the category ID.
unsigned DiagID = DiagnosticIDs::getCategoryNumberForDiag(Info->getID());
Record.push_back(getEmitCategory(DiagID));
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index ad49eac66e98e5..c3ff247a6316d3 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -17700,9 +17700,11 @@ Sema::ActOnTag(Scope *S, unsigned TagSpec, TagUseKind TUK, SourceLocation KWLoc,
return PrevTagDecl;
QualType EnumUnderlyingTy;
- if (TypeSourceInfo *TI = EnumUnderlying.dyn_cast<TypeSourceInfo*>())
+ if (TypeSourceInfo *TI =
+ dyn_cast_if_present<TypeSourceInfo *>(EnumUnderlying))
EnumUnderlyingTy = TI->getType().getUnqualifiedType();
- else if (const Type *T = EnumUnderlying.dyn_cast<const Type*>())
+ else if (const Type *T =
+ dyn_cast_if_present<const Type *>(EnumUnderlying))
EnumUnderlyingTy = QualType(T, 0);
// All conflicts with previous declarations are recovered by
diff --git a/clang/tools/libclang/CIndexDiagnostic.cpp b/clang/tools/libclang/CIndexDiagnostic.cpp
index 34792d5bdfaaff..92271d9c37f862 100644
--- a/clang/tools/libclang/CIndexDiagnostic.cpp
+++ b/clang/tools/libclang/CIndexDiagnostic.cpp
@@ -92,7 +92,8 @@ class CXDiagnosticRenderer : public DiagnosticNoteRenderer {
void beginDiagnostic(DiagOrStoredDiag D,
DiagnosticsEngine::Level Level) override {
- const StoredDiagnostic *SD = D.dyn_cast<const StoredDiagnostic*>();
+ const StoredDiagnostic *SD =
+ dyn_cast_if_present<const StoredDiagnostic *>(D);
if (!SD)
return;
>From 4bf553e5c55376b42869de01440aadcf8acc2d8c Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sat, 25 Jan 2025 12:41:18 -0800
Subject: [PATCH 2/2] Address comments.
---
clang/include/clang/AST/DeclBase.h | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/clang/include/clang/AST/DeclBase.h b/clang/include/clang/AST/DeclBase.h
index 6074e0aee06161..2c0c3a8dc2f9d5 100644
--- a/clang/include/clang/AST/DeclBase.h
+++ b/clang/include/clang/AST/DeclBase.h
@@ -1391,9 +1391,7 @@ class DeclContextLookupResult {
const_iterator end() const { return iterator(); }
bool empty() const { return Result.isNull(); }
- bool isSingleResult() const {
- return dyn_cast_if_present<NamedDecl *>(Result);
- }
+ bool isSingleResult() const { return isa_and_present<NamedDecl *>(Result); }
reference front() const { return *begin(); }
// Find the first declaration of the given type in the list. Note that this
More information about the cfe-commits
mailing list