[PATCH] Support "nothrow" function attribute

Kaveh R. Ghazi ghazi@caip.rutgers.edu
Mon May 27 10:09:00 GMT 2002


 > From: Richard Henderson <rth at redhat dot com> 
 > 
 > Rather than this generic non-test, perhaps you'd like to come up
 > with some functional tests for these attributes?  Const and Pure,
 > in particular, should be easy enough to come up with aliasing tests
 > like
 > 
 > 	int foo(int) __attribute__((pure));
 > 	void link_error(void);
 > 	int main ()
 > 	{
 > 	  int i[10], r;
 > 
 > 	  i[0] = 0;
 > 	  r = foo(0);
 > 	  if (i[0])
 > 	    link_error();
 > 	  return r;
 > 	}
 > 	int foo(int x) { return x - x; }


Neat idea.  I wrote up a c-torture execute-style testcase for this and
added tests for the automatic pure/const detection of static
functions.  Assuming what I wrote is correct, we should get no link
failures.

However we do get some failures and they're different for latest
3.1.1-pre vs. latest 3.2-trunk, indicating a regression.  (I'm using
sparc-sun-solaris2.7.)  For -O0 I get no failures (by design) and at
-O3 I also get no failures I suppose because everything gets inlined.

However at -O1/-O2, using 3.1.1-pre I get:
 > Undefined                       first referenced
 >  symbol                             in file
 > link_error2                         /var/tmp//ccPXsSka.o
 > link_error4                         /var/tmp//ccPXsSka.o
 > link_error6                         /var/tmp//ccPXsSka.o
This seems to indicate that auto-detecting const works but detecting
pure is broken.

At -O1/-O2 using 3.2-trunk I get:
 > Undefined                       first referenced
 >  symbol                             in file
 > link_error4                         /var/tmp//ccADHkwc.o
 > link_error5                         /var/tmp//ccADHkwc.o
 > link_error6                         /var/tmp//ccADHkwc.o
 > link_error7                         /var/tmp//ccADHkwc.o
This seems to indicate that auto-detecting functions which call other
const/pure functions is broken.


If the testcase is correct, I'd like to install it on the trunk in the
execute directory.  (Care to suggest a name?  or should I just use the
date?)  I'll also open a PR for 3.1.x and 3.2.

		Thanks,
		--Kaveh


-------------snip--------------------------
/* Use a different function for each test so the link failures
   indicate which one is broken.  */
extern void link_error0 (void);
extern void link_error1 (void);
extern void link_error2 (void);
extern void link_error3 (void);
extern void link_error4 (void);
extern void link_error5 (void);
extern void link_error6 (void);
extern void link_error7 (void);

extern int i;

extern int func0 (int) __attribute__ ((__pure__));
extern int func1 (int) __attribute__ ((__const__));
/* GCC should automatically detect these attributes.  */
static int func2 (int a) { return i + a; } /* pure */
static int func3 (int a) { return a * 3; } /* const */
static int func4 (int a) { return func0(a) + a; } /* pure */
static int func5 (int a) { return a + func1(a); } /* const */
static int func6 (int a) { return func2(a) + a; } /* pure */
static int func7 (int a) { return a + func3(a); } /* const */

int main ()
{
  int i[10], r;

  i[0] = 0;
  r = func0(0);
  if (i[0])
    link_error0();

  i[1] = 0;
  r = func1(0);
  if (i[1])
    link_error1();

  i[2] = 0;
  r = func2(0);
  if (i[2])
    link_error2();

  i[3] = 0;
  r = func3(0);
  if (i[3])
    link_error3();

  i[4] = 0;
  r = func4(0);
  if (i[4])
    link_error4();

  i[5] = 0;
  r = func5(0);
  if (i[5])
    link_error5();

  i[6] = 0;
  r = func6(0);
  if (i[6])
    link_error6();

  i[7] = 0;
  r = func7(0);
  if (i[7])
    link_error7();

  return r;
}

int func0 (int a) { return a - i; } /* pure */
int func1 (int a) { return a - a; } /* const */

int i = 2;

#ifndef __OPTIMIZE__
/* Avoid link failures when not optimizing. */
void link_error0() {}
void link_error1() {}
void link_error2() {}
void link_error3() {}
void link_error4() {}
void link_error5() {}
void link_error6() {}
void link_error7() {}
#endif /* ! __OPTIMIZE__ */



More information about the Gcc-patches mailing list