This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

[PATCH] __attribute__ ((deprecated))


Below is a patch file for adding __attribute__ ((deprecated)) based 
on the earlier discussions in the gcc mail list.  Here are some 
points to consider about this attribute.

1. The attribute is allowed for all type, function/method, and object
    declarations.

2. The use of names associated with this attribute will result in
    the warnings:

    file1:line1: warning: `name' is deprecated (declared at file2:line2)

    where file1:line1 is the usual file/line info where the warning
    is being issued for the identifier (name) that has been
    associated with the attribute at file2:line2.

    It is assumed that there will be some appropriate comment in the
    neighborhood of file2:line2 explaining why the name is
    deprecated.

3. The attribute is not allowed for tags since I don't see any
    way to get file/line info from them.

4. The warning is not issued for uses of names that were deprecated
    in the SAME file.

This stuff will obviously be consolidated into appropriate 
documentation when the dust settles.

Ira Ruben
Apple Computer, Inc.
Java & Core Tools

                          ------------------

2001-05-04  Ira Ruben	<ira@apple.com>

	Add __attribute__ ((deprecated)).
	* c-common.c (enum attrs): Add A_DEPRECATED.
	(init_attributes): Add A_DEPRECATED attribute.
	(decl_attributes): Add case for A_DEPRECATED.
	* c-decl.c (c_decode_option): Include deprecated in -Wall.
	(grokdeclarator): Call warn_deprecated_use for types.
	* c-typeck.c (build_component_ref): Call warn_deprecated_use
	for field references.
	(build_external_ref): Call warn_deprecated_use for primaries.
	* diagnostic.c (warn_deprecated_use) New function to issue
	warnings about __attribute__ ((deprecated)) references.
	* flags.h (warn_deprecated_decl): Extern declared.
	* print-tree.c (print_node): Show deprecated flag status.
	* toplev.c (warn_deprecated_decl): Define.
	(W_options): Add "deprecated-declarations".
	* toplev.h (warn_deprecated_use): Extern declared.
	* tree.h (struct tree_common): Define deprecated_flag.
	(TREE_DEPRECATED): New macro to access flag.
	* cp/call.c (build_call): Call warn_deprecated_use for calls.
	* cp/decl.c (grokdeclarator): Call warn_deprecated_use for
	types.
	(cxx_decode_option): Include deprecated in -Wall.
	* cp/lex.c (do_identifier) Call warn_deprecated_use for
	primaries.

Index: c-common.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-common.c,v
retrieving revision 1.232
diff -c -3 -p -r1.232 c-common.c
*** c-common.c	2001/05/01 12:11:31	1.232
--- c-common.c	2001/05/04 22:38:46
*************** enum attrs {A_PACKED, A_NOCOMMON, A_COMM
*** 232,238 ****
   	    A_NO_CHECK_MEMORY_USAGE, A_NO_INSTRUMENT_FUNCTION,
   	    A_CONSTRUCTOR, A_DESTRUCTOR, A_MODE, A_SECTION, A_ALIGNED,
   	    A_UNUSED, A_FORMAT, A_FORMAT_ARG, A_WEAK, A_ALIAS, A_MALLOC,
! 	    A_NO_LIMIT_STACK, A_PURE};

   /* Information about how a function name is generated. */
   struct fname_var_t
--- 232,238 ----
   	    A_NO_CHECK_MEMORY_USAGE, A_NO_INSTRUMENT_FUNCTION,
   	    A_CONSTRUCTOR, A_DESTRUCTOR, A_MODE, A_SECTION, A_ALIGNED,
   	    A_UNUSED, A_FORMAT, A_FORMAT_ARG, A_WEAK, A_ALIAS, A_MALLOC,
! 	    A_NO_LIMIT_STACK, A_PURE, A_DEPRECATED};

   /* Information about how a function name is generated. */
   struct fname_var_t
*************** init_attributes ()
*** 707,712 ****
--- 707,713 ----
     add_attribute (A_MALLOC, "malloc", 0, 0, 1);
     add_attribute (A_NO_LIMIT_STACK, "no_stack_limit", 0, 0, 1);
     add_attribute (A_PURE, "pure", 0, 0, 1);
+   add_attribute (A_DEPRECATED, "deprecated", 0, 0, 0);
   }
   
   /* Default implementation of valid_lang_attribute, below.  By default, there
*************** decl_attributes (node, attributes, prefi
*** 1156,1161 ****
--- 1157,1201 ----
   	  else
   	    DECL_NO_LIMIT_STACK (decl) = 1;
   	  break;
+
+         case A_DEPRECATED:
+           {
+             int warn = 0;
+             char *what = NULL;
+
+ 	    if (is_type)
+ 	      if (decl)
+ 		TREE_DEPRECATED (decl) = 1;
+ 	      else
+ 		/*TREE_DEPRECATED (type) = 1;*/
+ 		/* It would be nice to handle these, but how? */
+ 		warn = 1;
+ 	    else if (TREE_CODE (decl) == PARM_DECL
+ 		     || TREE_CODE (decl) == VAR_DECL
+ 		     || TREE_CODE (decl) == FUNCTION_DECL
+ 		     || TREE_CODE (decl) == FIELD_DECL)
+ 	      TREE_DEPRECATED (decl) = 1;
+ 	    else
+ 	      warn = 1;
+ 	    if (warn)
+ 	      {
+ 		if (TYPE_NAME (type))
+ 		  {
+ 		    if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
+ 		      what = IDENTIFIER_POINTER (TYPE_NAME (node));
+ 		    else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
+ 			     && DECL_NAME (TYPE_NAME (type)))
+ 		      what = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
+ 		  }
+ 		if (what)
+ 		  warning ("`%s' attribute ignored for `%s'",
+ 		  	    IDENTIFIER_POINTER (name), what);
+ 		else
+ 		  warning ("`%s' attribute ignored",
+ 		  		IDENTIFIER_POINTER (name));
+ 	      }
+ 	    break;
+           }
   	}
       }
   }
