================
@@ -1881,15 +1881,43 @@ Sema::ConditionResult
Parser::ParseCXXCondition(StmtResult *InitStmt,
}
ParsedAttributes attrs(AttrFactory);
- MaybeParseCXX11Attributes(attrs);
+ bool ParsedAttrs = MaybeParseCXX11Attributes(attrs);
const auto WarnOnInit = [this, &CK] {
- Diag(Tok.getLocation(), getLangOpts().CPlusPlus17
- ? diag::warn_cxx14_compat_init_statement
- : diag::ext_init_statement)
- << (CK == Sema::ConditionKind::Switch);
+ if (getLangOpts().CPlusPlus)
+ Diag(Tok.getLocation(), getLangOpts().CPlusPlus17
+ ? diag::warn_cxx14_compat_init_statement
+ : diag::ext_init_statement)
+ << (CK == Sema::ConditionKind::Switch);
+ else
+ Diag(Tok.getLocation(), getLangOpts().C2y
+ ? diag::warn_c2y_compat_init_statement
+ : diag::ext_c2y_init_statement)
+ << (CK == Sema::ConditionKind::Switch);
};
+ if (!getLangOpts().CPlusPlus) {
+ if (Tok.isOneOf(tok::kw_static_assert, tok::kw__Static_assert)) {
+ WarnOnInit();
+ DeclGroupPtrTy DG;
+ SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
+ ParsedAttributes DeclSpecAttrs(AttrFactory);
+ // C2y replaces the init-statement in C++17 to be a declaration instead.
+ DG = ParseDeclaration(DeclaratorContext::SelectionInit, DeclEnd, attrs,
+ DeclSpecAttrs);
+ *InitStmt = Actions.ActOnDeclStmt(DG, DeclStart, DeclEnd);
+ return ParseCondition(nullptr, Loc, CK, MissingOK);
+ }
+
+ if (Tok.is(tok::semi) && ParsedAttrs) {
+ // Parse if ([[...]]; true).
+ WarnOnInit();
+ *InitStmt = Actions.ActOnAttributedStmt(
+ attrs, Actions.ActOnNullStmt(ConsumeToken()).get());
+ return ParseCondition(nullptr, Loc, CK, MissingOK);
+ }
----------------
Sirraide wrote:
```suggestion
// Handle 'if (; true)' and 'if ([[...]]; true)'.
if (Tok.is(tok::semi)) {
StmtResult Null = Actions.ActOnNullStmt(ConsumeToken());
if (!ParsedAttrs) {
Diag(Tok.getLocation(),
diag::err_c2y_first_condition_clause_is_not_declaration);
*InitStmt = Null.get();
} else {
WarnOnInit();
*InitStmt = Actions.ActOnAttributedStmt(
attrs, Null.get());
}
return ParseCondition(nullptr, Loc, CK, MissingOK);
}
```
Something like this (no need to call `WarnOnInit()` in the other branch of the
`if` since that emits an error anyway
https://github.com/llvm/llvm-project/pull/198244
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits