summaryrefslogtreecommitdiffstats
path: root/docs/proto/cdoc
blob: 360bee3b61e8c34f0dc1b6a3644d50c6d9fe00d5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/perl

# follow javadoc
# @see http://java.sun.com/j2se/javadoc/writingdoccomments/

use strict;

=cut

這個程式可以從 C 的程式碼中,列出所有 statis/non-static 的 functions。
如果有以下這種 pattern 出現,會原封不動丟出來。

/**
 * Something
 **/

每個 function 前面可以有 javadoc-style 的註解。範例如下:

/**
 * Function of the function func.
 * @param x variable, if there are more than one @-style description, it
 *        will end with the next @-style description.
 * @return void
 */
void func(int x)
{
    ...
}
=cut

my $content;

foreach my $f (@ARGV) {
    makedoc($f);
}


sub grep_desc
{
    my $name = '\b\w+\b';
    my $type = '\b\w+\s*\*?';
    my $sentence = '.*';

    my $one_desc = "$name\\s+$sentence";
    my $desc_head = "\\/\\*\\*\n";
    my $desc_tail = "\\s*\\*\\/\n";
    my $desc_line = "\\s*\\*.*\n";
    my $paramdesc = "\@param\\s+$one_desc(?:\n$desc_line)*";
    my $returndesc = "\@return\\s+$sentence(?:\n$desc_line)*";
    my $seedesc = "\@see\\s+$sentence(?:\n$desc_line)*";
    my $desc = "$desc_head(?:$desc_line)*$desc_tail";

    my $modifier = '(?:static|inline)\s+';
    my $one_param = "$type\\s*$name";
    my $more_param = ",\\s*$one_param";
    my $param = "(?:$one_param(?:$more_param)*|void)?";
    my $func_proto = "(?:$modifier)*$type\\s+$name\\($param\\)";

    $content =~ s/($desc_head(?:$desc_line)*\s*\*\*\/)//mo and return "$1\n";

    $content =~ s/($desc_head(?:$desc_line)*$desc_tail)?($func_proto)\s*{(?:.|\n)*?\n}//mo
    or return undef;
    my $comment = $1;
    my $function = $2;
    $function =~ s/\n/ /g;
    $function =~ s/\s+/ /g;

    if ($comment) {
    $comment =~ s#^/\*\*##;
    $comment =~ s#\s*\*/$##;
    $comment =~ s/^\s*\*/  /mg;
    }

    # check comment style here

    return ($function, $comment);
}

sub makedoc
{
    my $file = shift @_;
    open SRC, "<$file";
    $content = join "",<SRC>;
    close SRC;

    open STATIC, ">$file.tmp";

    # just to break them up to avoid vim's misunderstanding
    print "// vim".":ft=c\n\n";

    $content =~ s#/\*[^*](.|\n)*?\*/##gm;
    while (my ($function, $comment) = grep_desc()) {
    last unless $function;
    my $FILE = ($function =~ /\bstatic\b/) ? *STATIC : *STDOUT;
    print $FILE "$function;\n".  ($comment ? $comment : "   no comment\n")."\n";
    }

    close STATIC;

    print <<XXX;
/*
 * static function
 */

XXX
    open STATIC, "<$file.tmp";
    print <STATIC>;
    close STATIC;
}