Bug 46181 - attribute for marking things as undefined use after a function call
attribute for marking things as undefined use after a function call
Status: NEW
Product: gcc
Classification: Unclassified
Component: middle-end
unknown
: P3 enhancement
: ---
Assigned To: Not yet assigned to anyone
:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2010-10-26 10:17 UTC by Michael Thayer
Modified: 2010-10-26 18:29 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2010-10-26 18:29:33


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Michael Thayer 2010-10-26 10:17:16 UTC
It would be nice for purposes of static error checking to be able to mark functions as destructors for some resource.  Specifically, that a if a value is passed from a variable to argument n of the function then it is no longer valid after the call to the function.  Optionally with an "invalid" value that may be assigned to the variable afterwards.

E.g.

int close(int fd) __attribute__(( free(1, -1) ));  /* Says that the value passed to the first argument is invalid hereafter, and that the variable it came from can be marked invalid by setting it to -1 */

So that the compiler can see that the following is bad:

int fd = open("filename", O_CREAT);
...
if (close(fd) < 0) { ... }
do_something_with(fd);

but the following might be alright:

int fd = open("filename", O_CREAT);
...
if (close(fd) < 0) { ... }
fd = -1;
do_something_with(fd);

It might also make sense to be able to tag a typedef as a resource type with information about a free-like function and an invalid value:

typedef int myfile __attribute(( resource(close, -1) ));
Comment 1 Andrew Pinski 2010-10-26 18:29:33 UTC
Confirmed.