Index: c-decl.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-decl.c,v
retrieving revision 1.220
diff -c -3 -p -r1.220 c-decl.c
*** c-decl.c	2001/05/01 12:11:31	1.220
--- c-decl.c	2001/05/04 22:38:47
*************** c_decode_option (argc, argv)
*** 781,786 ****
--- 781,787 ----
         warn_main = 2;
         /* Only warn about unknown pragmas that are not in system headers.  */
         warn_unknown_pragmas = 1;
+       warn_deprecated_decl = 1;
       }
     else
       return strings_processed;
*************** grokdeclarator (declarator, declspecs, d
*** 3911,3916 ****
--- 3912,3920 ----
     for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
       {
         register tree id = TREE_VALUE (spec);
+
+       if (id && TREE_DEPRECATED (id))
+         warn_deprecated_use (id);

         if (id == ridpointers[(int) RID_INT])
   	explicit_int = 1;
Index: c-typeck.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-typeck.c,v
retrieving revision 1.118
diff -c -3 -p -r1.118 c-typeck.c
*** c-typeck.c	2001/04/30 23:59:00	1.118
--- c-typeck.c	2001/05/04 22:38:49
*************** build_component_ref (datum, component)
*** 1223,1228 ****
--- 1223,1231 ----
   	TREE_READONLY (ref) = 1;
         if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field))
   	TREE_THIS_VOLATILE (ref) = 1;
+
+       if (TREE_DEPRECATED (field))
+         warn_deprecated_use (field);

         return ref;
       }
*************** build_external_ref (id, fun)
*** 1437,1442 ****
--- 1440,1448 ----
     tree ref;
     tree decl = lookup_name (id);
     tree objc_ivar = lookup_objc_ivar (id);
