blob: 64fe7ecbce34f2699c9bd30e56a7ffef6cfec84e [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001#!/usr/bin/env python3
2
3import os.path
4import re
5import sys
6
7def process_comment(fd, comment, first_param, last_param):
8 if first_param < 0:
9 # Nothing to do: just copy the comment
10 fd.write("".join(comment))
11 else:
12 params = list()
13
14 # Copy the non param lines unmodified:
15 fd.write("".join(comment[:first_param]))
16
17 # Measure the indentation of the first param and use that to create an empty comment line string:
18 m = re.match(r" *\*", comment[first_param])
19
20 if not m:
21 raise Exception("Not a comment ? '{}'".format(comment[first_param]))
22
23 empty_line = "{}\n".format(m.group(0))
24
25 # For each param split the line into 3 columns: param, param_name, description
26 for param in range(first_param, last_param):
27 m = re.match(r"([^@]+@param\[[^\]]+\]) +(\S+) +(.+)", comment[param])
28
29 if m:
30 params.append( (m.group(1), m.group(2), m.group(3)) )
31 else:
32 # If it's not a match then it must be a multi-line param description:
33
34 m = re.match("( *\*) +(.*)", comment[param])
35
36 if not m:
37 raise Exception("Not a comment line ? ({})".format(n_line - len(comment) + param))
38
39 params.append( (m.group(1), "", m.group(2)) )
40
41 # Now that we've got a list of params, find what is the longest string for each column:
42 max_len = [0, 0]
43
44 for p in params:
45 for l in range(len(max_len)):
46 max_len[l] = max(max_len[l], len(p[l]))
47
48 # Insert an empty line if needed before the first param to make it easier to read:
49 m = re.match(r" *\* *$", comment[first_param - 1])
50
51 if not m:
52 # insert empty line
53 fd.write(empty_line)
54
55 # Write out the formatted list of params:
56 for p in params:
57 fd.write("{}{} {}{} {}\n".format(
58 p[0], " " * (max_len[0] - len(p[0])),
59 p[1], " " * (max_len[1] - len(p[1])),
60 p[2]))
61
62 # If the next line after the list of params is a command (@return, @note, @warning, etc), insert an empty line to separate it from the list of params
63 if last_param < len(comment) - 1:
64 if re.match(r" *\* *@\w+", comment[last_param]):
65 # insert empty line
66 fd.write(empty_line)
67
68 # Copy the remaining of the comment unmodified:
69 fd.write("".join(comment[last_param:]))
70
71if __name__ == "__main__":
72 n_file=0
73
74 if len(sys.argv) == 1:
75 paths = []
76
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010077 for top_level in ["./arm_compute", "./src", "./examples", "./tests", "./utils", "./framework", "./support"]:
Anthony Barbier6ff3b192017-09-04 18:44:23 +010078 for root, _, files in os.walk(top_level):
79 paths.extend([os.path.join(root, f) for f in files])
80 else:
81 paths = sys.argv[1:]
82
83 for path in paths:
84 if (path[-3:] not in ("cpp", "inl") and
85 path[-2:] not in ("cl") and
86 path[-1] not in ("h")):
87 continue
88
89 print("[{}] {}".format(n_file, path))
90
91 n_file += 1
92
93 with open(path,'r+', encoding="utf-8") as fd:
94 comment = list()
95 first_param = -1
96 last_param = -1
97 n_line = 0
98
99 lines = fd.readlines()
100 fd.seek(0)
101 fd.truncate()
102
103 for line in lines:
104 n_line += 1
105
106 # Start comment
107 # Match C-style comment /* anywhere in the line
108 if re.search(r"/\*", line):
109 #print("Start comment {}".format(n_line))
110
111 if len(comment) > 0:
112 raise Exception("Already in a comment! ({})".format(n_line))
113
114 comment.append(line)
115
116 # Comment already started
117 elif len(comment) > 0:
118 #print("Add line to comment {}".format(n_line))
119
120 comment.append(line)
121
122 # Non-comment line
123 else:
124 #print("Normal line {}".format(n_line))
125
126 fd.write(line)
127
128 # Match param declaration in Doxygen comment
129 # @param[in] name description
130 if re.search(r"@param\[[^\]]+\] +\S+ +\S", line):
131 #print("Param {}".format(n_line))
132
133 if first_param < 0:
134 first_param = len(comment) - 1
135
136 last_param = len(comment)
137
138 # Match end of C-style comment */
139 if re.search(r"\*/", line):
140 #print("End comment {}".format(n_line))
141
142 if len(comment) < 1:
143 raise Exception("Was not in a comment! ({})".format(n_line))
144
145 #print("Process comment {} {}".format(first_param, last_param))
146
147 process_comment(fd, comment, first_param, last_param)
148
149 comment = list()
150 first_param = -1
151 last_param = -1