Skip to content
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

[Clang] prevent setting default lexical access specifier for missing primary declarations #112424

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a22c6ba
[Clang] prevent setting default lexical access specifier for missing …
a-tarasyuk Oct 15, 2024
83ce02f
prevent assertion failure by handling invalid enum forward declarations
a-tarasyuk Oct 18, 2024
47633cc
Merge branch 'main' into fix/112208
a-tarasyuk Oct 18, 2024
1c95b7f
Merge branch 'main' into fix/112208
a-tarasyuk Oct 18, 2024
ca046e4
Merge branch 'main' into fix/112208
a-tarasyuk Oct 18, 2024
a17e84b
Merge branch 'main' into fix/112208
a-tarasyuk Oct 18, 2024
59e54ca
Merge branch 'main' of https://github.com/llvm/llvm-project into fix/…
a-tarasyuk Oct 18, 2024
d3aea99
Merge branch 'main' into fix/112208
a-tarasyuk Oct 19, 2024
f1e0357
Merge branch 'main' into fix/112208
a-tarasyuk Oct 19, 2024
e473224
Merge branch 'main' into fix/112208
a-tarasyuk Oct 19, 2024
047dada
Merge branch 'main' into fix/112208
a-tarasyuk Oct 19, 2024
f3b7b64
Merge branch 'main' into fix/112208
a-tarasyuk Oct 19, 2024
3a3439d
Merge branch 'main' into fix/112208
a-tarasyuk Oct 20, 2024
b857a8a
Merge branch 'main' into fix/112208
a-tarasyuk Oct 20, 2024
d879fd1
Merge branch 'main' into fix/112208
a-tarasyuk Oct 22, 2024
d321cd0
Merge branch 'main' into fix/112208
a-tarasyuk Oct 24, 2024
a78331f
Merge branch 'main' into fix/112208
a-tarasyuk Oct 24, 2024
f490f77
Merge branch 'main' into fix/112208
a-tarasyuk Oct 24, 2024
ef27720
Merge branch 'main' into fix/112208
a-tarasyuk Oct 25, 2024
8d50c3f
Merge branch 'main' into fix/112208
a-tarasyuk Oct 29, 2024
82e3dda
remove redundant Name check
a-tarasyuk Oct 30, 2024
1d1d39c
Merge branch 'fix/112208' of https://github.com/a-tarasyuk/llvm-proje…
a-tarasyuk Oct 30, 2024
05e13d9
Merge branch 'main' of https://github.com/llvm/llvm-project into fix/…
a-tarasyuk Oct 30, 2024
5ee7443
Merge branch 'main' into fix/112208
a-tarasyuk Oct 30, 2024
66573ba
Merge branch 'main' into fix/112208
a-tarasyuk Oct 30, 2024
dc3da53
Merge branch 'main' into fix/112208
a-tarasyuk Oct 30, 2024
fe033be
Merge branch 'main' into fix/112208
a-tarasyuk Oct 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,8 @@ Bug Fixes to C++ Support
certain situations. (#GH47400), (#GH90896)
- Fix erroneous templated array size calculation leading to crashes in generated code. (#GH41441)
- During the lookup for a base class name, non-type names are ignored. (#GH16855)
- Fixed an assertion failure when the default lexical access specifier was set for missing
primary declarations. (#GH112208)

Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Sema/SemaAccess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ bool Sema::SetMemberAccessSpecifier(NamedDecl *MemberDecl,
AccessSpecifier LexicalAS) {
if (!PrevMemberDecl) {
// Use the lexical access specifier.
MemberDecl->setAccess(LexicalAS);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@shafik #112208 (comment) when PrevMemberDecl exists but isn't equal to LexicalAS — with AS_none excluded

if (LexicalAS != AS_none && LexicalAS != PrevMemberDecl->getAccess()) {

I suppose AS_none should be excluded when PrevMemberDecl is unknown. What do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I feel like we should be catching this in Sema::ActOnTag here:

if ((Name || Kind == TagTypeKind::Enum) &&
getNonFieldDeclScope(S)->isFunctionPrototypeScope()) {
if (getLangOpts().CPlusPlus) {
// C++ [dcl.fct]p6:
// Types shall not be defined in return or parameter types.
if (TUK == TagUseKind::Definition && !IsTypeSpecifier) {
Diag(Loc, diag::err_type_defined_in_param_type)
<< Name;
Invalid = true;
}
} else if (!PrevDecl) {
Diag(Loc, diag::warn_decl_in_param_list) << Context.getTagDeclType(New);
}
}

It looks like we are not b/c TUK is TUK_Declataion but we should not be forward declaring here either.

The current diagnostic does not fit this case though and maybe it has to be tweaked some more.

CC @AaronBallman

Copy link
Collaborator

Choose a reason for hiding this comment

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

Please also add more details to the summary, it is helpful to have some idea what you think the underlying problem is and why this fix is correct before reviewing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The current diagnostic does not fit this case though and maybe it has to be tweaked some more.

@shafik Thanks for the feedback! I initially considered marking the new declaration invalid based on TUK_Declaration, but I wasn’t entirely sure because of its relation to the new declaration

if (Invalid)
New->setInvalidDecl();

I’ve made changes to handle this case based on TUK_Declaration. If we need to add a diagnostic message (something like a general message saying "enum" cannot appear here), just let me know and I’ll add it...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@shafik could you take a look at the latest changes? thanks

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think these changes make sense, the diagnostic on forward reference is not great but that feels like a seperate already existing problem and not directly related to the crash.

Please update the summary. I also had one question on why we are checking Name as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@shafik thanks for the feedback. I've checked, and it seems safe to omit Name here, so I’ve removed it.

if (LexicalAS != AS_none)
MemberDecl->setAccess(LexicalAS);
return false;
}

Expand Down
8 changes: 8 additions & 0 deletions clang/test/SemaCXX/enum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,11 @@ struct PR28903 {
})
};
};

namespace GH112208 {
class C {
enum E { e = 0 };
void f(int, enum E;); // expected-error {{ISO C++ forbids forward references to 'enum' types}} \
// expected-error {{unexpected ';' before ')'}}
};
}
Loading