Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Anthony Barbier | 2fe7d1c | 2017-09-15 13:07:36 +0100 | [diff] [blame] | 2 | # FIXME: Remove before the release |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 3 | |
| 4 | import os.path |
| 5 | import re |
| 6 | import sys |
| 7 | |
| 8 | def 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 15 | # Measure the indentation of the first param and use that to create an empty comment line string: |
Anthony Barbier | f202e50 | 2017-11-23 18:02:04 +0000 | [diff] [blame] | 16 | m = re.match(r" */", comment[0]) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 17 | |
| 18 | if not m: |
Anthony Barbier | f202e50 | 2017-11-23 18:02:04 +0000 | [diff] [blame] | 19 | raise Exception("{}: Not a comment ? '{}'".format(path,comment[first_param])) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 20 | |
Anthony Barbier | f202e50 | 2017-11-23 18:02:04 +0000 | [diff] [blame] | 21 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 32 | |
| 33 | # For each param split the line into 3 columns: param, param_name, description |
| 34 | for param in range(first_param, last_param): |
Anthony Barbier | f202e50 | 2017-11-23 18:02:04 +0000 | [diff] [blame] | 35 | m = re.match(r"[^@]+(@param\[[^\]]+\]) +(\S+) +(.+)", comment[param]) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 36 | |
| 37 | if m: |
Anthony Barbier | f202e50 | 2017-11-23 18:02:04 +0000 | [diff] [blame] | 38 | params.append( (" "+m.group(1), m.group(2), m.group(3)) ) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 39 | else: |
| 40 | # If it's not a match then it must be a multi-line param description: |
| 41 | |
Anthony Barbier | f202e50 | 2017-11-23 18:02:04 +0000 | [diff] [blame] | 42 | m = re.match(" *\* +(.*)", comment[param]) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 43 | if not m: |
Anthony Barbier | f202e50 | 2017-11-23 18:02:04 +0000 | [diff] [blame] | 44 | raise Exception("{}:{}: Not a comment line ? ".format(path, n_line - len(comment) + param + 1)) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 45 | |
Anthony Barbier | f202e50 | 2017-11-23 18:02:04 +0000 | [diff] [blame] | 46 | params.append( ("", "", m.group(1)) ) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 47 | |
| 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 Barbier | f202e50 | 2017-11-23 18:02:04 +0000 | [diff] [blame] | 64 | fd.write("{}{}{} {}{} {}\n".format( line_prefix, |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 65 | 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 Barbier | f202e50 | 2017-11-23 18:02:04 +0000 | [diff] [blame] | 75 | # 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 82 | |
| 83 | if __name__ == "__main__": |
| 84 | n_file=0 |
| 85 | |
| 86 | if len(sys.argv) == 1: |
| 87 | paths = [] |
| 88 | |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 89 | for top_level in ["./arm_compute", "./src", "./examples", "./tests", "./utils", "./framework", "./support"]: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 90 | 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 Liang | f1f3ebd | 2017-11-10 09:59:19 +0800 | [diff] [blame] | 98 | path[-2:] not in ("cs") and |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 99 | 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 Barbier | f202e50 | 2017-11-23 18:02:04 +0000 | [diff] [blame] | 125 | raise Exception("{}:{}: Already in a comment!".format(path,n_line)) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 126 | |
| 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 Barbier | 81bf196 | 2017-07-25 11:02:08 +0100 | [diff] [blame] | 153 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 158 | |
Anthony Barbier | 81bf196 | 2017-07-25 11:02:08 +0100 | [diff] [blame] | 159 | if len(comment) < 1: |
Anthony Barbier | f202e50 | 2017-11-23 18:02:04 +0000 | [diff] [blame] | 160 | raise Exception("{}:{}: Was not in a comment! ".format(path, n_line)) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 161 | |
Anthony Barbier | 81bf196 | 2017-07-25 11:02:08 +0100 | [diff] [blame] | 162 | #print("Process comment {} {}".format(first_param, last_param)) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 163 | |
Anthony Barbier | 81bf196 | 2017-07-25 11:02:08 +0100 | [diff] [blame] | 164 | process_comment(fd, comment, first_param, last_param) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 165 | |
Anthony Barbier | 81bf196 | 2017-07-25 11:02:08 +0100 | [diff] [blame] | 166 | comment = list() |
| 167 | first_param = -1 |
| 168 | last_param = -1 |