On 08/30/2016 06:22 AM, Jens Bauer wrote:
Hi all.

I know it's possible to declare a variable 'read-only' by using 'const'.

When working with microcontrollers (small ICs, which often requires you to 
write your code at driver-level), you need to be able to declare a structure 
member 'read-only', 'write-only' or 'read+write'.
In addition to that, it would definitely be handy to declare variables 'no 
access'

So I'd like to propose the following two attributes, which is 'off' by default 
(eg. read access + write access):
__attribute__((not_readable))
__attribute__((not_writable))

Any combination of those allowed.

Presumably you are proposing to have GCC diagnose accesses to objects
declared using these attributes that were in conflict with the intent
of the attributes (as is modifying a const object).

This sounds reasonable and useful to me but to be fully integrated
into the language, attribute not_readable would need to be elevated
to the status of a type qualifier analogous to const.  Otherwise it
would (likely, if applied as most other attributes) be lost during
conversions such as in

  __attribute__ ((not_readable)) int write_only;
  int *preadwrite = &write_only;

partly defeating the purpose of feature.  That should be doable but
AFAIK it's different from most other attributes GCC supports and so
worth calling out as a potential pitfall.

In any case, unless someone pokes a major hole in your proposal
I suggest to raise an enhancement request for it in Bugzilla and
provide as much detail as you can specifying how you envision this
to work (including any other feedback you may get here).  Fleshing
out a prototype implementation of this proposal and submitting it
to gcc-patches would be even better.

Martin

Reply via email to