[clang] [clang-tools-extra] [CLANGD] Do not crash on designator initialization of union (PR #83369)

2024-03-16 Thread via cfe-commits

github-actions[bot] wrote:



@alirezamoshtaghi Congratulations on having your first Pull Request (PR) merged 
into the LLVM Project!

Your changes will be combined with recent changes from other authors, then 
tested
by our [build bots](https://lab.llvm.org/buildbot/). If there is a problem with 
a build, you may recieve a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as
the builds can include changes from many authors. It is not uncommon for your
change to be included in a build that fails due to someone else's changes, or
infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail 
[here](https://llvm.org/docs/MyFirstTypoFix.html#myfirsttypofix-issues-after-landing-your-pr).

If your change does cause a problem, it may be reverted, or you can revert it 
yourself.
This is a normal part of [LLVM 
development](https://llvm.org/docs/DeveloperPolicy.html#patch-reversion-policy).
 You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are 
working as expected, well done!


https://github.com/llvm/llvm-project/pull/83369
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] [CLANGD] Do not crash on designator initialization of union (PR #83369)

2024-03-16 Thread Nathan Ridge via cfe-commits

https://github.com/HighCommander4 closed 
https://github.com/llvm/llvm-project/pull/83369
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] [CLANGD] Do not crash on designator initialization of union (PR #83369)

2024-03-15 Thread Nathan Ridge via cfe-commits

https://github.com/HighCommander4 approved this pull request.

LGTM

https://github.com/llvm/llvm-project/pull/83369
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] [CLANGD] Do not crash on designator initialization of union (PR #83369)

2024-03-15 Thread via cfe-commits

https://github.com/alirezamoshtaghi updated 
https://github.com/llvm/llvm-project/pull/83369

>From 3d6afe011221ac239bb668b375ed3f6c356fc47d Mon Sep 17 00:00:00 2001
From: alirezamoshtaghi 
Date: Wed, 28 Feb 2024 13:55:11 -0800
Subject: [PATCH 1/5] [CLANGD] Do not crash on designator initialization of
 union

---
 .../clangd/test/designator_init.test  | 31 +++
 clang/lib/AST/Expr.cpp| 14 +++--
 2 files changed, 43 insertions(+), 2 deletions(-)
 create mode 100644 clang-tools-extra/clangd/test/designator_init.test

diff --git a/clang-tools-extra/clangd/test/designator_init.test 
b/clang-tools-extra/clangd/test/designator_init.test
new file mode 100644
index 00..739f2bfab54bcf
--- /dev/null
+++ b/clang-tools-extra/clangd/test/designator_init.test
@@ -0,0 +1,31 @@
+//# RUN: rm -rf %t.dir/* && mkdir -p %t.dir
+//# RUN: echo '[{"directory": "%/t.dir", "command": "clang -x c -c %s", 
"file": "%s"}]' > %t.dir/compile_commands.json
+//# RUN: clangd --compile-commands-dir=%t.dir --check=%s 2>&1 | FileCheck %s
+
+typedef struct S {
+  unsigned char id;
+  union {
+unsigned int mask;
+struct {
+  unsigned int unused:10;
+  unsigned int reserved:3;
+  unsigned int rest:19;
+};
+  };
+} __attribute__((packed)) S_t;
+
+typedef struct H {
+  unsigned char hid;
+  unsigned int val;
+} handler_t;
+
+struct S
+get_foo (handler_t *h)
+{
+  S_t retval =
+{.id=h->hid,
+ .mask=h->val};
+  return retval;
+}
+
+// CHECK: All checks completed, 0 errors
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index b4de2155adcebd..33eeeda89fe7a5 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -4601,11 +4601,21 @@ SourceRange 
DesignatedInitExpr::getDesignatorsSourceRange() const {
 SourceLocation DesignatedInitExpr::getBeginLoc() const {
   auto *DIE = const_cast(this);
   Designator &First = *DIE->getDesignator(0);
-  if (First.isFieldDesignator())
-return GNUSyntax ? First.getFieldLoc() : First.getDotLoc();
+  if (First.isFieldDesignator()) {
+/* search all designators in case the first one is not
+   initialized */
+for (unsigned int i=0; isize(); i++) {
+  Designator &Des = *DIE->getDesignator(i);
+  SourceLocation retval = GNUSyntax ? Des.getFieldLoc() : Des.getDotLoc();
+  if (!retval.isValid ())
+   continue;
+  return retval;
+}
+  }
   return First.getLBracketLoc();
 }
 
+
 SourceLocation DesignatedInitExpr::getEndLoc() const {
   return getInit()->getEndLoc();
 }

>From 397f6cd893a6a07426d39f1dabd7ad27282435af Mon Sep 17 00:00:00 2001
From: alirezamoshtaghi 
Date: Mon, 4 Mar 2024 11:54:56 -0800
Subject: [PATCH 2/5] fix formatting errors

---
 clang/lib/AST/Expr.cpp | 9 +++--
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index 33eeeda89fe7a5..2e7170ecac0618 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -4602,20 +4602,17 @@ SourceLocation DesignatedInitExpr::getBeginLoc() const {
   auto *DIE = const_cast(this);
   Designator &First = *DIE->getDesignator(0);
   if (First.isFieldDesignator()) {
-/* search all designators in case the first one is not
-   initialized */
-for (unsigned int i=0; isize(); i++) {
+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;
+  if (!retval.isValid())
+continue;
   return retval;
 }
   }
   return First.getLBracketLoc();
 }
 
-
 SourceLocation DesignatedInitExpr::getEndLoc() const {
   return getInit()->getEndLoc();
 }

>From d1d1458809e0b27dc05c70ef449ebbe6896437d4 Mon Sep 17 00:00:00 2001
From: alirezamoshtaghi 
Date: Tue, 5 Mar 2024 00:04:52 -0800
Subject: [PATCH 3/5] dont test designator on Windows

---
 clang-tools-extra/clangd/test/designator_init.test | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang-tools-extra/clangd/test/designator_init.test 
b/clang-tools-extra/clangd/test/designator_init.test
index 739f2bfab54bcf..4009945ed81fd4 100644
--- a/clang-tools-extra/clangd/test/designator_init.test
+++ b/clang-tools-extra/clangd/test/designator_init.test
@@ -1,7 +1,7 @@
 //# RUN: rm -rf %t.dir/* && mkdir -p %t.dir
 //# RUN: echo '[{"directory": "%/t.dir", "command": "clang -x c -c %s", 
"file": "%s"}]' > %t.dir/compile_commands.json
 //# RUN: clangd --compile-commands-dir=%t.dir --check=%s 2>&1 | FileCheck %s
-
+//# UNSUPPORTED: system-windows
 typedef struct S {
   unsigned char id;
   union {

>From a792acd4eec0f549bdbf9779dce14aca5357c2db Mon Sep 17 00:00:00 2001
From: alirezamoshtaghi 
Date: Wed, 13 Mar 2024 18:51:38 -0700
Subject: [PATCH 4/5] Move the test to clangd unit tests

---
 .../clangd/test/designator_init.test  | 31 ---
 .../unittests/tweaks/ExtractVariableTests.cpp | 16 

[clang] [clang-tools-extra] [CLANGD] Do not crash on designator initialization of union (PR #83369)

2024-03-15 Thread Nathan Ridge via cfe-commits

https://github.com/HighCommander4 edited 
https://github.com/llvm/llvm-project/pull/83369
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] [CLANGD] Do not crash on designator initialization of union (PR #83369)

2024-03-15 Thread Nathan Ridge via cfe-commits


@@ -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};

HighCommander4 wrote:

This test case does not fail without the fix.

The reason is that the selection `[[x]]` does not trigger the crash.

The selection needs to be on the `[[->]]` to trigger the crash (and then, the 
result will be `AVAILABLE`).

Otherwise looks good, thanks!

https://github.com/llvm/llvm-project/pull/83369
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] [CLANGD] Do not crash on designator initialization of union (PR #83369)

2024-03-15 Thread Nathan Ridge via cfe-commits

https://github.com/HighCommander4 requested changes to this pull request.


https://github.com/llvm/llvm-project/pull/83369
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] [CLANGD] Do not crash on designator initialization of union (PR #83369)

2024-03-13 Thread via cfe-commits

https://github.com/alirezamoshtaghi deleted 
https://github.com/llvm/llvm-project/pull/83369
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] [CLANGD] Do not crash on designator initialization of union (PR #83369)

2024-03-13 Thread via cfe-commits


@@ -4601,8 +4601,15 @@ SourceRange 
DesignatedInitExpr::getDesignatorsSourceRange() const {
 SourceLocation DesignatedInitExpr::getBeginLoc() const {
   auto *DIE = const_cast(this);
   Designator &First = *DIE->getDesignator(0);
-  if (First.isFieldDesignator())
-return GNUSyntax ? First.getFieldLoc() : First.getDotLoc();
+  if (First.isFieldDesignator()) {
+for (unsigned int i = 0; i < DIE->size(); i++) {

alirezamoshtaghi wrote:

I ended up using EXPECT_UNAVAILABLE because it still fails for crashing case.

but when I use EXPECT_AVAILABLE, it complains as:
Expected equality of these values:
  true
  isAvailable(AST, R)
Which is: false

struct A {
  struct {
int x;
  };
};
struct B {
  int y;
};
void foo(struct B *b) {
  struct A a = {.[[x]]=b->y};
}


https://github.com/llvm/llvm-project/pull/83369
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] [CLANGD] Do not crash on designator initialization of union (PR #83369)

2024-03-13 Thread via cfe-commits

https://github.com/alirezamoshtaghi updated 
https://github.com/llvm/llvm-project/pull/83369

>From 3d6afe011221ac239bb668b375ed3f6c356fc47d Mon Sep 17 00:00:00 2001
From: alirezamoshtaghi 
Date: Wed, 28 Feb 2024 13:55:11 -0800
Subject: [PATCH 1/4] [CLANGD] Do not crash on designator initialization of
 union

---
 .../clangd/test/designator_init.test  | 31 +++
 clang/lib/AST/Expr.cpp| 14 +++--
 2 files changed, 43 insertions(+), 2 deletions(-)
 create mode 100644 clang-tools-extra/clangd/test/designator_init.test

diff --git a/clang-tools-extra/clangd/test/designator_init.test 
b/clang-tools-extra/clangd/test/designator_init.test
new file mode 100644
index 00..739f2bfab54bcf
--- /dev/null
+++ b/clang-tools-extra/clangd/test/designator_init.test
@@ -0,0 +1,31 @@
+//# RUN: rm -rf %t.dir/* && mkdir -p %t.dir
+//# RUN: echo '[{"directory": "%/t.dir", "command": "clang -x c -c %s", 
"file": "%s"}]' > %t.dir/compile_commands.json
+//# RUN: clangd --compile-commands-dir=%t.dir --check=%s 2>&1 | FileCheck %s
+
+typedef struct S {
+  unsigned char id;
+  union {
+unsigned int mask;
+struct {
+  unsigned int unused:10;
+  unsigned int reserved:3;
+  unsigned int rest:19;
+};
+  };
+} __attribute__((packed)) S_t;
+
+typedef struct H {
+  unsigned char hid;
+  unsigned int val;
+} handler_t;
+
+struct S
+get_foo (handler_t *h)
+{
+  S_t retval =
+{.id=h->hid,
+ .mask=h->val};
+  return retval;
+}
+
+// CHECK: All checks completed, 0 errors
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index b4de2155adcebd..33eeeda89fe7a5 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -4601,11 +4601,21 @@ SourceRange 
DesignatedInitExpr::getDesignatorsSourceRange() const {
 SourceLocation DesignatedInitExpr::getBeginLoc() const {
   auto *DIE = const_cast(this);
   Designator &First = *DIE->getDesignator(0);
-  if (First.isFieldDesignator())
-return GNUSyntax ? First.getFieldLoc() : First.getDotLoc();
+  if (First.isFieldDesignator()) {
+/* search all designators in case the first one is not
+   initialized */
+for (unsigned int i=0; isize(); i++) {
+  Designator &Des = *DIE->getDesignator(i);
+  SourceLocation retval = GNUSyntax ? Des.getFieldLoc() : Des.getDotLoc();
+  if (!retval.isValid ())
+   continue;
+  return retval;
+}
+  }
   return First.getLBracketLoc();
 }
 
+
 SourceLocation DesignatedInitExpr::getEndLoc() const {
   return getInit()->getEndLoc();
 }

>From 397f6cd893a6a07426d39f1dabd7ad27282435af Mon Sep 17 00:00:00 2001
From: alirezamoshtaghi 
Date: Mon, 4 Mar 2024 11:54:56 -0800
Subject: [PATCH 2/4] fix formatting errors

---
 clang/lib/AST/Expr.cpp | 9 +++--
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index 33eeeda89fe7a5..2e7170ecac0618 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -4602,20 +4602,17 @@ SourceLocation DesignatedInitExpr::getBeginLoc() const {
   auto *DIE = const_cast(this);
   Designator &First = *DIE->getDesignator(0);
   if (First.isFieldDesignator()) {
-/* search all designators in case the first one is not
-   initialized */
-for (unsigned int i=0; isize(); i++) {
+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;
+  if (!retval.isValid())
+continue;
   return retval;
 }
   }
   return First.getLBracketLoc();
 }
 
-
 SourceLocation DesignatedInitExpr::getEndLoc() const {
   return getInit()->getEndLoc();
 }

>From d1d1458809e0b27dc05c70ef449ebbe6896437d4 Mon Sep 17 00:00:00 2001
From: alirezamoshtaghi 
Date: Tue, 5 Mar 2024 00:04:52 -0800
Subject: [PATCH 3/4] dont test designator on Windows

---
 clang-tools-extra/clangd/test/designator_init.test | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang-tools-extra/clangd/test/designator_init.test 
b/clang-tools-extra/clangd/test/designator_init.test
index 739f2bfab54bcf..4009945ed81fd4 100644
--- a/clang-tools-extra/clangd/test/designator_init.test
+++ b/clang-tools-extra/clangd/test/designator_init.test
@@ -1,7 +1,7 @@
 //# RUN: rm -rf %t.dir/* && mkdir -p %t.dir
 //# RUN: echo '[{"directory": "%/t.dir", "command": "clang -x c -c %s", 
"file": "%s"}]' > %t.dir/compile_commands.json
 //# RUN: clangd --compile-commands-dir=%t.dir --check=%s 2>&1 | FileCheck %s
-
+//# UNSUPPORTED: system-windows
 typedef struct S {
   unsigned char id;
   union {

>From a792acd4eec0f549bdbf9779dce14aca5357c2db Mon Sep 17 00:00:00 2001
From: alirezamoshtaghi 
Date: Wed, 13 Mar 2024 18:51:38 -0700
Subject: [PATCH 4/4] Move the test to clangd unit tests

---
 .../clangd/test/designator_init.test  | 31 ---
 .../unittests/tweaks/ExtractVariableTests.cpp | 16 

[clang] [clang-tools-extra] [CLANGD] Do not crash on designator initialization of union (PR #83369)

2024-03-12 Thread Nathan Ridge via cfe-commits


@@ -4601,8 +4601,15 @@ SourceRange 
DesignatedInitExpr::getDesignatorsSourceRange() const {
 SourceLocation DesignatedInitExpr::getBeginLoc() const {
   auto *DIE = const_cast(this);
   Designator &First = *DIE->getDesignator(0);
-  if (First.isFieldDesignator())
-return GNUSyntax ? First.getFieldLoc() : First.getDotLoc();
+  if (First.isFieldDesignator()) {
+for (unsigned int i = 0; i < DIE->size(); i++) {

HighCommander4 wrote:

Please add a comment explaining what this code is doing. Something like:

```c++
// Skip past implicit designators for anonymous structs/unions, since
// these do not have valid source locations.
```

https://github.com/llvm/llvm-project/pull/83369
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] [CLANGD] Do not crash on designator initialization of union (PR #83369)

2024-03-12 Thread Nathan Ridge via cfe-commits

https://github.com/HighCommander4 requested changes to this pull request.

Thanks for the patch!

I wrote some notes in the issue 
(https://github.com/llvm/llvm-project/issues/83185) to help me understand the 
problem the patch is fixing.

The fix looks good to me.

Regarding the test:

 * Usually, for fixes in frontend code (`clang` directory), it's preferred to 
have a test which is also in `clang`. For an issue like this (an AST node 
having a bad source location), a common way to test it is a lit test with runs 
`ast-dump` on the source file and checks the node's source location in the AST 
dump.
   * However, `DesignatedInitExpr` does not appear at all in the AST dump. (The 
reason is that it's only part of the syntactic form of `InitListExpr`, and the 
AST dumping code only prints the semantic form.)
 * Therefore, I would say writing a clangd test is fine.
   * For clangd tests, we generally prefer unit tests. Could you put the 
testcase in `ExtractVariableTests.cpp`, like 
[this](https://searchfox.org/llvm/rev/ffe41819e58365dfbe85a22556c0d9d284e746b9/clang-tools-extra/clangd/unittests/tweaks/ExtractVariableTests.cpp#68-73)?
 The input is the source code with the selection that causes the crash 
annotated with `[[]]`, and then you can call `EXPECT_AVAILABLE()` on it (with 
the fix, the refactoring is in fact available for this selection).
 
Thanks!

https://github.com/llvm/llvm-project/pull/83369
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] [CLANGD] Do not crash on designator initialization of union (PR #83369)

2024-03-12 Thread Nathan Ridge via cfe-commits

https://github.com/HighCommander4 edited 
https://github.com/llvm/llvm-project/pull/83369
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] [CLANGD] Do not crash on designator initialization of union (PR #83369)

2024-03-05 Thread via cfe-commits

https://github.com/alirezamoshtaghi updated 
https://github.com/llvm/llvm-project/pull/83369

>From 3d6afe011221ac239bb668b375ed3f6c356fc47d Mon Sep 17 00:00:00 2001
From: alirezamoshtaghi 
Date: Wed, 28 Feb 2024 13:55:11 -0800
Subject: [PATCH 1/3] [CLANGD] Do not crash on designator initialization of
 union

---
 .../clangd/test/designator_init.test  | 31 +++
 clang/lib/AST/Expr.cpp| 14 +++--
 2 files changed, 43 insertions(+), 2 deletions(-)
 create mode 100644 clang-tools-extra/clangd/test/designator_init.test

diff --git a/clang-tools-extra/clangd/test/designator_init.test 
b/clang-tools-extra/clangd/test/designator_init.test
new file mode 100644
index 00..739f2bfab54bcf
--- /dev/null
+++ b/clang-tools-extra/clangd/test/designator_init.test
@@ -0,0 +1,31 @@
+//# RUN: rm -rf %t.dir/* && mkdir -p %t.dir
+//# RUN: echo '[{"directory": "%/t.dir", "command": "clang -x c -c %s", 
"file": "%s"}]' > %t.dir/compile_commands.json
+//# RUN: clangd --compile-commands-dir=%t.dir --check=%s 2>&1 | FileCheck %s
+
+typedef struct S {
+  unsigned char id;
+  union {
+unsigned int mask;
+struct {
+  unsigned int unused:10;
+  unsigned int reserved:3;
+  unsigned int rest:19;
+};
+  };
+} __attribute__((packed)) S_t;
+
+typedef struct H {
+  unsigned char hid;
+  unsigned int val;
+} handler_t;
+
+struct S
+get_foo (handler_t *h)
+{
+  S_t retval =
+{.id=h->hid,
+ .mask=h->val};
+  return retval;
+}
+
+// CHECK: All checks completed, 0 errors
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index b4de2155adcebd..33eeeda89fe7a5 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -4601,11 +4601,21 @@ SourceRange 
DesignatedInitExpr::getDesignatorsSourceRange() const {
 SourceLocation DesignatedInitExpr::getBeginLoc() const {
   auto *DIE = const_cast(this);
   Designator &First = *DIE->getDesignator(0);
-  if (First.isFieldDesignator())
-return GNUSyntax ? First.getFieldLoc() : First.getDotLoc();
+  if (First.isFieldDesignator()) {
+/* search all designators in case the first one is not
+   initialized */
+for (unsigned int i=0; isize(); i++) {
+  Designator &Des = *DIE->getDesignator(i);
+  SourceLocation retval = GNUSyntax ? Des.getFieldLoc() : Des.getDotLoc();
+  if (!retval.isValid ())
+   continue;
+  return retval;
+}
+  }
   return First.getLBracketLoc();
 }
 
+
 SourceLocation DesignatedInitExpr::getEndLoc() const {
   return getInit()->getEndLoc();
 }

>From 397f6cd893a6a07426d39f1dabd7ad27282435af Mon Sep 17 00:00:00 2001
From: alirezamoshtaghi 
Date: Mon, 4 Mar 2024 11:54:56 -0800
Subject: [PATCH 2/3] fix formatting errors

---
 clang/lib/AST/Expr.cpp | 9 +++--
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index 33eeeda89fe7a5..2e7170ecac0618 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -4602,20 +4602,17 @@ SourceLocation DesignatedInitExpr::getBeginLoc() const {
   auto *DIE = const_cast(this);
   Designator &First = *DIE->getDesignator(0);
   if (First.isFieldDesignator()) {
-/* search all designators in case the first one is not
-   initialized */
-for (unsigned int i=0; isize(); i++) {
+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;
+  if (!retval.isValid())
+continue;
   return retval;
 }
   }
   return First.getLBracketLoc();
 }
 
-
 SourceLocation DesignatedInitExpr::getEndLoc() const {
   return getInit()->getEndLoc();
 }

>From d1d1458809e0b27dc05c70ef449ebbe6896437d4 Mon Sep 17 00:00:00 2001
From: alirezamoshtaghi 
Date: Tue, 5 Mar 2024 00:04:52 -0800
Subject: [PATCH 3/3] dont test designator on Windows

---
 clang-tools-extra/clangd/test/designator_init.test | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang-tools-extra/clangd/test/designator_init.test 
b/clang-tools-extra/clangd/test/designator_init.test
index 739f2bfab54bcf..4009945ed81fd4 100644
--- a/clang-tools-extra/clangd/test/designator_init.test
+++ b/clang-tools-extra/clangd/test/designator_init.test
@@ -1,7 +1,7 @@
 //# RUN: rm -rf %t.dir/* && mkdir -p %t.dir
 //# RUN: echo '[{"directory": "%/t.dir", "command": "clang -x c -c %s", 
"file": "%s"}]' > %t.dir/compile_commands.json
 //# RUN: clangd --compile-commands-dir=%t.dir --check=%s 2>&1 | FileCheck %s
-
+//# UNSUPPORTED: system-windows
 typedef struct S {
   unsigned char id;
   union {

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] [CLANGD] Do not crash on designator initialization of union (PR #83369)

2024-03-04 Thread via cfe-commits

https://github.com/alirezamoshtaghi updated 
https://github.com/llvm/llvm-project/pull/83369

>From 3d6afe011221ac239bb668b375ed3f6c356fc47d Mon Sep 17 00:00:00 2001
From: alirezamoshtaghi 
Date: Wed, 28 Feb 2024 13:55:11 -0800
Subject: [PATCH 1/2] [CLANGD] Do not crash on designator initialization of
 union

---
 .../clangd/test/designator_init.test  | 31 +++
 clang/lib/AST/Expr.cpp| 14 +++--
 2 files changed, 43 insertions(+), 2 deletions(-)
 create mode 100644 clang-tools-extra/clangd/test/designator_init.test

diff --git a/clang-tools-extra/clangd/test/designator_init.test 
b/clang-tools-extra/clangd/test/designator_init.test
new file mode 100644
index 00..739f2bfab54bcf
--- /dev/null
+++ b/clang-tools-extra/clangd/test/designator_init.test
@@ -0,0 +1,31 @@
+//# RUN: rm -rf %t.dir/* && mkdir -p %t.dir
+//# RUN: echo '[{"directory": "%/t.dir", "command": "clang -x c -c %s", 
"file": "%s"}]' > %t.dir/compile_commands.json
+//# RUN: clangd --compile-commands-dir=%t.dir --check=%s 2>&1 | FileCheck %s
+
+typedef struct S {
+  unsigned char id;
+  union {
+unsigned int mask;
+struct {
+  unsigned int unused:10;
+  unsigned int reserved:3;
+  unsigned int rest:19;
+};
+  };
+} __attribute__((packed)) S_t;
+
+typedef struct H {
+  unsigned char hid;
+  unsigned int val;
+} handler_t;
+
+struct S
+get_foo (handler_t *h)
+{
+  S_t retval =
+{.id=h->hid,
+ .mask=h->val};
+  return retval;
+}
+
+// CHECK: All checks completed, 0 errors
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index b4de2155adcebd..33eeeda89fe7a5 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -4601,11 +4601,21 @@ SourceRange 
DesignatedInitExpr::getDesignatorsSourceRange() const {
 SourceLocation DesignatedInitExpr::getBeginLoc() const {
   auto *DIE = const_cast(this);
   Designator &First = *DIE->getDesignator(0);
-  if (First.isFieldDesignator())
-return GNUSyntax ? First.getFieldLoc() : First.getDotLoc();
+  if (First.isFieldDesignator()) {
+/* search all designators in case the first one is not
+   initialized */
+for (unsigned int i=0; isize(); i++) {
+  Designator &Des = *DIE->getDesignator(i);
+  SourceLocation retval = GNUSyntax ? Des.getFieldLoc() : Des.getDotLoc();
+  if (!retval.isValid ())
+   continue;
+  return retval;
+}
+  }
   return First.getLBracketLoc();
 }
 
+
 SourceLocation DesignatedInitExpr::getEndLoc() const {
   return getInit()->getEndLoc();
 }

>From 397f6cd893a6a07426d39f1dabd7ad27282435af Mon Sep 17 00:00:00 2001
From: alirezamoshtaghi 
Date: Mon, 4 Mar 2024 11:54:56 -0800
Subject: [PATCH 2/2] fix formatting errors

---
 clang/lib/AST/Expr.cpp | 9 +++--
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index 33eeeda89fe7a5..2e7170ecac0618 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -4602,20 +4602,17 @@ SourceLocation DesignatedInitExpr::getBeginLoc() const {
   auto *DIE = const_cast(this);
   Designator &First = *DIE->getDesignator(0);
   if (First.isFieldDesignator()) {
-/* search all designators in case the first one is not
-   initialized */
-for (unsigned int i=0; isize(); i++) {
+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;
+  if (!retval.isValid())
+continue;
   return retval;
 }
   }
   return First.getLBracketLoc();
 }
 
-
 SourceLocation DesignatedInitExpr::getEndLoc() const {
   return getInit()->getEndLoc();
 }

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] [CLANGD] Do not crash on designator initialization of union (PR #83369)

2024-03-04 Thread via cfe-commits

https://github.com/alirezamoshtaghi updated 
https://github.com/llvm/llvm-project/pull/83369

>From 3d6afe011221ac239bb668b375ed3f6c356fc47d Mon Sep 17 00:00:00 2001
From: alirezamoshtaghi 
Date: Wed, 28 Feb 2024 13:55:11 -0800
Subject: [PATCH] [CLANGD] Do not crash on designator initialization of union

---
 .../clangd/test/designator_init.test  | 31 +++
 clang/lib/AST/Expr.cpp| 14 +++--
 2 files changed, 43 insertions(+), 2 deletions(-)
 create mode 100644 clang-tools-extra/clangd/test/designator_init.test

diff --git a/clang-tools-extra/clangd/test/designator_init.test 
b/clang-tools-extra/clangd/test/designator_init.test
new file mode 100644
index 00..739f2bfab54bcf
--- /dev/null
+++ b/clang-tools-extra/clangd/test/designator_init.test
@@ -0,0 +1,31 @@
+//# RUN: rm -rf %t.dir/* && mkdir -p %t.dir
+//# RUN: echo '[{"directory": "%/t.dir", "command": "clang -x c -c %s", 
"file": "%s"}]' > %t.dir/compile_commands.json
+//# RUN: clangd --compile-commands-dir=%t.dir --check=%s 2>&1 | FileCheck %s
+
+typedef struct S {
+  unsigned char id;
+  union {
+unsigned int mask;
+struct {
+  unsigned int unused:10;
+  unsigned int reserved:3;
+  unsigned int rest:19;
+};
+  };
+} __attribute__((packed)) S_t;
+
+typedef struct H {
+  unsigned char hid;
+  unsigned int val;
+} handler_t;
+
+struct S
+get_foo (handler_t *h)
+{
+  S_t retval =
+{.id=h->hid,
+ .mask=h->val};
+  return retval;
+}
+
+// CHECK: All checks completed, 0 errors
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index b4de2155adcebd..33eeeda89fe7a5 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -4601,11 +4601,21 @@ SourceRange 
DesignatedInitExpr::getDesignatorsSourceRange() const {
 SourceLocation DesignatedInitExpr::getBeginLoc() const {
   auto *DIE = const_cast(this);
   Designator &First = *DIE->getDesignator(0);
-  if (First.isFieldDesignator())
-return GNUSyntax ? First.getFieldLoc() : First.getDotLoc();
+  if (First.isFieldDesignator()) {
+/* search all designators in case the first one is not
+   initialized */
+for (unsigned int i=0; isize(); i++) {
+  Designator &Des = *DIE->getDesignator(i);
+  SourceLocation retval = GNUSyntax ? Des.getFieldLoc() : Des.getDotLoc();
+  if (!retval.isValid ())
+   continue;
+  return retval;
+}
+  }
   return First.getLBracketLoc();
 }
 
+
 SourceLocation DesignatedInitExpr::getEndLoc() const {
   return getInit()->getEndLoc();
 }

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] [CLANGD] Do not crash on designator initialization of union (PR #83369)

2024-02-28 Thread via cfe-commits

github-actions[bot] wrote:




:warning: C/C++ code formatter, clang-format found issues in your code. 
:warning:



You can test this locally with the following command:


``bash
git-clang-format --diff 267beb10f2812107734a1cd2172b46e928af76b7 
3d6afe011221ac239bb668b375ed3f6c356fc47d -- clang/lib/AST/Expr.cpp
``





View the diff from clang-format here.


``diff
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index 33eeeda89f..4104fad181 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -4604,18 +4604,17 @@ SourceLocation DesignatedInitExpr::getBeginLoc() const {
   if (First.isFieldDesignator()) {
 /* search all designators in case the first one is not
initialized */
-for (unsigned int i=0; isize(); i++) {
+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;
+  if (!retval.isValid())
+continue;
   return retval;
 }
   }
   return First.getLBracketLoc();
 }
 
-
 SourceLocation DesignatedInitExpr::getEndLoc() const {
   return getInit()->getEndLoc();
 }

``




https://github.com/llvm/llvm-project/pull/83369
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] [CLANGD] Do not crash on designator initialization of union (PR #83369)

2024-02-28 Thread via cfe-commits

llvmbot wrote:



@llvm/pr-subscribers-clang-tools-extra
@llvm/pr-subscribers-clangd

@llvm/pr-subscribers-clang

Author: None (alirezamoshtaghi)


Changes



---
Full diff: https://github.com/llvm/llvm-project/pull/83369.diff


2 Files Affected:

- (added) clang-tools-extra/clangd/test/designator_init.test (+31) 
- (modified) clang/lib/AST/Expr.cpp (+12-2) 


``diff
diff --git a/clang-tools-extra/clangd/test/designator_init.test 
b/clang-tools-extra/clangd/test/designator_init.test
new file mode 100644
index 00..739f2bfab54bcf
--- /dev/null
+++ b/clang-tools-extra/clangd/test/designator_init.test
@@ -0,0 +1,31 @@
+//# RUN: rm -rf %t.dir/* && mkdir -p %t.dir
+//# RUN: echo '[{"directory": "%/t.dir", "command": "clang -x c -c %s", 
"file": "%s"}]' > %t.dir/compile_commands.json
+//# RUN: clangd --compile-commands-dir=%t.dir --check=%s 2>&1 | FileCheck %s
+
+typedef struct S {
+  unsigned char id;
+  union {
+unsigned int mask;
+struct {
+  unsigned int unused:10;
+  unsigned int reserved:3;
+  unsigned int rest:19;
+};
+  };
+} __attribute__((packed)) S_t;
+
+typedef struct H {
+  unsigned char hid;
+  unsigned int val;
+} handler_t;
+
+struct S
+get_foo (handler_t *h)
+{
+  S_t retval =
+{.id=h->hid,
+ .mask=h->val};
+  return retval;
+}
+
+// CHECK: All checks completed, 0 errors
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index b4de2155adcebd..33eeeda89fe7a5 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -4601,11 +4601,21 @@ SourceRange 
DesignatedInitExpr::getDesignatorsSourceRange() const {
 SourceLocation DesignatedInitExpr::getBeginLoc() const {
   auto *DIE = const_cast(this);
   Designator &First = *DIE->getDesignator(0);
-  if (First.isFieldDesignator())
-return GNUSyntax ? First.getFieldLoc() : First.getDotLoc();
+  if (First.isFieldDesignator()) {
+/* search all designators in case the first one is not
+   initialized */
+for (unsigned int i=0; isize(); i++) {
+  Designator &Des = *DIE->getDesignator(i);
+  SourceLocation retval = GNUSyntax ? Des.getFieldLoc() : Des.getDotLoc();
+  if (!retval.isValid ())
+   continue;
+  return retval;
+}
+  }
   return First.getLBracketLoc();
 }
 
+
 SourceLocation DesignatedInitExpr::getEndLoc() const {
   return getInit()->getEndLoc();
 }

``




https://github.com/llvm/llvm-project/pull/83369
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] [CLANGD] Do not crash on designator initialization of union (PR #83369)

2024-02-28 Thread via cfe-commits

github-actions[bot] wrote:



Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this 
page.

If this is not working for you, it is probably because you do not have write
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using `@` followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from 
other developers.

If you have further questions, they may be answered by the [LLVM GitHub User 
Guide](https://llvm.org/docs/GitHub.html).

You can also ask questions in a comment on this PR, on the [LLVM 
Discord](https://discord.com/invite/xS7Z362) or on the 
[forums](https://discourse.llvm.org/).

https://github.com/llvm/llvm-project/pull/83369
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] [CLANGD] Do not crash on designator initialization of union (PR #83369)

2024-02-28 Thread via cfe-commits

https://github.com/alirezamoshtaghi created 
https://github.com/llvm/llvm-project/pull/83369

None

>From 3d6afe011221ac239bb668b375ed3f6c356fc47d Mon Sep 17 00:00:00 2001
From: alirezamoshtaghi 
Date: Wed, 28 Feb 2024 13:55:11 -0800
Subject: [PATCH] [CLANGD] Do not crash on designator initialization of union

---
 .../clangd/test/designator_init.test  | 31 +++
 clang/lib/AST/Expr.cpp| 14 +++--
 2 files changed, 43 insertions(+), 2 deletions(-)
 create mode 100644 clang-tools-extra/clangd/test/designator_init.test

diff --git a/clang-tools-extra/clangd/test/designator_init.test 
b/clang-tools-extra/clangd/test/designator_init.test
new file mode 100644
index 00..739f2bfab54bcf
--- /dev/null
+++ b/clang-tools-extra/clangd/test/designator_init.test
@@ -0,0 +1,31 @@
+//# RUN: rm -rf %t.dir/* && mkdir -p %t.dir
+//# RUN: echo '[{"directory": "%/t.dir", "command": "clang -x c -c %s", 
"file": "%s"}]' > %t.dir/compile_commands.json
+//# RUN: clangd --compile-commands-dir=%t.dir --check=%s 2>&1 | FileCheck %s
+
+typedef struct S {
+  unsigned char id;
+  union {
+unsigned int mask;
+struct {
+  unsigned int unused:10;
+  unsigned int reserved:3;
+  unsigned int rest:19;
+};
+  };
+} __attribute__((packed)) S_t;
+
+typedef struct H {
+  unsigned char hid;
+  unsigned int val;
+} handler_t;
+
+struct S
+get_foo (handler_t *h)
+{
+  S_t retval =
+{.id=h->hid,
+ .mask=h->val};
+  return retval;
+}
+
+// CHECK: All checks completed, 0 errors
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index b4de2155adcebd..33eeeda89fe7a5 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -4601,11 +4601,21 @@ SourceRange 
DesignatedInitExpr::getDesignatorsSourceRange() const {
 SourceLocation DesignatedInitExpr::getBeginLoc() const {
   auto *DIE = const_cast(this);
   Designator &First = *DIE->getDesignator(0);
-  if (First.isFieldDesignator())
-return GNUSyntax ? First.getFieldLoc() : First.getDotLoc();
+  if (First.isFieldDesignator()) {
+/* search all designators in case the first one is not
+   initialized */
+for (unsigned int i=0; isize(); i++) {
+  Designator &Des = *DIE->getDesignator(i);
+  SourceLocation retval = GNUSyntax ? Des.getFieldLoc() : Des.getDotLoc();
+  if (!retval.isValid ())
+   continue;
+  return retval;
+}
+  }
   return First.getLBracketLoc();
 }
 
+
 SourceLocation DesignatedInitExpr::getEndLoc() const {
   return getInit()->getEndLoc();
 }

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits