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