Bug 37064

Summary: missing warning on pure function with side-effects
Product: gcc Reporter: Martin Sebor <msebor>
Component: cAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED DUPLICATE    
Severity: enhancement CC: ed, gcc-bugs, msebor
Priority: P3 Keywords: diagnostic
Version: 4.3.0   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2008-08-08 20:53:56

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 ***