Skip to content

[clang-format] RemoveParentheses confused by function calls containing comma operators #100768

@urnathan

Description

@urnathan

I know Remove Parentheses has some be-careful stickers on it.

I discovered an unexpected removal which changes program semantics. Specifically, if a function call is of the form foo((a, b)), it is transformed from a single argument call to a multi-argument call of the form foo(a, b). I.e. the comma operator is removed. Similar variants like foo((a, b), (c, d)) are not so transformed -- it seems to happen only when a call's single argument is a comma expression.

[In my actual use case this was a macro invocation, and moving the LHS of the comma operator out of the call was not possible.]

// .clang-format:
// BasedOnStyle: LLVM
// RemoveParentheses: MultipleParentheses

void frob () {
    // no transform here, expected
    foo(a, b);           // foo(a, b);
    foo((a, b), c);      // foo((a, b), c);
    foo(a, (b, c));      // foo(a, (b, c));
    foo((a, b), (c, d)); // foo((a, b), (c, d));

    // loses parens here, unexpected and harmful
    bar((a, b));    // bar(a, b);

    // doesn't lose parens here, unexpected but harmless
    baz(a, (b), c); // baz(a, (b), c);
}

Metadata

Metadata

Assignees

Labels

clang-formatinvalid-code-generationTool (e.g. clang-format) produced invalid code that no longer compiles

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions