[clang] [Clang][C2y] Add support for if declarations (N3356 paper) (PR #198244)
Muhammad Bassiouni via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 8 04:51:34 PDT 2026
================
@@ -1970,6 +2024,13 @@ Sema::ConditionResult Parser::ParseCXXCondition(StmtResult *InitStmt,
DeclSpec DS(AttrFactory);
ParseSpecifierQualifierList(DS, AS_none, DeclSpecContext::DSC_condition);
+ // The Declarator's DeclarationAttrs only accepts [[]] and keyword attributes;
+ // move any GNU attributes onto the DeclSpec instead.
+ if (!getLangOpts().CPlusPlus)
----------------
bassiounix wrote:
OK so C++ does handle all the cases properly: https://godbolt.org/z/KGfnjdsdf
The reason for not sharing it with C++ is we parse the GNU attribute as part of a `declspec` to handle the special case for `(__attribute__((...)); expr)` in C2y
```cpp
// Handle '(; expr)', '(__attribute__((...)); expr)' and '([[...]]; expr)'.
if (Tok.is(tok::semi)) {
StmtResult Null = Actions.ActOnNullStmt(ConsumeToken());
if (ParsedAttrs) {
WarnOnInit();
*InitStmt = Actions.ActOnAttributedStmt(attrs, Null.get());
} else
Diag(Null.get()->getBeginLoc(),
diag::err_c2y_first_condition_clause_is_not_declaration);
return ParseCondition(nullptr, Loc, CK, MissingOK);
}
```
This is not valid C++ case, it's C2y specific case.
So it makes sense to convert it from `declspec` attribute to become an attribute for the declarator itself
Refer back to this in `clang/lib/Parse/ParseDecl.cpp` `Parser::ParseDeclGroup`
```cpp
// Accept attributes in an init-declarator. In the first declarator in a
// declaration, these would be part of the declspec. In subsequent
// declarators, they become part of the declarator itself, so that they
// don't apply to declarators after *this* one. Examples:
// short __attribute__((common)) var; -> declspec
// short var __attribute__((common)); -> declarator
// short x, __attribute__((common)) var; -> declarator
MaybeParseGNUAttributes(D);
```
So I'm effectively taking it from this form `short __attribute__((common)) var;` as `declspec` to this form `short var __attribute__((common));` to be part of the declarator itself when it's a `ConditionDecl`.
I don't believe that there's any bugs here, let alone a deeper one. It's working as intended and handles the special case for C2y case.
https://github.com/llvm/llvm-project/pull/198244
More information about the cfe-commits
mailing list