Correct the behavior of __func__ for C++ special member functions.
authoraustern <austern@138bc75d-0d04-0410-961f-82ee72b054a4>
Thu, 4 Sep 2003 21:32:48 +0000 (21:32 +0000)
committeraustern <austern@138bc75d-0d04-0410-961f-82ee72b054a4>
Thu, 4 Sep 2003 21:32:48 +0000 (21:32 +0000)
* c-common.c (fname_as_string): Use lang_hooks.decl_printable_name
with verbosity 0, instead of DECL_NAME, for human-readable string.
* g++.dg/ext/fnname1.C: New test. (__func__ for C++.)
* g++.dg/ext/fnname2.C: Likewise.
* g++.dg/ext/fnname3.C: Likewise.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@71088 138bc75d-0d04-0410-961f-82ee72b054a4

gcc/ChangeLog
gcc/c-common.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/ext/fnname1.C [new file with mode: 0644]
gcc/testsuite/g++.dg/ext/fnname2.C [new file with mode: 0644]
gcc/testsuite/g++.dg/ext/fnname3.C [new file with mode: 0644]

index e2c0fc6..a3ebefc 100644 (file)
@@ -1,3 +1,8 @@
+2003-09-04  Matt Austern  <austern@apple.com>
+
+       * c-common.c (fname_as_string): Use lang_hooks.decl_printable_name
+       with verbosity 0, instead of DECL_NAME, for human-readable string.
+       
 2003-09-04  Eric Christopher  <echristo@redhat.com>
 
        * targhooks.c (default_return_in_memory): Allow
index 9dd1826..69dd081 100644 (file)
@@ -1071,16 +1071,18 @@ finish_fname_decls (void)
 const char *
 fname_as_string (int pretty_p)
 {
-  const char *name = NULL;
-
-  if (pretty_p)
-    name = (current_function_decl
-           ? (*lang_hooks.decl_printable_name) (current_function_decl, 2)
-           : "top level");
-  else if (current_function_decl && DECL_NAME (current_function_decl))
-    name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
-  else
-    name = "";
+  const char *name = "top level";
+  int vrb = 2;
+
+  if (! pretty_p)
+    {
+      name = "";
+      vrb = 0;
+    }
+
+  if (current_function_decl)
+    name = (*lang_hooks.decl_printable_name) (current_function_decl, vrb);
+
   return name;
 }
 
index 562ed09..c475b9a 100644 (file)
@@ -1,3 +1,9 @@
+2003-09-04  Matt Austern  <austern@apple.com>
+
+       * g++.dg/ext/fnname1.C: New test. (__func__ for C++.)
+       * g++.dg/ext/fnname2.C: Likewise.
+       * g++.dg/ext/fnname3.C: Likewise.
+       
 2003-09-04  Mark Mitchell  <mark@codesourcery.com>
 
        * g++.dg/expr/lval1.C: New test.
diff --git a/gcc/testsuite/g++.dg/ext/fnname1.C b/gcc/testsuite/g++.dg/ext/fnname1.C
new file mode 100644 (file)
index 0000000..521d5a7
--- /dev/null
@@ -0,0 +1,26 @@
+// Test whether __func__ works for namespace-scope C++ functions.
+
+// Copyright (C) 2003 Free Software Foundation, Inc.
+// Contributed by Matt Austern <austern@apple.com>, 3 Aug 2003
+// { dg-do run }
+
+namespace xyzzy
+{
+  const char* ab6(double, void*)
+  {
+    return __func__;
+  }
+}
+
+int main()
+{
+  const char* s = xyzzy::ab6(2.3, (void*) 0);
+  bool ok = true;
+
+  ok = ok && s[0] == 'a';
+  ok = ok && s[1] == 'b';
+  ok = ok && s[2] == '6';
+  ok = ok && s[3] == '\0';
+
+  return ok ? 0 : 1;
+}
diff --git a/gcc/testsuite/g++.dg/ext/fnname2.C b/gcc/testsuite/g++.dg/ext/fnname2.C
new file mode 100644 (file)
index 0000000..ea0c182
--- /dev/null
@@ -0,0 +1,31 @@
+// Test whether __func__ works for ordinary member functions.
+
+// Copyright (C) 2003 Free Software Foundation, Inc.
+// Contributed by Matt Austern <austern@apple.com>, 3 Aug 2003
+// { dg-do run }
+
+struct y8a
+{
+  const char* zqjx(int, char);
+};
+
+const char* y8a::zqjx(int, char)
+{
+  return __func__;
+}
+
+
+int main()
+{
+  y8a tmp;
+  const char* s = tmp.zqjx(16, 'x');
+  bool ok = true;
+
+  ok = ok && s[0] == 'z';
+  ok = ok && s[1] == 'q';
+  ok = ok && s[2] == 'j';
+  ok = ok && s[3] == 'x';
+  ok = ok && s[4] == '\0';
+
+  return ok ? 0 : 1;
+}
diff --git a/gcc/testsuite/g++.dg/ext/fnname3.C b/gcc/testsuite/g++.dg/ext/fnname3.C
new file mode 100644 (file)
index 0000000..c29170a
--- /dev/null
@@ -0,0 +1,65 @@
+// Test whether __func__ works for constructors and destructors.
+
+// Copyright (C) 2003 Free Software Foundation, Inc.
+// Contributed by Matt Austern <austern@apple.com>, 3 Aug 2003
+// { dg-do run }
+
+struct uk9i
+{
+  uk9i();
+  ~uk9i();
+
+  static const char* fname;
+  static bool obj_exists;
+};
+
+uk9i::uk9i()
+{
+  obj_exists = true;
+  fname = __func__;
+}
+
+uk9i::~uk9i()
+{
+  obj_exists = false;
+  fname = __func__;
+}
+
+const char* uk9i::fname = 0;
+bool uk9i::obj_exists = false;
+
+int main()
+{
+  bool ok = true;
+
+  ok = ok && uk9i::fname == 0;
+  ok = ok && !uk9i::obj_exists;
+
+  {
+    uk9i tmp;
+    ok = ok && uk9i::obj_exists;
+    ok = ok && uk9i::fname != 0;
+    if (ok)
+      {
+       ok = ok && uk9i::fname[0] == 'u';
+       ok = ok && uk9i::fname[1] == 'k';
+       ok = ok && uk9i::fname[2] == '9';
+       ok = ok && uk9i::fname[3] == 'i';
+       ok = ok && uk9i::fname[4] == '\0';
+      }
+  }
+
+  ok = ok && !uk9i::obj_exists;
+  ok = ok && uk9i::fname != 0;
+  if (ok)
+    {
+      ok = ok && uk9i::fname[0] == '~';
+      ok = ok && uk9i::fname[1] == 'u';
+      ok = ok && uk9i::fname[2] == 'k';
+      ok = ok && uk9i::fname[3] == '9';
+      ok = ok && uk9i::fname[4] == 'i';
+      ok = ok && uk9i::fname[5] == '\0';
+    }  
+
+  return ok ? 0 : 1;
+}