[AST] Fix an assertion violation in FieldDecl::getParent.
authorHaojian Wu <hokein.wu@gmail.com>
Tue, 19 May 2020 13:26:42 +0000 (15:26 +0200)
committerHaojian Wu <hokein.wu@gmail.com>
Tue, 19 May 2020 13:35:04 +0000 (15:35 +0200)
commitfcf0764998b45279cfdf4039c69aec2cd09051a5
tree25e763aea31e22e7391226f1ecc665f80292aa92
parent0320ce8916a815d10a0449f5581e0f3ca7183922
[AST] Fix an assertion violation in FieldDecl::getParent.

Summary:
FieldDecl::getParent assumes that the FiledDecl::getDeclContext returns a
RecordDecl, this is true for C/C++, but not for ObjCIvarDecl:

The Decls hierarchy is like following

FieldDecl <-- ObjCIvarDecl

DeclContext <-- ObjCContainerDecl <-- ObjCInterfaceDecl
         ^
         |----- TagDecl <-- RecordDecl

calling getParent() on ObjCIvarDecl will:
1. invoke getDeclContext(), which returns a DeclContext*, which points to an ObjCInterfaceDecl;
2. then downcast the "DeclContext" pointer to a RecordDecl*, and we will hit
the "is_a<RecordDecl>" assertion in llvm::cast (undefined behavior
in release build without assertion enabled);

Fixes https://github.com/clangd/clangd/issues/369

Reviewers: sammccall

Reviewed By: sammccall

Subscribers: rsmith, jkorous, arphaman, kadircet, usaxena95, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D79627
clang-tools-extra/clangd/Hover.cpp
clang-tools-extra/clangd/unittests/HoverTests.cpp
clang/include/clang/AST/Decl.h