Skip to content

Commit

Permalink
Merge pull request #646 from andreasfertig/fixIssue644
Browse files Browse the repository at this point in the history
Fixed #644: Handle `AttributeStmt` with sub statements.
  • Loading branch information
andreasfertig authored Jun 21, 2024
2 parents bcc971b + 14e037f commit 26b7ffc
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
11 changes: 9 additions & 2 deletions CodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3626,6 +3626,8 @@ void CodeGenerator::InsertArg(const AttributedStmt* stmt)
for(const auto& attr : stmt->getAttrs()) {
InsertAttribute(*attr);
}

InsertArg(stmt->getSubStmt());
}
//-----------------------------------------------------------------------------

Expand Down Expand Up @@ -5003,7 +5005,7 @@ void CodeGenerator::WrapInParensOrCurlys(const BraceKind braceKind,

void CodeGenerator::WrapInCompoundIfNeeded(const Stmt* stmt, const AddNewLineAfter addNewLineAfter)
{
const bool hasNoCompoundStmt = not isa<CompoundStmt>(stmt);
const bool hasNoCompoundStmt = not(isa<CompoundStmt>(stmt) or isa<AttributedStmt>(stmt));

if(hasNoCompoundStmt) {
mOutputFormatHelper.OpenScope();
Expand All @@ -5012,8 +5014,13 @@ void CodeGenerator::WrapInCompoundIfNeeded(const Stmt* stmt, const AddNewLineAft
if(not isa<NullStmt>(stmt)) {
InsertArg(stmt);

const bool isAttrWithCompound{[&] {
auto* attrStmt = dyn_cast_or_null<AttributedStmt>(stmt);
return attrStmt and isa<CompoundStmt>(attrStmt->getSubStmt());
}()};

// Add semi-colon if necessary. A do{} while does already add one.
if(IsStmtRequiringSemi<IfStmt, CompoundStmt, NullStmt, WhileStmt, DoStmt>(stmt)) {
if(IsStmtRequiringSemi<IfStmt, CompoundStmt, NullStmt, WhileStmt, DoStmt>(stmt) and not isAttrWithCompound) {
mOutputFormatHelper.AppendSemiNewLine();
}
}
Expand Down
22 changes: 22 additions & 0 deletions tests/Issue644.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// cmdline:-std=c++20

int main(int argc, char* argv[])
{
if(argc) [[likely]]
return 5;
else
return 8;

if(argc) [[likely]] {
return 5;
} else {
return 8;
}


switch(argc) {
case 1: return 6;
[[likely]] case 2: return 9;
}

}
19 changes: 19 additions & 0 deletions tests/Issue644.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
int main(int argc, char ** argv)
{
if(static_cast<bool>(argc)) [[likely]] return 5;
else {
return 8;
}

if(static_cast<bool>(argc)) [[likely]] {
return 5;
} else {
return 8;
}

switch(argc) {
case 1: return 6;
[[likely]] case 2: return 9;
}
return 0;
}

0 comments on commit 26b7ffc

Please sign in to comment.