Jakub pointed out this cute typeof trick on the gcc thread about
decl_as_string. This patch changes the plugin to use it.
The end result is similar to using dlsym, but has the benefit of being
type-safe.
If you'd prefer I can reword the patch to continue to use dlsym, and
just change the declaration of the local decl_as_string to use typeof.
That would also be type-safe.
This uses a GCC extension, but I figured that was ok for a GCC plugin.
This passed all tests on x86-64 F15.
Tom
From 51353c7cc59c471a764c1f4fa99171810f7432b1 Mon Sep 17 00:00:00 2001
From: Tom Tromey <tromey(a)redhat.com>
Date: Tue, 13 Dec 2011 12:11:42 -0700
Subject: [PATCH] redeclare decl_as_string as weak
This changes gcc-python-tree.c to redeclare decl_as_string
as weak, rather than using dlsym. This has the same effect,
but is type-safe.
---
gcc-python-tree.c | 30 +++++++++++++-----------------
1 files changed, 13 insertions(+), 17 deletions(-)
diff --git a/gcc-python-tree.c b/gcc-python-tree.c
index 3ba3b4c..384be65 100644
--- a/gcc-python-tree.c
+++ b/gcc-python-tree.c
@@ -25,6 +25,19 @@
#include "cp/cp-tree.h" /* for TFF_* for use by gcc_FunctionDecl_get_fullname
*/
+/*
+ Unfortunately, decl_as_string() is only available from the C++
+ frontend: cc1plus (it's defined in gcc/cp/error.c).
+
+ See
http://gcc.gnu.org/ml/gcc/2011-11/msg00504.html
+
+ Hence we redeclare the symbol as weak, and then check its definition
+ against 0 before using it.
+*/
+
+__typeof__ (decl_as_string) decl_as_string __attribute__ ((weak));
+
+
//#include "rtl.h"
/*
"struct rtx_def" is declarted within rtl.h, c.f:
@@ -158,25 +171,8 @@ error:
PyObject *
gcc_FunctionDecl_get_fullname(struct PyGccTree *self, void *closure)
{
- /*
- Unfortunately, decl_as_string() is only available from the C++
- frontend: cc1plus (it's defined in gcc/cp/error.c).
-
- See
http://gcc.gnu.org/ml/gcc/2011-11/msg00504.html
-
- Hence we try to dynamically load the symbol, which will be already
- present if we were loaded by cc1plus (for C++), and not be found
- otherwise
-
- Declared in cp/cp-tree.h as:
- extern const char *decl_as_string (tree, int);
- */
-
- const char *(*decl_as_string)(tree, int);
const char *str;
- decl_as_string = dlsym(RTLD_DEFAULT, "decl_as_string");
-
if (NULL == decl_as_string) {
return PyErr_Format(PyExc_RuntimeError,
"attribute 'fullname' is only available when
compiling C++ code");
--
1.7.6.4