[clangd] Fix an inlay-hint crash on a broken designator.
authorHaojian Wu <hokein.wu@gmail.com>
Thu, 11 Aug 2022 17:26:14 +0000 (19:26 +0200)
committerHaojian Wu <hokein.wu@gmail.com>
Fri, 12 Aug 2022 12:37:46 +0000 (14:37 +0200)
Differential Revision: https://reviews.llvm.org/D131696

clang-tools-extra/clangd/InlayHints.cpp
clang-tools-extra/clangd/unittests/InlayHintTests.cpp

index 8983f6b..bff07dd 100644 (file)
@@ -141,8 +141,10 @@ void collectDesignators(const InitListExpr *Sem,
       Fields.next();       // Always advance to the next subobject name.
       Prefix.resize(Size); // Erase any designator we appended.
     });
-    if (llvm::isa<ImplicitValueInitExpr>(Init))
-      continue; // a "hole" for a subobject that was not explicitly initialized
+    // Skip for a broken initializer or if it is a "hole" in a subobject that
+    // was not explicitly initialized.
+    if (!Init || llvm::isa<ImplicitValueInitExpr>(Init))
+      continue;
 
     const auto *BraceElidedSubobject = llvm::dyn_cast<InitListExpr>(Init);
     if (BraceElidedSubobject &&
index 87bb0cf..a429c08 100644 (file)
@@ -1417,6 +1417,17 @@ TEST(DesignatorHints, OnlyAggregateInit) {
   )cpp" /*no designator hints expected (but param hints!)*/);
 }
 
+TEST(DesignatorHints, NoCrash) {
+  assertDesignatorHints(R"cpp(
+    /*error-ok*/
+    struct A {};
+    struct Foo {int a; int b;};
+    void test() {
+      Foo f{A(), $b[[1]]};
+    }
+  )cpp", ExpectedHint{".b=", "b"});
+}
+
 TEST(InlayHints, RestrictRange) {
   Annotations Code(R"cpp(
     auto a = false;