[llvm-branch-commits] [clang-tools-extra] 0334c1a - [clangd] Fix an inlay-hint crash on a broken designator.

Tobias Hieta via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Aug 17 23:29:47 PDT 2022


Author: Haojian Wu
Date: 2022-08-18T08:29:04+02:00
New Revision: 0334c1ac1b029a4191f16d2fc19360bb15728571

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

LOG: [clangd] Fix an inlay-hint crash on a broken designator.

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

(cherry picked from commit 06b97b4985ad0415f6cde4baad2bc7d73b456244)

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/InlayHints.cpp b/clang-tools-extra/clangd/InlayHints.cpp
index 9b3b2b1c53224..7be05fbc3b348 100644
--- a/clang-tools-extra/clangd/InlayHints.cpp
+++ b/clang-tools-extra/clangd/InlayHints.cpp
@@ -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 &&

diff  --git a/clang-tools-extra/clangd/unittests/InlayHintTests.cpp b/clang-tools-extra/clangd/unittests/InlayHintTests.cpp
index 87bb0cfc01f60..a429c08991879 100644
--- a/clang-tools-extra/clangd/unittests/InlayHintTests.cpp
+++ b/clang-tools-extra/clangd/unittests/InlayHintTests.cpp
@@ -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;


        


More information about the llvm-branch-commits mailing list