================
@@ -1881,15 +1881,51 @@ 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 (isDeclarationStatement() && !isCXXSimpleDeclaration(false)) {
+ 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();
+ if (attrs.empty()) {
+ SourceLocation SemiLoc = Tok.getLocation();
+ Diag(SemiLoc, diag::warn_c2y_empty_declaration_statement)
+ << (CK == Sema::ConditionKind::Switch)
+ << FixItHint::CreateRemoval(SemiLoc);
----------------
Sirraide wrote:
I don’t think we need/want this warning: as I see it, we warn about empty
statements because `;` is easy to typo, but no-one is going to ‘accidentally’
write `[[]]` (also, if the attribute list is empty because the user wrote a
macro that expanded to nothing, we definitely wouldn’t want to warn about it).
Actually, we can probably just drop this part of the `if` and call
`ActOnAttributedStmt()` unconditionally (it should know how to handle the case
of there being no attributes)
https://github.com/llvm/llvm-project/pull/198244
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits