Skip to content

Commit

Permalink
[clangd] Avoid null result in FindRecordTypeAt()
Browse files Browse the repository at this point in the history
Fixes clangd/clangd#1743

Differential Revision: https://reviews.llvm.org/D158851

(cherry picked from commit 1994e11)
  • Loading branch information
HighCommander4 authored and tru committed Nov 20, 2023
1 parent 308c816 commit a71237b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
3 changes: 2 additions & 1 deletion clang-tools-extra/clangd/XRefs.cpp
Expand Up @@ -1857,7 +1857,8 @@ std::vector<const CXXRecordDecl *> findRecordTypeAt(ParsedAST &AST,

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

Expand Down
15 changes: 14 additions & 1 deletion clang-tools-extra/clangd/unittests/TypeHierarchyTests.cpp
Expand Up @@ -78,6 +78,19 @@ int main() {
}
}

TEST(FindRecordTypeAt, Nonexistent) {
Annotations Source(R"cpp(
int *wa^ldo;
)cpp");
TestTU TU = TestTU::withCode(Source.code());
auto AST = TU.build();

for (Position Pt : Source.points()) {
auto Records = findRecordTypeAt(AST, Pt);
ASSERT_THAT(Records, SizeIs(0));
}
}

TEST(FindRecordTypeAt, Method) {
Annotations Source(R"cpp(
struct Child2 {
Expand Down Expand Up @@ -119,7 +132,7 @@ int main() {

for (Position Pt : Source.points()) {
// A field does not unambiguously specify a record type
// (possible associated reocrd types could be the field's type,
// (possible associated record types could be the field's type,
// or the type of the record that the field is a member of).
EXPECT_THAT(findRecordTypeAt(AST, Pt), SizeIs(0));
}
Expand Down

0 comments on commit a71237b

Please sign in to comment.