diff --git a/clang-tools-extra/clangd/Hover.cpp b/clang-tools-extra/clangd/Hover.cpp --- a/clang-tools-extra/clangd/Hover.cpp +++ b/clang-tools-extra/clangd/Hover.cpp @@ -662,7 +662,9 @@ } if (const auto *FD = llvm::dyn_cast(&ND)) { - const auto *Record = FD->getParent()->getDefinition(); + const auto *Record = FD->getParent(); + if (Record) + Record = Record->getDefinition(); if (Record && !Record->isDependentType()) { uint64_t OffsetBits = Ctx.getFieldOffset(FD); if (auto Size = Ctx.getTypeSizeInCharsIfKnown(FD->getType())) { diff --git a/clang-tools-extra/clangd/unittests/HoverTests.cpp b/clang-tools-extra/clangd/unittests/HoverTests.cpp --- a/clang-tools-extra/clangd/unittests/HoverTests.cpp +++ b/clang-tools-extra/clangd/unittests/HoverTests.cpp @@ -1737,6 +1737,19 @@ HI.Definition = "template <> void foo(const int &)"; HI.NamespaceScope = ""; }}, + { + R"cpp(// should not crash + @interface ObjC { + char [[da^ta]]; + }@end + )cpp", + [](HoverInfo &HI) { + HI.Name = "data"; + HI.Type = "char"; + HI.Kind = index::SymbolKind::Field; + HI.NamespaceScope = "ObjC::"; // FIXME: fix it + HI.Definition = "char data"; + }}, }; // Create a tiny index, so tests above can verify documentation is fetched. @@ -1753,6 +1766,8 @@ Annotations T(Case.Code); TestTU TU = TestTU::withCode(T.code()); TU.ExtraArgs.push_back("-std=c++17"); + TU.ExtraArgs.push_back("-xobjective-c++"); + TU.ExtraArgs.push_back("-Wno-gnu-designator"); // Types might be different depending on the target triplet, we chose a // fixed one to make sure tests passes on different platform. diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h --- a/clang/include/clang/AST/Decl.h +++ b/clang/include/clang/AST/Decl.h @@ -2920,12 +2920,15 @@ /// Returns the parent of this field declaration, which /// is the struct in which this field is defined. + /// + /// Returns null if this is not a normal class/struct field declaration, e.g. + /// ObjCAtDefsFieldDecl, ObjCIvarDecl. const RecordDecl *getParent() const { - return cast(getDeclContext()); + return dyn_cast(getDeclContext()); } RecordDecl *getParent() { - return cast(getDeclContext()); + return dyn_cast(getDeclContext()); } SourceRange getSourceRange() const override LLVM_READONLY;