+
+   if (decl && TREE_DEPRECATED (decl))
+     warn_deprecated_use (decl);

     if (!decl || decl == error_mark_node || C_DECL_ANTICIPATED (decl))
       {
Index: diagnostic.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/diagnostic.c,v
retrieving revision 1.55
diff -c -3 -p -r1.55 diagnostic.c
*** diagnostic.c	2001/03/22 18:48:27	1.55
--- diagnostic.c	2001/05/04 22:38:49
*************** default_diagnostic_finalizer (buffer, dc
*** 1836,1838 ****
--- 1836,1851 ----
   {
     output_destroy_prefix (buffer);
   }
+
+ /* Do NOT warn about the use of deprecated decls that were declared
+    in the same file.  */
+ void
+ warn_deprecated_use (node)
+      tree node;
+ {
+   if (warn_deprecated_decl && node && DECL_P (node))
+     if (strcmp (DECL_SOURCE_FILE (node), input_filename) != 0)
+       warning ("`%s' is deprecated (declared at %s:%d)",
+ 		IDENTIFIER_POINTER (DECL_NAME (node)),
+ 		DECL_SOURCE_FILE (node), DECL_SOURCE_LINE (node));
+ }
Index: flags.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/flags.h,v
retrieving revision 1.62
diff -c -3 -p -r1.62 flags.h
*** flags.h	2001/04/09 14:27:01	1.62
--- flags.h	2001/05/04 22:38:49
*************** extern int warn_padded;
*** 172,177 ****
--- 172,182 ----

   extern int warn_disabled_optimization;

+ /* Nonzero means warn about uses of __attribute__((deprecated))
+    declarations.  */
+
+ extern int warn_deprecated_decl;
+
   /* Nonzero if generating code to do profiling.  */

   extern int profile_flag;
Index: print-tree.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/print-tree.c,v
retrieving revision 1.45
diff -c -3 -p -r1.45 print-tree.c
*** print-tree.c	2001/03/27 02:17:42	1.45
--- print-tree.c	2001/05/04 22:38:49
*************** print_node (file, prefix, node, indent)
*** 293,298 ****
--- 293,300 ----
       fputs (" protected", file);
     if (TREE_STATIC (node))
       fputs (" static", file);
+   if (TREE_DEPRECATED (node))
+     fputs (" deprecated", file);
     if (TREE_LANG_FLAG_0 (node))
       fputs (" tree_0", file);
     if (TREE_LANG_FLAG_1 (node))
Index: toplev.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/toplev.c,v
retrieving revision 1.454
diff -c -3 -p -r1.454 toplev.c
*** toplev.c	2001/05/02 14:31:45	1.454
--- toplev.c	2001/05/04 22:38:50
*************** int warn_disabled_optimization;
*** 1454,1459 ****
--- 1454,1464 ----

   int warn_missing_noreturn;

+ /* Nonzero means warn about uses of __attribute__((deprecated))
+    declarations.  */
+
+ int warn_deprecated_decl = 1;
+
   /* Likewise for -W.  */

   lang_independent_options W_options[] =
*************** lang_independent_options W_options[] =
*** 1485,1491 ****
     {"disabled-optimization", &warn_disabled_optimization, 1,
      "Warn when an optimization pass is disabled"},
     {"missing-noreturn", &warn_missing_noreturn, 1,
!    "Warn about functions which might be candidates for attribute noreturn"}
   };

   /* The following routines are useful in setting all the flags that
--- 1490,1498 ----
     {"disabled-optimization", &warn_disabled_optimization, 1,
      "Warn when an optimization pass is disabled"},
     {"missing-noreturn", &warn_missing_noreturn, 1,
!    "Warn about functions which might be candidates for attribute noreturn"},
!   {"deprecated-declarations", &warn_deprecated_decl, 1,
!    "Warn about uses of __attribute__((deprecated)) declarations"}
   };

   /* The following routines are useful in setting all the flags that
Index: toplev.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/toplev.h,v
retrieving revision 1.61
diff -c -3 -p -r1.61 toplev.h
*** toplev.h	2001/04/19 19:52:16	1.61
--- toplev.h	2001/05/04 22:38:50
*************** extern void error_for_asm		PARAMS ((stru
*** 108,113 ****
--- 108,114 ----
   extern void warning_for_asm		PARAMS ((struct rtx_def *,
   						 const char *, ...))
   					       ATTRIBUTE_PRINTF_2;
+ extern void warn_deprecated_use		PARAMS ((union tree_node *));
   #if defined (_JBLEN) || defined (setjmp)
   extern void set_float_handler PARAMS ((jmp_buf));
   #endif
Index: tree.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/tree.h,v
retrieving revision 1.237
diff -c -3 -p -r1.237 tree.h
*** tree.h	2001/04/27 23:25:20	1.237
--- tree.h	2001/05/04 22:38:52
*************** struct tree_common
*** 146,151 ****
--- 146,152 ----
     unsigned private_flag : 1;
     unsigned protected_flag : 1;
     unsigned bounded_flag : 1;
+   unsigned deprecated_flag : 1;

     unsigned lang_flag_0 : 1;
     unsigned lang_flag_1 : 1;
*************** struct tree_common
*** 264,269 ****
--- 265,275 ----
   	   expressions, VAR_DECL, PARM_DECL, FIELD_DECL, FUNCTION_DECL
          TYPE_BOUNDED in
   	   ..._TYPE
+
+    deprecated_flag:
+
+ 	TREE_DEPRECATED in IDENTIFIER_NODE
+
   */

   /* Define accessors for the fields that all tree nodes have
*************** extern void tree_class_check_failed PARA
*** 633,638 ****
--- 639,648 ----
      argument type(s).  */

   #define TREE_BOUNDED(NODE) ((NODE)->common.bounded_flag)
+
+ /* Nonzero in a IDENTIFIER_NODE if the use of the name is defined as a
+    deprecated feature by __attribute__((deprecated))  */
+ #define TREE_DEPRECATED(NODE) ((NODE)->common.deprecated_flag)

   /* These flags are available for each language front end to use 
internally.  */
   #define TREE_LANG_FLAG_0(NODE) ((NODE)->common.lang_flag_0)
Index: cp/call.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/call.c,v
retrieving revision 1.269
diff -c -3 -p -r1.269 call.c
*** call.c	2001/05/02 14:38:29	1.269
--- call.c	2001/05/04 22:38:53
*************** build_call (function, parms)
*** 401,406 ****
--- 401,409 ----
        throw without being declared throw().  */
     nothrow = ((decl && TREE_NOTHROW (decl))
   	     || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function))));
