aboutsummaryrefslogtreecommitdiff
path: root/gcc/c-family
diff options
context:
space:
mode:
authorIain Sandoe <iain@sandoe.co.uk>2020-10-25 07:49:16 +0000
committerIain Sandoe <iain@sandoe.co.uk>2020-11-13 10:39:48 +0000
commit3fe07cdec8c79bce53ea5aeb8e607df6eb5c8c2c (patch)
tree989f0b35f1a61b36a0bfc388398ec51850bf1de3 /gcc/c-family
parent4852c3266ec0887316b9fbbb106b8540e3e948d6 (diff)
downloadgcc-3fe07cdec8c79bce53ea5aeb8e607df6eb5c8c2c.zip
gcc-3fe07cdec8c79bce53ea5aeb8e607df6eb5c8c2c.tar.gz
gcc-3fe07cdec8c79bce53ea5aeb8e607df6eb5c8c2c.tar.bz2
C-family, Objective-C [1/3] : Implement Wobjc-root-class [PR77404].
This warning catches the case that the user has left the superclass specification from a class interface. Root classes are, of course, permitted and an attribute is added to mark these so that the diagnostic is suppressed. The warning and attribute spellings have been kept in sync with the language reference implementation (clang). The diagnostic location information present in the objective-c interface and class definitions is relatively poor. This patch adds a location for the class name to the interface and makes use of it in existing warnings. Part 1 is the changes to code and added tests. Many entries in the testsuite make use of root classes so there are a large number of mechanical changes there adding "-Wno-objc-root-class" to the options. The test changes are parts 2 (objective-c) and 3 (objective-c++) in the patch series. gcc/c-family/ChangeLog: PR objc/77404 * c-attribs.c (handle_objc_root_class_attribute): New * c-objc.h (objc_start_class_interface): Add a location value for the position of the class name. * c.opt: Add Wobjc-root-class. * stub-objc.c (objc_start_class_interface): Add a location value for the position of the class name. gcc/c/ChangeLog: PR objc/77404 * c-parser.c (c_parser_objc_class_definition): Pass the location of the class name to the interface declaration. gcc/cp/ChangeLog: PR objc/77404 * parser.c (cp_parser_objc_class_interface): Pass the location of the class name to the interface declaration. gcc/objc/ChangeLog: PR objc/77404 * objc-act.c (objc_start_class_interface): Accept the location of the class name, use it in existing diagnostic. (start_class): Accept obj_root_class type attributes. Warn when the interface for an implementation does not contain a super class (unless the diagnostic is suppressed by the the command line flag or the objc_root_class type attribute). gcc/testsuite/ChangeLog: PR objc/77404 * objc.dg/attributes/root-class-01.m: New test. * objc.dg/root-class-00.m: New test. * obj-c++.dg/attributes/root-class-01.mm: New test. * obj-c++.dg/root-class-00.mm: New test. gcc/ChangeLog: PR objc/77404 * doc/extend.texi: Document the objc_root_class attribute. * doc/invoke.texi: Document -Wobjc-root-class.
Diffstat (limited to 'gcc/c-family')
-rw-r--r--gcc/c-family/c-attribs.c19
-rw-r--r--gcc/c-family/c-objc.h2
-rw-r--r--gcc/c-family/c.opt5
-rw-r--r--gcc/c-family/stub-objc.c1
4 files changed, 26 insertions, 1 deletions
diff --git a/gcc/c-family/c-attribs.c b/gcc/c-family/c-attribs.c
index f168082..24bcd70 100644
--- a/gcc/c-family/c-attribs.c
+++ b/gcc/c-family/c-attribs.c
@@ -158,6 +158,7 @@ static tree handle_patchable_function_entry_attribute (tree *, tree, tree,
int, bool *);
static tree handle_copy_attribute (tree *, tree, tree, int, bool *);
static tree handle_nsobject_attribute (tree *, tree, tree, int, bool *);
+static tree handle_objc_root_class_attribute (tree *, tree, tree, int, bool *);
/* Helper to define attribute exclusions. */
#define ATTR_EXCL(name, function, type, variable) \
@@ -513,6 +514,8 @@ const struct attribute_spec c_common_attribute_table[] =
/* Attributes used by Objective-C. */
{ "NSObject", 0, 0, true, false, false, false,
handle_nsobject_attribute, NULL },
+ { "objc_root_class", 0, 0, true, false, false, false,
+ handle_objc_root_class_attribute, NULL },
{ NULL, 0, 0, false, false, false, false, NULL, NULL }
};
@@ -5163,6 +5166,22 @@ handle_nsobject_attribute (tree *node, tree name, tree args,
return NULL_TREE;
}
+/* Handle a "objc_root_class" attributes; arguments as in
+ struct attribute_spec.handler. */
+
+static tree
+handle_objc_root_class_attribute (tree */*node*/, tree name, tree /*args*/,
+ int /*flags*/, bool *no_add_attrs)
+{
+ /* This has no meaning outside Objective-C. */
+ if (!c_dialect_objc())
+ warning (OPT_Wattributes, "%qE is only applicable to Objective-C"
+ " class interfaces, attribute ignored", name);
+
+ *no_add_attrs = true;
+ return NULL_TREE;
+}
+
/* Attempt to partially validate a single attribute ATTR as if
it were to be applied to an entity OPER. */
diff --git a/gcc/c-family/c-objc.h b/gcc/c-family/c-objc.h
index 6e96731..9414aec 100644
--- a/gcc/c-family/c-objc.h
+++ b/gcc/c-family/c-objc.h
@@ -124,7 +124,7 @@ extern tree objc_get_protocol_qualified_type (tree, tree);
extern tree objc_get_class_reference (tree);
extern tree objc_get_class_ivars (tree);
extern bool objc_detect_field_duplicates (bool);
-extern void objc_start_class_interface (tree, tree, tree, tree);
+extern void objc_start_class_interface (tree, location_t, tree, tree, tree);
extern void objc_start_category_interface (tree, tree, tree, tree);
extern void objc_start_protocol (tree, tree, tree);
extern void objc_continue_interface (void);
diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index fe16357..a008363 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -1002,6 +1002,11 @@ Enum(cpp_normalize_level) String(id) Value(normalized_identifier_C)
EnumValue
Enum(cpp_normalize_level) String(nfc) Value(normalized_C)
+Wobjc-root-class
+ObjC ObjC++ Var(warn_objc_root_class) Warning Init(1)
+Warn if a class interface has no superclass. Root classes may use an attribute
+to suppress this warning.
+
Wold-style-cast
C++ ObjC++ Var(warn_old_style_cast) Warning
Warn if a C-style cast is used in a program.
diff --git a/gcc/c-family/stub-objc.c b/gcc/c-family/stub-objc.c
index 2f53557..26941aa 100644
--- a/gcc/c-family/stub-objc.c
+++ b/gcc/c-family/stub-objc.c
@@ -137,6 +137,7 @@ objc_set_method_opt (bool ARG_UNUSED (optional))
void
objc_start_class_interface (tree ARG_UNUSED (name),
+ location_t /*name_loc*/,
tree ARG_UNUSED (super),
tree ARG_UNUSED (protos),
tree ARG_UNUSED (attribs))