Skip to content

Commit b441fd8

Browse files
yulongzhangdvyukov
authored andcommitted
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.
1 parent 9a54276 commit b441fd8

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

tools/clang/codesearch/codesearch.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ bool Indexer::TraverseCallExpr(CallExpr* CE) {
203203
}
204204

205205
bool Indexer::VisitDeclRefExpr(const DeclRefExpr* DeclRef) {
206-
if (const auto* Func = dyn_cast<FunctionDecl>(DeclRef->getDecl()))
206+
if (const auto* Func = dyn_cast_if_present<FunctionDecl>(DeclRef->getDecl()))
207207
EmitReference(DeclRef->getBeginLoc(), DeclRef->getDecl(), EntityKindFunction,
208208
InCallee ? RefKindCall : RefKindTakesAddr);
209209
return true;
@@ -246,25 +246,25 @@ bool Indexer::VisitTypedefType(const TypedefType* T) {
246246
return true;
247247
EmitReference(TypeRefingLocation, T->getDecl(), EntityKindTypedef, RefKindUses);
248248
// If it's a struct typedef, also note the struct use.
249-
if (const auto* Tag = dyn_cast<TagType>(T->getCanonicalTypeInternal().getTypePtr()))
249+
if (const auto* Tag = dyn_cast_if_present<TagType>(T->getCanonicalTypeInternal().getTypePtr()))
250250
VisitTagType(Tag);
251251
return true;
252252
}
253253

254254
bool Indexer::VisitMemberExpr(const MemberExpr* E) {
255255
auto* Record = E->getBase()->getType()->getAsRecordDecl();
256-
if (auto* Ptr = dyn_cast<PointerType>(E->getBase()->getType()))
256+
if (auto* Ptr = dyn_cast_if_present<PointerType>(E->getBase()->getType()))
257257
Record = Ptr->getPointeeType()->getAsRecordDecl();
258258
if (!Record)
259259
return true;
260260
const std::string Field = Record->getNameAsString() + "::" + E->getMemberDecl()->getNameAsString();
261261
const char* RefKind = RefKindRead;
262262
const Stmt* P = GetParent(E);
263-
if (auto* BO = dyn_cast<BinaryOperator>(P)) {
263+
if (auto* BO = dyn_cast_if_present<BinaryOperator>(P)) {
264264
if (E == BO->getLHS() && (BO->isAssignmentOp() || BO->isCompoundAssignmentOp() || BO->isShiftAssignOp()))
265265
RefKind = RefKindWrite;
266266
}
267-
if (auto* UO = dyn_cast<UnaryOperator>(P))
267+
if (auto* UO = dyn_cast_if_present<UnaryOperator>(P))
268268
RefKind = RefKindTakesAddr;
269269
EmitReference(E->getMemberLoc(), Field, EntityKindField, RefKind);
270270
return true;

0 commit comments

Comments
 (0)