Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d9a85f58a92b75dab2b45d9a9981af63012513fc
[simgrid.git] / tools / doxygen / xbt_log_extract_hierarchy.pl
1 #!/usr/bin/perl
2
3 # Copyright (c) 2008, 2010, 2012-2014. The SimGrid Team.
4 # All rights reserved.
5
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the license (GNU LGPL) which comes with this package.
8
9 use strict;
10 use warnings;
11
12 my $debug = 0;
13
14 print "/* Generated file, do not edit */\n";
15 print "/** \\addtogroup XBT_log_cats\n";
16 print "        \@{\n";
17
18 # Search for calls to macros defining new channels, and prepare the tree representation
19 my %ancestor;
20 my %desc;
21 # $ancestor{"toto"} is the ancestor of the toto channel
22 #    as declared by XBT_LOG_NEW_SUBCATEGORY and XBT_LOG_NEW_DEFAULT_SUBCATEGORY
23 #    ie, when the channel toto is initialized (does not work under windows)
24
25 # $desc{"toto"} is its description
26 my %connected;
27 # $connected{"toto"} is defined if XBT_LOG_CONNECT("toto") is used
28
29 sub cleanup_ctn {
30     my $ctn = shift;        # cleanup the content of a macro call
31     $ctn =~ s/^\s*(.*)\s*$/$1/gs;
32     my @elms;
33     print "ctn=$ctn\n" if $debug > 1;
34     if ($ctn =~ m/^(\w+)\s*,\s*(\w+)\s*,\s*"?([^"]*)"?$/s) {
35         # Perfect, we got 0->name; 1->anc; 2->desc
36         $elms[0] = $1;
37         $elms[1] = $2;
38         $elms[2] = $3;
39     } elsif ($ctn =~ m/^(\w+)\s*,\s*"?([^"]*)"?$/s) {
40         # Mmm. got no ancestor. Add the default one.
41         $elms[0] = $1;
42         $elms[1] = "XBT_LOG_ROOT_CAT";
43         $elms[2] = $2;
44     } else {
45         die "Unparsable content: $ctn\n";
46     }
47     $elms[2] =~ s/\\\\/\\/gs;
48     return @elms;
49 }
50
51
52 sub parse_file {
53     my $filename = shift;
54     
55     my $data = "";
56     
57     print "Parse $filename\n" if $debug;
58     open IN, "$filename" || die "Cannot read $filename: $!\n";
59     while (<IN>) {
60         $data .= $_;
61     }
62     close IN;
63
64     # Purge $data from C comments
65     $data =~ s|/\*.*?\*/||sg;
66
67     # C++ comments are forbiden in SG for portability reasons, but deal with it anyway
68     $data =~ s|//.*$||mg;
69
70     my $connect_data = $data; # save a copy for second parsing phase
71     while ($data =~ s/^.*?XBT_LOG_NEW(_DEFAULT)?_(SUB)?CATEGORY\(//s) {
72         $data =~ s/([^"]*"[^"]*")\)//s || die "unparsable macro: $data";
73             
74         my ($name,$anc,$desc) = cleanup_ctn($1);
75             
76         # build the tree, checking for name conflict
77         die "ERROR: Category name conflict: $name used several times (in $ancestor{$name} and $anc, last time in $filename)\n"
78            if defined ($ancestor{$name}) && $ancestor{$name} ne $anc &&
79               defined ($desc{$name}) && $desc{$name} ne $desc;
80        $ancestor{$name}=$anc;
81        $desc{$name}=$desc;
82    
83        print " $name -> $anc\n" if $debug;
84    }
85
86    # Now, look for XBT_LOG_CONNECT calls
87    $data = $connect_data;
88    while ($data =~ s/^.*?XBT_LOG_CONNECT\(//s) {
89        $data =~ s/\s*(\w+)\s*\)//s || die "unparsable macro: $data";
90        $connected{$1} = 1;
91    }
92 }
93 # Retrieve all the file names, and add their content to $data
94 my $data;
95 open FILES, "find ../src/ ../tools/ ../include/ -name '*.c' -o -name '*.cpp' |" || die "Cannot search for the source file names: $!\n";
96 while (my $file=<FILES>) {
97     chomp $file;
98     parse_file($file);  
99 }
100 close FILES;
101
102 # Display the tree, looking for disconnected elems    
103 my %used;
104         
105 sub display_subtree {
106     my $name=shift;
107     my $indent=shift;
108     
109     $used{$name} = 1;
110     unless ($name eq "XBT_LOG_ROOT_CAT") { # do not display the root
111         print "$indent - $name: ".($desc{$name}|| "(undocumented)")."\n";
112     }
113     foreach my $cat (grep {$ancestor{$_} eq $name} sort keys %ancestor) {
114         display_subtree($cat,"$indent  ");
115     }
116 }
117     
118 display_subtree("XBT_LOG_ROOT_CAT","");
119
120 map {
121     warn "Category $_ does not seem to be connected.  Use XBT_LOG_CONNECT($_).\n";
122 } grep {!defined $connected{$_}} sort keys %ancestor;
123 map {
124     warn "Category $_ does not seem to be connected to the root (anc=$ancestor{$_})\n";
125 } grep {!defined $used{$_}} sort keys %ancestor;
126
127         
128 print "@}*/\n";