Ticket #21: genstdint.pl

File genstdint.pl, 4.3 KB (added by debrouxl, 3 years ago)

Simple generator for stdint.h documentation

Line 
1#!/usr/bin/perl
2use strict;
3
4package main;
5    my $line;
6    my $name;
7    my $size;
8    my $description;
9
10    (open INFILE, "<stdint.h") or die "Cannot open stdint.h: $!\n";
11
12    while ($line = <INFILE>) {
13       
14        if ($line =~ m@^typedef (\C*?) ([a-zA-Z0-9_]+);$@) {
15            # Typedef
16            $name = $2;
17            next if ($name !~ m@\C*?(\d{0,2})_t@);
18            $size = $1;
19            $description = $name;
20            my $description2 = 'Definition mandated by the C99 standard.';
21            my $unsigned = ((index($name,'u') == 0) ? 'n unsigned ' : ' signed ');
22            my $unsigned2 = ((index($name,'u') == 0) ? 'unsigned' : 'signed');
23           
24            if (index($description, 'least') != -1) {
25                $description = "An alias type for a${unsigned}integer type that contains at least $size bits.";
26            }
27            elsif (index($description, 'fast') != -1) {
28                $description = "An alias type for a${unsigned}integer type that contains at least $size bits, the one that the processor handles most efficiently.";
29            }
30            elsif (index($description, 'ptr') != -1) {
31                $description = "An alias type for a${unsigned}integer type such that a <I>void *</I> can be converted to this type and back, while remaining equal to the original pointer.";
32                $description2 = '';
33            }
34            elsif (index($description, 'max') != -1) {
35                $description = "An alias type for a${unsigned}integer type capable of representing any value of any $unsigned2 type.";
36            }
37            else {
38                $description = "An alias type for a${unsigned}integer type that contains exactly $size bits.";
39                $description2 = '';
40            }
41           
42            $line =~ s/\n//g; # Swallow newline.
43            open OUTFILE, "+>trunk/tigcc/doc/System/Include/stdint.h/$name.hsf";
44            print OUTFILE <<EOF
45[Main]\r
46Name=$name\r
47Type=Type\r
48Subtype=Scalar\r
49Header Files=stdint.h\r
50Definition=$line\r
51\r
52[Description]\r
53$description\r
54\r
55[Explanation]\r
56$description2\r
57EOF
58;
59            close OUTFILE;
60        }
61        elsif ($line =~ m@^#\s*define (\S+) (\C*?)$@) {
62            $name = $1;
63            my $definition = $line;
64           
65            if (index($name, '(') != -1) {
66                my $type;
67                # Macro with argument
68                next if ($name !~ m@\C*?(\d{0,2})_C\(c\)@);
69                $size = $1;
70               
71                if (index($name, 'MAX') != -1) {
72                    $type = ((index($name,'U') == 0) ? 'u' : '') . "intmax_t";
73                }
74                else {
75                    $type = ((index($name,'U') == 0) ? 'u' : '') . "int_least${size}_t";
76                }
77                $description = "Integer constant expression having the value specified by its argument and the type $type.";
78
79                $line =~ s/\n//g; # Swallow newline.
80                $name = substr($name, 0, index($name,'('));
81                open OUTFILE, "+>trunk/tigcc/doc/System/Include/stdint.h/$name.hsf";
82                print OUTFILE <<EOF
83[Main]\r
84Name=$name\r
85Type=Function\r
86Subtype=Macro\r
87Header Files=stdint.h\r
88Definition=$line\r
89\r
90[Description]\r
91$description\r
92\r
93[Explanation]\r
94Definition mandated by the C99 standard.\r
95EOF
96;
97                close OUTFILE;
98            }
99            else {
100                # Macro without argument
101                next if ($name !~ m@(\C*?(\d{0,2})_)(MAX|MIN)@);
102                $size = $2;
103                my $type = lc($1) . 't';
104               
105                if (index($name, 'MAX') != -1) {
106                    $description = "Maximal value which can be stored in a $type variable.";
107                }
108                else {
109                    # Assume it's a MIN.
110                    $description = "Minimal value which can be stored in a $type variable.";
111                }
112
113                $line =~ s/\n//g; # Swallow newline.
114                open OUTFILE, "+>trunk/tigcc/doc/System/Include/stdint.h/$name.hsf";
115                print OUTFILE <<EOF
116[Main]\r
117Name=$name\r
118Type=Constant\r
119Header Files=stdint.h\r
120Definition=$line\r
121\r
122[Description]\r
123$description\r
124\r
125[Explanation]\r
126Definition mandated by the C99 standard.\r
127EOF
128;
129                close OUTFILE;
130            }
131        }
132        # else do nothing
133       
134    }