+
+   if (decl && TREE_DEPRECATED (decl))
+     warn_deprecated_use (decl);

     if (decl && DECL_CONSTRUCTOR_P (decl))
       is_constructor = 1;
Index: cp/decl.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/decl.c,v
retrieving revision 1.782
diff -c -3 -p -r1.782 decl.c
*** decl.c	2001/05/04 06:28:52	1.782
--- decl.c	2001/05/04 22:38:56
*************** grokdeclarator (declarator, declspecs, d
*** 9953,9958 ****
--- 9953,9961 ----
   	return 0;

         id = TREE_VALUE (spec);
+
+       if (id && TREE_DEPRECATED (id))
+         warn_deprecated_use (id);

         if (TREE_CODE (id) == IDENTIFIER_NODE)
   	{
Index: cp/decl2.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/decl2.c,v
retrieving revision 1.464
diff -c -3 -p -r1.464 decl2.c
*** decl2.c	2001/05/04 03:03:39	1.464
--- decl2.c	2001/05/04 22:38:57
*************** cxx_decode_option (argc, argv)
*** 747,752 ****
--- 747,753 ----
   	  /* Only warn about unknown pragmas that are not in system
   	     headers.  */
   	  warn_unknown_pragmas = 1;
+ 	  warn_deprecated_decl = 1;

   	  /* C++-specific warnings.  */
   	  warn_ctor_dtor_privacy = setting;
Index: cp/lex.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/lex.c,v
retrieving revision 1.243
diff -c -3 -p -r1.243 lex.c
*** lex.c	2001/04/24 08:22:05	1.243
--- lex.c	2001/05/04 22:38:57
*************** do_identifier (token, parsing, args)
*** 1219,1224 ****
--- 1219,1227 ----
   {
     register tree id;
     int lexing = (parsing == 1);
+
+   if (token && TREE_DEPRECATED (token))
+     warn_deprecated_use (token);

     if (! lexing)
       id = lookup_name (token, 0);


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]