Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8d9e70cd843f1bf70543e4f1b181021d515c30d7
[simgrid.git] / tools / doxygen / xbt_log_extract_hierarchy.pl
1 #! /usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 my $debug = 0;
7
8
9 # Search for calls to macros defining new channels, and prepare the tree representation
10 my %ancestor;
11 my %desc;
12 # $ancestor{"toto"} is the ancestor of the toto channel
13 #    as declared by XBT_LOG_NEW_SUBCATEGORY and XBT_LOG_NEW_DEFAULT_SUBCATEGORY
14 #    ie, when the channel toto is initialized (does not work under windows)
15
16 # $desc{"toto"} is its description
17 my %c_ancestor;
18 # $c_ancestor{"toto"} is the ancestor of the toto channel, as declared by XBT_LOG_CONNECT
19 #    ie, in a initialization function (only way to do so under windows)
20 #    we want $ancestor{"toto"} == $c_ancestor{"toto"} for each toto, or bad things will happen under windows
21
22 sub cleanup_ctn {
23     my $ctn = shift;        # cleanup the content of a macro call
24     $ctn =~ s/ *\n//gs;                                                                          
25     $ctn =~ s/,\s*"/,"/gs;
26     $ctn =~ s/"\s*$/"/gs;
27     $ctn =~ s/,\s*/,/gs;
28     my @elms_tmp=split (/,/,$ctn); 
29     my @elms;
30     print "ctn=$ctn\n" if $debug > 1;
31     # There may be some ',' in the description. Remerge the stuff like: "description, really"
32     while (1) {
33         my $acc = shift @elms_tmp;
34         last unless defined $acc;
35         if ($acc =~ /^"/) { # ") {
36             while (shift @elms_tmp) { 
37                 $acc .= $_;
38             }
39             die "Unparsable content: $ctn\n"
40               unless ($acc =~ s/^"(.*)"$/$1/);
41         }
42         print "  seen $acc\n" if $debug > 1;
43         push @elms, $acc;
44     }
45     if (scalar(@elms) eq 3) {
46         # Perfect, we got 0->name; 1->anc; 2->desc
47     } elsif (scalar(@elms) eq 2) {
48         # Mmm. got no ancestor. Add the default one.
49         $elms[2] = $elms[1]; # shift the desc
50         $elms[1] = "XBT_LOG_ROOT_CAT";
51     } else {
52         my $l = scalar(@elms);
53         my $s = "";
54         map {$s .= $_;} @elms;
55         die "Unparsable content: $ctn (length=$l) (content=$s)\n";
56     }
57     
58     $elms[0] =~ s/^\s*(\S*)\s*$/$1/; # trim
59     $elms[1]  =~ s/^\s*(\S*)\s*$/$1/; # trim
60
61     return @elms;
62 }
63
64
65 sub parse_file {
66     my $filename = shift;
67     
68     my $data = "";
69     
70     print "Parse $filename\n" if $debug;
71     open IN, "$filename" || die "Cannot read $filename: $!\n";
72     while (<IN>) {
73         $data .= $_;
74     }
75     close IN;
76
77     # Purge $data from C comments
78     $data =~ s|/\*.*?\*/||sg;
79
80     # C++ comments are forbiden in SG for portability reasons, but deal with it anyway
81     $data =~ s|//.*$||mg;
82
83     my $connect_data = $data; # save a copy for second parsing phase
84     while ($data =~ s/^.*?XBT_LOG_NEW(_DEFAULT)?_(SUB)?CATEGORY\(//s) {
85         $data =~ s/([^"]*"[^"]*")\)//s || die "unparsable macro: $data"; # ]]);
86             
87         my ($name,$anc,$desc) = cleanup_ctn($1);
88             
89         # build the tree, checking for name conflict
90         die "ERROR: Category name conflict: $name used several times (in $ancestor{$name} and $anc, last time in $filename)\n"
91            if defined ($ancestor{$name}) && $ancestor{$name} ne $anc &&
92               defined ($desc{$name}) && $desc{$name} ne $desc;
93        $ancestor{$name}=$anc;
94        $desc{$name}=$desc;
95    
96        print " $name -> $anc\n" if $debug;
97    }
98
99    # Now, look for XBT_LOG_CONNECT calls
100    $data = $connect_data;
101    while ($data =~ s/^.*?XBT_LOG_CONNECT\(//s) {
102                                                                          
103         $data =~ s/([^\)]*)\)//s || die "unparsable macro: $data"; # ]]);           
104         my ($name, $ignoreme, $anc) = cleanup_ctn($1);
105             
106         # build the tree, checking for name conflict
107        $c_ancestor{$name}=$anc;
108    
109        print STDERR " $name -> $anc\n" if $debug;
110    }
111 }
112 # Retrieve all the file names, and add their content to $data
113 my $data;
114 open FILES, "find -name '*.c'|" || die "Cannot search for the source file names: $!\n";
115 while (my $file=<FILES>) {
116     chomp $file;
117     parse_file($file);  
118 }
119 close FILES;
120
121 # Display the tree, looking for disconnected elems    
122 my %used;
123         
124 sub display_subtree {
125     my $name=shift;
126     my $indent=shift;
127     
128     $used{$name} = 1;
129     unless ($name eq "XBT_LOG_ROOT_CAT") { # do not display the root
130         print "$indent - $name: ".($desc{$name}|| "(undocumented)")."\n";
131     }
132     foreach my $cat (grep {$ancestor{$_} eq $name} sort keys %ancestor) {
133         display_subtree($cat,"$indent  ");
134     }
135 }
136     
137 display_subtree("XBT_LOG_ROOT_CAT","");
138
139 sub check_connection {
140     my $name=shift;
141     
142     foreach my $cat (grep {$ancestor{$_} eq $name} sort keys %ancestor) {
143         unless ($ancestor{$cat} eq "XBT_LOG_ROOT_CAT" || (defined($c_ancestor{$cat}) && $c_ancestor{$cat} eq $name)) {
144             warn "Category $cat will be disconnected under windows. Add the following to an initialization function:\n   XBT_LOG_CONNECT($cat, $ancestor{$cat});\n";
145         } else {
146             warn "Correctly connected, even under windows: Category $cat.\n" if $debug;
147         }
148         check_connection($cat);
149     }
150 }
151 check_connection("XBT_LOG_ROOT_CAT");   
152 map {warn "Category $_ does not seem to be connected to the root (anc=$ancestor{$_})\n";} grep {!defined $used{$_}} sort keys %ancestor;