[clangd] Avoid type hierarchy crash on incomplete type

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

Differential Revision: https://reviews.llvm.org/D92077
This commit is contained in:
Nathan Ridge 2020-11-25 03:11:54 -05:00
parent ae7ac2d665
commit 3d2c681f28
2 changed files with 17 additions and 0 deletions

View File

@ -1553,6 +1553,10 @@ std::vector<const CXXRecordDecl *> typeParents(const CXXRecordDecl *CXXRD) {
CXXRD = CTSD->getSpecializedTemplate()->getTemplatedDecl();
}
// Can't query bases without a definition.
if (!CXXRD->hasDefinition())
return Result;
for (auto Base : CXXRD->bases()) {
const CXXRecordDecl *ParentDecl = nullptr;

View File

@ -30,6 +30,7 @@ namespace {
using ::testing::AllOf;
using ::testing::ElementsAre;
using ::testing::Field;
using ::testing::IsEmpty;
using ::testing::Matcher;
using ::testing::UnorderedElementsAre;
@ -318,6 +319,18 @@ struct Child3 : T {};
EXPECT_THAT(typeParents(Child3), ElementsAre());
}
TEST(TypeParents, IncompleteClass) {
Annotations Source(R"cpp(
class Incomplete;
)cpp");
TestTU TU = TestTU::withCode(Source.code());
auto AST = TU.build();
const CXXRecordDecl *Incomplete =
dyn_cast<CXXRecordDecl>(&findDecl(AST, "Incomplete"));
EXPECT_THAT(typeParents(Incomplete), IsEmpty());
}
// Parts of getTypeHierarchy() are tested in more detail by the
// FindRecordTypeAt.* and TypeParents.* tests above. This test exercises the
// entire operation.