Bug 37064 - missing warning on pure function with side-effects
Summary: missing warning on pure function with side-effects
Status: RESOLVED DUPLICATE of bug 18487
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.3.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2008-08-08 19:44 UTC by Martin Sebor
Modified: 2008-08-08 21:01 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2008-08-08 20:53:56


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Sebor 2008-08-08 19:44:13 UTC
gcc assumes that functions defined with the pure attribute have no side
effects and do not modify program state, including through pointers passed
to such functions as arguments. The function below does modify the object
pointed to by its argument. It would be useful if gcc issued a diagnostic
for this and other similar violations of the "pure" assumption, analogously
to how it diagnoses noreturn functions that do return.

$ cat -n t.C && g++ -c -O2 -Wall -W t.C
     1  int f0 () __attribute ((noreturn));
     2  int f0 () { return 0; }
     3
     4  int f1 (int*) __attribute ((pure));
     5  int f1 (int *i)
     6  {
     7      *i = 0;
     8      return 0;
     9  }
    10
t.C: In function ‘int f0()’:
t.C:2: warning: function declared ‘noreturn’ has a ‘return’ statement
t.C:2: warning: ‘noreturn’ function does return
Comment 1 Martin Sebor 2008-08-08 19:47:58 UTC
Similarly, functions declared with the const attribute such as f1() in the
test case below that violate the compiler's assumptions should be diagnosed:

$ cat -n t.C && g++ -c -O2 -Wall -W t.C
     1  extern int i;
     2  int f1 () __attribute ((const));
     3  int f1 ()
     4  {
     5      return i;
     6  }
     7
Comment 2 Richard Biener 2008-08-08 20:53:56 UTC
Confirmed.
Comment 3 Andrew Pinski 2008-08-08 21:01:12 UTC

*** This bug has been marked as a duplicate of 18487 ***