Skip to content

Commit 8492b82

Browse files
committed
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. For backward compatibility, in LLVM < 16 we switch to dyn_cast_or_null.
1 parent 7c10111 commit 8492b82

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

tools/clang/codesearch/codesearch.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "clang/Frontend/CompilerInstance.h"
1515
#include "clang/Tooling/CommonOptionsParser.h"
1616
#include "clang/Tooling/Tooling.h"
17+
#include "llvm/Config/llvm-config.h"
1718
#include "llvm/Support/Casting.h"
1819
#include "llvm/Support/CommandLine.h"
1920
#include "llvm/Support/ErrorHandling.h"
@@ -23,6 +24,11 @@
2324
#include <string>
2425
#include <unordered_map>
2526

27+
// LLVM 16 renamed dyn_cast_or_null to dyn_cast_if_present
28+
#if LLVM_VERSION_MAJOR < 16
29+
#define dyn_cast_if_present dyn_cast_or_null
30+
#endif
31+
2632
using namespace clang;
2733

2834
// MacroDef/MacroMap hold information about macros defined in the file.
@@ -203,7 +209,7 @@ bool Indexer::TraverseCallExpr(CallExpr* CE) {
203209
}
204210

205211
bool Indexer::VisitDeclRefExpr(const DeclRefExpr* DeclRef) {
206-
if (const auto* Func = dyn_cast<FunctionDecl>(DeclRef->getDecl()))
212+
if (const auto* Func = dyn_cast_if_present<FunctionDecl>(DeclRef->getDecl()))
207213
EmitReference(DeclRef->getBeginLoc(), DeclRef->getDecl(), EntityKindFunction,
208214
InCallee ? RefKindCall : RefKindTakesAddr);
209215
return true;
@@ -246,25 +252,25 @@ bool Indexer::VisitTypedefType(const TypedefType* T) {
246252
return true;
247253
EmitReference(TypeRefingLocation, T->getDecl(), EntityKindTypedef, RefKindUses);
248254
// If it's a struct typedef, also note the struct use.
249-
if (const auto* Tag = dyn_cast<TagType>(T->getCanonicalTypeInternal().getTypePtr()))
255+
if (const auto* Tag = dyn_cast_if_present<TagType>(T->getCanonicalTypeInternal().getTypePtr()))
250256
VisitTagType(Tag);
251257
return true;
252258
}
253259

254260
bool Indexer::VisitMemberExpr(const MemberExpr* E) {
255261
auto* Record = E->getBase()->getType()->getAsRecordDecl();
256-
if (auto* Ptr = dyn_cast<PointerType>(E->getBase()->getType()))
262+
if (auto* Ptr = dyn_cast_if_present<PointerType>(E->getBase()->getType()))
257263
Record = Ptr->getPointeeType()->getAsRecordDecl();
258264
if (!Record)
259265
return true;
260266
const std::string Field = Record->getNameAsString() + "::" + E->getMemberDecl()->getNameAsString();
261267
const char* RefKind = RefKindRead;
262268
const Stmt* P = GetParent(E);
263-
if (auto* BO = dyn_cast<BinaryOperator>(P)) {
269+
if (auto* BO = dyn_cast_if_present<BinaryOperator>(P)) {
264270
if (E == BO->getLHS() && (BO->isAssignmentOp() || BO->isCompoundAssignmentOp() || BO->isShiftAssignOp()))
265271
RefKind = RefKindWrite;
266272
}
267-
if (auto* UO = dyn_cast<UnaryOperator>(P))
273+
if (auto* UO = dyn_cast_if_present<UnaryOperator>(P))
268274
RefKind = RefKindTakesAddr;
269275
EmitReference(E->getMemberLoc(), Field, EntityKindField, RefKind);
270276
return true;

0 commit comments

Comments
 (0)