This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH 15/22] Add plugin to recursively dump the source-ranges in a tree
- From: Martin Sebor <msebor at gmail dot com>
- To: David Malcolm <dmalcolm at redhat dot com>, gcc-patches at gcc dot gnu dot org
- Date: Thu, 10 Sep 2015 20:48:09 -0600
- Subject: Re: [PATCH 15/22] Add plugin to recursively dump the source-ranges in a tree
- Authentication-results: sourceware.org; auth=none
- References: <1441916913-11547-1-git-send-email-dmalcolm at redhat dot com> <1441916913-11547-16-git-send-email-dmalcolm at redhat dot com>
On 09/10/2015 02:28 PM, David Malcolm wrote:
This patch adds a test plugin that recurses down an expression tree,
printing diagnostics showing the ranges of each node in the tree.
Screenshot:
https://dmalcolm.fedorapeople.org/gcc/2015-09-09/show-trees.html
This needs a linker hack, since it's the only user of
gcc_rich_location::add_expr
which thus doesn't appear in "cc1" until later patches in the kit
add uses of it; is there a clean way to fix that?
...
+ Hack: fails with linker error:
+./diagnostic_plugin_show_trees.so: undefined symbol: _ZN17gcc_rich_location8add_exprEP9tree_node
+ since nothing in the tree is using gcc_rich_location::add_expr yet.
+
+ I've tried various workarounds (adding DEBUG_FUNCTION to the
+ method, taking its address), but can't seem to fix it that way.
+ So as a nasty workaround, the following material is copied&pasted
+ from gcc-rich-location.c: */
I would expect this to work so long as cc1 is linked with
--export-dynamic (or -rdynamic which I think it is), and it
does in the attached example. I wonder what's different about
the way it's built (I haven't tried to reproduce it with gcc).
Martin
PS I've only skimmed the patch but besides being in awe at how
you managed to structure it and not get lost in the dependencies
I really like the output in the snapshots. Very cool!
/*
$ (set -x; fl='-O2 -Wall -flto'; cat t.c && g++ -DFOO=1 -c $fl -o foo.o t.c && g++ -DBAR=1 -c $fl -o bar.o t.c && g++ -DMAIN=1 -c $fl t.c && g++ $fl -Wl,--export-dynamic -ldl foo.o bar.o t.o && g++ -DDSO=1 -fpic $fl -o plugin.so -shared t.c) && LD_LIBRARY_PATH=. ./a.out
+ fl='-O2 -Wall -flto'
+ cat t.c
*/
#include <dlfcn.h>
#include <stdio.h>
struct S {
void foo ();
int bar ();
};
#if FOO
void S::foo () { puts (__func__); }
#elif BAR
int S::bar () { puts (__func__); return 0; }
#elif DSO
extern "C" {
void foobar (S &s) {
puts (__func__);
s.foo ();
}
}
#elif MAIN
int main ()
{
S s;
void *dl = dlopen ("plugin.so", RTLD_LAZY);
if (!dl) {
fprintf (stderr, "dlopen: %s\n", dlerror ());
return 1;
}
void (*f)(S&) = (void (*)(S&))dlsym (dl, "foobar");
if (!f) {
fprintf (stderr, "dlsym: %s\n", dlerror ());
return 1;
}
f (s);
dlclose (dl);
return s.bar ();
}
#endif
/*
+ g++ -DFOO=1 -c -O2 -Wall -flto -o foo.o t.c
+ g++ -DBAR=1 -c -O2 -Wall -flto -o bar.o t.c
+ g++ -DMAIN=1 -c -O2 -Wall -flto t.c
+ g++ -O2 -Wall -flto -Wl,--export-dynamic -ldl foo.o bar.o t.o
+ g++ -DDSO=1 -fpic -O2 -Wall -flto -o plugin.so -shared t.c
foobar
foo
bar
$
*/