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]
Other format: [Raw text]

Re: PING: [PATCH: c++/diagnostic/18313] Warn for pointless qualifiers on return type


On Tuesday, 14. November 2006 07:51, Gabriel Dos Reis wrote:

> I think we should warn for non-dependent case, and warn only at
> instantiation time for the dependent case.

Here's a patch that implements the requested behaviour, bootstrapped, 
regtested against r118902 on i686-suse-linux with no additional failures. 

Ok for mainline, assuming that it regtests fine as soon as mainline bootstraps 
again?

2006-11-17  Dirk Mueller  <dmueller@suse.de>

        PR c++/18313
        * decl.c (grokdeclarator): Warn for type qualifiers on return
        type for non-dependent types.
        * pt.c (tsubst_function_type): Warn for type qualifiers on
        return type for dependent types.

        * g++.dg/warn/Wreturn-type-4.C: New testcase.

===================================================================
--- cp/decl.c	(revision 118902)
+++ cp/decl.c	(working copy)
@@ -7526,13 +7526,21 @@ grokdeclarator (const cp_declarator *dec
 	  {
 	    tree arg_types;
 	    int funcdecl_p;
+	    bool set_no_warning = false;
 
 	    /* Declaring a function type.
 	       Make sure we have a valid type for the function to return.  */
 
-	    /* We now know that the TYPE_QUALS don't apply to the
-	       decl, but to its return type.  */
-	    type_quals = TYPE_UNQUALIFIED;
+	    if (type_quals != TYPE_UNQUALIFIED)
+	      {
+		if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type))
+		  warning (OPT_Wreturn_type,
+			   "type qualifiers ignored on function return type");
+		/* We now know that the TYPE_QUALS don't apply to the
+		   decl, but to its return type.  */
+		type_quals = TYPE_UNQUALIFIED;
+		set_no_warning = true;
+	      }
 
 	    /* Warn about some types functions can't return.  */
 
@@ -7645,6 +7653,8 @@ grokdeclarator (const cp_declarator *dec
 	      }
 
 	    type = build_function_type (type, arg_types);
+	    if (set_no_warning)
+	      TREE_NO_WARNING (TREE_TYPE (type)) = 1;
 	  }
 	  break;
 
Index: cp/pt.c
===================================================================
--- cp/pt.c	(revision 118902)
+++ cp/pt.c	(working copy)
@@ -7123,6 +7123,12 @@ tsubst_function_type (tree t,
   if (arg_types == error_mark_node)
     return error_mark_node;
 
+  if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED
+      && !TREE_NO_WARNING (TREE_TYPE (t))
+      && (SCALAR_TYPE_P (return_type) || VOID_TYPE_P (return_type)))
+    warning (OPT_Wreturn_type,
+            "type qualifiers ignored on function return type");
+
   /* Construct a new type node and return it.  */
   if (TREE_CODE (t) == FUNCTION_TYPE)
     fntype = build_function_type (return_type, arg_types);
Index: testsuite/g++.dg/warn/Wreturn-type-4.C
===================================================================
--- testsuite/g++.dg/warn/Wreturn-type-4.C	(revision 0)
+++ testsuite/g++.dg/warn/Wreturn-type-4.C	(revision 0)
@@ -0,0 +1,43 @@
+/* PR c++/18313 */
+/* { dg-do compile } */
+/* { dg-options "-Wreturn-type" } */
+
+volatile void bar(); /* { dg-warning "type qualifiers ignored" } */
+
+struct A
+{
+    const int bla(); /* { dg-warning "type qualifiers ignored" } */
+    static const A getA(); /* { dg-bogus "type qualifiers" } */
+};
+
+template<typename T> const T getfoo(const T def) /* { dg-bogus "type 
qualifiers ignored" } */
+{ return def; } 
+
+template<typename T> class Pair
+{
+    public:
+        T getLeft() const { return T(); }   /* { dg-warning "type qualifiers 
ignored" } */
+        const T getRight() const { return T(); } /* { dg-bogus "type 
qualifiers ignored" } */
+};
+
+template <typename T> struct S {
+    const int f();                     /* { dg-warning "type qualifiers 
ignored" } */
+    const T g();                       /* { dg-bogus "type qualifiers 
ignored" } */
+    T h();
+};
+
+int* testtemplate()
+{
+    int i;
+
+    Pair<const int> a;
+
+    a.getLeft();
+    a.getRight();
+
+    S<bool> b;
+    b.h();              /* { dg-bogus "type qualifiers ignored" } */
+    b.g();              /* { dg-bogus "type qualifiers ignored" } */
+
+    return getfoo<int*>(&i);
+}


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