Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Destroy objects at end of scope #322

Closed
dkolsen-pgi opened this issue Nov 19, 2023 · 7 comments
Closed

Destroy objects at end of scope #322

dkolsen-pgi opened this issue Nov 19, 2023 · 7 comments
Assignees

Comments

@dkolsen-pgi
Copy link
Collaborator

ClangIR does not call destructors when objects go out of scope. It does that silently, not even a "NYI" message.

$ cat basic.cpp
extern "C" int printf(char const*, ...);
struct A {
  A() { printf("++A\n"); }
  ~A() { printf("--A\n"); }
};
int main() {
  {
    A a;
  }
  printf("Done\n");
}
$ clang++ basic.cpp ; ./a.out
++A
--A
Done
$ clang++ -fclangir-enable basic.cpp ; ./a.out
++A
Done
@bcardosolopes
Copy link
Member

We actually do clang/test/CIR/CodeGen/dtors.cpp but we're still missing some more basic cases. This should be an assertion, but it's probably due to my workaround in 0a59c5c to unbreak an internal build. Do you intend to work on this? If not I can take a look sooner than later.

@dkolsen-pgi
Copy link
Collaborator Author

I am not planning to work on this right away. I am working on vector types #284 right now. (Can I somehow get permission to make some changes to issues, at least to assign them to myself?)

@bcardosolopes bcardosolopes self-assigned this Nov 22, 2023
@bcardosolopes
Copy link
Member

Can I somehow get permission to make some changes to issues, at least to assign them to myself?

Done, let me know if it works!

@bcardosolopes
Copy link
Member

First part of the fix: d732147, next is to fix some dtor codegen issues.

Note that this would still crash if -fexceptions is on, but I'm tackling that separately as part of adding support for exceptions.

@bcardosolopes
Copy link
Member

Fixed now after both 7683175 and 2aac175, the example runs on my macOS laptop, let me know if it doesn't work on Linux for any reason.

@dkolsen-pgi
Copy link
Collaborator Author

The original test program now works for me. Destructors are called when falling off the end of a scope. But destructors are not called when jumping out of a scope. This program is missing some destructor calls. Should I open a new issue for this, or reopen this one?

extern "C" int printf(char const*, ...);
struct A {
  A() { printf("++A\n"); }
  A(int n) { printf("++A(%d)\n", n); }
  ~A() { printf("--A\n"); }
};
void test(int x) {
  printf("test(%d)\n", x);
  A a;
  if (x == 1) return;
  {
    A b(3);
    if (x == 2) return;
  }
  if (x == 3) return;
}
int main() {
  test(1);
  test(2);
  test(3);
  test(4);
}

@bcardosolopes
Copy link
Member

Thanks for reporting more variations, created #348 to track it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants