Skip to content

Commit

Permalink
Fix incorrect removal of braces of an 'if' statement that is followed by
Browse files Browse the repository at this point in the history
and 'else' and have a nested 'if' before the 'else'.
  • Loading branch information
bengardner committed Nov 20, 2010
1 parent 2d1498c commit bda365f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/braces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,23 @@ static bool can_remove_braces(chunk_t *bopen)
return(false);
}

if ((pc->type == CT_BRACE_CLOSE) && (pc->parent_type == CT_IF))
{
chunk_t *next = chunk_get_next_ncnl(pc, CNAV_PREPROC);

prev = chunk_get_prev_ncnl(pc, CNAV_PREPROC);

if ((next != NULL) && (next->type == CT_ELSE) &&
((prev->type == CT_BRACE_CLOSE) || (prev->type == CT_VBRACE_CLOSE)) &&
(prev->parent_type == CT_IF))
{
LOG_FMT(LBRDEL, " - bailed on '%s'[%s] on line %d due to 'if' and 'else' sequence\n",
get_token_name(pc->type), get_token_name(pc->parent_type),
pc->orig_line);
return(false);
}
}

LOG_FMT(LBRDEL, " - end on '%s' on line %d. if_count=%d semi_count=%d\n",
get_token_name(pc->type), pc->orig_line, if_count, semi_count);

Expand Down
12 changes: 12 additions & 0 deletions tests/input/c/if_chain.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,15 @@ void bar(void)
#endif /* ifdef SOME_DEFINE */
}
}

void funct(int v1, int v2, int v3)
{
if ( v1 )
{
if ( v2 ) f1();
}
else
{
if ( v3 ) f2();
}
}
9 changes: 9 additions & 0 deletions tests/output/c/00054-if_chain.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,12 @@ void bar(void)
#endif /* ifdef SOME_DEFINE */
}
}

void funct(int v1, int v2, int v3)
{
if (v1) {
if (v2) f1();
} else {
if (v3) f2();
}
}

0 comments on commit bda365f

Please sign in to comment.