cppfront icon indicating copy to clipboard operation
cppfront copied to clipboard

[FIX] preserve parens around expressions calculating value of argument for a function call

Open filipsajdak opened this issue 3 years ago • 4 comments

The current implementation removes parentheses used in expression that calculates the value of arguments for a function.

cpp2 code:

f: (x:_) = {}

main: () -> int = {
    i := 2;
    f((i+(i+1)*2)/2);
}

When compiled with cppfront we get (I am skipping the boilerplate)

[[nodiscard]] auto main() -> int{
    auto i { 2 }; 
    f( i + i + 1 * 2 / 2);
}

All parentheses inside the function call are gone. (i+(i+1)*2)/2 is not equal i + i + 1 * 2 / 2.

This fix corrects that behavior and preserves these parentheses. The generated code after the fix:

[[nodiscard]] auto main() -> int{
    auto i { 2 }; 
    f((i + (i + 1) * 2) / 2);
}

Close https://github.com/hsutter/cppfront/issues/66

filipsajdak avatar Oct 11 '22 00:10 filipsajdak

This fix breaks

v : std::vector<int> = ( 1, 2, 3 );

cppfront generates

std::vector<int> v { (1, 2, 3) };

which compiles but thanks to the coma operator it will compile by cpp1 compiler to a vector with only 3 value.

filipsajdak avatar Oct 11 '22 00:10 filipsajdak

OK, the above issue is now fixed.

filipsajdak avatar Oct 11 '22 01:10 filipsajdak

Some issue still exists

cpp2 code

ddd := fun(fun("abc").substr(1));

compiles to

auto ddd { fun(CPP2_UFCS(substr, fun(("abc")), 1)) };

filipsajdak avatar Oct 11 '22 01:10 filipsajdak

The issue with additional parens around function arguments is solved.

The issue showed up in the code merged with https://github.com/hsutter/cppfront/pull/18 (I was not able to trigger it on the main branch). I remove that commit from the patch set.

filipsajdak avatar Oct 11 '22 05:10 filipsajdak

Good fix -- this actually corrects the output for two existing test cases, one of which is mixed-test-parens.cpp2 that I hadn't noticed had the wrong output. As Bjarne would say: "Eek!" Thanks.

hsutter avatar Oct 18 '22 00:10 hsutter