Bug ID: 88391
Summary: Request - attribute for adding tags to functions
Assignee: unassigned at gcc dot gnu.org
Reporter: david at westcontrol dot com
Target Milestone: ---
Sometimes it would be very useful to have "tags" on functions, which can be
used to restrict the kinds of functions that can call them or be called by
them. I can think of two use-cases from the embedded world, and one more
1. A function that can only be called when interrupts are disabled, has special
powers (such as direct access to locked or atomic data, at least on single-core
systems) and special responsibilities (it should be fast, should not block,
etc.). It should only be called by a function that disables interrupts.
2. A function that needs to run from ram should not call other functions that
are not also in ram. This might be for programming code flash, or perhaps to
minimise speed variation in a real-time system with slow external flash.
3. A function that should only be called when you have a certain lock or
There are many other uses possible uses. I think that a few general attributes
could allow this to be handled in the C (and C++) front-end, with the compiler
issuing warnings or errors when the "tag" rules are broken. It would not
affect code generation, name mangling, etc. - it's just a mechanism to help
spot incorrect code. I am not suggesting that we have attributes that actually
disable interrupts, or place code in ram (though these might be nice!) - all
embedded targets already have mechanisms for that, such as "section"
attributes, and we don't need to duplicate functionality that can be handled by
a macro or two.
My suggestions for function attributes are:
Mark a function as being tagged with "tagname" (or multiple tag names).
Mark a function as only being callable by a function with this tag.
Mark a function as only being allowed to call functions with this tag.
Mark a function as not being callable by a function with this tag.
Mark a function as not being allowed to call functions with this tag.
There would also need to be a statement attribute:
Mark the statement as having this tag (and therefore being allowed to call
matching "caller_tag" functions, and not allowed to call matching
// Some code that should never be interrupted
// Use critical data
// Collect data
releaseLock(); // Still tag "got_lock"
One big question is the compiler target library. In small embedded systems,
apparently simple operations like "x = y / z;" may result in a library call if
the processor does not have hardware instructions for the operation. It is
important in "callee_tag" functions that any such library calls also issue a