Skip to content

[[gnu::cleanup()]] function executed before return value evaluation #100868

Open
@fuhsnn

Description

@fuhsnn

The code use the [[gnu::cleanup()]] attribute to register a cleanup function. The cleanup function should be called after the return value being determined.

godbolt link: https://godbolt.org/z/zY5abr361

#include <stdio.h>

typedef struct {
    int i;
} S;

void inc(S *obj) {
    obj->i += 7;
}

S test(void) {
    S s [[gnu::cleanup(inc)]];

    printf("returning %d\n", s.i = 22);
    return s;
}

int main()
{
  printf("returned  %d\n", test().i);
  return 0;
}

GCC print:

returning 22
returned  22

Clang print:

returning 22
returned  29

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"confirmedVerified by a second partyextension:gnu

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions