public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* ObjC/ObjC++: updated documentation on supported standards, and added -fobjc-std=objc1
@ 2010-11-02 10:07 Nicola Pero
  2010-11-02 10:13 ` IainS
  2010-11-02 10:21 ` Mike Stump
  0 siblings, 2 replies; 8+ messages in thread
From: Nicola Pero @ 2010-11-02 10:07 UTC (permalink / raw)
  To: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 3274 bytes --]

This patch:

 * updates the documentation on the version of Objective-C/Objective-C++ 
supported by the compiler;

 * adds a new -fobjc-std=objc1 option which causes the compiler to accept only 
the Objective-C 1.0 syntax (which for clarity of definition I defined as the 
Objective-C syntax accepted by GCC 4.0);

 * adds testcases for the new option.

The reason that this new option is useful is that some projects have
a large Objective-C codebase and require the codebase to compile with different
versions of GCC, including old ones.  It is handy to be able to have a flag
that will prevent people with new compilers from committing code that uses
a syntax not supported by old compilers.

Of course, the default is to allow all the Objective-C 2.0 extensions.  This flag is
only for people who have a specific reason to disable the extensions. :-)

The option name itself is not set in stone; suggestions I welcome.  I like 
-fobjc-std=xxx (as opposed to, say, -fobjc-language-version=1 / -fobjc-language-version=2) 
for consistency with -std=xxx and because we don't know how future versions of the language 
may be named or presented so having a free string there helps to be adaptable (in the same 
way as it works for -std=xxx in C/C++).

About the short string used to mean 'Objective-C 1.0', I chose 'objc1'.  An alternative
would be choosing 'gcc40', so the option could become -fobjc-std=gcc40.  That may be 
more accurate, but less obvious (or maybe not).

I'm happy to take suggestions on that.

Else, Ok to commit to trunk ?

Thanks

In gcc/:
2010-11-02  Nicola Pero  <nicola.pero@meta-innovation.com>

        Implemented -fobjc-std=objc1 flag.
        * doc/invoke.texi (Option Summary): Document -fobjc-std=objc1
        option.
        * doc/standards.texi (Standards): Updated the section on
        Objective-C; mention support for Objective-C 2.0 features and the
        new -fstd-objc=objc1 flag to disable them.

In gcc/c-family/:
2010-11-02  Nicola Pero  <nicola.pero@meta-innovation.com>

        Implemented -fobjc-std=objc1 flag.
        * c.opt (fobjc-std=objc1): New option.

In gcc/objc/:
2010-11-02  Nicola Pero  <nicola.pero@meta-innovation.com>

        Implemented -fobjc-std=objc1 flag.
        * objc-act.c (objc_start_class_interface): If attributes are
        specified when flag_objc1_only is set, print an error.
        (objc_start_category_interface): Same change.
        (objc_start_protocol): Same change.
        (objc_add_method_declaration): Same change.
        (objc_start_method_definition): Same change.
        (objc_build_keyword_decl): Same change.
        (objc_set_method_opt): If flag_objc1_only is set, print an error.
        (objc_add_property_declaration): Same change.
        (objc_add_synthesize_declaration): Same change.
        (objc_add_dynamic_declaration): Same change.
        (objc_finish_foreach_loop): Same change.
        (objc_maybe_build_component_ref): If flag_objc1_only is set,
        return immediately.

In gcc/testsuite/:
2010-11-02  Nicola Pero  <nicola.pero@meta-innovation.com>

        Implemented -fobjc-std=objc1 flag.
        * objc.dg/fobjc-std-1.mm: New.
        * obj-c++.dg/fobjc-std-1.mm: New.

[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 17319 bytes --]

Index: gcc/doc/standards.texi
===================================================================
--- gcc/doc/standards.texi	(revision 166154)
+++ gcc/doc/standards.texi	(working copy)
@@ -209,11 +209,13 @@ no C++ language dialect options are given, is @opt
 @cindex Objective-C++
 
 GCC supports ``traditional'' Objective-C (also known as ``Objective-C
-1.0''), and contains support for the Objective-C exception and
-synchronization syntax.  At the time of writing, it has no support for
-``Objective-C 2.0'' language extensions.  GCC supports Objective-C++
-and features available in Objective-C are also available in
-Objective-C++@.
+1.0'') and contains support for the Objective-C exception and
+synchronization syntax.  It has also support for a number of
+``Objective-C 2.0'' language extensions, including properties, fast
+enumeration (only for Objective-C), method attributes and the
+@@optional and @@required keywords in protocols.  GCC supports
+Objective-C++ and features available in Objective-C are also available
+in Objective-C++@.
 
 GCC by default uses the GNU Objective-C runtime library, which is part
 of GCC and is not the same as the Apple/NeXT Objective-C runtime
@@ -242,12 +244,25 @@ not documentation on the newer ``Objective-C 2.0''
 
 The Objective-C exception and synchronization syntax (that is, the
 keywords @@try, @@throw, @@catch, @@finally and @@synchronized) is
-fully supported by GCC and is enabled with the option
+supported by GCC and is enabled with the option
 @option{-fobjc-exceptions}.  The syntax is briefly documented in this
 manual and in the Objective-C 2.0 manuals from Apple.
 
-At the time of writing, GCC supports no Objective-C 2.0 features.  The
-authoritative manual on Objective-C 2.0 is available from Apple:
+The Objective-C 2.0 language extensions and features are automatically
+enabled; they include properties (via the @@property, @@synthesize and
+@@dynamic keywords), fast enumeration (not available in
+Objective-C++), attributes for methods (such as deprecated, noreturn,
+sentinel, format), the unused attribute for method arguments, the
+@@package keyword for instance variables and the @@optional and
+@@required keywords in protocols.  You can disable all these
+Objective-C 2.0 language extensions with the option
+@option{-fobjc-std=objc1}, which causes the compiler to recognize the
+same Objective-C language syntax recognized by GCC 4.0, and to produce
+an error if one of the new features is used.
+
+GCC has currently no support for non-fragile instance variables.
+
+The authoritative manual on Objective-C 2.0 is available from Apple:
 @itemize
 @item
 @uref{http://developer.apple.com/@/mac/@/library/@/documentation/@/Cocoa/@/Conceptual/@/ObjectiveC/}
Index: gcc/doc/invoke.texi
===================================================================
--- gcc/doc/invoke.texi	(revision 166154)
+++ gcc/doc/invoke.texi	(working copy)
@@ -212,6 +212,7 @@ Objective-C and Objective-C++ Dialects}.
 -fobjc-direct-dispatch @gol
 -fobjc-exceptions @gol
 -fobjc-gc @gol
+-fobjc-std=objc1 @gol
 -freplace-objc-classes @gol
 -fzero-link @gol
 -gen-decls @gol
@@ -2591,6 +2592,17 @@ programs.  This option is only available with the
 GNU runtime has a different garbage collection implementation that
 does not require special compiler flags.
 
+@item -fobjc-std=objc1
+@opindex fobjc-std
+Conform to the language syntax of Objective-C 1.0, the language
+recognized by GCC 4.0.  This only affects the Objective-C additions to
+the C/C++ language; it does not affect conformance to C/C++ standards,
+which is controlled by the separate C/C++ dialect option flags.  When
+this option is used with the Objective-C or Objective-C++ compiler,
+any Objective-C syntax that is not recognized by GCC 4.0 is rejected.
+This is useful if you need to make sure that your Objective-C code can
+be compiled with older versions of GCC.
+
 @item -freplace-objc-classes
 @opindex freplace-objc-classes
 Emit a special marker instructing @command{ld(1)} not to statically link in
Index: gcc/c-family/c.opt
===================================================================
--- gcc/c-family/c.opt	(revision 166154)
+++ gcc/c-family/c.opt	(working copy)
@@ -870,6 +870,10 @@ fobjc-sjlj-exceptions
 ObjC ObjC++ Var(flag_objc_sjlj_exceptions) Init(-1)
 Enable Objective-C setjmp exception handling runtime
 
+fobjc-std=objc1
+ObjC ObjC++ Var(flag_objc1_only)
+Conform to the Objective-C 1.0 language as implemented in GCC 4.0
+
 fopenmp
 C ObjC C++ ObjC++ Var(flag_openmp)
 Enable OpenMP (implies -frecursive in Fortran)
Index: gcc/c-family/ChangeLog
===================================================================
--- gcc/c-family/ChangeLog	(revision 166154)
+++ gcc/c-family/ChangeLog	(working copy)
@@ -1,3 +1,8 @@
+2010-11-02  Nicola Pero  <nicola.pero@meta-innovation.com>
+
+	Implemented -fobjc-std=objc1 flag.
+	* c.opt (fobjc-std=objc1): New option.
+
 2010-11-01  Nicola Pero  <nicola.pero@meta-innovation.com>
 
 	Implemented format and noreturn attributes for Objective-C methods.
