3 ms·
C has separate compilation: the compiler isn’t guaranteed to have all of the relevant source code at one time. When the signal handler is compiled, there’s not
by maxlybbert 9y ago
C has separate compilation: the compiler isn’t guaranteed to have all of the relevant source code at one time.
When the signal handler is compiled, there’s nothing to say “this will be used as a signal handler,” so while the compiler has the source it doesn’t have a reason to complain about calling any particular functions.
Then, when that function is used as a signal handler, the compiler has the source to the calling code (well, the code setting up the callback) but may not have the source for the handler itself. So now it knows which rules apply but may not have the code it needs to enforce those rules. If they’re in the same file, it could. And while that is a common case, it’s not the only possibility.
How much should the compiler or linter know about the platform’s API? How can I tell the compiler about any arbitrary rule my own code must follow?
As someone else suggested, you could do it with annotations, but that’s nonstandard.