Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
please sonar by removing 2 redundent forward declaration
[simgrid.git] / tools / doxygen / xbt_log_extract_hierarchy.pl
1 #!/usr/bin/env 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 as declared by XBT_LOG_NEW_SUBCATEGORY and 
22 # XBT_LOG_NEW_DEFAULT_SUBCATEGORY ie, when the channel toto is initialized (does not work under windows)
23
24 # $desc{"toto"} is its description
25 my %connected;
26 # $connected{"toto"} is defined if XBT_LOG_CONNECT("toto") is used
27
28 sub cleanup_ctn {
29     my $ctn = shift;        # cleanup the content of a macro call
30     $ctn =~ s/^\s*(.*)\s*$/$1/gs;
31     my @elms;
32     print "ctn=$ctn\n" if $debug > 1;
33     if ($ctn =~ m/^(\w+)\s*,\s*(\w+)\s*,\s*"?([^"]*)"?$/s) {
34         # Perfect, we got 0->name; 1->anc; 2->desc
35         $elms[0] = $1;
36         $elms[1] = $2;
37         $elms[2] = $3;
38     } elsif ($ctn =~ m/^(\w+)\s*,\s*"?([^"]*)"?$/s) {
39         # Mmm. got no ancestor. Add the default one.
40         $elms[0] = $1;
41         $elms[1] = "XBT_LOG_ROOT_CAT";
42         $elms[2] = $2;
43     } else {
44         die "Unparsable content: $ctn\n";
45     }
46     $elms[2] =~ s/\\\\/\\/gs;
47     return @elms;
48 }
49
50 sub parse_file {
51     my $filename = shift;
52
53     my $data = "";
54
55     print "Parse $filename\n" if $debug;
56     open IN, "$filename" || die "Cannot read $filename: $!\n";
57     while (<IN>) {
58         $data .= $_;
59     }
60     close IN;
61
62     # Purge $data from C comments
63     $data =~ s|/\*.*?\*/||sg;
64
65     # C++ comments are forbiden in SG for portability reasons, but deal with it anyway
66     $data =~ s|//.*$||mg;
67
68     my $connect_data = $data; # save a copy for second parsing phase
69     while ($data =~ s/^.*?XBT_LOG_NEW(_DEFAULT)?_(SUB)?CATEGORY\(//s) {
70         $data =~ s/([^"]*"[^"]*")\)//s || die "unparsable macro: $data";
71
72         my ($name,$anc,$desc) = cleanup_ctn($1);
73     
74         # build the tree, checking for name conflict
75         die "ERROR: Category name conflict: $name used several times (in $ancestor{$name} and $anc, last time in $filename)\n"
76            if defined ($ancestor{$name}) && $ancestor{$name} ne $anc && defined ($desc{$name}) && $desc{$name} ne $desc;
77        $ancestor{$name}=$anc;
78        $desc{$name}=$desc;
79
80        print " $name -> $anc\n" if $debug;
81    }
82
83    # Now, look for XBT_LOG_CONNECT calls
84    $data = $connect_data;
85    while ($data =~ s/^.*?XBT_LOG_CONNECT\(//s) {
86        $data =~ s/\s*(\w+)\s*\)//s || die "unparsable macro: $data";
87        $connected{$1} = 1;
88    }
89 }
90 # Retrieve all the file names, and add their content to $data
91 my $data;
92 open FILES, "find ../src/ ../tools/ ../include/ -name '*.c' -o -name '*.cpp' |" || die "Cannot search for the source file names: $!\n";
93 while (my $file=<FILES>) {
94     chomp $file;
95     parse_file($file);  
96 }
97 parse_file("../include/xbt/sysdep.h");  
98 close FILES;
99
100 # Display the tree, looking for disconnected elems    
101 my %used;
102
103 sub display_subtree {
104     my $name=shift;
105     my $indent=shift;
106     
107     $used{$name} = 1;
108     unless ($name eq "XBT_LOG_ROOT_CAT") { # do not display the root
109         print "$indent - $name: ".($desc{$name}|| "(undocumented)")."\n";
110     }
111     foreach my $cat (grep {$ancestor{$_} eq $name} sort keys %ancestor) {
112         display_subtree($cat,"$indent  ");
113     }
114 }
115
116 display_subtree("XBT_LOG_ROOT_CAT","");
117
118 map {
119     if ($_ ne "mc_main") { # This one is not in libsimgrid
120       warn "Category $_ does not seem to be connected.  Use XBT_LOG_CONNECT($_).\n";
121     }
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 print "@}*/\n";