Skip to content

Commit 1994e11

Browse files
[clangd] Avoid null result in FindRecordTypeAt()
Fixes clangd/clangd#1743 Differential Revision: https://reviews.llvm.org/D158851
1 parent cccf4d6 commit 1994e11

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

clang-tools-extra/clangd/XRefs.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1851,7 +1851,8 @@ std::vector<const CXXRecordDecl *> findRecordTypeAt(ParsedAST &AST,
18511851

18521852
if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
18531853
// If this is a variable, use the type of the variable.
1854-
Records.push_back(VD->getType().getTypePtr()->getAsCXXRecordDecl());
1854+
if (const auto *RD = VD->getType().getTypePtr()->getAsCXXRecordDecl())
1855+
Records.push_back(RD);
18551856
continue;
18561857
}
18571858

clang-tools-extra/clangd/unittests/TypeHierarchyTests.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,19 @@ int main() {
7878
}
7979
}
8080

81+
TEST(FindRecordTypeAt, Nonexistent) {
82+
Annotations Source(R"cpp(
83+
int *wa^ldo;
84+
)cpp");
85+
TestTU TU = TestTU::withCode(Source.code());
86+
auto AST = TU.build();
87+
88+
for (Position Pt : Source.points()) {
89+
auto Records = findRecordTypeAt(AST, Pt);
90+
ASSERT_THAT(Records, SizeIs(0));
91+
}
92+
}
93+
8194
TEST(FindRecordTypeAt, Method) {
8295
Annotations Source(R"cpp(
8396
struct Child2 {
@@ -119,7 +132,7 @@ int main() {
119132

120133
for (Position Pt : Source.points()) {
121134
// A field does not unambiguously specify a record type
122-
// (possible associated reocrd types could be the field's type,
135+
// (possible associated record types could be the field's type,
123136
// or the type of the record that the field is a member of).
124137
EXPECT_THAT(findRecordTypeAt(AST, Pt), SizeIs(0));
125138
}

0 commit comments

Comments
 (0)