Index: gcc/objc/objc-act.c
===================================================================
--- gcc/objc/objc-act.c	(revision 166154)
+++ gcc/objc/objc-act.c	(working copy)
@@ -686,9 +686,14 @@ objc_start_class_interface (tree klass, tree super
 			    tree protos, tree attributes)
 {
   if (attributes)
-    warning_at (input_location, OPT_Wattributes, 
-		"class attributes are not available in this version"
-		" of the compiler, (ignored)");
+    {
+      if (flag_objc1_only)
+	error_at (input_location, "class attributes are not available in Objective-C 1.0");
+      else
+	warning_at (input_location, OPT_Wattributes, 
+		    "class attributes are not available in this version"
+		    " of the compiler, (ignored)");
+    }
   objc_interface_context
     = objc_ivar_context
     = start_class (CLASS_INTERFACE_TYPE, klass, super_class, protos);
@@ -700,9 +705,14 @@ objc_start_category_interface (tree klass, tree ca
 			       tree protos, tree attributes)
 {
   if (attributes)
-    warning_at (input_location, OPT_Wattributes, 
-		"category attributes are not available in this version"
-		" of the compiler, (ignored)");
+    {
+      if (flag_objc1_only)
+	error_at (input_location, "category attributes are not available in Objective-C 1.0");
+      else
+	warning_at (input_location, OPT_Wattributes, 
+		    "category attributes are not available in this version"
+		    " of the compiler, (ignored)");
+    }
   objc_interface_context
     = start_class (CATEGORY_INTERFACE_TYPE, klass, categ, protos);
   objc_ivar_chain
@@ -713,9 +723,14 @@ void
 objc_start_protocol (tree name, tree protos, tree attributes)
 {
   if (attributes)
-    warning_at (input_location, OPT_Wattributes, 
-		"protocol attributes are not available in this version"
-		" of the compiler, (ignored)");
+    {
+      if (flag_objc1_only)
+	error_at (input_location, "protocol attributes are not available in Objective-C 1.0");	
+      else
+	warning_at (input_location, OPT_Wattributes, 
+		    "protocol attributes are not available in this version"
+		    " of the compiler, (ignored)");
+    }
   objc_interface_context
     = start_protocol (PROTOCOL_INTERFACE_TYPE, name, protos);
   objc_method_optional_flag = false;
@@ -783,13 +798,21 @@ void
 objc_set_visibility (objc_ivar_visibility_kind visibility)
 {
   if (visibility == OBJC_IVAR_VIS_PACKAGE)
-    warning (0, "%<@package%> presently has the same effect as %<@public%>");
+    {
+      if (flag_objc1_only)
+	error ("%<@package%> is not available in Objective-C 1.0");
+      else
+	warning (0, "%<@package%> presently has the same effect as %<@public%>");
+    }
   objc_ivar_visibility = visibility;
 }
 
 void
 objc_set_method_opt (bool optional)
 {
+  if (flag_objc1_only)
+    error_at (input_location, "@optional/@required are not available in Objective-C 1.0");	
+
   objc_method_optional_flag = optional;
   if (!objc_interface_context 
       || TREE_CODE (objc_interface_context) != PROTOCOL_INTERFACE_TYPE)
@@ -827,6 +850,9 @@ objc_add_property_declaration (location_t location
   bool property_readonly = false;
   objc_property_assign_semantics property_assign_semantics = OBJC_PROPERTY_ASSIGN;
 
+  if (flag_objc1_only)
+    error_at (input_location, "%<@property%> is not available in Objective-C 1.0");
+
   if (parsed_property_readonly && parsed_property_readwrite)
     {
       error_at (location, "%<readonly%> attribute conflicts with %<readwrite%> attribute");
@@ -1069,6 +1095,11 @@ objc_maybe_build_component_ref (tree object, tree
   tree x = NULL_TREE;
   tree rtype;
 
+  /* If we are in Objective-C 1.0 mode, properties are not
+     available.  */
+  if (flag_objc1_only)
+    return NULL_TREE;
+
   /* Try to determine quickly if 'object' is an Objective-C object or
      not.  If not, return.  */
   if (object == NULL_TREE || object == error_mark_node 
@@ -1232,6 +1263,9 @@ objc_add_method_declaration (bool is_class_method,
       fatal_error ("method declaration not in @interface context");
     }
 
+  if (flag_objc1_only && attributes)
+    error_at (input_location, "method attributes are not available in Objective-C 1.0");
+
   objc_decl_method_attributes (&decl, attributes, 0);
   objc_add_method (objc_interface_context,
 		   decl,
@@ -6518,6 +6552,9 @@ objc_build_keyword_decl (tree key_name, tree arg_t
 {
   tree keyword_decl;
 
+  if (flag_objc1_only && attributes)
+    error_at (input_location, "method argument attributes are not available in Objective-C 1.0");
+
   /* If no type is specified, default to "id".  */
   arg_type = adjust_type_for_id_default (arg_type);
 
@@ -9225,6 +9262,9 @@ objc_add_synthesize_declaration (location_t locati
 {
   tree interface, chain;
 
+  if (flag_objc1_only)
+    error_at (input_location, "%<@synthesize%> is not available in Objective-C 1.0");
+
   if (property_and_ivar_list == error_mark_node)
     return;
 
@@ -9345,6 +9385,9 @@ objc_add_dynamic_declaration (location_t location,
 {
   tree interface, chain;
 
+  if (flag_objc1_only)
+    error_at (input_location, "%<@dynamic%> is not available in Objective-C 1.0");
+
   if (property_list == error_mark_node)
     return;
 
@@ -12049,6 +12092,9 @@ objc_finish_foreach_loop (location_t location, tre
   tree t;
   int i;
 
+  if (flag_objc1_only)
+    error_at (location, "fast enumeration is not available in Objective-C 1.0");
+
   if (object_expression == error_mark_node)
     return;
 
Index: gcc/objc/ChangeLog
===================================================================
--- gcc/objc/ChangeLog	(revision 166154)
+++ gcc/objc/ChangeLog	(working copy)
@@ -1,3 +1,23 @@
+2010-11-02  Nicola Pero  <nicola.pero@meta-innovation.com>
+
+	Implemented -fobjc-std=objc1 flag.
+	* objc-act.c (objc_start_class_interface): If attributes are
+	specified when flag_objc1_only is set, print an error.
+	(objc_start_category_interface): Same change.
+	(objc_start_protocol): Same change.
+	(objc_add_method_declaration): Same change.
+	(objc_start_method_definition): Same change.
+	(objc_build_keyword_decl): Same change.
+	(objc_set_visibility): If OBJC_IVAR_VIS_PACKAGE is used when
+	flag_objc1_set is set, print an error.
+	(objc_set_method_opt): If flag_objc1_only is set, print an error.
+	(objc_add_property_declaration): Same change.
+	(objc_add_synthesize_declaration): Same change.
+	(objc_add_dynamic_declaration): Same change.
+	(objc_finish_foreach_loop): Same change.
+	(objc_maybe_build_component_ref): If flag_objc1_only is set,
+	return immediately.
+
 2010-11-01  Nicola Pero  <nicola.pero@meta-innovation.com>
 
 	Implemented format and noreturn attributes for Objective-C methods.
Index: gcc/ChangeLog
===================================================================
--- gcc/ChangeLog	(revision 166154)
+++ gcc/ChangeLog	(working copy)
@@ -1,3 +1,12 @@
+2010-11-02  Nicola Pero  <nicola.pero@meta-innovation.com>
+
+	Implemented -fobjc-std=objc1 flag.
+	* doc/invoke.texi (Option Summary): Document -fobjc-std=objc1
+	option.
+	* doc/standards.texi (Standards): Updated the section on
+	Objective-C; mention support for Objective-C 2.0 features and the
+	new -fstd-objc=objc1 flag to disable them.
+
 2010-11-01  Paul Koning  <ni1d@arrl.net>
 
 	* config/pdp11/pdp11.md: Use iterators.
Index: gcc/testsuite/ChangeLog
===================================================================
--- gcc/testsuite/ChangeLog	(revision 166154)
+++ gcc/testsuite/ChangeLog	(working copy)
@@ -1,3 +1,9 @@
+2010-11-02  Nicola Pero  <nicola.pero@meta-innovation.com>
+
+	Implemented -fobjc-std=objc1 flag.
+	* objc.dg/fobjc-std-1.mm: New.	
+	* obj-c++.dg/fobjc-std-1.mm: New.
+
 2010-11-01  Nicola Pero  <nicola.pero@meta-innovation.com>
 
 	Implemented format and noreturn attributes for Objective-C methods.
Index: gcc/testsuite/objc.dg/fobjc-std-1.m
===================================================================
--- gcc/testsuite/objc.dg/fobjc-std-1.m	(revision 0)
+++ gcc/testsuite/objc.dg/fobjc-std-1.m	(revision 0)
@@ -0,0 +1,54 @@
+/* Test warnings when using -fobjc-std=objc1.  */
+/* { dg-do compile } */
+/* { dg-options "-fobjc-std=objc1" } */
+
+#include <objc/objc.h>
+
+__attribute__ ((deprecated))
+@interface MyRootClass
+{  /* { dg-error "class attributes are not available in Objective.C 1.0" } */
+  Class isa;
+@package /* { dg-error "not available in Objective.C 1.0" } */
+  int a;
+  int b;
+}
++ (id) alloc __attribute__ ((deprecated)); /* { dg-error "not available in Objective.C 1.0" } */
+- (id) init;
+- (id) testMe: (id) __attribute__((unused)) argument; /* { dg-error "not available in Objective.C 1.0" } */
+@property (nonatomic) int a; /* { dg-error "not available in Objective.C 1.0" } */
+@property (nonatomic) int b; /* { dg-error "not available in Objective.C 1.0" } */
+@end
+
+@implementation MyRootClass
++ (id) alloc { return self; }
+- (id) init  { return self; }
+- (id) testMe: (id) __attribute__((unused)) argument { return self; } /* { dg-error "not available in Objective.C 1.0" } */
+@synthesize a; /* { dg-error "not available in Objective.C 1.0" } */
+@dynamic b; /* { dg-error "not available in Objective.C 1.0" } */
+@end
+
+__attribute__ ((deprecated))
+@protocol MyProtocol
+- (id) test; /* { dg-error "protocol attributes are not available in Objective.C 1.0" } */
+@required /* { dg-error "not available in Objective.C 1.0" } */
+- (id) variable __attribute__ ((deprecated)); /* { dg-error "not available in Objective.C 1.0" } */
+@optional /* { dg-error "not available in Objective.C 1.0" } */
+@end
+
+@interface MyRootClass (NSFastEnumeration)
+- (unsigned long)countByEnumeratingWithState: (struct __objcFastEnumerationState *)state
+                                     objects:(id *)stackbuf 
+                                       count:(unsigned int)len;
+@end
+
+@class NSArray;
+
+int array_length (NSArray *array)
+{
+  int i = 0;
+
+  for (id object in array) /* { dg-error "not available in Objective.C 1.0" } */
+    i++;
+      
+  return i;
+}
Index: gcc/testsuite/obj-c++.dg/fobjc-std-1.mm
===================================================================
--- gcc/testsuite/obj-c++.dg/fobjc-std-1.mm	(revision 0)
+++ gcc/testsuite/obj-c++.dg/fobjc-std-1.mm	(revision 0)
@@ -0,0 +1,55 @@
+/* Test warnings when using -fobjc-std=objc1.  */
+/* { dg-do compile } */
+/* { dg-options "-fobjc-std=objc1" } */
+
+#include <objc/objc.h>
+
+__attribute__ ((deprecated))
+@interface MyRootClass /* { dg-error "class attributes are not available in Objective.C 1.0" } */
+{
+  Class isa; /* { dg-error ".@package. is not available in Objective.C 1.0" } */
+@package
+  int a;
+  int b;
+}
++ (id) alloc __attribute__ ((deprecated)); /* { dg-error "not available in Objective.C 1.0" } */
+- (id) init;
+- (id) testMe: (id) __attribute__((unused)) argument; /* { dg-error "not available in Objective.C 1.0" } */
+@property (nonatomic) int a; /* { dg-error "not available in Objective.C 1.0" } */
+@property (nonatomic) int b; /* { dg-error "not available in Objective.C 1.0" } */
+@end
+
+@implementation MyRootClass
++ (id) alloc { return self; }
+- (id) init  { return self; }
+- (id) testMe: (id) __attribute__((unused)) argument { return self; } /* { dg-error "not available in Objective.C 1.0" } */
+@synthesize a; /* { dg-error "not available in Objective.C 1.0" } */
+@dynamic b; /* { dg-error "not available in Objective.C 1.0" } */
+@end
+
+__attribute__ ((deprecated))
+@protocol MyProtocol /* { dg-error "protocol attributes are not available in Objective.C 1.0" } */
+
+@required /* { dg-error "not available in Objective.C 1.0" } */
+- (id) variable __attribute__ ((deprecated)); /* { dg-error "not available in Objective.C 1.0" } */
+@optional /* { dg-error "not available in Objective.C 1.0" } */
+@end
+#if 0 /* fast enumeration is not implemented even in Objective-C 2.0 */
+@interface MyRootClass (NSFastEnumeration)
+- (unsigned long)countByEnumeratingWithState: (struct __objcFastEnumerationState *)state
+                                     objects:(id *)stackbuf 
+                                       count:(unsigned int)len;
+@end
+
+@class NSArray;
+
+int array_length (NSArray *array)
+{
+  int i = 0;
+
+  for (id object in array) /*  dg-error "not available in Objective.C 1.0"  */
+    i++;
+      
+  return i;
+}
+#endif
\ No newline at end of file

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2010-11-02 19:54 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-11-02 10:07 ObjC/ObjC++: updated documentation on supported standards, and added -fobjc-std=objc1 Nicola Pero
2010-11-02 10:13 ` IainS
2010-11-02 13:48   ` Nicola Pero
2010-11-02 14:10     ` IainS
2010-11-02 17:59       ` Mike Stump
2010-11-02 19:19       ` Nicola Pero
2010-11-02 20:03         ` IainS
2010-11-02 10:21 ` Mike Stump

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).