Implements paper P2324R2
https://wg21.link/p2324r2
https://github.com/cplusplus/papers/issues/1006
Details
- Reviewers
aaron.ballman cor3ntin - Group Reviewers
Restricted Project - Commits
- rG510383626fe1: [Clang] Support label at end of compound statement
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
I thought it was a good idea to slightly rename err_label_end_of_compound_statement to err_SWITCH_label_end_of_compound_statement because we aren't going to support empty labels in switch statements. (GCC also doesn't compile it - https://godbolt.org/z/xabKaon8v)
So now we need to clarify that we don't support empty labels in SWITCH compound statements (just there).
Thanks for doing that work.
Given that this paper was touted as a C compatibility feature, I think we should implement the C feature at the same time
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2508.pdf
If my understanding is correct the change implemented here should just be allowed - without warning in C23
What do you think @aaron.ballman ?
The proposal says:
A label at the end of a compound statement is treated as if it were followed by a null statement
Does it make sense to write an AST test to verify this?
I don't think the two papers diverge in any way, so I think the implementation for one should basically cover the work for the other (aside from test cases, etc). It should definitely be without warning in C23 mode and give an extension warning in earlier modes.
I think a test like that would be reasonable, yes.
It should definitely be without warning in C23 mode and give an extension warning in earlier modes.
@aaron.ballman we have this extension warning for pre-C++23:
def ext_label_end_of_compound_statement : ExtWarn< "label at end of compound statement is a C++2b extension">, InGroup<CXX2b>;
Should I add a new warning for pre-C23? We could have two warnings for each language:
ext_cxx_label_end_of_compound_statement ext_c_label_end_of_compound_statement
clang/lib/Parse/ParseStmt.cpp | ||
---|---|---|
738 | I do not understand this. Why checking getLangOpts().C99 instead of just C? Whyt no compatibility warning in C23 mode? |
Why checking getLangOpts().C99 instead of just C
There is no getLangOpts().C. Here are possible C/C++ opt flags:
https://github.com/llvm/llvm-project/blob/7914e53e312074828293356f569d190ac6eae3bd/clang/include/clang/Basic/LangOptions.def#L86-L100
I have no understanding why there is no getLangOpts().C flag. Maybe the C89 standard is a subset of all other C/C++/ObjC standards, so we don't need the flag?..
Whyt no compatibility warning in C23 mode
@aaron.ballman said so in https://reviews.llvm.org/D133887#3793027
It should definitely be without warning in C23 mode and give an extension warning in earlier modes.
I don't know much about extension/incompatible warnings policy (when to apply and not apply them), could you please help me to figure this out with Aaron? 😃
I missed that.
Yes, i think the way do that is to check !getLangOpts().CPlusPlus
Whyt no compatibility warning in C23 mode
@aaron.ballman said so in https://reviews.llvm.org/D133887#3793027
It should definitely be without warning in C23 mode and give an extension warning in earlier modes.I don't know much about extension/incompatible warnings policy (when to apply and not apply them), could you please help me to figure this out with Aaron? 😃
I checked with Aaron, he meant no warning _by default_, but there should definitely be an extension warning. exact same thing as C++.
Sorry for the confusion.
Thank you for working on this! I spotted an issue where we're not quite complete with this implementation work. I pushed up a new test case in https://github.com/llvm/llvm-project/commit/a244194f73788de6dfd6d753ff09be3625613f9f that shows it (and set the C status page back to Partial in case this takes a while to address), but case and default labels should be accepted the same as any other label at the end of a compound statement.
Btw, when working on C standards features, I've started adding test coverage that demonstrates we fully implement the paper into the clang/test/C/ directory, under the correct standard version. This helps us to track the status of proposals a bit more easily within the code base.
clang/include/clang/Basic/DiagnosticParseKinds.td | ||
---|---|---|
298–299 | This is not an error in C2x or in C++2b. |
Hi! Thanks for this information! How did you find out that we should accept labels at end of statement in C, but not in C++? That is not obvious for me unfortunately. If C supports it, why would C++ not support it?
I knew the answer for C off the top of my head, but was still looking up the info for C++. C++ does actually behave the same way:
https://eel.is/c++draft/stmt.label#1 -- case and default are both a label
https://eel.is/c++draft/stmt.block#1.sentence-2 -- all labels at the end of a compound statement are treated as if they are followed by a null statement
https://eel.is/c++draft/stmt.stmt#nt:selection-statement -- a switch is followed by a statement
https://eel.is/c++draft/stmt.stmt#stmt.pre-1 -- a compound-statement is a statement
I can fix up the C++ status page as well if you'd like, or I can hold off if you're already working on this, it's up to you!
Thank you! I'll support empty labels in switch statements in a new pull request as soon as I can 😁
This is not an error in C2x or in C++2b.