xref: /aosp_15_r20/frameworks/av/media/codec2/docs/doxyfilter.sh (revision ec779b8e0859a360c3d303172224686826e6e0e1)
1#!/usr/bin/env python3
2import re, sys
3
4global in_comment, current, indent, hold
5in_comment, current, indent, hold = False, None, '', []
6
7class ChangeCStyleCommentsToDoxy:
8    def dump_hold():
9        global hold
10        for h in hold:
11            print(h, end='')
12        hold[:] = []
13
14    def doxy_hold():
15        global current, hold
16        if current == '//':
17            for h in hold:
18                print(re.sub(r'^( *//(?!/))', r'\1/', h), end='')
19        else:
20            first = True
21            for h in hold:
22                if first:
23                    h = re.sub(r'^( */[*](?![*]))', r'\1*', h)
24                    first = False
25                print(h, end='')
26        hold[:] = []
27
28    def process_comment(t, ind, line):
29        global current, indent, hold
30        if t != current or ind not in (indent, indent + ' '):
31            dump_hold()
32            current, indent = t, ind
33        hold.append(line)
34
35    def process_line(ind, line):
36        global current, indent
37        if ind in (indent, ''):
38            doxy_hold()
39        else:
40            dump_hold()
41        current, indent = None, None
42        print(line, end='')
43
44    def process(self, input, path):
45        for line in input:
46            ind = re.match(r'^( *)', line).group(1)
47            if in_comment:
48                # TODO: this is not quite right, but good enough
49                m = re.match(r'^ *[*]/', line)
50                if m:
51                    process_comment('/*', ind, line)
52                    in_comment = False
53                else:
54                    process_comment('/*', ind, line)
55                continue
56            m = re.match(r'^ *//', line)
57            if m:
58                # one-line comment
59                process_comment('//', ind, line)
60                continue
61            m = re.match(r'^ */[*]', line)
62            if m:
63                # multi-line comment
64                process_comment('/*', ind, line)
65                # TODO: this is not quite right, but good enough
66                in_comment = not re.match(r'^ *[*]/', line)
67                continue
68            process_line(ind, line)
69
70class AutoGroup:
71    def process(self, input, path):
72        if '/codec2/include/' in path:
73            group = 'API Codec2 API'
74        elif False:
75            return
76        elif '/codec2/vndk/' in path:
77            group = 'VNDK Platform provided glue'
78        elif '/codec2/tests/' in path:
79            group = 'Tests Unit tests'
80        else:
81            group = 'Random Misc. sandbox'
82
83        print('#undef __APPLE__')
84
85        for line in input:
86            if re.match(r'^namespace android {', line):
87                print(line, end='')
88                print()
89                print(r'/// \addtogroup {}'.format(group))
90                print(r'/// @{')
91                continue
92            elif re.match(r'^} +// +namespace', line):
93                print(r'/// @}')
94                print()
95            print(line, end='')
96
97P = AutoGroup()
98for path in sys.argv[1:]:
99    with open(path, 'rt') as input:
100        P.process(input, path)
101