[PATCH] D132830: [clangd] Avoid crash when printing call to string literal operator template in hover

Nathan Ridge via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 29 01:20:23 PDT 2022


nridge created this revision.
nridge added reviewers: hokein, kadircet, sammccall.
Herald added a subscriber: arphaman.
Herald added a project: All.
nridge requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added projects: clang, clang-tools-extra.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D132830

Files:
  clang-tools-extra/clangd/unittests/HoverTests.cpp
  clang/lib/AST/StmtPrinter.cpp


Index: clang/lib/AST/StmtPrinter.cpp
===================================================================
--- clang/lib/AST/StmtPrinter.cpp
+++ clang/lib/AST/StmtPrinter.cpp
@@ -1993,7 +1993,7 @@
       cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
     assert(Args);
 
-    if (Args->size() != 1) {
+    if (Args->size() != 1 || Args->get(0).getKind() != TemplateArgument::Pack) {
       const TemplateParameterList *TPL = nullptr;
       if (!DRE->hadMultipleCandidates())
         if (const auto *TD = dyn_cast<TemplateDecl>(DRE->getDecl()))
Index: clang-tools-extra/clangd/unittests/HoverTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/HoverTests.cpp
+++ clang-tools-extra/clangd/unittests/HoverTests.cpp
@@ -3200,6 +3200,24 @@
   EXPECT_EQ(*HI->Value, "&bar");
 }
 
+TEST(Hover, ClassNTTPNoCrash) {
+  Annotations T(R"cpp(
+  struct A {
+    template <unsigned N> constexpr A(const char (&)[N]) : n(N) {}
+    unsigned n;
+  };
+  template <A a> constexpr auto operator""_a() { return a.n; }
+  constexpr auto w^aldo = "abc"_a;
+  )cpp");
+  TestTU TU = TestTU::withCode(T.code());
+  TU.ExtraArgs.push_back("-std=c++20");
+  auto AST = TU.build();
+  auto HI = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
+  ASSERT_TRUE(HI);
+  EXPECT_EQ(HI->Definition, "constexpr auto waldo = operator\"\"_a<{4}>()");
+  EXPECT_EQ(*HI->Value, "4");
+}
+
 TEST(Hover, DisableShowAKA) {
   Annotations T(R"cpp(
     using m_int = int;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D132830.456277.patch
Type: text/x-patch
Size: 1530 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220829/86db30cd/attachment.bin>


More information about the cfe-commits mailing list