Skip to content

[Clang] Remove IDNS_Ordinary flag in IndirectFieldDecl::IdentifierNamespace #100525

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 1 commit into from
Jul 26, 2024

Conversation

zwuis
Copy link
Contributor

@zwuis zwuis commented Jul 25, 2024

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

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.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jul 25, 2024
@llvmbot
Copy link
Member

llvmbot commented Jul 25, 2024

@llvm/pr-subscribers-clang

Author: Yanzuo Liu (zwuis)

Changes

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

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.


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

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+3)
  • (modified) clang/lib/AST/DeclBase.cpp (+1-2)
  • (added) clang/test/Parser/namelookup-anonymous-struct.c (+6)
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'}}
+};

@zwuis zwuis changed the title [Clang] Remove IDNS_Ordinary flag in IndirectField::IdentifierNamespace [Clang] Remove IDNS_Ordinary flag in IndirectFieldDecl::IdentifierNamespace Jul 25, 2024
@shafik shafik requested review from AaronBallman, Sirraide and cor3ntin and removed request for AaronBallman, Sirraide and cor3ntin July 25, 2024 21:14
Copy link
Member

@Sirraide Sirraide left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks reasonable, but I’d prefer it if someone more familiar w/ the C standard than me could take another look at this.
CC @AaronBallman

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The C standard does not make this particularly clear, but the changes are correct.

Per C23 6.2.1p4 Each member of the structure has file scope (because why not) but per 6.2.3p1, each member is in a distinct name space and the member is disambiguated by the type of the expression used to access the member via the . or -> operator. That last bit is why the member cannot be an ordinary identifier within the structure itself.

So LGTM, thank you!

@zwuis
Copy link
Contributor Author

zwuis commented Jul 26, 2024

Thank you for your review!

I don't have commit access. Please help me merge this PR if it's ready.

@AaronBallman AaronBallman merged commit 9d22095 into llvm:main Jul 26, 2024
11 checks passed
@cor3ntin
Copy link
Contributor

@AaronBallman @Sirraide Do we have tests for C++? https://godbolt.org/z/bdvrdKso1

@Sirraide
Copy link
Member

@AaronBallman @Sirraide Do we have tests for C++? https://godbolt.org/z/bdvrdKso1

I don’t believe the standard supports anonymous structs in C++, but it seems we’re handling them the same way as GCC, i.e. the code in the godbolt link is accepted in C++ mode by both us and GCC.

That said, I don’t know whether we have tests for this off the top of my head, but I should hope so considering that we support the feature...

@Sirraide
Copy link
Member

Well, looks like I spoke too soon, because I can’t find any C++ tests for this particular case

@zwuis
Copy link
Contributor Author

zwuis commented Jul 26, 2024

I don’t believe the standard supports anonymous structs in C++

Do we need tests about anonymous union? C++ standard supports it.

@AaronBallman
Copy link
Collaborator

I don’t believe the standard supports anonymous structs in C++

Do we need tests about anonymous union? C++ standard supports it.

Yes, we should add tests for C++ as well; good call @cor3ntin and @Sirraide!

@zwuis zwuis deleted the fix#31295 branch January 28, 2025 17:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Invalid cast to CXXRecordDecl in Sema::BuildPossibleImplicitMemberExpr
5 participants