Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix xbt_log_extract_hierarchy.pl.
[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/^\s*(.*)\s*$/$1/gs;
28     my @elms;
29     print "ctn=$ctn\n" if $debug > 1;
30     if ($ctn =~ m/^(\w+)\s*,\s*(\w+)\s*,\s*"?([^"]*)"?$/s) {
31         # Perfect, we got 0->name; 1->anc; 2->desc
32         $elms[0] = $1;
33         $elms[1] = $2;
34         $elms[2] = $3;
35     } elsif ($ctn =~ m/^(\w+)\s*,\s*"?([^"]*)"?$/s) {
36         # Mmm. got no ancestor. Add the default one.
37         $elms[0] = $1;
38         $elms[1] = "XBT_LOG_ROOT_CAT";
39         $elms[2] = $2;
40     } else {
41         die "Unparsable content: $ctn\n";
42     }
43     return @elms;
44 }
45
46
47 sub parse_file {
48     my $filename = shift;
49     
50     my $data = "";
51     
52     print "Parse $filename\n" if $debug;
53     open IN, "$filename" || die "Cannot read $filename: $!\n";
54     while (<IN>) {
55         $data .= $_;
56     }
57     close IN;
58
59     # Purge $data from C comments
60     $data =~ s|/\*.*?\*/||sg;
61
62     # C++ comments are forbiden in SG for portability reasons, but deal with it anyway
63     $data =~ s|//.*$||mg;
64
65     my $connect_data = $data; # save a copy for second parsing phase
66     while ($data =~ s/^.*?XBT_LOG_NEW(_DEFAULT)?_(SUB)?CATEGORY\(//s) {
67         $data =~ s/([^"]*"[^"]*")\)//s || die "unparsable macro: $data"; # ]]);
68             
69         my ($name,$anc,$desc) = cleanup_ctn($1);
70             
71         # build the tree, checking for name conflict
72         die "ERROR: Category name conflict: $name used several times (in $ancestor{$name} and $anc, last time in $filename)\n"
73            if defined ($ancestor{$name}) && $ancestor{$name} ne $anc &&
74               defined ($desc{$name}) && $desc{$name} ne $desc;
75        $ancestor{$name}=$anc;
76        $desc{$name}=$desc;
77    
78        print " $name -> $anc\n" if $debug;
79    }
80
81    # Now, look for XBT_LOG_CONNECT calls
82    $data = $connect_data;
83    while ($data =~ s/^.*?XBT_LOG_CONNECT\(//s) {
84                                                                          
85         $data =~ s/([^\)]*)\)//s || die "unparsable macro: $data"; # ]]);           
86         my ($name, $ignoreme, $anc) = cleanup_ctn($1);
87             
88         # build the tree, checking for name conflict
89        $c_ancestor{$name}=$anc;
90    
91        print STDERR " $name -> $anc\n" if $debug;
92    }
93 }
94 # Retrieve all the file names, and add their content to $data
95 my $data;
96 open FILES, "find src/ tools/ include/ -name '*.c'|" || die "Cannot search for the source file names: $!\n";
97 while (my $file=<FILES>) {
98     chomp $file;
99     parse_file($file);  
100 }
101 close FILES;
102
103 # Display the tree, looking for disconnected elems    
104 my %used;
105         
106 sub display_subtree {
107     my $name=shift;
108     my $indent=shift;
109     
110     $used{$name} = 1;
111     unless ($name eq "XBT_LOG_ROOT_CAT") { # do not display the root
112         print "$indent - $name: ".($desc{$name}|| "(undocumented)")."\n";
113     }
114     foreach my $cat (grep {$ancestor{$_} eq $name} sort keys %ancestor) {
115         display_subtree($cat,"$indent  ");
116     }
117 }
118     
119 display_subtree("XBT_LOG_ROOT_CAT","");
120
121 sub check_connection {
122     my $name=shift;
123     
124     foreach my $cat (grep {$ancestor{$_} eq $name} sort keys %ancestor) {
125         unless ($ancestor{$cat} eq "XBT_LOG_ROOT_CAT" || (defined($c_ancestor{$cat}) && $c_ancestor{$cat} eq $name)) {
126             warn "Category $cat will be disconnected under windows. Add the following to an initialization function:\n   XBT_LOG_CONNECT($cat, $ancestor{$cat});\n";
127         } else {
128             warn "Correctly connected, even under windows: Category $cat.\n" if $debug;
129         }
130         check_connection($cat);
131     }
132 }
133 check_connection("XBT_LOG_ROOT_CAT");   
134 map {warn "Category $_ does not seem to be connected to the root (anc=$ancestor{$_})\n";} grep {!defined $used{$_}} sort keys %ancestor;    
135
136         
137 print "@}*/";