[llvm-branch-commits] [clang-tools-extra] a71237b - [clangd] Avoid null result in FindRecordTypeAt()

Tobias Hieta via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Nov 20 01:04:49 PST 2023


Author: Nathan Ridge
Date: 2023-11-20T10:04:33+01:00
New Revision: a71237b9f67fdf1a9bd779f33fa91453cd1bb72c

URL: https://github.com/llvm/llvm-project/commit/a71237b9f67fdf1a9bd779f33fa91453cd1bb72c
DIFF: https://github.com/llvm/llvm-project/commit/a71237b9f67fdf1a9bd779f33fa91453cd1bb72c.diff

LOG: [clangd] Avoid null result in FindRecordTypeAt()

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

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

(cherry picked from commit 1994e1173b64b61c07f8acf107f29c2c015575b4)

Added: 
    

Modified: 
    clang-tools-extra/clangd/XRefs.cpp
    clang-tools-extra/clangd/unittests/TypeHierarchyTests.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/XRefs.cpp b/clang-tools-extra/clangd/XRefs.cpp
index da1a803daebb0d0..f81e022085bf669 100644
--- a/clang-tools-extra/clangd/XRefs.cpp
+++ b/clang-tools-extra/clangd/XRefs.cpp
@@ -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;
       }
 

diff  --git a/clang-tools-extra/clangd/unittests/TypeHierarchyTests.cpp b/clang-tools-extra/clangd/unittests/TypeHierarchyTests.cpp
index a73ae78766c3819..2f82ec7444d7a9d 100644
--- a/clang-tools-extra/clangd/unittests/TypeHierarchyTests.cpp
+++ b/clang-tools-extra/clangd/unittests/TypeHierarchyTests.cpp
@@ -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 {
@@ -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));
   }


        


More information about the llvm-branch-commits mailing list