Index: clang/tools/libclang/CIndex.cpp =================================================================== --- clang/tools/libclang/CIndex.cpp +++ clang/tools/libclang/CIndex.cpp @@ -1294,7 +1294,7 @@ bool CursorVisitor::VisitStaticAssertDecl(StaticAssertDecl *D) { if (Visit(MakeCXCursor(D->getAssertExpr(), StmtParent, TU, RegionOfInterest))) return true; - if (auto *Message = dyn_cast(D->getMessage())) + if (auto *Message = dyn_cast_if_present(D->getMessage())) if (Visit(MakeCXCursor(Message, StmtParent, TU, RegionOfInterest))) return true; return false; Index: clang/unittests/libclang/LibclangTest.cpp =================================================================== --- clang/unittests/libclang/LibclangTest.cpp +++ clang/unittests/libclang/LibclangTest.cpp @@ -1172,6 +1172,25 @@ }); } +TEST_F(LibclangParseTest, VisitStaticAssertDecl_noMessage) { + const char testSource[] = R"cpp(static_assert(true))cpp"; + std::string fileName = "main.cpp"; + WriteFile(fileName, testSource); + const char *Args[] = {"-xc++"}; + ClangTU = clang_parseTranslationUnit(Index, fileName.c_str(), Args, 1, + nullptr, 0, TUFlags); + + std::optional typeRefCsr; + Traverse([&](CXCursor cursor, CXCursor parent) -> CXChildVisitResult { + if (cursor.kind == CXCursor_StaticAssert) { + typeRefCsr.emplace(cursor); + } + return CXChildVisit_Recurse; + }); + ASSERT_TRUE(typeRefCsr.has_value()); + EXPECT_EQ(fromCXString(clang_getCursorSpelling(*typeRefCsr)), ""); +} + class LibclangRewriteTest : public LibclangParseTest { public: CXRewriter Rew = nullptr;