[clang] [Clang] Remove `IDNS_Ordinary` flag in `IndirectField::IdentifierNamespace` (PR #100525)

Yanzuo Liu via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 25 00:12:22 PDT 2024


https://github.com/zwuis created https://github.com/llvm/llvm-project/pull/100525

There is a `IDNS_Ordinary` flag in `IndirectField::IdentifierNamespace` so that members in nested anonymous struct/union can be found as ordinary identifiers.

```c
struct S {
  struct { int x; };
  // Previous behaviour: `x` in previous line is found
  // Expected: nothing is found
  int arr[sizeof(x)];
};
```

This PR fixes this issue.

Fixes #31295.

>From 217d3978f48ea074420fecd432b0b65b7e1c5b58 Mon Sep 17 00:00:00 2001
From: Yanzuo Liu <zwuis at outlook.com>
Date: Thu, 25 Jul 2024 14:50:21 +0800
Subject: [PATCH] Remove `IDNS_Ordinary` flag in
 `IndirectField::IdentifierNamespace`

---
 clang/docs/ReleaseNotes.rst                     | 3 +++
 clang/lib/AST/DeclBase.cpp                      | 3 +--
 clang/test/Parser/namelookup-anonymous-struct.c | 6 ++++++
 3 files changed, 10 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/Parser/namelookup-anonymous-struct.c

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 83fd5dc31547e..24be9e26a9adf 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -137,6 +137,9 @@ Miscellaneous Bug Fixes
 Miscellaneous Clang Crashes Fixed
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
+- Fixed a crash in C due to incorrect lookup that members in nested anonymous struct/union
+  can be found as ordinary identifiers in struct/union definition. (#GH31295)
+
 OpenACC Specific Changes
 ------------------------
 
diff --git a/clang/lib/AST/DeclBase.cpp b/clang/lib/AST/DeclBase.cpp
index bc5a9206c0db2..a1f70546bde42 100644
--- a/clang/lib/AST/DeclBase.cpp
+++ b/clang/lib/AST/DeclBase.cpp
@@ -879,8 +879,6 @@ unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
       return IDNS_Ordinary;
     case Label:
       return IDNS_Label;
-    case IndirectField:
-      return IDNS_Ordinary | IDNS_Member;
 
     case Binding:
     case NonTypeTemplateParm:
@@ -918,6 +916,7 @@ unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
       return IDNS_ObjCProtocol;
 
     case Field:
+    case IndirectField:
     case ObjCAtDefsField:
     case ObjCIvar:
       return IDNS_Member;
diff --git a/clang/test/Parser/namelookup-anonymous-struct.c b/clang/test/Parser/namelookup-anonymous-struct.c
new file mode 100644
index 0000000000000..cb691c22f97ff
--- /dev/null
+++ b/clang/test/Parser/namelookup-anonymous-struct.c
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1 -std=c11 -verify %s
+
+struct GH31295 {
+  struct { int x; };
+  int arr[sizeof(x)]; // expected-error{{use of undeclared identifier 'x'}}
+};



More information about the cfe-commits mailing list