tests: add an empty project where we even regenerate the tester-docs.xml
[gtk-doc.git] / gtkdoc-scan.in
blob2db8a0ea25dcb99d3aaa0033c1c79a611b0b7b4c
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 not to scan
94 --output-dir=DIRNAME       The directory where the results are stored
95 --deprecated-guards=GUARDS A |-separated list of symbols used as deprecation
96                            guards
97 --ignore-decorators=DECS   A |-separated list of addition decorators in
98                            declarations that should be ignored
99 --rebuild-sections         Rebuild (overwrite) the MODULE-sections.txt file
100 --rebuild-types            Automatically recreate the MODULE.types file using
101                            all the *_get_type() functions found
102 --version                  Print the version of this program
103 --help                     Print this help
105     exit 0;
108 $DEPRECATED_GUARDS = $DEPRECATED_GUARDS ? $DEPRECATED_GUARDS : "does_not_match_any_cpp_symbols_at_all_nope";
110 $IGNORE_DECORATORS = $IGNORE_DECORATORS || "(?=no)match";
112 $OUTPUT_DIR = $OUTPUT_DIR ? $OUTPUT_DIR : ".";
114 if (!-d ${OUTPUT_DIR}) {
115     mkdir($OUTPUT_DIR, 0755) || die "Cannot create $OUTPUT_DIR: $!";
118 my $old_decl_list = "${OUTPUT_DIR}/$MODULE-decl-list.txt";
119 my $new_decl_list = "${OUTPUT_DIR}/$MODULE-decl-list.new";
120 my $old_decl = "${OUTPUT_DIR}/$MODULE-decl.txt";
121 my $new_decl = "${OUTPUT_DIR}/$MODULE-decl.new";
122 my $old_types = "${OUTPUT_DIR}/$MODULE.types";
123 my $new_types = "${OUTPUT_DIR}/$MODULE.types.new";
124 my $sections_file = "${OUTPUT_DIR}/$MODULE-sections.txt";
126 # If this is the very first run then we create the .types file automatically.
127 if (! -e $sections_file && ! -e $old_types) {
128     $REBUILD_TYPES = 1;
131 open (DECLLIST, ">$new_decl_list")
132     || die "Can't open $new_decl_list";
133 open (DECL, ">$new_decl")
134     || die "Can't open $new_decl";
135 if ($REBUILD_TYPES) {
136     open (TYPES, ">$new_types")
137         || die "Can't open $new_types";
140 my $main_list = "";
141 my $object_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, \$object_list, \$main_list);
156 for my $dir (@SOURCE_DIRS) {
157     &ScanHeaders ($dir, \$object_list, \$main_list);
160 print DECLLIST $object_list, $main_list;
161 close (DECLLIST);
162 close (DECL);
163 if ($REBUILD_TYPES) {
164     my $func;
166     foreach $func (sort(@get_types)) {
167        print TYPES "$func\n"; 
168     }
169     close (TYPES);
172 &UpdateFileIfChanged ($old_decl_list, $new_decl_list, 1);
173 &UpdateFileIfChanged ($old_decl, $new_decl, 1);
174 &UpdateFileIfChanged ($old_types, $new_types, 1) if ($REBUILD_TYPES);
176 # If there is no MODULE-sections.txt file yet or we are asked to rebuild it,
177 # we copy the MODULE-decl-list.txt file into its place. The user can tweak it
178 # later if they want.
179 if ($REBUILD_SECTIONS || ! -e $sections_file) {
180   `cp $old_decl_list $sections_file`;
183 # If there is no MODULE-overrides.txt file we create an empty one.
184 # FIXME: why?
185 #my $overrides_file = "${OUTPUT_DIR}/$MODULE-overrides.txt";
186 #if (! -e $overrides_file) {
187 #  `touch $overrides_file`;
192 #############################################################################
193 # Function    : ScanHeaders
194 # Description : This scans a directory tree looking for header files.
196 # Arguments   : $source_dir - the directory to scan.
197 #               $object_list - a reference to the list of object functions &
198 #                       declarations.
199 #               $main_list - a reference to the list of other declarations.
200 #############################################################################
202 sub ScanHeaders {
203     my ($source_dir, $object_list, $main_list) = @_;
204     #print "Scanning source directory: $source_dir\n";
206     # This array holds any subdirectories found.
207     my (@subdirs) = ();
209     opendir (SRCDIR, $source_dir)
210         || die "Can't open source directory $source_dir: $!";
211     my $file;
212     foreach $file (readdir (SRCDIR)) {
213         if ($file eq '.' || $file eq '..' || $file =~ /^\./) {
214             next;
215         } elsif (-d "$source_dir/$file") {
216             push (@subdirs, $file);
217         } elsif ($file =~ m/\.h$/) {
218             &ScanHeader ("$source_dir/$file", $object_list, $main_list);
219         }
220     }
221     closedir (SRCDIR);
223     # Now recursively scan the subdirectories.
224     my $dir;
225     foreach $dir (@subdirs) {
226         next if ($IGNORE_HEADERS =~ m/(\s|^)\Q${dir}\E(\s|$)/);
227         &ScanHeaders ("$source_dir/$dir", $object_list, $main_list);
228     }
232 #############################################################################
233 # Function    : ScanHeader
234 # Description : This scans a header file, looking for declarations of
235 #               functions, macros, typedefs, structs and unions, which it
236 #               outputs to the DECL file.
237 # Arguments   : $input_file - the header file to scan.
238 #               $object_list - a reference to the list of object functions &
239 #                       declarations.
240 #               $main_list - a reference to the list of other declarations.
241 # Returns     : it adds declarations to the appropriate list.
242 #############################################################################
244 sub ScanHeader {
245     my ($input_file, $object_list, $main_list) = @_;
247     my $list = "";                # Holds the resulting list of declarations.
248     my ($in_comment) = 0;                 # True if we are in a comment.
249     my ($in_declaration) = "";    # The type of declaration we are in, e.g.
250                                   #   'function' or 'macro'.
251     my ($skip_block) = 0;                 # True if we should skip a block.
252     my ($symbol);                 # The current symbol being declared.
253     my ($decl);                   # Holds the declaration of the current symbol.
254     my ($ret_type);               # For functions and function typedefs this
255                                   #   holds the function's return type.
256     my ($pre_previous_line) = "";   # The pre-previous line read in - some Gnome
257                                   #   functions have the return type on one
258                                   #   line, the function name on the next,
259                                   #   and the rest of the declaration after.
260     my ($previous_line) = "";     # The previous line read in - some Gnome
261                                   #   functions have the return type on one line
262                                   #   and the rest of the declaration after.
263     my ($first_macro) = 1;        # Used to try to skip the standard #ifdef XXX
264                                   #   #define XXX at the start of headers.
265     my ($level);                          # Used to handle structs/unions which contain
266                                   #   nested structs or unions.
267     my @objects = ();             # Holds declarations that look like GtkObject
268                                   #   subclasses, which we remove from the list.
270     my $file_basename;
272     my $deprecated_conditional_nest = 0;
274     my $deprecated = "";
276     # Don't scan headers twice
277     my $canonical_input_file = realpath $input_file;
278     return if exists $seen_headers{$canonical_input_file};
279     $seen_headers{$canonical_input_file} = 1;
281     if ($input_file =~ m/^.*[\/\\](.*)\.h+$/) {
282         $file_basename = $1;
283     } else {
284         print "WARNING: Can't find basename of file $input_file\n";
285         $file_basename = $input_file;
286     }
288     # Check if the basename is in the list of headers to ignore.
289     if ($IGNORE_HEADERS =~ m/(\s|^)\Q${file_basename}\E\.h(\s|$)/) {
290         #print "DEBUG: File ignored: $input_file\n";
291         return;
292     }
294     if (! -f $input_file) {
295         print "WARNING: File doesn't exist: $input_file\n";
296         return;
297     }
299     #print "DEBUG: Scanning $input_file\n";
301     open(INPUT, $input_file)
302         || die "Can't open $input_file: $!";
303     while(<INPUT>) {
304         # If this is a private header, skip it.
305         if (m%^\s*/\*\s*<\s*private_header\s*>\s*\*/%) {
306             close(INPUT);
307             return;
308         }
310         # Skip to the end of the current comment.
311         if ($in_comment) {
312             #print "Comment: $_";
313             if (m%\*/%) {
314                 $in_comment = 0;
315             }
316             next;
317         }
318         # Skip complete blocks, needed fo inline macros
319         if ($skip_block==1) {
320             if (m%{%) {
321                 $skip_block=2;
322             }
323             next;
324         } elsif ($skip_block==2) {
325             if (m%}%) {
326                 $skip_block=0;
327             }
328             next;
329         }
331         # Keep a count of #if, #ifdef, #ifndef nesting,
332         # and if we enter a deprecation-symbol-bracketed
333         # zone, take note.
334         if (m/^\s*#\s*if(?:n?def\b|\s+!?\s*defined\s*\()\s*(\w+)/) {
335             if ($deprecated_conditional_nest == 0 and $1 =~ /$DEPRECATED_GUARDS/) {
336                  $deprecated_conditional_nest = 1;
337             } elsif ($deprecated_conditional_nest > 0) {
338                  $deprecated_conditional_nest += 1;
339             }
340         } elsif (m/^\s*#\sif/) {
341             if ($deprecated_conditional_nest > 0) {
342                  $deprecated_conditional_nest += 1;
343             }
344         } elsif (m/^\s*#endif/) {
345             if ($deprecated_conditional_nest > 0) {
346                 $deprecated_conditional_nest -= 1;
347             }
348         }
350         # set global that affects AddSymbolToList
351         if ($deprecated_conditional_nest > 0) {
352             $deprecated = "<DEPRECATED/>\n";
353         } else {
354             $deprecated = "";
355         }
357         if (!$in_declaration) {
358             # Skip top-level comments.
359             if (s%^\s*/\*%%) {
360                 if (m%\*/%) {
361                     #print "Found one-line comment: $_";
362                 } else {
363                     $in_comment = 1;
364                     #print "Found start of comment: $_";
365                 }
366                 next;
367             }
369             #print "0: $_";
371             # MACROS
373             if (m/^\s*#\s*define\s+(\w+)/) {
374                 $symbol = $1;
375                 # We assume all macros which start with '_' are private, but
376                 # we accept '_' itself which is the standard gettext macro.
377                 # We also try to skip the first macro if it looks like the
378                 # standard #ifndef HEADER_FILE #define HEADER_FILE etc.
379                 # And we only want TRUE & FALSE defined in GLib (libdefs.h in
380                 # libgnome also defines them if they are not already defined).
381                 if (($symbol !~ m/^_/
382                      && ($previous_line !~ m/#ifndef\s+$symbol/
383                          || $first_macro == 0)
384                      && (($symbol ne 'TRUE' && $symbol ne 'FALSE')
385                          || $MODULE eq 'glib'))
386                     || $symbol eq "_") {
387                     $decl = $_;
388                     $in_declaration = "macro";
389                 }
390                 $first_macro = 0;
391                 #print "DEBUG: Macro: $symbol\n";
394             # TYPEDEF'D FUNCTIONS (i.e. user functions)
396             #                        $1                                $3            $4             $5
397             } elsif (m/^\s*typedef\s+((const\s+|G_CONST_RETURN\s+)?\w+)(\s+const)?\s*(\**)\s*\(\*\s*(\w+)\)\s*\(/) {
398                 my $p3 = defined($3) ? $3 : "";
399                 $ret_type = "$1$p3 $4";
400                 $symbol = $5;
401                 $decl = $';
402                 $in_declaration = "user_function";
403                 #print "DEBUG: user function (1): $symbol, Returns: $ret_type\n";
405             #                                                       $1                                $3            $4             $5
406             } elsif (($previous_line =~ m/^\s*typedef\s*/) && m/^\s*((const\s+|G_CONST_RETURN\s+)?\w+)(\s+const)?\s*(\**)\s*\(\*\s*(\w+)\)\s*\(/) {
407                 my $p3 = defined($3) ? $3 : "";
408                 $ret_type = "$1$p3 $4";
409                 $symbol = $5;
410                 $decl = $';
411                 $in_declaration = "user_function";
412                 #print "DEBUG: user function (2): $symbol, Returns: $ret_type\n";
414             #                                                       $1            $2
415             } elsif (($previous_line =~ m/^\s*typedef\s*/) && m/^\s*(\**)\s*\(\*\s*(\w+)\)\s*\(/) {
416                 $ret_type = $1;
417                 $symbol = $2;
418                 $decl = $';
419                 #                                     $1                                $3
420                 if ($previous_line =~ m/^\s*typedef\s*((const\s+|G_CONST_RETURN\s+)?\w+)(\s+const)?\s*/) {
421                     my $p3 = defined($3) ? $3 : "";
422                     $ret_type = "$1$p3 ".$ret_type;
423                     $in_declaration = "user_function";
424                     #print "DEBUG: user function (3): $symbol, Returns: $ret_type\n";
426                 }
427             # FUNCTION POINTER VARIABLES
428             #                                                                       $1                                $3            $4             $5
429             } elsif (m/^\s*(?:\b(?:extern|G_INLINE_FUNC|${IGNORE_DECORATORS})\b\s*)*((const\s+|G_CONST_RETURN\s+)?\w+)(\s+const)?\s*(\**)\s*\(\*\s*(\w+)\)\s*\(/o) {
430                 my $p3 = defined($3) ? $3 : "";
431                 $ret_type = "$1$p3 $4";
432                 $symbol = $5;
433                 $decl = $';
434                 $in_declaration = "user_function";
435                 #print "DEBUG: function pointer variable: $symbol, Returns: $ret_type\n";
436             
437             # ENUMS
439             } elsif (s/^\s*enum\s+_(\w+)\s+\{/enum $1 {/) {
440                 # We assume that 'enum _<enum_name> {' is really the
441                 # declaration of enum <enum_name>.
442                 $symbol = $1;
443                 #print "DEBUG: plain enum: $symbol\n";
444                 $decl = $_;
445                 $in_declaration = "enum";
447             } elsif (m/^\s*typedef\s+enum\s+_?(\w+)\s+\1\s*;/) {
448                 # We skip 'typedef enum <enum_name> _<enum_name>;' as the enum will
449                 # be declared elsewhere.
450                 #print "DEBUG: skipping enum typedef: $1\n";
452             } elsif (m/^\s*typedef\s+enum/) {
453                 $symbol = "";
454                 #print "DEBUG: typedef enum: -\n";
455                 $decl = $_;
456                 $in_declaration = "enum";
459             # STRUCTS AND UNIONS
461             } elsif (m/^\s*typedef\s+(struct|union)\s+_(\w+)\s+\2\s*;/) {
462                 # We've found a 'typedef struct _<name> <name>;'
463                 # This could be an opaque data structure, so we output an
464                 # empty declaration. If the structure is actually found that
465                 # will override this.
466                 my $structsym = uc $1;
467                 #print "DEBUG: $structsym typedef: $2\n";
468                 &AddSymbolToList (\$list, $2);
469                 print DECL "<$structsym>\n<NAME>$2</NAME>\n$deprecated</$structsym>\n";
471             } elsif (m/^\s*(?:struct|union)\s+_(\w+)\s*;/) {
472                 # Skip private structs/unions.
473                 #print "DEBUG: private struct/union\n";
475             } elsif (m/^\s*(struct|union)\s+(\w+)\s*;/) {
476                 # Do a similar thing for normal structs as for typedefs above.
477                 # But we output the declaration as well in this case, so we
478                 # can differentiate it from a typedef.
479                 my $structsym = uc $1;
480                 #print "DEBUG: $structsym: $2\n";
481                 &AddSymbolToList (\$list, $2);
482                 print DECL "<$structsym>\n<NAME>$2</NAME>\n$_$deprecated</$structsym>\n";
484             } elsif (m/^\s*typedef\s+(struct|union)\s*\w*\s*{/) {
485                 $symbol = "";
486                 $decl = $_;
487                 $level = 0;
488                 $in_declaration = $1;
489                 #print "DEBUG: $1\n";
491             # OTHER TYPEDEFS
493             } elsif (m/^\s*typedef\s+(?:struct|union)\s+\w+[\s\*]+(\w+)\s*;/) {
494                 #print "DEBUG: Found struct/union(*) typedef $1: $_";
495                 &AddSymbolToList (\$list, $1);
496                 print DECL "<TYPEDEF>\n<NAME>$1</NAME>\n$deprecated$_</TYPEDEF>\n";
498             } elsif (m/^\s*(G_GNUC_EXTENSION\s+)?typedef\s+(.+[\s\*])(\w+)(\s*\[[^\]]+\])*\s*;/) {
499                 if ($2 !~ m/^struct\s/ && $2 !~ m/^union\s/) {
500                     #print "DEBUG: Found typedef: $_";
501                     &AddSymbolToList (\$list, $3);
502                     print DECL "<TYPEDEF>\n<NAME>$3</NAME>\n$deprecated$_</TYPEDEF>\n";
503                 }
504             } elsif (m/^\s*typedef\s+/) {
505                 #print "DEBUG: Skipping typedef: $_";
508             # VARIABLES (extern'ed variables)
510             } elsif (m/^\s*(extern|[A-Za-z_]+VAR)\s+((const\s+|unsigned\s+)*\w+)(\s+\*+|\*+|\s)\s*([A-Za-z]\w*)\s*;/) {
511                 $symbol = $5;
512                 s/^\s*([A-Za-z_]+VAR)\b/extern/;
513                 #print "DEBUG: Possible extern: $_";
514                 &AddSymbolToList (\$list, $symbol);
515                 print DECL "<VARIABLE>\n<NAME>$symbol</NAME>\n$deprecated$_</VARIABLE>\n";
518             # FUNCTIONS
520             # We assume that functions which start with '_' are private, so
521             # we skip them.
522             #                                                                       $1                                                                                                                                                             $2
523             } elsif (m/^\s*(?:\b(?:extern|G_INLINE_FUNC|${IGNORE_DECORATORS})\b\s*)*((?:const\s+|G_CONST_RETURN\s+|unsigned\s+|long\s+|short\s+|struct\s+|union\s+|enum\s+)*_\w+(?:\**\s+\**(?:const|G_CONST_RETURN))?(?:\s+|\s*\*+))\s*\(\s*\*+\s*([A-Za-z]\w*)\s*\)\s*\(/o) {
524                 $ret_type = $1;
525                 $symbol = $2;
526                 $decl = $';
527                 #print "DEBUG: internal Function: $symbol, Returns: $ret_type\n";
528                 $in_declaration = "function";
530             #                                                                       $1                                                                                                                                                 $2
531             } elsif (m/^\s*(?:\b(?:extern|G_INLINE_FUNC|${IGNORE_DECORATORS})\b\s*)*((?:const\s+|G_CONST_RETURN\s+|unsigned\s+|long\s+|short\s+|struct\s+|union\s+|enum\s+)*\w+(?:\**\s+\**(?:const|G_CONST_RETURN))?(?:\s+|\s*\*+))\s*([A-Za-z]\w*)\s*\(/o) {
532                 $ret_type = $1;
533                 $symbol = $2;
534                 $decl = $';
535                 #print "DEBUG: Function (1): $symbol, Returns: $ret_type\n";
536                 $in_declaration = "function";
538             # Try to catch function declarations which have the return type on
539             # the previous line. But we don't want to catch complete functions
540             # which have been declared G_INLINE_FUNC, e.g. g_bit_nth_lsf in
541             # glib, or 'static inline' functions.
542             } elsif (m/^\s*([A-Za-z]\w*)\s*\(/) {
543                 $symbol = $1;
544                 $decl = $';
546                 if ($previous_line !~ m/^\s*G_INLINE_FUNC/) {
547                     if ($previous_line !~ m/^\s*static\s+/) {
548                         #                                                                       $1                                                                         $2
549                         if ($previous_line =~ m/^\s*(?:\b(?:extern|${IGNORE_DECORATORS})\b\s*)*((?:const\s+|G_CONST_RETURN\s+|unsigned\s+|struct\s+|union\s+|enum\s+)?\w+)((?:\s*(?:\*+|\bconst\b|\bG_CONST_RETURN\b))*)\s*$/o) {
550                             $ret_type = $1;
551                             if (defined ($2)) { $ret_type .= " $2"; }
552                             #print "DEBUG: Function  (2): $symbol, Returns: $ret_type\n";
553                             $in_declaration = "function";
554                         }
555                     }
556                 }
557                 else {
558                     if ($previous_line !~ m/^\s*static\s+/) {
559                         #print "DEBUG: skip block after inline function\n";
560                         # now we we need to skip a whole { } block
561                         $skip_block = 1;
562                         #                                                                       $1                                                                         $2
563                         if ($previous_line =~ m/^\s*(?:\b(?:extern|G_INLINE_FUNC|${IGNORE_DECORATORS})\b\s*)*((?:const\s+|G_CONST_RETURN\s+|unsigned\s+|struct\s+|union\s+|enum\s+)?\w+)((?:\s*(?:\*+|\bconst\b|\bG_CONST_RETURN\b))*)\s*$/o) {
564                             $ret_type = $1;
565                             if (defined ($2)) { $ret_type .= " $2"; }
566                             #print "DEBUG: Function (3): $symbol, Returns: $ret_type\n";
567                             $in_declaration = "function";
568                             # this is a hack to detect the end of declaration
569                             $decl.=";"
570                         }
571                     }
572                 }
574             # Try to catch function declarations with the return type and name
575             # on the previous line(s), and the start of the parameters on this.
576             } elsif (m/^\s*\(/) {
577                 $decl = $';
578                 if ($previous_line =~ m/^\s*(?:\b(?:extern|G_INLINE_FUNC|${IGNORE_DECORATORS})\b\s*)*((?:const\s+|G_CONST_RETURN\s+|unsigned\s+|enum\s+)*\w+)(\s+\*+|\*+|\s)\s*([A-Za-z]\w*)\s*$/o) {
579                     $ret_type = "$1 $2";
580                     $symbol = $3;
581                     #print "DEBUG: Function (4): $symbol, Returns: $ret_type\n";
582                     $in_declaration = "function";
584                 } elsif ($previous_line =~ m/^\s*\w+\s*$/
585                          && $pre_previous_line =~ m/^\s*(?:\b(?:extern|G_INLINE_FUNC|${IGNORE_DECORATORS})\b\s*)*((?:const\s+|G_CONST_RETURN\s+|unsigned\s+|struct\s+|union\s+|enum\s+)*\w+(?:\**\s+\**(?:const|G_CONST_RETURN))?(?:\s+|\s*\*+))\s*$/o) {
586                     $ret_type = $1;
587                     $ret_type =~ s/\s*\n//;
588                     $in_declaration = "function";
589                     
590                     $symbol = $previous_line;
591                     $symbol =~ s/^\s+//;
592                     $symbol =~ s/\s*\n//;
593                     #print "DEBUG: Function (5): $symbol, Returns: $ret_type\n";
594                 }
596             #} elsif (m/^extern\s+/) {
597                 #print "DEBUG: Skipping extern: $_";
600             # STRUCTS
602             } elsif (m/^\s*struct\s+_(\w+)\s*\*/) {
603                 # Skip 'struct _<struct_name> *', since it could be a
604                 # return type on its own line.
606             } elsif (m/^\s*struct\s+_(\w+)/) {
607                 # We assume that 'struct _<struct_name>' is really the
608                 # declaration of struct <struct_name>.
609                 $symbol = $1;
610                 $decl = $_;
611                 # we will find the correct level as below we do $level += tr/{//;
612                 $level = 0;
613                 $in_declaration = "struct";
614                 #print "DEBUG: Struct(_): $symbol\n";
617             # UNIONS
619             } elsif (m/^\s*union\s+_(\w+)\s*\*/) {
620                 # Skip 'union _<union_name> *' (see above)
621             } elsif (m/^\s*union\s+_(\w+)/) {
622                 $symbol = $1;
623                 $decl = $_;
624                 $level = 0;
625                 $in_declaration = "union";
626                 #print "DEBUG: Union(_): $symbol\n";
627             }
629         } else {
630             #print "1: $_";
631             # If we were already in the middle of a declaration, we simply add
632             # the current line onto the end of it.
633             $decl .= $_;
634         }
636         #if ($in_declaration ne '') {
637         #    print "$in_declaration = $decl\n";
638         #}
640         # Note that sometimes functions end in ') G_GNUC_PRINTF (2, 3);' or
641         # ') __attribute__ (...);'.
642         if ($in_declaration eq 'function') {
643             if ($decl =~ s/\)\s*(G_GNUC_.*|${IGNORE_DECORATORS}\s*|__attribute__\s*\(.*\)\s*)?;.*$//) {
644                 $decl =~ s%/\*.*?\*/%%gs;       # remove comments.
645                 #$decl =~ s/^\s+//;             # remove leading whitespace.
646                 #$decl =~ s/\s+$//;             # remove trailing whitespace.
647                 $decl =~ s/\s*\n\s*/ /gs;               # consolidate whitespace at start
648                                                 # and end of lines.
649                 $ret_type =~ s%/\*.*?\*/%%g;    # remove comments in ret type.
650                 &AddSymbolToList (\$list, $symbol);
651                 print DECL "<FUNCTION>\n<NAME>$symbol</NAME>\n$deprecated<RETURNS>$ret_type</RETURNS>\n$decl\n</FUNCTION>\n";
652                 if ($REBUILD_TYPES) {
653                     # check if this looks like a get_type function and if so remember
654                     if (($symbol =~ m/_get_type$/) && ($ret_type =~ m/GType/) && ($decl =~ m/(void|)/)) {
655                         #print "Adding get-type: [$ret_type] [$symbol] [$decl]\tfrom $input_file\n";
656                         push (@get_types, $symbol);
657                     }
658                 }
659                 $in_declaration = "";
660             }
661         }
663         if ($in_declaration eq 'user_function') {
664             if ($decl =~ s/\).*$//) {
665                 &AddSymbolToList (\$list, $symbol);
666                 print DECL "<USER_FUNCTION>\n<NAME>$symbol</NAME>\n$deprecated<RETURNS>$ret_type</RETURNS>\n$decl</USER_FUNCTION>\n";
667                 $in_declaration = "";
668             }
669         }
671         if ($in_declaration eq 'macro') {
672             if ($decl !~ m/\\\s*$/) {
673                 &AddSymbolToList (\$list, $symbol);
674                 print DECL "<MACRO>\n<NAME>$symbol</NAME>\n$deprecated$decl</MACRO>\n";
675                 $in_declaration = "";
676             }
677         }
679         if ($in_declaration eq 'enum') {
680             if ($decl =~ m/\}\s*(\w+)?;\s*$/) {
681                 if ($symbol eq "") {
682                     $symbol = $1;
683                 }
684                 &AddSymbolToList (\$list, $symbol);
685                 print DECL "<ENUM>\n<NAME>$symbol</NAME>\n$deprecated$decl</ENUM>\n";
686                 $in_declaration = "";
687             }
688         }
690         # We try to handle nested stucts/unions, but unmatched brackets in
691         # comments will cause problems.
692         if ($in_declaration eq 'struct' or $in_declaration eq 'union') {
693             if ($level <= 1 && $decl =~ m/\}\s*(\w*);\s*$/) {
694                 if ($symbol eq "") {
695                     $symbol = $1;
696                 }
698                 if ($symbol =~ m/^(\S+)(Class|Iface|Interface)\b/) {
699                     my $objectname = $1;
700                     #print "Found object: $1\n";
701                     $list = "<TITLE>$objectname</TITLE>\n$list";
702                     push (@objects, $objectname);
703                 }
704                 #print "Store struct: $symbol\n";
705                 &AddSymbolToList (\$list, $symbol);
707                 my $structsym = uc $in_declaration;
708                 print DECL "<$structsym>\n<NAME>$symbol</NAME>\n$deprecated$decl</$structsym>\n";
709                 $in_declaration = "";
710             } else {
711                 # We use tr to count the brackets in the line, and adjust
712                 # $level accordingly.
713                 $level += tr/{//;
714                 $level -= tr/}//;
715                 #print "struct/union level : $level\n";
716             }
717         }
719         $pre_previous_line = $previous_line;
720         $previous_line = $_;
721     }
722     close(INPUT);
723     
724     #print "DEBUG: Scanning $input_file done\n\n\n";
727     # Try to separate the standard macros and functions, placing them at the
728     # end of the current section, in a subsection named 'Standard'.
729     my ($class) = "";
730     my ($standard_decl) = "";
731     if ($list =~ m/^\S+_IS_(\S*)_CLASS/m) {
732         $class = $1;
733     } elsif ($list =~ m/^\S+_IS_(\S*)/m) {
734         $class = $1;
735     }
737     if ($class ne "") {
738         if ($list =~ s/^\S+_IS_$class\n//m)          { $standard_decl .= $&; }
739         if ($list =~ s/^\S+_TYPE_$class\n//m)        { $standard_decl .= $&; }
740         if ($list =~ s/^\S+_.*_get_type\n//m)        { $standard_decl .= $&; }
741         if ($list =~ s/^\S+_${class}_CLASS\n//m)     { $standard_decl .= $&; }
742         if ($list =~ s/^\S+_IS_${class}_CLASS\n//m)  { $standard_decl .= $&; }
743         if ($list =~ s/^\S+_${class}_GET_CLASS\n//m) { $standard_decl .= $&; }
744         if ($list =~ s/^\S+_${class}_GET_IFACE\n//m) { $standard_decl .= $&; }
745         if ($list =~ s/^\S+_${class}_GET_INTERFACE\n//m) { $standard_decl .= $&; }
747         # We do this one last, otherwise it tends to be caught by the IS_$class macro
748         if ($list =~ s/^\S+_$class\n//m)             { $standard_decl = $& . $standard_decl; }
750         if ($standard_decl ne "") {
751             $list .= "<SUBSECTION Standard>\n$standard_decl";
752         }
754         if ($list ne "") {
755             $$object_list .= "<SECTION>\n<FILE>$file_basename</FILE>\n$list</SECTION>\n\n";
756         }
757     } else {
758         if ($list ne "") {
759             $$main_list .= "<SECTION>\n<FILE>$file_basename</FILE>\n$list</SECTION>\n\n";
760         }
761     }
765 #############################################################################
766 # Function    : AddSymbolToList
767 # Description : This adds the symbol to the list of declarations, but only if
768 #               it is not already in the list.
769 # Arguments   : $list - reference to the list of symbols, one on each line.
770 #               $symbol - the symbol to add to the list.
771 #############################################################################
773 sub AddSymbolToList {
774     my ($list, $symbol) = @_;
776     if ($$list =~ m/\b\Q$symbol\E\b/) {
777 #       print "Symbol $symbol already in list. skipping\n";
778         return;
779     }
780     $$list .= "$symbol\n";