Skip to content

Commit a71237b

Browse files
HighCommander4tru
authored andcommitted
[clangd] Avoid null result in FindRecordTypeAt()
Fixes clangd/clangd#1743 Differential Revision: https://reviews.llvm.org/D158851 (cherry picked from commit 1994e1173b64b61c07f8acf107f29c2c015575b4)
1 parent 308c816 commit a71237b

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

Diff for: clang-tools-extra/clangd/XRefs.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1857,7 +1857,8 @@ std::vector<const CXXRecordDecl *> findRecordTypeAt(ParsedAST &AST,
18571857

18581858
if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
18591859
// If this is a variable, use the type of the variable.
1860-
Records.push_back(VD->getType().getTypePtr()->getAsCXXRecordDecl());
1860+
if (const auto *RD = VD->getType().getTypePtr()->getAsCXXRecordDecl())
1861+
Records.push_back(RD);
18611862
continue;
18621863
}
18631864

Diff for: clang-tools-extra/clangd/unittests/TypeHierarchyTests.cpp

+14-1
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)