Skip to content

Commit 0334c1a

Browse files
hokeintru
authored andcommitted
[clangd] Fix an inlay-hint crash on a broken designator.
Differential Revision: https://reviews.llvm.org/D131696 (cherry picked from commit 06b97b4)
1 parent 67ea7a1 commit 0334c1a

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

clang-tools-extra/clangd/InlayHints.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,10 @@ void collectDesignators(const InitListExpr *Sem,
141141
Fields.next(); // Always advance to the next subobject name.
142142
Prefix.resize(Size); // Erase any designator we appended.
143143
});
144-
if (llvm::isa<ImplicitValueInitExpr>(Init))
145-
continue; // a "hole" for a subobject that was not explicitly initialized
144+
// Skip for a broken initializer or if it is a "hole" in a subobject that
145+
// was not explicitly initialized.
146+
if (!Init || llvm::isa<ImplicitValueInitExpr>(Init))
147+
continue;
146148

147149
const auto *BraceElidedSubobject = llvm::dyn_cast<InitListExpr>(Init);
148150
if (BraceElidedSubobject &&

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,6 +1417,17 @@ TEST(DesignatorHints, OnlyAggregateInit) {
14171417
)cpp" /*no designator hints expected (but param hints!)*/);
14181418
}
14191419

1420+
TEST(DesignatorHints, NoCrash) {
1421+
assertDesignatorHints(R"cpp(
1422+
/*error-ok*/
1423+
struct A {};
1424+
struct Foo {int a; int b;};
1425+
void test() {
1426+
Foo f{A(), $b[[1]]};
1427+
}
1428+
)cpp", ExpectedHint{".b=", "b"});
1429+
}
1430+
14201431
TEST(InlayHints, RestrictRange) {
14211432
Annotations Code(R"cpp(
14221433
auto a = false;

0 commit comments

Comments
 (0)