-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[Clang] adjust caret placement for the suggested attribute location for enum class #168092
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
68dc4b9
5152768
9f93393
a495ee9
7bc4552
303b91b
782923b
253af19
f0f5dfd
1f565db
a394503
444bfc5
55ab1a4
3b4f729
adefaa1
62c9940
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1100,30 +1100,31 @@ Parser::DeclGroupPtrTy Parser::ParseDeclOrFunctionDefInternal( | |
| // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" | ||
| // declaration-specifiers init-declarator-list[opt] ';' | ||
| if (Tok.is(tok::semi)) { | ||
| auto LengthOfTSTToken = [](DeclSpec::TST TKind) { | ||
| assert(DeclSpec::isDeclRep(TKind)); | ||
| switch(TKind) { | ||
| case DeclSpec::TST_class: | ||
| return 5; | ||
| case DeclSpec::TST_struct: | ||
| return 6; | ||
| case DeclSpec::TST_union: | ||
| return 5; | ||
| case DeclSpec::TST_enum: | ||
| return 4; | ||
| case DeclSpec::TST_interface: | ||
| return 9; | ||
| default: | ||
| llvm_unreachable("we only expect to get the length of the class/struct/union/enum"); | ||
| auto GetAdjustedAttrsLoc = [&]() { | ||
| auto TKind = DS.getTypeSpecType(); | ||
| if (!DeclSpec::isDeclRep(TKind)) | ||
| return SourceLocation(); | ||
|
|
||
| if (TKind == DeclSpec::TST_enum) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO, the 'get location after the TST loc' logic works for all of these, right? With the exception being this is an enum class/enum struct? Which, incidentially, doesn't actually work for enum class/enum struct? We potentially have OTHER tokens between the 'enum' and the 'class/struct' keywords (even if ill-formed potentially?), so in reality, if this is a scoped enum, we actually have to track down the class/struct token instead. I find myself wondering if we should just have Alternatively, and probably better than all of this: I find myself wondering if we SHOULD be smarter about the OTHER cases as well, rather than a sizeof-keyword-offset funny business, since that also doesn't necessarily work if there are other tokens in the way.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
@erichkeane Thanks for the feedback. I had considered using the
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I THINK it is appropriate (identifier, lbrace, or semicolon)? I'm curious what @AaronBallman has to say. Regarding the rest of the DeclSpec kinds, we could probably leave those alone + a fixme to 'get good' here.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @AaronBallman, whenever you have time, could you please review these changes?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My reading of the grammar is that the attribute can go after the key (so either Because "before" means more logic; there could be an optional nested name specifier. e.g., https://godbolt.org/z/eMo5T8r53 or there could be no name but an enum base, e.g., https://godbolt.org/z/jbqjnrn3d and so on. So I think what makes the most sense is for WDYT? |
||
| const auto *ED = dyn_cast_or_null<EnumDecl>(DS.getRepAsDecl()); | ||
| if (ED) { | ||
a-tarasyuk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (ED->isScoped() && ED->getIdentifier()) | ||
a-tarasyuk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return ED->getLocation(); | ||
|
|
||
| const auto Begin = ED->getBraceRange().getBegin(); | ||
| if (Begin.isValid()) | ||
| return Begin; | ||
a-tarasyuk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| const auto &Policy = Actions.getASTContext().getPrintingPolicy(); | ||
| unsigned Offset = | ||
| StringRef(DeclSpec::getSpecifierName(TKind, Policy)).size(); | ||
| return DS.getTypeSpecTypeLoc().getLocWithOffset(Offset); | ||
| }; | ||
|
|
||
| // Suggest correct location to fix '[[attrib]] struct' to 'struct [[attrib]]' | ||
| SourceLocation CorrectLocationForAttributes = | ||
| DeclSpec::isDeclRep(DS.getTypeSpecType()) | ||
| ? DS.getTypeSpecTypeLoc().getLocWithOffset( | ||
| LengthOfTSTToken(DS.getTypeSpecType())) | ||
| : SourceLocation(); | ||
| SourceLocation CorrectLocationForAttributes = GetAdjustedAttrsLoc(); | ||
| ProhibitAttributes(Attrs, CorrectLocationForAttributes); | ||
| ConsumeToken(); | ||
| RecordDecl *AnonRecord = nullptr; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| // RUN: %clang_cc1 -fsyntax-only -verify %s | ||
| // RUN: not %clang_cc1 -fsyntax-only -fdiagnostics-parseable-fixits -fno-diagnostics-show-line-numbers %s 2>&1 | FileCheck %s -strict-whitespace | ||
|
|
||
| [[nodiscard]] enum class E1 { }; | ||
| // expected-error@-1 {{misplaced attributes; expected attributes here}} | ||
| // CHECK: {{^}}{{\[\[}}nodiscard]] enum class E1 { }; | ||
| // CHECK: {{^}}~~~~~~~~~~~~~ ^ | ||
| // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:1-[[@LINE-4]]:15}:"" | ||
| // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:26-[[@LINE-5]]:26}:"{{\[\[}}nodiscard]]" | ||
|
|
||
| [[nodiscard]] enum struct E2 { }; | ||
| // expected-error@-1 {{misplaced attributes; expected attributes here}} | ||
| // CHECK: {{^}}{{\[\[}}nodiscard]] enum struct E2 { }; | ||
| // CHECK: {{^}}~~~~~~~~~~~~~ ^ | ||
| // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:1-[[@LINE-4]]:15}:"" | ||
| // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:27-[[@LINE-5]]:27}:"{{\[\[}}nodiscard]]" | ||
|
|
||
| [[nodiscard]] enum class E3 { }; | ||
| // expected-error@-1 {{misplaced attributes; expected attributes here}} | ||
| // CHECK: {{^}}{{\[\[}}nodiscard]] enum class E3 { }; | ||
| // CHECK: {{^}}~~~~~~~~~~~~~ ^ | ||
| // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:1-[[@LINE-4]]:15}:"" | ||
| // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:35-[[@LINE-5]]:35}:"{{\[\[}}nodiscard]]" | ||
|
|
||
| [[nodiscard]] enum /*comment*/ class E4 { }; | ||
| // expected-error@-1 {{misplaced attributes; expected attributes here}} | ||
| // CHECK: {{^}}{{\[\[}}nodiscard]] enum /*comment*/ class E4 { }; | ||
| // CHECK: {{^}}~~~~~~~~~~~~~ ^ | ||
| // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:1-[[@LINE-4]]:15}:"" | ||
| // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:39-[[@LINE-5]]:39}:"{{\[\[}}nodiscard]]" | ||
|
|
||
| [[nodiscard]] enum { A = 0 }; | ||
a-tarasyuk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // expected-error@-1 {{misplaced attributes; expected attributes here}} | ||
| // CHECK: {{^}}{{\[\[}}nodiscard]] enum { A = 0 }; | ||
| // CHECK: {{^}}~~~~~~~~~~~~~ ^ | ||
| // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:1-[[@LINE-4]]:15}:"" | ||
| // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:20-[[@LINE-5]]:20}:"{{\[\[}}nodiscard]]" | ||
Uh oh!
There was an error while loading. Please reload this page.