ifStmt::getThen() returns ifStmt again?

Hi

I recently started to test the current master
https://github.com/llvm/llvm-project/commit/e04483ee35ba28c089cbbcec97a96b67ce0a035e

and found that ifStmt::getThen() now returns the ifStmt itself like in the
following code where both dump calls give the same output:

stmt.dump(dbgs());
const IfStmt ifS = cast<IfStmt>(stmt);
auto x = ifS.getThen();
x->dump(dbgs());

The AST was generated for a simple program:

int main(int argc, char ** argv) {
  int a = 0;
  if (true) {
    a = 10;
  }
  return a;
}

This was not the case for LLVM 8, previously the a=10 stmt was returned.
Should it be that way or am I doing something wrong?

Best
Ulrich

I cannot reproduce this with a recent clang (and if IfStmt::getThen returned the
IfStmt itself, clang would be hopelessly broken). If you look at the implementation of
IfStmt::getThen in AST/Stmt.h you can see that it returns the then statement, which
is stored as a trailing object.

Best,
Bruno

Hi

Hi

I recently started to test the current master
https://github.com/llvm/llvm-project/commit/e04483ee35ba28c089cbbcec97a96b67ce0a035e

and found that ifStmt::getThen() now returns the ifStmt itself like in
the
following code where both dump calls give the same output:

stmt.dump(dbgs());
const IfStmt ifS = cast<IfStmt>(stmt);
auto x = ifS.getThen();
x->dump(dbgs());

The AST was generated for a simple program:

int main(int argc, char ** argv) {
  int a = 0;
  if (true) {
    a = 10;
  }
  return a;
}

This was not the case for LLVM 8, previously the a=10 stmt was returned.
Should it be that way or am I doing something wrong?

Best
Ulrich

I cannot reproduce this with a recent clang (and if IfStmt::getThen
returned the
IfStmt itself, clang would be hopelessly broken). If you look at the
implementation of
IfStmt::getThen in AST/Stmt.h you can see that it returns the then
statement, which
is stored as a trailing object.

Thank you for checking.

I casted wrong...

  const IfStmt ifS = cast<IfStmt>(stmt);

should be

  const IfStmt &ifS = cast<IfStmt>(stmt);

Working now! Thanks!

Best
Ulrich

Hi

Hi

Hi

I recently started to test the current master
https://github.com/llvm/llvm-project/commit/e04483ee35ba28c089cbbcec97a96b67ce0a035e

and found that ifStmt::getThen() now returns the ifStmt itself like in
the
following code where both dump calls give the same output:

stmt.dump(dbgs());
const IfStmt ifS = cast(stmt);
auto x = ifS.getThen();
x->dump(dbgs());

The AST was generated for a simple program:

int main(int argc, char ** argv) {
int a = 0;
if (true) {
a = 10;
}
return a;
}

This was not the case for LLVM 8, previously the a=10 stmt was returned.
Should it be that way or am I doing something wrong?

Best
Ulrich

I cannot reproduce this with a recent clang (and if IfStmt::getThen
returned the
IfStmt itself, clang would be hopelessly broken). If you look at the
implementation of
IfStmt::getThen in AST/Stmt.h you can see that it returns the then
statement, which
is stored as a trailing object.

Thank you for checking.

I casted wrong…

const IfStmt ifS = cast(stmt);

should be

const IfStmt &ifS = cast(stmt);

Working now! Thanks!

Thought about it again and was wondering, should copying not create either a fully-functional object or be disallowed, i.e. have a deleted copy constructor?

Best
Ulrich

I think people rarely make this mistake because most of the time statements are passed by pointers rather than by references.