Bug 46181 - attribute for marking things as undefined use after a function call
Summary: attribute for marking things as undefined use after a function call
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: unknown
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
Depends on:
Reported: 2010-10-26 10:17 UTC by Michael Thayer
Modified: 2010-10-26 18:29 UTC (History)
0 users

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


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.


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) { ... }

but the following might be alright:

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

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