blob: 43c0ff49a377cb2355cc34bc55f3187de7310168 [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
87 path[-1] not in ("h")):
88 continue
89
90 print("[{}] {}".format(n_file, path))
91
92 n_file += 1
93
94 with open(path,'r+', encoding="utf-8") as fd:
95 comment = list()
96 first_param = -1
97 last_param = -1
98 n_line = 0
99
100 lines = fd.readlines()
101 fd.seek(0)
102 fd.truncate()
103
104 for line in lines:
105 n_line += 1
106
107 # Start comment
108 # Match C-style comment /* anywhere in the line
109 if re.search(r"/\*", line):
110 #print("Start comment {}".format(n_line))
111
112 if len(comment) > 0:
113 raise Exception("Already in a comment! ({})".format(n_line))
114
115 comment.append(line)
116
117 # Comment already started
118 elif len(comment) > 0:
119 #print("Add line to comment {}".format(n_line))
120
121 comment.append(line)
122
123 # Non-comment line
124 else:
125 #print("Normal line {}".format(n_line))
126
127 fd.write(line)
128
129 # Match param declaration in Doxygen comment
130 # @param[in] name description
131 if re.search(r"@param\[[^\]]+\] +\S+ +\S", line):
132 #print("Param {}".format(n_line))
133
134 if first_param < 0:
135 first_param = len(comment) - 1
136
137 last_param = len(comment)
138
139 # Match end of C-style comment */
140 if re.search(r"\*/", line):
Anthony Barbier81bf1962017-07-25 11:02:08 +0100141 if re.search('"[^"]*\*/[^"]*"', line):
142 #print("End of comment inside a string: ignoring")
143 pass
144 else:
145 #print("End comment {}".format(n_line))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100146
Anthony Barbier81bf1962017-07-25 11:02:08 +0100147 if len(comment) < 1:
148 raise Exception("Was not in a comment! ({})".format(n_line))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100149
Anthony Barbier81bf1962017-07-25 11:02:08 +0100150 #print("Process comment {} {}".format(first_param, last_param))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100151
Anthony Barbier81bf1962017-07-25 11:02:08 +0100152 process_comment(fd, comment, first_param, last_param)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100153
Anthony Barbier81bf1962017-07-25 11:02:08 +0100154 comment = list()
155 first_param = -1
156 last_param = -1