Skip to content

[Sema] Don't drop weak_import from a declaration that follows a declaration directly contained in a linkage-specification #85886

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 5 commits into from
Jul 17, 2024

Conversation

ahatanak
Copy link
Collaborator

Only drop it if the declaration follows a definition. I believe this is what 33e0226 was trying to do.

rdar://61865848

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

llvmbot commented Mar 20, 2024

@llvm/pr-subscribers-clang

Author: Akira Hatanaka (ahatanak)

Changes

Only drop it if the declaration follows a definition. I believe this is what 33e0226 was trying to do.

rdar://61865848


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

2 Files Affected:

  • (modified) clang/lib/Sema/SemaDecl.cpp (+1-2)
  • (modified) clang/test/SemaCXX/attr-weak.cpp (+7)
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 5850cd0ab6b9aa..2e45f1191273a4 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -4613,8 +4613,7 @@ void Sema::MergeVarDecl(VarDecl *New, LookupResult &Previous) {
   mergeDeclAttributes(New, Old);
   // Warn if an already-declared variable is made a weak_import in a subsequent
   // declaration
-  if (New->hasAttr<WeakImportAttr>() &&
-      Old->getStorageClass() == SC_None &&
+  if (New->hasAttr<WeakImportAttr>() && Old->isThisDeclarationADefinition() &&
       !Old->hasAttr<WeakImportAttr>()) {
     Diag(New->getLocation(), diag::warn_weak_import) << New->getDeclName();
     Diag(Old->getLocation(), diag::note_previous_declaration);
diff --git a/clang/test/SemaCXX/attr-weak.cpp b/clang/test/SemaCXX/attr-weak.cpp
index f065bfd9483f8a..a2c5fd4abd35f6 100644
--- a/clang/test/SemaCXX/attr-weak.cpp
+++ b/clang/test/SemaCXX/attr-weak.cpp
@@ -55,3 +55,10 @@ constexpr bool weak_method_is_non_null = &WithWeakMember::weak_method != nullptr
 // virtual member function is present.
 constexpr bool virtual_weak_method_is_non_null = &WithWeakMember::virtual_weak_method != nullptr; // expected-error {{must be initialized by a constant expression}}
 // expected-note@-1 {{comparison against pointer to weak member 'WithWeakMember::virtual_weak_method' can only be performed at runtime}}
+
+// Check that no warnings are emitted.
+extern "C" int g0;
+extern int g0 __attribute__((weak_import));
+
+extern "C" int g1 = 0; // expected-note {{previous definition is here}}
+extern int g1 __attribute__((weak_import)); // expected-warning {{attribute declaration must precede definition}}

@ahatanak ahatanak requested a review from rjmccall March 20, 2024 18:41
declaration directly contained in a linkage-specification

Only drop it if the declaration follows a definition. I believe this is
what 33e0226 was trying to do.

rdar://61865848
Copy link
Contributor

@rjmccall rjmccall left a comment

Choose a reason for hiding this comment

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

I think you're right about the intended logic being to check for a definition, especially given the wording of the warning. IIRC, we didn't have high-level functions for checking some of these conditions at the time, so looking directly at the storage-class specifier would have been common.

With that said, I think you need to check if a definition exists at all and not just whether the last declaration is that definition.

@ahatanak
Copy link
Collaborator Author

ahatanak commented May 9, 2024

With that said, I think you need to check if a definition exists at all and not just whether the last declaration is that definition.

Note that checkNewAttributesAfterDef, which is called right before the check, removes attributes on New if there was a full definition. The updated patch just looks for any previously declared tentative definition.

@ahatanak
Copy link
Collaborator Author

ahatanak commented May 9, 2024

The updated patch checks that the decl isn't DeclarationOnly to make it clear that we are looking for all definitions although we know we won't find any full definitions.

Copy link
Contributor

@rjmccall rjmccall left a comment

Choose a reason for hiding this comment

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

LGTM. The (existing) diagnostic wording does seem a little awkward, so if you want to put effort into cleaning that up, it'd be welcome, but I won't hold up this fix.

@ahatanak ahatanak merged commit 884772f into llvm:main Jul 17, 2024
4 of 7 checks passed
@ahatanak ahatanak deleted the weak-import-decl branch July 17, 2024 23:19
ahatanaka pushed a commit to swiftlang/llvm-project that referenced this pull request Jul 17, 2024
…n't seen (llvm#85886)

I believe this is what the original commit (33e0226) was trying to do.

This fixes a bug where clang removes the attribute from a declaration that follows a declaration directly contained in a linkage-specification.

rdar://61865848
(cherry picked from commit 884772f)

Conflicts:
	clang/test/Sema/attr-weak.c
ahatanaka pushed a commit to swiftlang/llvm-project that referenced this pull request Jul 18, 2024
…n't seen (llvm#85886)

I believe this is what the original commit (33e0226) was trying to do.

This fixes a bug where clang removes the attribute from a declaration that follows a declaration directly contained in a linkage-specification.

rdar://61865848
(cherry picked from commit 884772f)

Conflicts:
	clang/test/Sema/attr-weak.c
yuxuanchen1997 pushed a commit that referenced this pull request Jul 25, 2024
…n't seen (#85886)

I believe this is what the original commit (33e0226) was trying to do.

This fixes a bug where clang removes the attribute from a declaration that follows a declaration directly contained in a linkage-specification.

rdar://61865848
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.

3 participants