blob: e18f24eb5232a9c128b073a5ad1b7a0afab94575 [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
Anthony Barbier6ff3b192017-09-04 18:44:23 +010015 # Measure the indentation of the first param and use that to create an empty comment line string:
Anthony Barbierf202e502017-11-23 18:02:04 +000016 m = re.match(r" */", comment[0])
Anthony Barbier6ff3b192017-09-04 18:44:23 +010017
18 if not m:
Anthony Barbierf202e502017-11-23 18:02:04 +000019 raise Exception("{}: Not a comment ? '{}'".format(path,comment[first_param]))
Anthony Barbier6ff3b192017-09-04 18:44:23 +010020
Anthony Barbierf202e502017-11-23 18:02:04 +000021 line_prefix = " " * len(m.group(0)) + "*"
22 empty_line = line_prefix +"\n"
23
24 fd.write(comment[0])
25 # Copy the non param lines with the correct indentation:
26 for comment_line in range(1,first_param):
27 line = comment[comment_line]
28 m = re.match(" *\*(.*)", line)
29 if not m:
30 raise Exception("{}:{}: Not a comment line ? ".format(path, n_line - len(comment) + comment_line + 1))
31 fd.write(line_prefix+ m.group(1)+"\n")
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032
33 # For each param split the line into 3 columns: param, param_name, description
34 for param in range(first_param, last_param):
Anthony Barbierf202e502017-11-23 18:02:04 +000035 m = re.match(r"[^@]+(@param\[[^\]]+\]) +(\S+) +(.+)", comment[param])
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036
37 if m:
Anthony Barbierf202e502017-11-23 18:02:04 +000038 params.append( (" "+m.group(1), m.group(2), m.group(3)) )
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039 else:
40 # If it's not a match then it must be a multi-line param description:
41
Anthony Barbierf202e502017-11-23 18:02:04 +000042 m = re.match(" *\* +(.*)", comment[param])
Anthony Barbier6ff3b192017-09-04 18:44:23 +010043 if not m:
Anthony Barbierf202e502017-11-23 18:02:04 +000044 raise Exception("{}:{}: Not a comment line ? ".format(path, n_line - len(comment) + param + 1))
Anthony Barbier6ff3b192017-09-04 18:44:23 +010045
Anthony Barbierf202e502017-11-23 18:02:04 +000046 params.append( ("", "", m.group(1)) )
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047
48 # Now that we've got a list of params, find what is the longest string for each column:
49 max_len = [0, 0]
50
51 for p in params:
52 for l in range(len(max_len)):
53 max_len[l] = max(max_len[l], len(p[l]))
54
55 # Insert an empty line if needed before the first param to make it easier to read:
56 m = re.match(r" *\* *$", comment[first_param - 1])
57
58 if not m:
59 # insert empty line
60 fd.write(empty_line)
61
62 # Write out the formatted list of params:
63 for p in params:
Anthony Barbierf202e502017-11-23 18:02:04 +000064 fd.write("{}{}{} {}{} {}\n".format( line_prefix,
Anthony Barbier6ff3b192017-09-04 18:44:23 +010065 p[0], " " * (max_len[0] - len(p[0])),
66 p[1], " " * (max_len[1] - len(p[1])),
67 p[2]))
68
69 # 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
70 if last_param < len(comment) - 1:
71 if re.match(r" *\* *@\w+", comment[last_param]):
72 # insert empty line
73 fd.write(empty_line)
74
Anthony Barbierf202e502017-11-23 18:02:04 +000075 # Copy the remaining of the comment with the correct indentation:
76 for comment_line in range(last_param,len(comment)):
77 line = comment[comment_line]
78 m = re.match(" *\*(.*)", line)
79 if not m:
80 raise Exception("{}:{}: Not a comment line ? ".format(path, n_line - len(comment) + comment_line + 1))
81 fd.write(line_prefix+ m.group(1)+"\n")
Anthony Barbier6ff3b192017-09-04 18:44:23 +010082
83if __name__ == "__main__":
84 n_file=0
85
86 if len(sys.argv) == 1:
87 paths = []
88
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010089 for top_level in ["./arm_compute", "./src", "./examples", "./tests", "./utils", "./framework", "./support"]:
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090 for root, _, files in os.walk(top_level):
91 paths.extend([os.path.join(root, f) for f in files])
92 else:
93 paths = sys.argv[1:]
94
95 for path in paths:
96 if (path[-3:] not in ("cpp", "inl") and
97 path[-2:] not in ("cl") and
Joel Liangf1f3ebd2017-11-10 09:59:19 +080098 path[-2:] not in ("cs") and
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099 path[-1] not in ("h")):
100 continue
101
102 print("[{}] {}".format(n_file, path))
103
104 n_file += 1
105
106 with open(path,'r+', encoding="utf-8") as fd:
107 comment = list()
108 first_param = -1
109 last_param = -1
110 n_line = 0
111
112 lines = fd.readlines()
113 fd.seek(0)
114 fd.truncate()
115
116 for line in lines:
117 n_line += 1
118
119 # Start comment
120 # Match C-style comment /* anywhere in the line
121 if re.search(r"/\*", line):
122 #print("Start comment {}".format(n_line))
123
124 if len(comment) > 0:
Anthony Barbierf202e502017-11-23 18:02:04 +0000125 raise Exception("{}:{}: Already in a comment!".format(path,n_line))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100126
127 comment.append(line)
128
129 # Comment already started
130 elif len(comment) > 0:
131 #print("Add line to comment {}".format(n_line))
132
133 comment.append(line)
134
135 # Non-comment line
136 else:
137 #print("Normal line {}".format(n_line))
138
139 fd.write(line)
140
141 # Match param declaration in Doxygen comment
142 # @param[in] name description
143 if re.search(r"@param\[[^\]]+\] +\S+ +\S", line):
144 #print("Param {}".format(n_line))
145
146 if first_param < 0:
147 first_param = len(comment) - 1
148
149 last_param = len(comment)
150
151 # Match end of C-style comment */
152 if re.search(r"\*/", line):
Anthony Barbier81bf1962017-07-25 11:02:08 +0100153 if re.search('"[^"]*\*/[^"]*"', line):
154 #print("End of comment inside a string: ignoring")
155 pass
156 else:
157 #print("End comment {}".format(n_line))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100158
Anthony Barbier81bf1962017-07-25 11:02:08 +0100159 if len(comment) < 1:
Anthony Barbierf202e502017-11-23 18:02:04 +0000160 raise Exception("{}:{}: Was not in a comment! ".format(path, n_line))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100161
Anthony Barbier81bf1962017-07-25 11:02:08 +0100162 #print("Process comment {} {}".format(first_param, last_param))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100163
Anthony Barbier81bf1962017-07-25 11:02:08 +0100164 process_comment(fd, comment, first_param, last_param)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100165
Anthony Barbier81bf1962017-07-25 11:02:08 +0100166 comment = list()
167 first_param = -1
168 last_param = -1