cppfront
cppfront copied to clipboard
[FIX] preserve parens around expressions calculating value of argument for a function call
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
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.
OK, the above issue is now fixed.
Some issue still exists
cpp2 code
ddd := fun(fun("abc").substr(1));
compiles to
auto ddd { fun(CPP2_UFCS(substr, fun(("abc")), 1)) };
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.
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.