codinuum / cca

Code Continuity Analysis Framework
https://codinuum.github.io/gallery-cca/
Apache License 2.0
20 stars 5 forks source link

Declaration as if-condition #28

Closed petr-bauch closed 2 years ago

petr-bauch commented 2 years ago

This is probably more of a feature-request:

int main() {
  if (A* a = get_A()) return 1;
  return 0;
}

Will translate the condition as assignment where lhs is a multiplicative-expression.

int main() {
  if (A a = get_A()) return 1;
  return 0;
}

Will translate as macro A applied to a.

I understand it's ambiguous without knowing what is a type but a similar code outside of an if-statement works as expected, e.g.

int main() {
  A* a = get_A();
  return 0;
}

Is it because the parser by default expects the condition to be an expression?

codinuum commented 2 years ago

No (partially). The parser tries to resolve such ambiguities based on a number of (context dependent) rules. Identifying what A is in the above depends on the rules.

codinuum commented 2 years ago

However, we found the following in the C++20 specification.

If a condition can be syntactically resolved as either an expression or a declaration, it is interpreted as the latter.

We are thinking about adding some rules.

codinuum commented 2 years ago

We have just modified the parser in 83ecfdd.

petr-bauch commented 2 years ago

Sorry for the delay, just checked and it works perfectly.