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