Skip to content

[CLANGD] Do not crash on designator initialization of union #83369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions clang-tools-extra/clangd/unittests/tweaks/ExtractVariableTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ TEST_F(ExtractVariableTest, Test) {
)cpp";
EXPECT_UNAVAILABLE(NoCrashCasesC);

ExtraArgs = {"-xc"};
const char *NoCrashDesignator = R"cpp(
struct A {
struct {
int x;
};
};
struct B {
int y;
};
void foo(struct B *b) {
struct A a = {.x=b[[->]]y};
}
)cpp";
EXPECT_AVAILABLE(NoCrashDesignator);

ExtraArgs = {"-xobjective-c"};
const char *AvailableObjC = R"cpp(
__attribute__((objc_root_class))
Expand Down
13 changes: 11 additions & 2 deletions clang/lib/AST/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4601,8 +4601,17 @@ SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
SourceLocation DesignatedInitExpr::getBeginLoc() const {
auto *DIE = const_cast<DesignatedInitExpr *>(this);
Designator &First = *DIE->getDesignator(0);
if (First.isFieldDesignator())
return GNUSyntax ? First.getFieldLoc() : First.getDotLoc();
if (First.isFieldDesignator()) {
// Skip past implicit designators for anonymous structs/unions, since
// these do not have valid source locations.
for (unsigned int i = 0; i < DIE->size(); i++) {
Designator &Des = *DIE->getDesignator(i);
SourceLocation retval = GNUSyntax ? Des.getFieldLoc() : Des.getDotLoc();
if (!retval.isValid())
continue;
return retval;
}
}
return First.getLBracketLoc();
}

Expand Down