Skip to content

Commit

Permalink
Fix formatting of call operator after init-list
Browse files Browse the repository at this point in the history
Correctly mark parentheses following a braced initializer list as a
function call. (Specifically, this is invoking operator() of the
just-constructed object.) Also, check for this when applying spaces so
that we use sp_func_call_paren[_empty] rather than failing to match any
rule and falling back to the default of always adding a space.
  • Loading branch information
mwoehlke committed Sep 19, 2020
1 parent 80aac66 commit 2bd221c
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/flag_braced_init_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ void flag_cpp_braced_init_list(chunk_t *pc, chunk_t *next)
if (tmp != nullptr)
{
chunk_flags_clr(tmp, PCF_EXPR_START | PCF_STMT_START);

// Flag call operator
if (chunk_is_token(tmp, CT_PAREN_OPEN))
{
if (auto *const c = chunk_skip_to_match(tmp))
{
set_chunk_type(tmp, CT_FPAREN_OPEN);
set_chunk_parent(tmp, CT_FUNC_CALL);
set_chunk_type(c, CT_FPAREN_CLOSE);
set_chunk_parent(c, CT_FUNC_CALL);
}
}
}
// TODO: Change pc->type CT_WORD -> CT_TYPE
// for the case CT_ASSIGN (and others).
Expand Down
2 changes: 1 addition & 1 deletion src/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ sp_square_fparen;
extern Option<iarf_e>
sp_fparen_brace;

// Add or remove space between ')' and '{' of s function call in object
// Add or remove space between ')' and '{' of a function call in object
// initialization.
//
// Overrides sp_fparen_brace.
Expand Down
5 changes: 4 additions & 1 deletion src/space.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1423,7 +1423,10 @@ static iarf_e do_space(chunk_t *first, chunk_t *second, int &min_sp)
if ( chunk_is_token(first, CT_FUNC_CALL)
|| chunk_is_token(first, CT_FUNC_CTOR_VAR)
|| chunk_is_token(first, CT_CNG_HASINC)
|| chunk_is_token(first, CT_CNG_HASINCN))
|| chunk_is_token(first, CT_CNG_HASINCN)
|| ( chunk_is_token(first, CT_BRACE_CLOSE)
&& first->parent_type == CT_BRACED_INIT_LIST
&& chunk_is_token(second, CT_FPAREN_OPEN)))
{
if ( (options::sp_func_call_paren_empty() != IARF_IGNORE)
&& chunk_is_token(second, CT_FPAREN_OPEN))
Expand Down
1 change: 1 addition & 0 deletions tests/config/sp_inside_fparens-f.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sp_inside_fparens = force
2 changes: 2 additions & 0 deletions tests/cpp.test
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,8 @@

34168 nl_type_brace_init_lst-r.cfg cpp/Issue_2910.cpp

34169 sp_inside_fparens-f.cfg cpp/init-list-call.cpp

34170 empty.cfg cpp/i1082.cpp
34171 empty.cfg cpp/i1181.cpp
34172 space_indent_columns-4.cfg cpp/i1165.cpp
Expand Down
1 change: 1 addition & 0 deletions tests/expected/cpp/34169-init-list-call.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
auto x = foo{0}( );
1 change: 1 addition & 0 deletions tests/input/cpp/init-list-call.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
auto x = foo{0}();

0 comments on commit 2bd221c

Please sign in to comment.