Logo AND Algorithmique Numérique Distribuée

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