[Bug web/79738] New: Documentation for __attribute__((const)) slightly misleading

m-gccbugs at bodyfour dot uk gcc-bugzilla@gcc.gnu.org
Mon Feb 27 21:40:00 GMT 2017


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79738

            Bug ID: 79738
           Summary: Documentation for __attribute__((const)) slightly
                    misleading
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: web
          Assignee: unassigned at gcc dot gnu.org
          Reporter: m-gccbugs at bodyfour dot uk
  Target Milestone: ---

Looking at the description of __attribute__((const)) at:
https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes

"Basically this is just slightly more strict class than the pure attribute
below, since function is not allowed to read global memory."

That's almost correct -- it's not allowed to read memory that can change.  You
could argue that "global" implies the .data space, but to a lay programmer it
would suggest you can't do any memory reads except on your own stack.

To take a trivial example, it's fine for this to be ((const)):

    static const int TABLE[] = { 1, 2, 3 };
    extern int lookup(unsigned x) __attribute__((const));
    int lookup(unsigned x)
    {
        return TABLE[x];
    }

...and if fact, -Wsuggest-attribute=const will warn if you don't have the
attribute there.

I suggest amending the documentation to say "...is not allowed to read from any
memory location whose contents can change between calls"


More information about the Gcc-bugs mailing list