]>
Commit | Line | Data |
---|---|---|
8c4eeeb6 | 1 | /** @file\r |
2 | \r | |
3 | Copyright (c) 2006, Intel Corporation\r | |
4 | All rights reserved. This program and the accompanying materials\r | |
5 | are licensed and made available under the terms and conditions of the BSD License\r | |
6 | which accompanies this distribution. The full text of the license may be found at\r | |
7 | http://opensource.org/licenses/bsd-license.php\r | |
8 | \r | |
9 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
10 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
11 | \r | |
12 | **/\r | |
fed802b1 | 13 | package org.tianocore.migration;\r |
14 | \r | |
15 | import java.util.regex.*;\r | |
3637381b | 16 | import java.io.*;\r |
fed802b1 | 17 | \r |
eee63a7b | 18 | public final class Critic {\r |
7ab9a5e5 | 19 | public static final Pattern PTN_NEW_HEAD_COMMENT = Pattern.compile("^\\/\\*\\*.*?\\*\\*\\/",Pattern.DOTALL);\r |
eee63a7b | 20 | private static final Pattern ptnheadcomment = Pattern.compile("^\\/\\*\\+\\+(.*?)\\-\\-\\*\\/",Pattern.DOTALL);\r |
68a05456 | 21 | private static final Pattern ptnfunccomment = Pattern.compile("([\\};\\/\">]\\s*)([\\w\\s\\*]*?[_\\w][_\\w\\d]*\\s*\\([^\\)\\(]*\\)\\s*)\\/\\*\\+\\+(.*?)\\-\\-\\*\\/(\\s*.*?)(?=[\\{;])",Pattern.DOTALL); // find function with {;">/ , may be unsafe\r |
7934aee7 | 22 | //private static Pattern ptncommentstructure = Pattern.compile("\\/\\*\\+\\+\\s*Routine Description:\\s*(.*?)\\s*Arguments:\\s*(.*?)\\s*Returns:\\s*(.*?)\\s*\\-\\-\\*\\/",Pattern.DOTALL);\r |
eee63a7b | 23 | private static final Pattern ptncommentequation = Pattern.compile("([^\\s]*)\\s+-\\s+(.*)\\s*");\r |
5ea254f6 | 24 | private static Matcher mtrcommentequation;\r |
eee63a7b | 25 | private static final Pattern ptnnewcomment = Pattern.compile("(\\s*@(param|retval)\\s+[^\\s]+)\\s+(.*)");\r |
5ea254f6 | 26 | private static Matcher mtrnewcomment;\r |
fed802b1 | 27 | \r |
5ea254f6 | 28 | private static final int totallinelength = 82;\r |
29 | \r | |
6f55be9f | 30 | public static final void run(String filepath) throws Exception {\r |
7deec148 | 31 | if (MigrationTool.doCritic) { // this is left here to set an example for future structure\r |
6f55be9f | 32 | critic(filepath);\r |
33 | }\r | |
34 | }\r | |
35 | \r | |
36 | private static final void critic(String filepath) throws Exception {\r | |
fed802b1 | 37 | if (filepath.contains(".c") || filepath.contains(".h")) {\r |
3637381b | 38 | BufferedReader rd = null;\r |
39 | String line = null;\r | |
40 | StringBuffer templine = new StringBuffer();\r | |
1af2e90c | 41 | boolean incomment = false;\r |
63406ead | 42 | \r |
8c4eeeb6 | 43 | System.out.println("Criticing " + filepath);\r |
fed802b1 | 44 | String wholeline = Common.file2string(filepath);\r |
63406ead | 45 | \r |
46 | wholeline = wholeline.replaceAll("\t", " ");\r | |
a756211f | 47 | wholeline = Common.replaceAll(wholeline, ptnheadcomment, "/** @file$1**/");\r |
b4648de2 | 48 | wholeline = Common.replaceAll(wholeline, ptnfunccomment, "$1/**$3**/$4$2");\r |
3637381b | 49 | //wholeline = Common.replaceAll(wholeline, ptncommentstructure, "/**\n#%\n$1\n%#\n#%%\n$2\n%%#\n#%%%\n$3\n%%%#\n**/");\r |
5ea254f6 | 50 | \r |
51 | // first scan\r | |
52 | boolean description = false;\r | |
53 | boolean arguments = false;\r | |
54 | boolean returns = false;\r | |
55 | boolean inequation = false;\r | |
3637381b | 56 | rd = new BufferedReader(new StringReader(wholeline));\r |
57 | while ((line = rd.readLine()) != null) {\r | |
1af2e90c | 58 | if (line.matches("\\/\\*\\*")) {\r |
59 | incomment = true;\r | |
969afc26 | 60 | description = false;\r |
61 | arguments = false;\r | |
62 | returns = false;\r | |
3637381b | 63 | templine.append(line + "\n");\r |
1af2e90c | 64 | } else if (line.matches("\\*\\*\\/")) {\r |
65 | incomment = false;\r | |
66 | templine.append(line + "\n");\r | |
8a5cdc3f | 67 | } else if (incomment) {\r |
68 | if (line.contains("Routine Description:")) {\r | |
69 | description = true;\r | |
70 | arguments = false;\r | |
71 | returns = false;\r | |
72 | } else if (line.contains("Arguments:")) {\r | |
73 | description = false;\r | |
74 | arguments = true;\r | |
75 | returns = false;\r | |
76 | } else if (line.contains("Returns:")) {\r | |
77 | description = false;\r | |
78 | arguments = false;\r | |
79 | returns = true;\r | |
80 | } else if (description) {\r | |
81 | if (line.trim().length() != 0) {\r | |
82 | templine.append(" " + line.trim() + "\n");\r | |
83 | }\r | |
84 | } else if (arguments) {\r | |
85 | mtrcommentequation = ptncommentequation.matcher(line);\r | |
86 | if (mtrcommentequation.find()) {\r | |
87 | inequation = true;\r | |
88 | templine.append(" @param " + mtrcommentequation.group(1) + " " + mtrcommentequation.group(2) + "\n");\r | |
89 | } else if (inequation && line.trim().length() == 0) {\r | |
90 | inequation = false;\r | |
91 | templine.append(line + "\n");\r | |
92 | } else if (inequation && line.trim().length() != 0) {\r | |
93 | templine.append("#%#%" + line + "\n");\r | |
94 | } else {\r | |
95 | templine.append(" " + line.trim() + "\n");\r | |
96 | }\r | |
97 | } else if (returns) {\r | |
98 | mtrcommentequation = ptncommentequation.matcher(line);\r | |
99 | if (mtrcommentequation.find()) {\r | |
100 | inequation = true;\r | |
101 | templine.append(" @retval " + mtrcommentequation.group(1) + " " + mtrcommentequation.group(2) + "\n");\r | |
102 | } else if (inequation && line.trim().length() == 0) {\r | |
103 | inequation = false;\r | |
104 | templine.append(line + "\n");\r | |
105 | } else if (inequation && line.trim().length() != 0) {\r | |
106 | templine.append("#%#%" + line + "\n");\r | |
107 | } else {\r | |
a817ecbf | 108 | if (line.trim().length() == 0) {\r |
109 | templine.append(" " + line.trim() + "\n");\r | |
110 | } else {\r | |
111 | templine.append(" @return " + line.trim() + "\n");\r | |
112 | }\r | |
8a5cdc3f | 113 | }\r |
3637381b | 114 | }\r |
3637381b | 115 | } else {\r |
116 | templine.append(line + "\n");\r | |
117 | }\r | |
118 | }\r | |
1af2e90c | 119 | wholeline = templine.toString();\r |
7934aee7 | 120 | wholeline = wholeline.replaceAll("\n#%#%\\s*", " ");\r |
5ea254f6 | 121 | //\r |
122 | \r | |
123 | // secend scan\r | |
124 | int startmax = 0;\r | |
125 | rd = new BufferedReader(new StringReader(wholeline));\r | |
126 | while ((line = rd.readLine()) != null) {\r | |
127 | if (line.matches("\\/\\*\\*")) {\r | |
128 | incomment = true;\r | |
129 | templine.append(line + "\n");\r | |
130 | } else if (line.matches("\\*\\*\\/")) {\r | |
131 | incomment = false;\r | |
132 | templine.append(line + "\n");\r | |
133 | } else if (incomment) {\r | |
134 | mtrnewcomment = ptnnewcomment.matcher(line);\r | |
135 | if (mtrnewcomment.find()) {\r | |
136 | startmax = mtrnewcomment.group(1).length() > startmax ? mtrnewcomment.group(1).length() : startmax;\r | |
137 | }\r | |
138 | }\r | |
139 | }\r | |
140 | startmax++;\r | |
141 | //\r | |
142 | \r | |
143 | // third scan\r | |
144 | int n = 0;\r | |
145 | String temp = null;\r | |
146 | String[] tempcont = null;\r | |
147 | int count = 0;\r | |
148 | templine = new StringBuffer();\r | |
149 | rd = new BufferedReader(new StringReader(wholeline));\r | |
150 | while ((line = rd.readLine()) != null) {\r | |
151 | if (line.matches("\\/\\*\\*")) {\r | |
152 | incomment = true;\r | |
153 | templine.append(line + "\n");\r | |
154 | } else if (line.matches("\\*\\*\\/")) {\r | |
155 | incomment = false;\r | |
156 | templine.append(line + "\n");\r | |
157 | } else if (incomment) {\r | |
158 | mtrnewcomment = ptnnewcomment.matcher(line);\r | |
159 | if (mtrnewcomment.find()) {\r | |
160 | n = startmax - mtrnewcomment.group(1).length();\r | |
161 | templine.append(mtrnewcomment.group(1));\r | |
162 | while (n-- >= 0) {\r | |
163 | templine.append(" ");\r | |
164 | }\r | |
165 | temp = mtrnewcomment.group(3);\r | |
166 | tempcont = temp.split(" "); // use \\s+ ?\r | |
167 | \r | |
168 | count = 0;\r | |
169 | for (int i = 0; i < tempcont.length; i++) {\r | |
170 | count += tempcont[i].length();\r | |
171 | if (count <= (totallinelength - startmax)) {\r | |
172 | templine.append(tempcont[i] + " ");\r | |
173 | count += 1;\r | |
174 | } else {\r | |
175 | templine.append("\n");\r | |
176 | n = startmax;\r | |
177 | while (n-- >= 0) {\r | |
178 | templine.append(" ");\r | |
179 | }\r | |
180 | templine.append(tempcont[i] + " ");\r | |
181 | count = tempcont[i].length() + 1;\r | |
182 | }\r | |
183 | }\r | |
184 | templine.append("\n");\r | |
185 | } else {\r | |
186 | templine.append(line + "\n");\r | |
187 | }\r | |
188 | } else {\r | |
189 | templine.append(line + "\n");\r | |
190 | }\r | |
191 | }\r | |
192 | wholeline = templine.toString();\r | |
193 | //\r | |
6f55be9f | 194 | \r |
8c4eeeb6 | 195 | Common.string2file(wholeline, filepath);\r |
fed802b1 | 196 | }\r |
197 | }\r | |
198 | \r | |
5ea254f6 | 199 | public static final void fireAt(String path) throws Exception {\r |
ac62aa9a | 200 | //Common.toDoAll(Common.dirCopy_(path), Critic.class.getMethod("critic", String.class), null, null, Common.FILE);\r |
6f55be9f | 201 | Common.toDoAll(path, Critic.class.getMethod("run", String.class), null, null, Common.FILE);\r |
5ea254f6 | 202 | //Common.toDoAll(Common.dirCopy_(path), critic, Common.FILE);\r |
fed802b1 | 203 | System.out.println("Critic Done");\r |
204 | }\r | |
6f55be9f | 205 | } |