Commit 92863013 by Martin Sebor Committed by Martin Sebor

PR 79738 - Documentation for __attribute__((const)) slightly misleading

gcc/ChangeLog:
	* doc/extend.texi (attribute const, pure): Clarify.

From-SVN: r267156
parent 3fd156b1
2018-12-14 Martin Sebor <msebor@redhat.com>
PR web/79738
* doc/extend.texi (attribute const, pure): Clarify.
2018-12-14 H.J. Lu <hongjiu.lu@intel.com> 2018-12-14 H.J. Lu <hongjiu.lu@intel.com>
* config/i386/i386.c (rest_of_insert_endbranch): Insert ENDBR * config/i386/i386.c (rest_of_insert_endbranch): Insert ENDBR
...@@ -2594,25 +2594,45 @@ are automatically detected and this attribute is ignored. ...@@ -2594,25 +2594,45 @@ are automatically detected and this attribute is ignored.
@item const @item const
@cindex @code{const} function attribute @cindex @code{const} function attribute
@cindex functions that have no side effects @cindex functions that have no side effects
Many functions do not examine any values except their arguments, and Calls to functions whose return value is not affected by changes to
have no effects except to return a value. Calls to such functions lend the observable state of the program and that have no observable effects
themselves to optimization such as common subexpression elimination. on such state other than to return a value may lend themselves to
The presence of the @code{const} attribute on a function declaration optimizations such as common subexpression elimination. Declaring such
allows GCC to emit more efficient code for some calls to the function. functions with the @code{const} attribute allows GCC to avoid emitting
some calls in repeated invocations of the function with the same argument
values.
For example,
@smallexample
int square (int) __attribute__ ((const));
@end smallexample
@noindent
tells GCC that subsequent calls to function @code{square} with the same
argument value can be replaced by the result of the first call regardless
of the statements in between.
The @code{const} attribute prohibits a function from reading objects
that affect its return value between successive invocations. However,
functions declared with the attribute can safely read objects that do
not change their return value, such as non-volatile constants.
The @code{const} attribute imposes greater restrictions on a function's The @code{const} attribute imposes greater restrictions on a function's
definition than the similar @code{pure} attribute below because it definition than the similar @code{pure} attribute. Declaring the same
additionally prohibits the function from reading memory except for function with both the @code{const} and the @code{pure} attribute is
constant global variables. Decorating the same function with diagnosed. Because a const function cannot have any observable side
both the @code{const} and the @code{pure} attribute is diagnosed. effects it does not make sense for it to return @code{void}. Declaring
such a function is diagnosed.
@cindex pointer arguments @cindex pointer arguments
Note that a function that has pointer arguments and examines the data Note that a function that has pointer arguments and examines the data
pointed to must @emph{not} be declared @code{const}. Likewise, a pointed to must @emph{not} be declared @code{const} if the pointed-to
function that calls a non-@code{const} function usually must not be data might change between successive invocations of the function. In
@code{const}. Because a @code{const} function cannot have any side general, since a function cannot distinguish data that might change
effects it does not make sense for such a function to return @code{void}. from data that cannot, const functions should never take pointer or,
Declaring such a function is diagnosed. in C++, reference arguments. Likewise, a function that calls a non-const
function usually must not be const itself.
@item constructor @item constructor
@itemx destructor @itemx destructor
...@@ -3377,34 +3397,52 @@ to prevent recursion. ...@@ -3377,34 +3397,52 @@ to prevent recursion.
@item pure @item pure
@cindex @code{pure} function attribute @cindex @code{pure} function attribute
@cindex functions that have no side effects @cindex functions that have no side effects
Many functions have no effects except the return value and their
return value depends only on the parameters and/or global variables. Calls to functions that have no observable effects on the state of
Calls to such functions can be subject the program other than to return a value may lend themselves to optimizations
to common subexpression elimination and loop optimization just as an such as common subexpression elimination. Declaring such functions with
arithmetic operator would be. These functions should be declared the @code{pure} attribute allows GCC to avoid emitting some calls in repeated
with the attribute @code{pure}. For example, invocations of the function with the same argument values.
The @code{pure} attribute prohibits a function from modifying the state
of the program that is observable by means other than inspecting
the function's return value. However, functions declared with the @code{pure}
attribute can safely read any non-volatile objects, and modify the value of
objects in a way that does not affect their return value or the observable
state of the program.
For example,
@smallexample @smallexample
int square (int) __attribute__ ((pure)); int hash (char *) __attribute__ ((pure));
@end smallexample @end smallexample
@noindent @noindent
says that the hypothetical function @code{square} is safe to call tells GCC that subsequent calls to the function @code{hash} with the same
fewer times than the program says. string can be replaced by the result of the first call provided the state
of the program observable by @code{hash}, including the contents of the array
itself, does not change in between. Even though @code{hash} takes a non-const
pointer argument it must not modify the array it points to, or any other object
whose value the rest of the program may depend on. However, the caller may
safely change the contents of the array between successive calls to
the function (doing so disables the optimization). The restriction also
applies to member objects referenced by the @code{this} pointer in C++
non-static member functions.
Some common examples of pure functions are @code{strlen} or @code{memcmp}. Some common examples of pure functions are @code{strlen} or @code{memcmp}.
Interesting non-pure functions are functions with infinite loops or those Interesting non-pure functions are functions with infinite loops or those
depending on volatile memory or other system resource, that may change between depending on volatile memory or other system resource, that may change between
two consecutive calls (such as @code{feof} in a multithreading environment). consecutive calls (such as the standard C @code{feof} function in
a multithreading environment).
The @code{pure} attribute imposes similar but looser restrictions on The @code{pure} attribute imposes similar but looser restrictions on
a function's definition than the @code{const} attribute: @code{pure} a function's definition than the @code{const} attribute: @code{pure}
allows the function to read any non-volatile memory, not just allows the function to read any non-volatile memory, even if it changes
constant global variables. Decorating the same function with in between successive invocations of the function. Declaring the same
both the @code{pure} and the @code{const} attribute is diagnosed. function with both the @code{pure} and the @code{const} attribute is
Because a @code{pure} function cannot have any side effects it does not diagnosed. Because a pure function cannot have any observable side
make sense for such a function to return @code{void}. Declaring such effects it does not make sense for such a function to return @code{void}.
a function is diagnosed. Declaring such a function is diagnosed.
@item returns_nonnull @item returns_nonnull
@cindex @code{returns_nonnull} function attribute @cindex @code{returns_nonnull} function attribute
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment