From 3dbbc9cec19aede0a83a8bc0d39c07fadffd97b6 Mon Sep 17 00:00:00 2001 From: Yulong Zhang Date: Tue, 27 Jan 2026 05:37:09 +0000 Subject: [PATCH] tools/clang/codesearch: migrate dyn_cast to dyn_cast_if_present In LLVM 16+ dyn_cast is no longer null-safe and hence leads to crashes. This commit switches it to dyn_cast_if_null. --- tools/clang/codesearch/codesearch.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tools/clang/codesearch/codesearch.cpp b/tools/clang/codesearch/codesearch.cpp index df91c84fcf57..11658f4eeb05 100644 --- a/tools/clang/codesearch/codesearch.cpp +++ b/tools/clang/codesearch/codesearch.cpp @@ -203,7 +203,7 @@ bool Indexer::TraverseCallExpr(CallExpr* CE) { } bool Indexer::VisitDeclRefExpr(const DeclRefExpr* DeclRef) { - if (const auto* Func = dyn_cast(DeclRef->getDecl())) + if (const auto* Func = dyn_cast_if_present(DeclRef->getDecl())) EmitReference(DeclRef->getBeginLoc(), DeclRef->getDecl(), EntityKindFunction, InCallee ? RefKindCall : RefKindTakesAddr); return true; @@ -246,25 +246,25 @@ bool Indexer::VisitTypedefType(const TypedefType* T) { return true; EmitReference(TypeRefingLocation, T->getDecl(), EntityKindTypedef, RefKindUses); // If it's a struct typedef, also note the struct use. - if (const auto* Tag = dyn_cast(T->getCanonicalTypeInternal().getTypePtr())) + if (const auto* Tag = dyn_cast_if_present(T->getCanonicalTypeInternal().getTypePtr())) VisitTagType(Tag); return true; } bool Indexer::VisitMemberExpr(const MemberExpr* E) { auto* Record = E->getBase()->getType()->getAsRecordDecl(); - if (auto* Ptr = dyn_cast(E->getBase()->getType())) + if (auto* Ptr = dyn_cast_if_present(E->getBase()->getType())) Record = Ptr->getPointeeType()->getAsRecordDecl(); if (!Record) return true; const std::string Field = Record->getNameAsString() + "::" + E->getMemberDecl()->getNameAsString(); const char* RefKind = RefKindRead; const Stmt* P = GetParent(E); - if (auto* BO = dyn_cast(P)) { + if (auto* BO = dyn_cast_if_present(P)) { if (E == BO->getLHS() && (BO->isAssignmentOp() || BO->isCompoundAssignmentOp() || BO->isShiftAssignOp())) RefKind = RefKindWrite; } - if (auto* UO = dyn_cast(P)) + if (auto* UO = dyn_cast_if_present(P)) RefKind = RefKindTakesAddr; EmitReference(E->getMemberLoc(), Field, EntityKindField, RefKind); return true;