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]

C++ PATCH to add -fno-pretty-templates


The new default behavior of omitting default template arguments may be undesirable in some situations, and some people have complained about our pretty-printing of function template specializations being obfuscatory, so this patch adds a flag -fno-pretty-templates to disable both of these transformations.

Tested x86_64-pc-linux-gnu, applied to trunk.

2009-04-07  Jason Merrill  <jason@redhat.com>

    	PR c++/25185
    	* c-common.h, c-common.c: Add flag_pretty_templates.
    	* c-opts.c (c_common_handle_option): Set it.
    	* c.opt: Add -fno-pretty-templates.
    	* doc/invoke.texi (C++ Dialect Options): Likewise.
    
    	* error.c (dump_function_decl): Don't pretty-print templates
    	if -fno-pretty-templates.
    	(count_non_default_template_args): Print all args if
    	-fno-pretty-templates.

diff --git a/gcc/cp/error.c b/gcc/cp/error.c
index a97017a..fc32d99 100644
--- a/gcc/cp/error.c
+++ b/gcc/cp/error.c
@@ -163,7 +163,7 @@ count_non_default_template_args (tree args, tree params)
   int n = TREE_VEC_LENGTH (args);
   int last;
 
-  if (params == NULL_TREE)
+  if (params == NULL_TREE || !flag_pretty_templates)
     return n;
 
   for (last = n - 1; last >= 0; --last)
@@ -1206,7 +1206,8 @@ dump_function_decl (tree t, int flags)
   exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (t));
 
   /* Pretty print template instantiations only.  */
-  if (DECL_USE_TEMPLATE (t) && DECL_TEMPLATE_INFO (t))
+  if (DECL_USE_TEMPLATE (t) && DECL_TEMPLATE_INFO (t)
+      && flag_pretty_templates)
     {
       tree tmpl;
 
diff --git a/gcc/c-common.c b/gcc/c-common.c
index 9886cdf..3d11455 100644
--- a/gcc/c-common.c
+++ b/gcc/c-common.c
@@ -593,6 +593,11 @@ int flag_enforce_eh_specs = 1;
 
 int flag_threadsafe_statics = 1;
 
+/* Nonzero if we want to pretty-print template specializations as the
+   template signature followed by the arguments.  */
+
+int flag_pretty_templates = 1;
+
 /* Nonzero means warn about implicit declarations.  */
 
 int warn_implicit = 1;
diff --git a/gcc/c-common.h b/gcc/c-common.h
index f5c755b..0d85a93 100644
--- a/gcc/c-common.h
+++ b/gcc/c-common.h
@@ -695,6 +695,11 @@ extern int flag_enforce_eh_specs;
 
 extern int flag_threadsafe_statics;
 
+/* Nonzero if we want to pretty-print template specializations as the
+   template signature followed by the arguments.  */
+
+extern int flag_pretty_templates;
+
 /* Nonzero means warn about implicit declarations.  */
 
 extern int warn_implicit;
diff --git a/gcc/c-opts.c b/gcc/c-opts.c
index 3953991..54edf30 100644
--- a/gcc/c-opts.c
+++ b/gcc/c-opts.c
@@ -808,6 +808,10 @@ c_common_handle_option (size_t scode, const char *arg, int value)
       flag_threadsafe_statics = value;
       break;
 
+    case OPT_fpretty_templates:
+      flag_pretty_templates = value;
+      break;
+
     case OPT_fzero_link:
       flag_zero_link = value;
       break;
diff --git a/gcc/c.opt b/gcc/c.opt
index 40681bd..3f1cd13 100644
--- a/gcc/c.opt
+++ b/gcc/c.opt
@@ -716,6 +716,10 @@ fpreprocessed
 C ObjC C++ ObjC++
 Treat the input file as already preprocessed
 
+fpretty-templates
+C++ ObjC++
+-fno-pretty-templates Do not pretty-print template specializations as the template signature followed by the arguments
+
 freplace-objc-classes
 ObjC ObjC++
 Used in Fix-and-Continue mode to indicate that object files may be swapped in at runtime
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 5eb9776..b5d795a 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -188,6 +188,7 @@ in the following sections.
 -fno-implement-inlines  -fms-extensions @gol
 -fno-nonansi-builtins  -fno-operator-names @gol
 -fno-optional-diags  -fpermissive @gol
+-fno-pretty-templates @gol
 -frepo  -fno-rtti  -fstats  -ftemplate-depth-@var{n} @gol
 -fno-threadsafe-statics -fuse-cxa-atexit  -fno-weak  -nostdinc++ @gol
 -fno-default-inline  -fvisibility-inlines-hidden @gol
@@ -1834,6 +1835,19 @@ Downgrade some diagnostics about nonconformant code from errors to
 warnings.  Thus, using @option{-fpermissive} will allow some
 nonconforming code to compile.
 
+@item -fno-pretty-templates
+@opindex fno-pretty-templates
+When an error message refers to a specialization of a function
+template, the compiler will normally print the signature of the
+template followed by the template arguments and any typedefs or
+typenames in the signature (e.g. @code{void f(T) [with T = int]}
+rather than @code{void f(int)}) so that it's clear which template is
+involved.  When an error message refers to a specialization of a class
+template, the compiler will omit any template arguments which match
+the default template arguments for that template.  If either of these
+behaviors make it harder to understand the error message rather than
+easier, using @option{-fno-pretty-templates} will disable them.
+
 @item -frepo
 @opindex frepo
 Enable automatic template instantiation at link time.  This option also
diff --git a/gcc/testsuite/g++.dg/template/error40.C b/gcc/testsuite/g++.dg/template/error40.C
new file mode 100644
index 0000000..f449832
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/error40.C
@@ -0,0 +1,19 @@
+// { dg-options "-fno-pretty-templates" }
+
+template <class T, int N=0, int X=1>
+struct A
+{
+};
+
+void foo(void)
+{
+  A<void> a = 0;		// { dg-error "A<void, 0, 1>" }
+}
+
+template <class T> T f(T);	    // { dg-message "int f<int>.int." }
+template <class T> T f(T, int = 0); // { dg-message "" }
+
+int main()
+{
+  f(1);				// { dg-error "" }
+}

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