This is an archive of the discontinued LLVM Phabricator instance.

[Sema] Fix parsing of anonymous union in language linkage specification
ClosedPublic

Authored by jkorous on Apr 20 2018, 8:07 AM.

Details

Summary

C++17 [dcl.link]p4:
A linkage specification does not establish a scope.

C++17 [class.union.anon]p2:
Namespace level anonymous unions shall be declared static.

The test produces a crash on master.

Diff Detail

Repository
rC Clang

Event Timeline

jkorous created this revision.Apr 20 2018, 8:07 AM

For the record, DR154: Anonymous unions in unnamed namespaces is relevant here.

Sema/SemaDecl.cpp
4654–4656 ↗(On Diff #143317)

Looks like DeclContext::isTransparentContext() might be relevant here. At least I was able to get the assertion failure

Assertion failed: (II && "Attempt to mangle unnamed decl."), function getMangledNameImpl, file llvm-project/clang/lib/CodeGen/CodeGenModule.cpp, line 913.

for

// clang -std=c++17 -fmodules-ts test-modules.cpp

export module M;
export {
    union {
        int int_val;
        float float_val;
    };
}

Also based on isTransparentContext() usage, inline namespaces can cause problems. Currently, there are no problems, we have the error

error: anonymous unions at namespace or global scope must be declared 'static'

and there are no negative consequences (as far as I can tell). According to my bad standard knowledge that should be OK (haven't found non-static anonymous unions to be allowed in this case).

4659 ↗(On Diff #143317)

Checked if we need to do the same change s/Owner/OwnerScope/ elsewhere in this method and looks like it is not required. We care if the owner is a Record and we don't allow linkage specification in classes, so skipping linkage scopes doesn't give us anything.

rsmith added inline comments.Apr 23 2018, 5:31 PM
Sema/SemaDecl.cpp
4654–4656 ↗(On Diff #143317)

Right, the correct way to spell this is OwnerScope = Owner->getRedeclContext();.

4659–4661 ↗(On Diff #143317)

While you're here, OwnerScope->isFileContext() && !OwnerScope->isInlineNamespace() might be clearer. Or at least replace the getDeclName() with !isInlineNamespace().

jkorous marked 3 inline comments as done.Apr 24 2018, 6:53 AM
jkorous updated this revision to Diff 143736.Apr 24 2018, 6:59 AM

Addressed comments + proposal for refactoring of the namespace-related logic.

vsapsai accepted this revision.Apr 26 2018, 10:59 AM
vsapsai added inline comments.
Sema/SemaDecl.cpp
4651–4653 ↗(On Diff #143736)

I think the code style favours avoiding excessive vertical whitespace and I don't feel like this statement is semantically far enough from the surrounding code to be separated from it. I haven't found a specific LLVM Coding Standards rule, so in this case it is my personal opinion and actual formatting decision is up to you.

Other than that I have no other comments.

This revision is now accepted and ready to land.Apr 26 2018, 10:59 AM
jkorous marked an inline comment as done.May 1 2018, 8:08 AM

Volodymyr, could you please confirm that the non-anonymous vs non-inline logic makes sense to you?

Sema/SemaDecl.cpp
4659–4661 ↗(On Diff #143317)

I'd like to explicitly flag this - we are interested in non-anonymous namespaces not non-inline namespaces, right?

jkorous added inline comments.May 1 2018, 8:11 AM
Sema/SemaDecl.cpp
4651–4653 ↗(On Diff #143736)

Strange, looks like phab lost my comment which basically was: "No problem, I see what you mean. Will remove those blank lines."

jkorous marked an inline comment as done.May 1 2018, 8:12 AM

Volodymyr, could you please confirm that the non-anonymous vs non-inline logic makes sense to you?

Looks correct to me that we are checking non-anonymous namespaces. Inline namespaces are somewhat similar to anonymous namespaces but my testing shows they are working correctly and don't require any change. By "working correctly" I mean

error: anonymous unions at namespace or global scope must be declared 'static'
rsmith accepted this revision.May 1 2018, 5:19 PM
rsmith added inline comments.
Sema/SemaDecl.cpp
4659–4661 ↗(On Diff #143317)

Sorry, yes, thinko on my part.

This revision was automatically updated to reflect the committed changes.