style.css: fix footnote placement
[gtk-doc.git] / gtkdoc-scan.in
blob567b5b01c397aa3e0897be1934e5f29fa265d274
1 #!@PERL@ -w
2 # -*- cperl -*-
4 # gtk-doc - GTK DocBook documentation generator.
5 # Copyright (C) 1998  Damon Chaplin
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #############################################################################
23 # Script      : gtkdoc-scan
24 # Description : Extracts declarations of functions, macros, enums, structs
25 #                and unions from header files.
27 #                It is called with a module name, an optional source directory,
28 #                an optional output directory, and the header files to scan.
30 #                It outputs all declarations found to a file named
31 #                '$MODULE-decl.txt', and the list of decarations to another
32 #                file '$MODULE-decl-list.txt'.
34 #                This second list file is typically copied to
35 #                '$MODULE-sections.txt' and organized into sections ready to
36 #                output the SGML pages.
37 #############################################################################
39 use strict;
40 use Getopt::Long;
41 use Cwd qw(realpath);
43 push @INC, '@PACKAGE_DATA_DIR@';
44 require "gtkdoc-common.pl";
46 # Options
48 # name of documentation module
49 my $MODULE;
50 my $OUTPUT_DIR;
51 my @SOURCE_DIRS;
52 my $IGNORE_HEADERS = "";
53 my $REBUILD_TYPES;
54 my $REBUILD_SECTIONS;
55 my $PRINT_VERSION;
56 my $PRINT_HELP;
57 # regexp matching cpp symbols which surround deprecated stuff
58 # e.g. 'GTK_ENABLE_BROKEN|GTK_DISABLE_DEPRECATED'
59 # these are detected if they are used as #if FOO, #ifndef FOO, or #ifdef FOO
60 my $DEPRECATED_GUARDS;
61 # regexp matching decorators that should be ignored
62 my $IGNORE_DECORATORS;
64 my %optctl = (module => \$MODULE,
65               'source-dir' => \@SOURCE_DIRS,
66               'ignore-headers' => \$IGNORE_HEADERS,
67               'output-dir' => \$OUTPUT_DIR,
68               'rebuild-types' => \$REBUILD_TYPES,
69               'rebuild-sections' => \$REBUILD_SECTIONS,
70               'version' => \$PRINT_VERSION,
71               'help' => \$PRINT_HELP,
72               'deprecated-guards' => \$DEPRECATED_GUARDS,
73               'ignore-decorators' => \$IGNORE_DECORATORS);
74 GetOptions(\%optctl, "module=s", "source-dir:s", "ignore-headers:s",
75            "output-dir:s", "rebuild-types", "rebuild-sections", "version",
76            "help", "deprecated-guards:s", "ignore-decorators:s");
78 if ($PRINT_VERSION) {
79     print "@VERSION@\n";
80     exit 0;
83 if (!$MODULE) {
84     $PRINT_HELP = 1;
87 if ($PRINT_HELP) {
88     print <<EOF;
89 gtkdoc-scan version @VERSION@ - scan header files for public symbols
91 --module=MODULE_NAME       Name of the doc module being parsed
92 --source-dir=DIRNAME       Directories containing the source files to scan
93 --ignore-headers=FILES     A space-separated list of header files/dirs not to
94                            scan
95 --output-dir=DIRNAME       The directory where the results are stored
96 --deprecated-guards=GUARDS A |-separated list of symbols used as deprecation
97                            guards
98 --ignore-decorators=DECS   A |-separated list of addition decorators in
99                            declarations that should be ignored
100 --rebuild-sections         Rebuild (overwrite) the MODULE-sections.txt file
101 --rebuild-types            Automatically recreate the MODULE.types file using
102                            all the *_get_type() functions found
103 --version                  Print the version of this program
104 --help                     Print this help
106     exit 0;
109 $DEPRECATED_GUARDS = $DEPRECATED_GUARDS ? $DEPRECATED_GUARDS : "does_not_match_any_cpp_symbols_at_all_nope";
111 $IGNORE_DECORATORS = $IGNORE_DECORATORS || "(?=no)match";
113 $OUTPUT_DIR = $OUTPUT_DIR ? $OUTPUT_DIR : ".";
115 if (!-d ${OUTPUT_DIR}) {
116     mkdir($OUTPUT_DIR, 0755) || die "Cannot create $OUTPUT_DIR: $!";
119 my $old_decl_list = "${OUTPUT_DIR}/$MODULE-decl-list.txt";
120 my $new_decl_list = "${OUTPUT_DIR}/$MODULE-decl-list.new";
121 my $old_decl = "${OUTPUT_DIR}/$MODULE-decl.txt";
122 my $new_decl = "${OUTPUT_DIR}/$MODULE-decl.new";
123 my $old_types = "${OUTPUT_DIR}/$MODULE.types";
124 my $new_types = "${OUTPUT_DIR}/$MODULE.types.new";
125 my $sections_file = "${OUTPUT_DIR}/$MODULE-sections.txt";
127 # If this is the very first run then we create the .types file automatically.
128 if (! -e $sections_file && ! -e $old_types) {
129     $REBUILD_TYPES = 1;
132 open (DECLLIST, ">$new_decl_list")
133     || die "Can't open $new_decl_list";
134 open (DECL, ">$new_decl")
135     || die "Can't open $new_decl";
136 if ($REBUILD_TYPES) {
137     open (TYPES, ">$new_types")
138         || die "Can't open $new_types";
141 my %section_list = ();
142 my $file;
144 my @get_types = ();
147 # do not read files twice; checking it here permits to give both srcdir and
148 # builddir as --source-dir without fear of duplicities
149 my %seen_headers;
151 # The header files to scan are passed in as command-line args.
152 for $file (@ARGV) {
153     &ScanHeader ($file, \%section_list);
156 for my $dir (@SOURCE_DIRS) {
157     &ScanHeaders ($dir, \%section_list);
160 ## FIXME: sort by key and output
161 #print DECLLIST $section_list;
162 my $section;
163 foreach $section (sort(keys %section_list)) {
164     print DECLLIST "$section_list{$section}";
167 close (DECLLIST);
168 close (DECL);
170 if ($REBUILD_TYPES) {
171     my $func;
173     foreach $func (sort(@get_types)) {
174        print TYPES "$func\n";
175     }
176     close (TYPES);
177     &UpdateFileIfChanged ($old_types, $new_types, 1);
179     # remove the file if empty
180     if (scalar (@get_types) == 0) {
181         unlink ("$new_types");
182     }
185 &UpdateFileIfChanged ($old_decl_list, $new_decl_list, 1);
186 &UpdateFileIfChanged ($old_decl, $new_decl, 1);
188 # If there is no MODULE-sections.txt file yet or we are asked to rebuild it,
189 # we copy the MODULE-decl-list.txt file into its place. The user can tweak it
190 # later if they want.
191 if ($REBUILD_SECTIONS || ! -e $sections_file) {
192   `cp $old_decl_list $sections_file`;
195 # If there is no MODULE-overrides.txt file we create an empty one
196 # because EXTRA_DIST in gtk-doc.make requires it.
197 my $overrides_file = "${OUTPUT_DIR}/$MODULE-overrides.txt";
198 if (! -e $overrides_file) {
199   `touch $overrides_file`;
204 #############################################################################
205 # Function    : ScanHeaders
206 # Description : This scans a directory tree looking for header files.
208 # Arguments   : $source_dir - the directory to scan.
209 #               $section_list - a reference to the hashmap of sections.
210 #############################################################################
212 sub ScanHeaders {
213     my ($source_dir, $section_list) = @_;
214     @TRACE@("Scanning source directory: $source_dir");
216     # This array holds any subdirectories found.
217     my (@subdirs) = ();
219     opendir (SRCDIR, $source_dir)
220         || die "Can't open source directory $source_dir: $!";
221     my $file;
222     foreach $file (readdir (SRCDIR)) {
223         if ($file eq '.' || $file eq '..' || $file =~ /^\./) {
224             next;
225         } elsif (-d "$source_dir/$file") {
226             push (@subdirs, $file);
227         } elsif ($file =~ m/\.h$/) {
228             &ScanHeader ("$source_dir/$file", $section_list);
229         }
230     }
231     closedir (SRCDIR);
233     # Now recursively scan the subdirectories.
234     my $dir;
235     foreach $dir (@subdirs) {
236         next if ($IGNORE_HEADERS =~ m/(\s|^)\Q${dir}\E(\s|$)/);
237         &ScanHeaders ("$source_dir/$dir", $section_list);
238     }
242 #############################################################################
243 # Function    : ScanHeader
244 # Description : This scans a header file, looking for declarations of
245 #                functions, macros, typedefs, structs and unions, which it
246 #                outputs to the DECL file.
247 # Arguments   : $input_file - the header file to scan.
248 #               $section_list - a reference to the hashmap of sections.
249 # Returns     : it adds declarations to the appropriate list.
250 #############################################################################
252 sub ScanHeader {
253     my ($input_file, $section_list) = @_;
255     my $list = "";                  # Holds the resulting list of declarations.
256     my $title = "";                 # Holds the title of the section    
257     my ($in_comment) = 0;                  # True if we are in a comment.
258     my ($in_declaration) = "";          # The type of declaration we are in, e.g.
259                                   #   'function' or 'macro'.
260     my ($skip_block) = 0;                  # True if we should skip a block.
261     my ($symbol);                  # The current symbol being declared.
262     my ($decl);                          # Holds the declaration of the current symbol.
263     my ($ret_type);                  # For functions and function typedefs this
264                                   #   holds the function's return type.
265     my ($pre_previous_line) = "";   # The pre-previous line read in - some Gnome
266                                   #   functions have the return type on one
267                                   #   line, the function name on the next,
268                                   #   and the rest of the declaration after.
269     my ($previous_line) = "";          # The previous line read in - some Gnome
270                                   #   functions have the return type on one line
271                                   #   and the rest of the declaration after.
272     my ($first_macro) = 1;          # Used to try to skip the standard #ifdef XXX
273                                   #   #define XXX at the start of headers.
274     my ($level);                          # Used to handle structs/unions which contain
275                                   #   nested structs or unions.
276     my @objects = ();                  # Holds declarations that look like GtkObject
277                                   #   subclasses, which we remove from the list.
278     my ($internal) = 0;             # Set to 1 for internal symbols, we need to
279                                     #   fully parse, but don't add them to docs
280     my %forward_decls = ();         # hashtable of forward declarations, we skip
281                                     #   them if we find the real declaration
282                                     #   later.
283     my %doc_comments = ();          # hastable of doc-comment we found. We can
284                                     # use that to put element to the right
285                                     # sction in the generated section-file 
287     my $file_basename;
289     my $deprecated_conditional_nest = 0;
290     my $ignore_conditional_nest = 0;
292     my $deprecated = "";
293     my $doc_comment = "";
295     # Don't scan headers twice
296     my $canonical_input_file = realpath $input_file;
297     if (exists $seen_headers{$canonical_input_file}) {
298         @TRACE@("File already scanned: $input_file");
299         return;
300     }
301     $seen_headers{$canonical_input_file} = 1;
303     if ($input_file =~ m/^.*[\/\\](.*)\.h+$/) {
304         $file_basename = $1;
305     } else {
306         LogWarning(__FILE__,__LINE__,"Can't find basename of file $input_file");
307         $file_basename = $input_file;
308     }
310     # Check if the basename is in the list of headers to ignore.
311     if ($IGNORE_HEADERS =~ m/(\s|^)\Q${file_basename}\E\.h(\s|$)/) {
312         @TRACE@("File ignored: $input_file");
313         return;
314     }
315     # Check if the full name is in the list of headers to ignore.
316     if ($IGNORE_HEADERS =~ m/(\s|^)\Q${input_file}\E(\s|$)/) {
317         @TRACE@("File ignored: $input_file");
318         return;
319     }
321     if (! -f $input_file) {
322         LogWarning(__FILE__,__LINE__,"File doesn't exist: $input_file");
323         return;
324     }
326     @TRACE@("Scanning $input_file");
328     open(INPUT, $input_file)
329         || die "Can't open $input_file: $!";
330     while(<INPUT>) {
331         # If this is a private header, skip it.
332         if (m%^\s*/\*\s*<\s*private_header\s*>\s*\*/%) {
333             close(INPUT);
334             return;
335         }
337         # Skip to the end of the current comment.
338         if ($in_comment) {
339             @TRACE@("Comment: $_");
340             $doc_comment .= $_;
341             if (m%\*/%) {
342                 if ($doc_comment =~ m/\* ([a-zA-Z][a-zA-Z0-9_]+):/) {
343                   $doc_comments{lc($1)} = 1;
344                 }
345                 $in_comment = 0;
346                 $doc_comment = "";
347             }
348             next;
349         }
351         # Keep a count of #if, #ifdef, #ifndef nesting,
352         # and if we enter a deprecation-symbol-bracketed
353         # zone, take note.
354         if (m/^\s*#\s*if(?:n?def\b|\s+!?\s*defined\s*\()\s*(\w+)/) {
355             my $define_name = $1;
356             if ($deprecated_conditional_nest < 1 and $define_name =~ /$DEPRECATED_GUARDS/) {
357                  $deprecated_conditional_nest = 1;
358             } elsif ($deprecated_conditional_nest >= 1) {
359                  $deprecated_conditional_nest += 1;
360             }
361             if ($ignore_conditional_nest == 0 and $define_name =~ /__GTK_DOC_IGNORE__/) {
362                  $ignore_conditional_nest = 1;
363             } elsif ($ignore_conditional_nest > 0) {
364                  $ignore_conditional_nest += 1;
365             }
366         } elsif (m/^\s*#\sif/) {
367             if ($deprecated_conditional_nest >= 1) {
368                  $deprecated_conditional_nest += 1;
369             }
370             if ($ignore_conditional_nest > 0) {
371                  $ignore_conditional_nest += 1;
372             }
373         } elsif (m/^\s*#endif/) {
374             if ($deprecated_conditional_nest >= 1) {
375                 $deprecated_conditional_nest -= 1;
376             }
377             if ($ignore_conditional_nest > 0) {
378                 $ignore_conditional_nest -= 1;
379             }
380         }
382         # If we find a line containing _DEPRECATED, we hope that this is
383         # attribute based deprecation and also treat this as a deprecation
384         # guard, unless it's a macro definition.
385         if ($deprecated_conditional_nest == 0 and m/_DEPRECATED/) {
386             unless (m/^\s*#\s*(if*|define)/ or $in_declaration eq "enum") {
387                 @TRACE@("Found deprecation annotation (decl: '$in_declaration'): $_");
388                 $deprecated_conditional_nest += 0.1;
389             }
390         }
392         # set global that is used later when we do AddSymbolToList
393         if ($deprecated_conditional_nest > 0) {
394             $deprecated = "<DEPRECATED/>\n";
395         } else {
396             $deprecated = "";
397         }
399         if($ignore_conditional_nest) {
400             next;
401         }
403         if (!$in_declaration) {
404             # Skip top-level comments.
405             if (s%^\s*/\*%%) {
406                 if (m%\*/%) {
407                     @TRACE@("Found one-line comment: $_");
408                 } else {
409                     $in_comment = 1;
410                     $doc_comment = $_;
411                     @TRACE@("Found start of comment: $_");
412                 }
413                 next;
414             }
416             @TRACE@("0: $_");
418             # MACROS
420             if (m/^\s*#\s*define\s+(\w+)/) {
421                 $symbol = $1;
422                 $decl = $_;
423                 # We assume all macros which start with '_' are private, but
424                 # we accept '_' itself which is the standard gettext macro.
425                 # We also try to skip the first macro if it looks like the
426                 # standard #ifndef HEADER_FILE #define HEADER_FILE etc.
427                 # And we only want TRUE & FALSE defined in GLib (libdefs.h in
428                 # libgnome also defines them if they are not already defined).
429                 if (($symbol !~ m/^_/
430                      && ($previous_line !~ m/#ifndef\s+$symbol/
431                          || $first_macro == 0)
432                      && (($symbol ne 'TRUE' && $symbol ne 'FALSE')
433                          || $MODULE eq 'glib'))
434                     || $symbol eq "_") {
435                     $in_declaration = "macro";
436                     @TRACE@("Macro: $symbol");
437                 } else {
438                     @TRACE@("skipping Macro: $symbol");
439                     $in_declaration = "macro";
440                     $internal = 1;
441                 }
442                 $first_macro = 0;
445             # TYPEDEF'D FUNCTIONS (i.e. user functions)
447             #                        $1                                $3            $4             $5
448             } elsif (m/^\s*typedef\s+((const\s+|G_CONST_RETURN\s+)?\w+)(\s+const)?\s*(\**)\s*\(\*\s*(\w+)\)\s*\(/) {
449                 my $p3 = defined($3) ? $3 : "";
450                 $ret_type = "$1$p3 $4";
451                 $symbol = $5;
452                 $decl = $';
453                 $in_declaration = "user_function";
454                 @TRACE@("user function (1): $symbol, Returns: $ret_type");
456             #                                                       $1                                $3            $4             $5
457             } elsif (($previous_line =~ m/^\s*typedef\s*/) && m/^\s*((const\s+|G_CONST_RETURN\s+)?\w+)(\s+const)?\s*(\**)\s*\(\*\s*(\w+)\)\s*\(/) {
458                 my $p3 = defined($3) ? $3 : "";
459                 $ret_type = "$1$p3 $4";
460                 $symbol = $5;
461                 $decl = $';
462                 $in_declaration = "user_function";
463                 @TRACE@("user function (2): $symbol, Returns: $ret_type");
465             #                                                       $1            $2
466             } elsif (($previous_line =~ m/^\s*typedef\s*/) && m/^\s*(\**)\s*\(\*\s*(\w+)\)\s*\(/) {
467                 $ret_type = $1;
468                 $symbol = $2;
469                 $decl = $';
470                 #                                     $1                                $3
471                 if ($previous_line =~ m/^\s*typedef\s*((const\s+|G_CONST_RETURN\s+)?\w+)(\s+const)?\s*/) {
472                     my $p3 = defined($3) ? $3 : "";
473                     $ret_type = "$1$p3 ".$ret_type;
474                     $in_declaration = "user_function";
475                     @TRACE@("user function (3): $symbol, Returns: $ret_type");
477                 }
478             # FUNCTION POINTER VARIABLES
479             #                                                                     $1                                $3            $4             $5
480             } elsif (m/^\s*(?:\b(?:extern|G_INLINE_FUNC|${IGNORE_DECORATORS})\s*)*((const\s+|G_CONST_RETURN\s+)?\w+)(\s+const)?\s*(\**)\s*\(\*\s*(\w+)\)\s*\(/o) {
481                 my $p3 = defined($3) ? $3 : "";
482                 $ret_type = "$1$p3 $4";
483                 $symbol = $5;
484                 $decl = $';
485                 $in_declaration = "user_function";
486                 @TRACE@("function pointer variable: $symbol, Returns: $ret_type");
488             # ENUMS
490             } elsif (s/^\s*enum\s+_?(\w+)\s+\{/enum $1 {/) {
491                 # We assume that 'enum _<enum_name> {' is really the
492                 # declaration of enum <enum_name>.
493                 $symbol = $1;
494                 @TRACE@("plain enum: $symbol");
495                 $decl = $_;
496                 $in_declaration = "enum";
498             } elsif (m/^\s*typedef\s+enum\s+_?(\w+)\s+\1\s*;/) {
499                 # We skip 'typedef enum <enum_name> _<enum_name>;' as the enum will
500                 # be declared elsewhere.
501                 @TRACE@("skipping enum typedef: $1");
503             } elsif (m/^\s*typedef\s+enum/) {
504                 $symbol = "";
505                 @TRACE@("typedef enum: -");
506                 $decl = $_;
507                 $in_declaration = "enum";
510             # STRUCTS AND UNIONS
512             } elsif (m/^\s*typedef\s+(struct|union)\s+_(\w+)\s+\2\s*;/) {
513                 # We've found a 'typedef struct _<name> <name>;'
514                 # This could be an opaque data structure, so we output an
515                 # empty declaration. If the structure is actually found that
516                 # will override this.
517                 my $structsym = uc $1;
518                 @TRACE@("$structsym typedef: $2");
519                 $forward_decls{$2} = "<$structsym>\n<NAME>$2</NAME>\n$deprecated</$structsym>\n"
521             } elsif (m/^\s*(?:struct|union)\s+_(\w+)\s*;/) {
522                 # Skip private structs/unions.
523                 @TRACE@("private struct/union");
525             } elsif (m/^\s*(struct|union)\s+(\w+)\s*;/) {
526                 # Do a similar thing for normal structs as for typedefs above.
527                 # But we output the declaration as well in this case, so we
528                 # can differentiate it from a typedef.
529                 my $structsym = uc $1;
530                 @TRACE@("$structsym: $2");
531                 $forward_decls{$2} = "<$structsym>\n<NAME>$2</NAME>\n$_$deprecated</$structsym>\n";
533             } elsif (m/^\s*typedef\s+(struct|union)\s*\w*\s*{/) {
534                 $symbol = "";
535                 $decl = $_;
536                 $level = 0;
537                 $in_declaration = $1;
538                 @TRACE@("typedef struct/union $1");
540             # OTHER TYPEDEFS
542             } elsif (m/^\s*typedef\s+(?:struct|union)\s+\w+[\s\*]+(\w+)\s*;/) {
543                 @TRACE@("Found struct/union(*) typedef $1: $_");
544                 if (&AddSymbolToList (\$list, $1)) {
545                     print DECL "<TYPEDEF>\n<NAME>$1</NAME>\n$deprecated$_</TYPEDEF>\n";
546                 }
548             } elsif (m/^\s*(G_GNUC_EXTENSION\s+)?typedef\s+(.+[\s\*])(\w+)(\s*\[[^\]]+\])*\s*;/) {
549                 if ($2 !~ m/^struct\s/ && $2 !~ m/^union\s/) {
550                     @TRACE@("Found typedef: $_");
551                     if (&AddSymbolToList (\$list, $3)) {
552                         print DECL "<TYPEDEF>\n<NAME>$3</NAME>\n$deprecated$_</TYPEDEF>\n";
553                     }
554                 }
555             } elsif (m/^\s*typedef\s+/) {
556                 @TRACE@("Skipping typedef: $_");
559             # VARIABLES (extern'ed variables)
561             } elsif (m/^\s*(extern|[A-Za-z_]+VAR|${IGNORE_DECORATORS})\s+((const\s+|signed\s+|unsigned\s+|long\s+|short\s+)*\w+)(\s+\*+|\*+|\s)\s*(const\s+)*([A-Za-z]\w*)\s*;/o) {
562                 $symbol = $6;
563                 s/^\s*([A-Za-z_]+VAR)\b/extern/;
564                 $decl = $_;
565                 @TRACE@("Possible extern var $symbol: $decl");
566                 if (&AddSymbolToList (\$list, $symbol)) {
567                     print DECL "<VARIABLE>\n<NAME>$symbol</NAME>\n$deprecated$decl</VARIABLE>\n";
568                 }
571             # VARIABLES
573             } elsif (m/^\s*((const\s+|signed\s+|unsigned\s+|long\s+|short\s+)*\w+)(\s+\*+|\*+|\s)\s*(const\s+)*([A-Za-z]\w*)\s*\=/) {
574                 $symbol = $5;
575                 $decl = $_;
576                 @TRACE@("Possible global var $symbol: $decl");
577                 if (&AddSymbolToList (\$list, $symbol)) {
578                     print DECL "<VARIABLE>\n<NAME>$symbol</NAME>\n$deprecated$decl</VARIABLE>\n";
579                 }
581             # G_DECLARE_*
583             } elsif (m/.*G_DECLARE_(FINAL_TYPE|DERIVABLE_TYPE|INTERFACE)\s*\(/) {
584                 $in_declaration = "g-declare";
585                 $symbol = "G_DECLARE_$1";
586                 $decl = $';
588             # FUNCTIONS
590             # We assume that functions which start with '_' are private, so
591             # we skip them.
592             #                                                                     $1                                                                                                    $2                                                          $3
593             } elsif (m/^\s*(?:\b(?:extern|G_INLINE_FUNC|${IGNORE_DECORATORS})\s*)*((?:const\s+|G_CONST_RETURN\s+|signed\s+|unsigned\s+|long\s+|short\s+|struct\s+|union\s+|enum\s+)*\w+)((?:\s+|\*)+(?:\s*(?:\*+|\bconst\b|\bG_CONST_RETURN\b))*)\s*(_[A-Za-z]\w*)\s*\(/o) {
594                 $ret_type = $1;
595                 if (defined ($2)) { $ret_type .= " $2"; }
596                 $symbol = $3;
597                 $decl = $';
598                 @TRACE@("internal Function: $symbol, Returns: [$1][$2]");
599                 $in_declaration = "function";
600                 $internal = 1;
601                 if (m/^\s*G_INLINE_FUNC/) {
602                     @TRACE@("skip block after inline function");
603                     # now we we need to skip a whole { } block
604                     $skip_block = 1;
605                 }
607             #                                                                     $1                                                                                                    $2                                                          $3
608             } elsif (m/^\s*(?:\b(?:extern|G_INLINE_FUNC|${IGNORE_DECORATORS})\s*)*((?:const\s+|G_CONST_RETURN\s+|signed\s+|unsigned\s+|long\s+|short\s+|struct\s+|union\s+|enum\s+)*\w+)((?:\s+|\*)+(?:\s*(?:\*+|\bconst\b|\bG_CONST_RETURN\b))*)\s*([A-Za-z]\w*)\s*\(/o) {
609                 $ret_type = $1;
610                 if (defined ($2)) { $ret_type .= " $2"; }
611                 $symbol = $3;
612                 $decl = $';
613                 @TRACE@("Function (1): $symbol, Returns: [$1][$2]");
614                 $in_declaration = "function";
615                 if (m/^\s*G_INLINE_FUNC/) {
616                     @TRACE@("skip block after inline function");
617                     # now we we need to skip a whole { } block
618                     $skip_block = 1;
619                 }
621             # Try to catch function declarations which have the return type on
622             # the previous line. But we don't want to catch complete functions
623             # which have been declared G_INLINE_FUNC, e.g. g_bit_nth_lsf in
624             # glib, or 'static inline' functions.
625             } elsif (m/^\s*([A-Za-z]\w*)\s*\(/) {
626                 $symbol = $1;
627                 $decl = $';
629                 if ($previous_line !~ m/^\s*G_INLINE_FUNC/) {
630                     if ($previous_line !~ m/^\s*static\s+/) {
631                         #                                                                     $1                                                                                                   $2
632                         if ($previous_line =~ m/^\s*(?:\b(?:extern|${IGNORE_DECORATORS})\s*)*((?:const\s+|G_CONST_RETURN\s+|signed\s+|unsigned\s+|long\s+|short\s+|struct\s+|union\s+|enum\s+)*\w+)((?:\s*(?:\*+|\bconst\b|\bG_CONST_RETURN\b))*)\s*$/o) {
633                             $ret_type = $1;
634                             if (defined ($2)) { $ret_type .= " $2"; }
635                             @TRACE@("Function  (2): $symbol, Returns: $ret_type");
636                             $in_declaration = "function";
637                         }
638                     } else {
639                         @TRACE@("skip block after inline function");
640                         # now we we need to skip a whole { } block
641                         $skip_block = 1;
642                         #                                                                                  $1                                                                                                    $2
643                         if ($previous_line =~ m/^\s*(?:\b(?:extern|static|inline|${IGNORE_DECORATORS})\s*)*((?:const\s+|G_CONST_RETURN\s+|signed\s+|unsigned\s+|long\s+|short\s+|struct\s+|union\s+|enum\s+)*\w+)((?:\s*(?:\*+|\bconst\b|\bG_CONST_RETURN\b))*)\s*$/o) {
644                             $ret_type = $1;
645                             if (defined ($2)) { $ret_type .= " $2"; }
646                             @TRACE@("Function  (3): $symbol, Returns: $ret_type");
647                             $in_declaration = "function";
648                         }
649                     }
650                 }
651                 else {
652                     if ($previous_line !~ m/^\s*static\s+/) {
653                         @TRACE@("skip block after inline function");
654                         # now we we need to skip a whole { } block
655                         $skip_block = 1;
656                         #                                                                                  $1                                                                                                    $2
657                         if ($previous_line =~ m/^\s*(?:\b(?:extern|G_INLINE_FUNC|${IGNORE_DECORATORS})\s*)*((?:const\s+|G_CONST_RETURN\s+|signed\s+|unsigned\s+|long\s+|short\s+|struct\s+|union\s+|enum\s+)*\w+)((?:\s*(?:\*+|\bconst\b|\bG_CONST_RETURN\b))*)\s*$/o) {
658                             $ret_type = $1;
659                             if (defined ($2)) { $ret_type .= " $2"; }
660                             @TRACE@("Function (4): $symbol, Returns: $ret_type");
661                             $in_declaration = "function";
662                         }
663                     }
664                 }
666             # Try to catch function declarations with the return type and name
667             # on the previous line(s), and the start of the parameters on this.
668             } elsif (m/^\s*\(/) {
669                 $decl = $';
670                 if ($previous_line =~ m/^\s*(?:\b(?:extern|G_INLINE_FUNC|${IGNORE_DECORATORS})\s*)*((?:const\s+|G_CONST_RETURN\s+|signed\s+|unsigned\s+|enum\s+)*\w+)(\s+\*+|\*+|\s)\s*([A-Za-z]\w*)\s*$/o) {
671                     $ret_type = "$1 $2";
672                     $symbol = $3;
673                     @TRACE@("Function (5): $symbol, Returns: $ret_type");
674                     $in_declaration = "function";
676                 } elsif ($previous_line =~ m/^\s*\w+\s*$/
677                          && $pre_previous_line =~ m/^\s*(?:\b(?:extern|G_INLINE_FUNC|${IGNORE_DECORATORS})\s*)*((?:const\s+|G_CONST_RETURN\s+|signed\s+|unsigned\s+|struct\s+|union\s+|enum\s+)*\w+(?:\**\s+\**(?:const|G_CONST_RETURN))?(?:\s+|\s*\*+))\s*$/o) {
678                     $ret_type = $1;
679                     $ret_type =~ s/\s*\n//;
680                     $in_declaration = "function";
682                     $symbol = $previous_line;
683                     $symbol =~ s/^\s+//;
684                     $symbol =~ s/\s*\n//;
685                     @TRACE@("Function (6): $symbol, Returns: $ret_type");
686                 }
688             #} elsif (m/^extern\s+/) {
689                 #print "DEBUG: Skipping extern: $_";
692             # STRUCTS
694             } elsif (m/^\s*struct\s+_?(\w+)\s*\*/) {
695                 # Skip 'struct _<struct_name> *', since it could be a
696                 # return type on its own line.
698             } elsif (m/^\s*struct\s+_?(\w+)/) {
699                 # We assume that 'struct _<struct_name>' is really the
700                 # declaration of struct <struct_name>.
701                 $symbol = $1;
702                 $decl = $_;
703                  # we will find the correct level as below we do $level += tr/{//;
704                 $level = 0;
705                 $in_declaration = "struct";
706                 @TRACE@("Struct(_): $symbol");
709             # UNIONS
711             } elsif (m/^\s*union\s+_(\w+)\s*\*/) {
712                     # Skip 'union _<union_name> *' (see above)
713             } elsif (m/^\s*union\s+_(\w+)/) {
714                 $symbol = $1;
715                 $decl = $_;
716                 $level = 0;
717                 $in_declaration = "union";
718                 @TRACE@("Union(_): $symbol");
719             }
721         } else {
722             @TRACE@("1: [$skip_block] $_");
723             # If we were already in the middle of a declaration, we simply add
724             # the current line onto the end of it.
725             if ($skip_block == 0) {
726                 $decl .= $_;
727             } else {
728                 # Remove all nested pairs of curly braces.
729                 while ($_ =~ s/{[^{]*}//g) { }
730                 # Then hope at most one remains in the line...
731                 if (m%(.*?){%) {
732                     if ($skip_block == 1) {
733                         $decl .= $1;
734                     }
735                     $skip_block += 1;
736                 } elsif (m%}%) {
737                     $skip_block -= 1;
738                     if ($skip_block == 1) {
739                         # this is a hack to detect the end of declaration
740                         $decl .= ";";
741                         $skip_block = 0;
742                         @TRACE@("2: ---");
743                     }
744                 } else {
745                     if ($skip_block == 1) {
746                         $decl .= $_;
747                     }
748                 }
749             }
750         }
752         #if ($in_declaration ne '') {
753         #    print "$in_declaration = $decl\n";
754         #}
756         if ($in_declaration eq "g-declare") {
757             if ($decl =~ s/\s*(\w+)\s*,\s*(\w+)\s*,\s*(\w+)\s*,\s*(\w+)\s*,\s*(\w+)\s*\).*$//) {
758                 my $ModuleObjName = $1;
759                 my $module_obj_name = $2;
760                 if ($REBUILD_TYPES) {
761                     push (@get_types, "${module_obj_name}_get_type");
762                 }
763                 $forward_decls{$ModuleObjName} = "<STRUCT>\n<NAME>$ModuleObjName</NAME>\n$deprecated</STRUCT>\n";
764                 if ($symbol =~ /^G_DECLARE_DERIVABLE/) {
765                     $forward_decls{"${ModuleObjName}Class"} = "<STRUCT>\n<NAME>${ModuleObjName}Class</NAME>\n$deprecated</STRUCT>\n";
766                 }
767                 if ($symbol =~ /^G_DECLARE_INTERFACE/) {
768                     $forward_decls{"${ModuleObjName}Interface"} = "<STRUCT>\n<NAME>${ModuleObjName}Interface</NAME>\n$deprecated</STRUCT>\n";
769                 }
770                 $in_declaration = "";
771             }
772         }
774         # Note that sometimes functions end in ') G_GNUC_PRINTF (2, 3);' or
775         # ') __attribute__ (...);'.
776         if ($in_declaration eq 'function') {
777             if ($decl =~ s/\)\s*(G_GNUC_.*|.*DEPRECATED.*|${IGNORE_DECORATORS}\s*|__attribute__\s*\(.*\)\s*)*;.*$//s) {
778                 if ($internal == 0) {
779                      $decl =~ s%/\*.*?\*/%%gs;        # remove comments.
780                      #$decl =~ s/^\s+//;                # remove leading whitespace.
781                      #$decl =~ s/\s+$//;                # remove trailing whitespace.
782                      $decl =~ s/\s*\n\s*/ /gs;        # consolidate whitespace at start
783                                                    # and end of lines.
784                      $ret_type =~ s%/\*.*?\*/%%g;        # remove comments in ret type.
785                      if (&AddSymbolToList (\$list, $symbol)) {
786                          print DECL "<FUNCTION>\n<NAME>$symbol</NAME>\n$deprecated<RETURNS>$ret_type</RETURNS>\n$decl\n</FUNCTION>\n";
787                          if ($REBUILD_TYPES) {
788                              # check if this looks like a get_type function and if so remember
789                              if (($symbol =~ m/_get_type$/) && ($ret_type =~ m/GType/) && ($decl =~ m/^(void|)$/)) {
790                                  @TRACE@("Adding get-type: [$ret_type] [$symbol] [$decl]\tfrom $input_file");
791                                  push (@get_types, $symbol);
792                              }
793                          }
794                      }
795                 } else {
796                      $internal = 0;
797                 }
798                 $deprecated_conditional_nest = int($deprecated_conditional_nest);
799                 $in_declaration = "";
800                 $skip_block = 0;
801             }
802         }
804         if ($in_declaration eq 'user_function') {
805             if ($decl =~ s/\).*$//) {
806                 if (&AddSymbolToList (\$list, $symbol)) {
807                     print DECL "<USER_FUNCTION>\n<NAME>$symbol</NAME>\n$deprecated<RETURNS>$ret_type</RETURNS>\n$decl</USER_FUNCTION>\n";
808                 }
809                 $deprecated_conditional_nest = int($deprecated_conditional_nest);
810                 $in_declaration = "";
811             }
812         }
814         if ($in_declaration eq 'macro') {
815             if ($decl !~ m/\\\s*$/) {
816                 if ($internal == 0) {
817                     if (&AddSymbolToList (\$list, $symbol)) {
818                         print DECL "<MACRO>\n<NAME>$symbol</NAME>\n$deprecated$decl</MACRO>\n";
819                     }
820                 } else {
821                     $internal = 0;
822                 }
823                 $deprecated_conditional_nest = int($deprecated_conditional_nest);
824                 $in_declaration = "";
825             }
826         }
828         if ($in_declaration eq 'enum') {
829             if ($decl =~ m/\}\s*(\w+)?;\s*$/) {
830                 if ($symbol eq "") {
831                     $symbol = $1;
832                 }
833                 if (&AddSymbolToList (\$list, $symbol)) {
834                     print DECL "<ENUM>\n<NAME>$symbol</NAME>\n$deprecated$decl</ENUM>\n";
835                 }
836                 $deprecated_conditional_nest = int($deprecated_conditional_nest);
837                 $in_declaration = "";
838             }
839         }
841         # We try to handle nested stucts/unions, but unmatched brackets in
842         # comments will cause problems.
843         if ($in_declaration eq 'struct' or $in_declaration eq 'union') {
844             if (($level <= 1) && ($decl =~ m/\n\}\s*(\w*);\s*$/)) {
845                 if ($symbol eq "") {
846                     $symbol = $1;
847                 }
849                 if ($symbol =~ m/^(\S+)(Class|Iface|Interface)\b/) {
850                     my $objectname = $1;
851                     @TRACE@("Found object: $1");
852                     $title = "<TITLE>$objectname</TITLE>\n";
853                     push (@objects, $objectname);
854                 }
855                 @TRACE@("Store struct: $symbol");
856                 if (&AddSymbolToList (\$list, $symbol)) {
857                     my $structsym = uc $in_declaration;
858                     print DECL "<$structsym>\n<NAME>$symbol</NAME>\n$deprecated$decl</$structsym>\n";
859                     if (defined($forward_decls{$symbol})) {
860                         undef($forward_decls{$symbol});
861                     }
862                 }
863                 $deprecated_conditional_nest = int($deprecated_conditional_nest);
864                 $in_declaration = "";
865             } else {
866                 # We use tr to count the brackets in the line, and adjust
867                 # $level accordingly.
868                 $level += tr/{//;
869                 $level -= tr/}//;
870                 @TRACE@("struct/union level : $level");
871             }
872         }
874         $pre_previous_line = $previous_line;
875         $previous_line = $_;
876     }
877     close(INPUT);
879     # print remaining forward declarations
880     foreach $symbol (keys %forward_decls) {
881         if (defined($forward_decls{$symbol})) {
882             &AddSymbolToList (\$list, $symbol);
883             print DECL $forward_decls{$symbol};
884         }
885     }
887     # add title
888     $list = "$title$list";
889     
890     @TRACE@("Scanning $input_file done\n");
891     
892     # Try to separate the standard macros and functions, placing them at the
893     # end of the current section, in a subsection named 'Standard'.
894     # do this in a loop to catch object, enums and flags
895     my ($class,$lclass,$prefix,$lprefix);
896     my ($standard_decl) = "";
897     do {
898         if ($list =~ m/^(\S+)_IS_(\S*)_CLASS\n/m) {
899             $prefix = $1;
900             $lprefix = lc($prefix);
901             $class = $2;
902             $lclass = lc($class);
903             @TRACE@("Found gobject type '${prefix}_$class' from is_class macro\n");
904         } elsif ($list =~ m/^(\S+)_IS_(\S*)\n/m) {
905             $prefix = $1;
906             $lprefix = lc($prefix);
907             $class = $2;
908             $lclass = lc($class);
909             @TRACE@("Found gobject type '${prefix}_$class' from is_ macro\n");
910         } elsif ($list =~ m/^(\S+?)_(\S*)_get_type\n/m) {
911             $lprefix = $1;
912             $prefix = uc($lprefix);
913             $lclass = $2;
914             $class = uc($lclass);
915             @TRACE@("Found gobject type '${prefix}_$class' from get_type function\n");
916         } else {
917           $class = $lclass = "";
918         }
919     
920         if ($class ne "") {
921             my ($cclass) = $lclass;
922             $cclass =~ s/_//g;
923             my ($type) = $lprefix.$cclass;
924             
925             if ($list =~ s/^${type}Private\n//im)               { $standard_decl .= $&; }
926             
927             # We only leave XxYy* in the normal section if they have docs 
928             if (! defined($doc_comments{$type})) {
929               @TRACE@("  Hide instance docs for $type");
930               if ($list =~ s/^${type}\n//im)                    { $standard_decl .= $&; }
931             }
932             if (! defined($doc_comments{$type."class"})) {
933               @TRACE@("  Hide class docs for $type");
934               if ($list =~ s/^${type}Class\n//im)               { $standard_decl .= $&; }
935             }
936             if (! defined($doc_comments{$type."interface"})) {
937               @TRACE@("  Hide iface docs for $type");
938               if ($list =~ s/^$type}Interface\n//im)            { $standard_decl .= $&; }
939             }
940             if (! defined($doc_comments{$type."iface"})) {
941               @TRACE@("  Hide iface docs for $type");
942               if ($list =~ s/${type}Iface\n//im)                { $standard_decl .= $&; }
943             }
944             
945             while ($list =~ s/^\S+_IS_$class\n//m)              { $standard_decl .= $&; }
946             while ($list =~ s/^\S+_TYPE_$class\n//m)            { $standard_decl .= $&; }
947             while ($list =~ s/^\S+_${lclass}_get_type\n//m)     { $standard_decl .= $&; }
948             while ($list =~ s/^\S+_${class}_CLASS\n//m)         { $standard_decl .= $&; }
949             while ($list =~ s/^\S+_IS_${class}_CLASS\n//m)      { $standard_decl .= $&; }
950             while ($list =~ s/^\S+_${class}_GET_CLASS\n//m)     { $standard_decl .= $&; }
951             while ($list =~ s/^\S+_${class}_GET_IFACE\n//m)     { $standard_decl .= $&; }
952             while ($list =~ s/^\S+_${class}_GET_INTERFACE\n//m) { $standard_decl .= $&; }
953     
954             # We do this one last, otherwise it tends to be caught by the IS_$class macro
955             while ($list =~ s/^\S+_$class\n//m)                 { $standard_decl .= $&; }
956             
957             @TRACE@("Decl '".join(",",split("\n",$list))."'\n");
958             @TRACE@("Std  '".join(",",split("\n",$standard_decl))."'\n");
959         }
960     } while ($class ne "");
961     if ($standard_decl ne "") {
962       # sort the symbols
963       $standard_decl=join("\n",sort(split("\n",$standard_decl)))."\n";
964       $list .= "<SUBSECTION Standard>\n$standard_decl";
965     }
966     if ($list ne "") {
967         $$section_list{$file_basename} .= "<SECTION>\n<FILE>$file_basename</FILE>\n$list</SECTION>\n\n";
968     }
972 #############################################################################
973 # Function    : AddSymbolToList
974 # Description : This adds the symbol to the list of declarations, but only if
975 #                it is not already in the list.
976 # Arguments   : $list - reference to the list of symbols, one on each line.
977 #                $symbol - the symbol to add to the list.
978 #############################################################################
980 sub AddSymbolToList {
981     my ($list, $symbol) = @_;
983     if ($$list =~ m/\b\Q$symbol\E\b/) {
984          #print "Symbol $symbol already in list. skipping\n";
985          # we return 0 to skip outputting another entry to -decl.txt
986          # this is to avoid redeclarations (e.g. in conditional
987          # sections).
988         return 0;
989     }
990     $$list .= "$symbol\n";
991     return 1;