Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright headers.
[simgrid.git] / tools / doxygen / xbt_log_extract_hierarchy.pl
1 #!/usr/bin/env perl
2
3 # Copyright (c) 2008-2018. 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
26 sub cleanup_ctn {
27     my $ctn = shift;        # cleanup the content of a macro call
28     $ctn =~ s/^\s*(.*)\s*$/$1/gs;
29     my @elms;
30     print "ctn=$ctn\n" if $debug > 1;
31     if ($ctn =~ m/^(\w+)\s*,\s*(\w+)\s*,\s*"?([^"]*)"?$/s) {
32         # Perfect, we got 0->name; 1->anc; 2->desc
33         $elms[0] = $1;
34         $elms[1] = $2;
35         $elms[2] = $3;
36     } elsif ($ctn =~ m/^(\w+)\s*,\s*"?([^"]*)"?$/s) {
37         # Mmm. got no ancestor. Add the default one.
38         $elms[0] = $1;
39         $elms[1] = "XBT_LOG_ROOT_CAT";
40         $elms[2] = $2;
41     } else {
42         die "Unparsable content: $ctn\n";
43     }
44     $elms[2] =~ s/\\\\/\\/gs;
45     return @elms;
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 and C++ comments
61     $data =~ s|/\*.*?\*/||sg;
62     $data =~ s|//.*$||mg;
63
64     while ($data =~ s/^.*?XBT_LOG_NEW(_DEFAULT)?_(SUB)?CATEGORY\(//s) {
65         $data =~ s/([^"]*"[^"]*")\)//s || die "unparsable macro: $data";
66
67         my ($name,$anc,$desc) = cleanup_ctn($1);
68
69         # build the tree, checking for name conflict
70         die "ERROR: Category name conflict: $name used several times (in $ancestor{$name} and $anc, last time in $filename)\n"
71            if defined ($ancestor{$name}) && $ancestor{$name} ne $anc && defined ($desc{$name}) && $desc{$name} ne $desc;
72        $ancestor{$name}=$anc;
73        $desc{$name}=$desc;
74
75        print " $name -> $anc\n" if $debug;
76    }
77 }
78 # Retrieve all the file names, and add their content to $data
79 my $data;
80 open FILES, "find ../src/ ../tools/ ../include/ -name '*.c' -o -name '*.cpp' |" || die "Cannot search for the source file names: $!\n";
81 while (my $file=<FILES>) {
82     chomp $file;
83     parse_file($file);  
84 }
85 parse_file("../include/xbt/sysdep.h");  
86 close FILES;
87
88 # Display the tree, looking for disconnected elems
89 my %used;
90
91 sub display_subtree {
92     my $name=shift;
93     my $indent=shift;
94
95     $used{$name} = 1;
96     unless ($name eq "XBT_LOG_ROOT_CAT") { # do not display the root
97         print "$indent - $name: ".($desc{$name}|| "(undocumented)")."\n";
98     }
99     foreach my $cat (grep {$ancestor{$_} eq $name} sort keys %ancestor) {
100         display_subtree($cat,"$indent  ");
101     }
102 }
103
104 display_subtree("XBT_LOG_ROOT_CAT","");
105
106 map {
107     warn "Category $_ does not seem to be connected to the root (anc=$ancestor{$_})\n";
108 } grep {!defined $used{$_}} sort keys %ancestor;
109
110 print "@}*/\n";