Logo AND Algorithmique Numérique Distribuée

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