[clang-tools-extra] 2867095 - [clang] Skip implicit designators in DesignatedInitExpr::getBeginLoc (#83369)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Mar 16 13:29:24 PDT 2024
Author: alirezamoshtaghi
Date: 2024-03-16T16:29:20-04:00
New Revision: 2867095917cef0d01a68b112bcb01e2f85de4308
URL: https://github.com/llvm/llvm-project/commit/2867095917cef0d01a68b112bcb01e2f85de4308
DIFF: https://github.com/llvm/llvm-project/commit/2867095917cef0d01a68b112bcb01e2f85de4308.diff
LOG: [clang] Skip implicit designators in DesignatedInitExpr::getBeginLoc (#83369)
Fixes https://github.com/llvm/llvm-project/issues/83185
Added:
Modified:
clang-tools-extra/clangd/unittests/tweaks/ExtractVariableTests.cpp
clang/lib/AST/Expr.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clangd/unittests/tweaks/ExtractVariableTests.cpp b/clang-tools-extra/clangd/unittests/tweaks/ExtractVariableTests.cpp
index 42dd612eeeec46..656b62c9a1f4e1 100644
--- a/clang-tools-extra/clangd/unittests/tweaks/ExtractVariableTests.cpp
+++ b/clang-tools-extra/clangd/unittests/tweaks/ExtractVariableTests.cpp
@@ -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))
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index f5ad402e3bd73e..131dace77f9c25 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -4604,8 +4604,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();
}
More information about the cfe-commits
mailing list