[clang-tools-extra] [clangd] Avoid crash on pseudo-destructor selection (PR #195939)

via cfe-commits cfe-commits at lists.llvm.org
Tue May 5 14:01:29 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-tools-extra

Author: nataliakokoromyti

<details>
<summary>Changes</summary>

clangd crashes during textDocument/codeAction on malformed pseudo-destructor expressions like y->~decltype(A())(). The bug is in Selection.cpp::earlySourceRange(), which assumes destructor names always have NamedTypeInfo. The fix is adding null checks before calling getTypeLoc(). 

Fixes #<!-- -->195788. 

---
Full diff: https://github.com/llvm/llvm-project/pull/195939.diff


2 Files Affected:

- (modified) clang-tools-extra/clangd/Selection.cpp (+8-4) 
- (modified) clang-tools-extra/clangd/unittests/SelectionTests.cpp (+18) 


``````````diff
diff --git a/clang-tools-extra/clangd/Selection.cpp b/clang-tools-extra/clangd/Selection.cpp
index b79ffc7d5a6e9..0babe71ce4788 100644
--- a/clang-tools-extra/clangd/Selection.cpp
+++ b/clang-tools-extra/clangd/Selection.cpp
@@ -895,13 +895,17 @@ class SelectionVisitor : public RecursiveASTVisitor<SelectionVisitor> {
     // rather than the TypeLoc nested inside it.
     // We still traverse the TypeLoc, because it may contain other targeted
     // things like the T in ~Foo<T>().
-    if (const auto *CDD = N.get<CXXDestructorDecl>())
-      return CDD->getNameInfo().getNamedTypeInfo()->getTypeLoc().getBeginLoc();
+    if (const auto *CDD = N.get<CXXDestructorDecl>()) {
+      if (auto *TypeInfo = CDD->getNameInfo().getNamedTypeInfo())
+        return TypeInfo->getTypeLoc().getBeginLoc();
+    }
     if (const auto *ME = N.get<MemberExpr>()) {
       auto NameInfo = ME->getMemberNameInfo();
       if (NameInfo.getName().getNameKind() ==
-          DeclarationName::CXXDestructorName)
-        return NameInfo.getNamedTypeInfo()->getTypeLoc().getBeginLoc();
+          DeclarationName::CXXDestructorName) {
+        if (auto *TypeInfo = NameInfo.getNamedTypeInfo())
+          return TypeInfo->getTypeLoc().getBeginLoc();
+      }
     }
 
     return SourceRange();
diff --git a/clang-tools-extra/clangd/unittests/SelectionTests.cpp b/clang-tools-extra/clangd/unittests/SelectionTests.cpp
index 5e897fae79df4..5b325d7a6a387 100644
--- a/clang-tools-extra/clangd/unittests/SelectionTests.cpp
+++ b/clang-tools-extra/clangd/unittests/SelectionTests.cpp
@@ -665,6 +665,24 @@ TEST(SelectionTest, InjectedClassName) {
   EXPECT_FALSE(D->isInjectedClassName());
 }
 
+TEST(SelectionTest, PseudoDestructorMissingTypeInfo) {
+  llvm::StringLiteral Code = R"cpp(
+    /*error-ok*/
+    struct A { ~A(); };
+    void b(const A *y) {
+      y->~decltype(A())();
+    }
+  )cpp";
+  auto AST = TestTU::withCode(Code).build();
+  bool Seen = false;
+  SelectionTree::createEach(AST.getASTContext(), AST.getTokens(), 0,
+                            Code.size(), [&](SelectionTree) {
+                              Seen = true;
+                              return true;
+                            });
+  EXPECT_TRUE(Seen);
+}
+
 TEST(SelectionTree, Metrics) {
   const char *Code = R"cpp(
     // error-ok: testing behavior on recovery expression

``````````

</details>


https://github.com/llvm/llvm-project/pull/195939


More information about the cfe-commits mailing list