]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Source/MigrationTools/org/tianocore/migration/Critic.java
modify critic
[mirror_edk2.git] / Tools / Source / MigrationTools / org / tianocore / migration / Critic.java
CommitLineData
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 13package org.tianocore.migration;\r
14\r
15import java.util.regex.*;\r
3637381b 16import java.io.*;\r
fed802b1 17\r
eee63a7b 18public final class Critic {\r
19 private static final Pattern ptnheadcomment = Pattern.compile("^\\/\\*\\+\\+(.*?)\\-\\-\\*\\/",Pattern.DOTALL);\r
20 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 21 //private static Pattern ptncommentstructure = Pattern.compile("\\/\\*\\+\\+\\s*Routine Description:\\s*(.*?)\\s*Arguments:\\s*(.*?)\\s*Returns:\\s*(.*?)\\s*\\-\\-\\*\\/",Pattern.DOTALL);\r
eee63a7b 22 private static final Pattern ptncommentequation = Pattern.compile("([^\\s]*)\\s+-\\s+(.*)\\s*");\r
5ea254f6 23 private static Matcher mtrcommentequation;\r
eee63a7b 24 private static final Pattern ptnnewcomment = Pattern.compile("(\\s*@(param|retval)\\s+[^\\s]+)\\s+(.*)");\r
5ea254f6 25 private static Matcher mtrnewcomment;\r
fed802b1 26 \r
5ea254f6 27 private static final int totallinelength = 82;\r
28 \r
29 public static final void critic(String filepath) throws Exception {\r
fed802b1 30 if (filepath.contains(".c") || filepath.contains(".h")) {\r
3637381b 31 BufferedReader rd = null;\r
32 String line = null;\r
33 StringBuffer templine = new StringBuffer();\r
1af2e90c 34 boolean incomment = false;\r
63406ead 35\r
8c4eeeb6 36 System.out.println("Criticing " + filepath);\r
fed802b1 37 String wholeline = Common.file2string(filepath);\r
63406ead 38\r
39 wholeline = wholeline.replaceAll("\t", " ");\r
a756211f 40 wholeline = Common.replaceAll(wholeline, ptnheadcomment, "/** @file$1**/");\r
63406ead 41 wholeline = Common.replaceAll(wholeline, ptnfunccomment, "$1/**$3**/$4$2$5");\r
3637381b 42 //wholeline = Common.replaceAll(wholeline, ptncommentstructure, "/**\n#%\n$1\n%#\n#%%\n$2\n%%#\n#%%%\n$3\n%%%#\n**/");\r
5ea254f6 43\r
44 // first scan\r
45 boolean description = false;\r
46 boolean arguments = false;\r
47 boolean returns = false;\r
48 boolean inequation = false;\r
3637381b 49 rd = new BufferedReader(new StringReader(wholeline));\r
50 while ((line = rd.readLine()) != null) {\r
1af2e90c 51 if (line.matches("\\/\\*\\*")) {\r
52 incomment = true;\r
3637381b 53 templine.append(line + "\n");\r
1af2e90c 54 } else if (line.matches("\\*\\*\\/")) {\r
55 incomment = false;\r
56 templine.append(line + "\n");\r
8a5cdc3f 57 } else if (incomment) {\r
58 if (line.contains("Routine Description:")) {\r
59 description = true;\r
60 arguments = false;\r
61 returns = false;\r
62 } else if (line.contains("Arguments:")) {\r
63 description = false;\r
64 arguments = true;\r
65 returns = false;\r
66 } else if (line.contains("Returns:")) {\r
67 description = false;\r
68 arguments = false;\r
69 returns = true;\r
70 } else if (description) {\r
71 if (line.trim().length() != 0) {\r
72 templine.append(" " + line.trim() + "\n");\r
73 }\r
74 } else if (arguments) {\r
75 mtrcommentequation = ptncommentequation.matcher(line);\r
76 if (mtrcommentequation.find()) {\r
77 inequation = true;\r
78 templine.append(" @param " + mtrcommentequation.group(1) + " " + mtrcommentequation.group(2) + "\n");\r
79 } else if (inequation && line.trim().length() == 0) {\r
80 inequation = false;\r
81 templine.append(line + "\n");\r
82 } else if (inequation && line.trim().length() != 0) {\r
83 templine.append("#%#%" + line + "\n");\r
84 } else {\r
85 templine.append(" " + line.trim() + "\n");\r
86 }\r
87 } else if (returns) {\r
88 mtrcommentequation = ptncommentequation.matcher(line);\r
89 if (mtrcommentequation.find()) {\r
90 inequation = true;\r
91 templine.append(" @retval " + mtrcommentequation.group(1) + " " + mtrcommentequation.group(2) + "\n");\r
92 } else if (inequation && line.trim().length() == 0) {\r
93 inequation = false;\r
94 templine.append(line + "\n");\r
95 } else if (inequation && line.trim().length() != 0) {\r
96 templine.append("#%#%" + line + "\n");\r
97 } else {\r
98 templine.append(" " + line.trim() + "\n");\r
99 }\r
3637381b 100 }\r
3637381b 101 } else {\r
102 templine.append(line + "\n");\r
103 }\r
104 }\r
1af2e90c 105 wholeline = templine.toString();\r
7934aee7 106 wholeline = wholeline.replaceAll("\n#%#%\\s*", " ");\r
5ea254f6 107 //\r
108 \r
109 // secend scan\r
110 int startmax = 0;\r
111 rd = new BufferedReader(new StringReader(wholeline));\r
112 while ((line = rd.readLine()) != null) {\r
113 if (line.matches("\\/\\*\\*")) {\r
114 incomment = true;\r
115 templine.append(line + "\n");\r
116 } else if (line.matches("\\*\\*\\/")) {\r
117 incomment = false;\r
118 templine.append(line + "\n");\r
119 } else if (incomment) {\r
120 mtrnewcomment = ptnnewcomment.matcher(line);\r
121 if (mtrnewcomment.find()) {\r
122 startmax = mtrnewcomment.group(1).length() > startmax ? mtrnewcomment.group(1).length() : startmax;\r
123 }\r
124 }\r
125 }\r
126 startmax++;\r
127 //\r
128 \r
129 // third scan\r
130 int n = 0;\r
131 String temp = null;\r
132 String[] tempcont = null;\r
133 int count = 0;\r
134 templine = new StringBuffer();\r
135 rd = new BufferedReader(new StringReader(wholeline));\r
136 while ((line = rd.readLine()) != null) {\r
137 if (line.matches("\\/\\*\\*")) {\r
138 incomment = true;\r
139 templine.append(line + "\n");\r
140 } else if (line.matches("\\*\\*\\/")) {\r
141 incomment = false;\r
142 templine.append(line + "\n");\r
143 } else if (incomment) {\r
144 mtrnewcomment = ptnnewcomment.matcher(line);\r
145 if (mtrnewcomment.find()) {\r
146 n = startmax - mtrnewcomment.group(1).length();\r
147 templine.append(mtrnewcomment.group(1));\r
148 while (n-- >= 0) {\r
149 templine.append(" ");\r
150 }\r
151 temp = mtrnewcomment.group(3);\r
152 tempcont = temp.split(" "); // use \\s+ ?\r
153 \r
154 count = 0;\r
155 for (int i = 0; i < tempcont.length; i++) {\r
156 count += tempcont[i].length();\r
157 if (count <= (totallinelength - startmax)) {\r
158 templine.append(tempcont[i] + " ");\r
159 count += 1;\r
160 } else {\r
161 templine.append("\n");\r
162 n = startmax;\r
163 while (n-- >= 0) {\r
164 templine.append(" ");\r
165 }\r
166 templine.append(tempcont[i] + " ");\r
167 count = tempcont[i].length() + 1;\r
168 }\r
169 }\r
170 templine.append("\n");\r
171 } else {\r
172 templine.append(line + "\n");\r
173 }\r
174 } else {\r
175 templine.append(line + "\n");\r
176 }\r
177 }\r
178 wholeline = templine.toString();\r
179 //\r
1af2e90c 180 \r
a756211f 181 /* -----slow edition of replacefirst with stringbuffer-----\r
182 line.append(wholeline);\r
183 mtrfunccomment = ptnfunccomment.matcher(line);\r
8c4eeeb6 184 while (mtrfunccomment.find()) {\r
a756211f 185 line.replace(0, line.length()-1, mtrfunccomment.replaceFirst("$2$4$3$1$5"));\r
8c4eeeb6 186 }\r
a756211f 187 */\r
1af2e90c 188 /* -----slow edition of replacefirst with string-----\r
a756211f 189 while ((mtrfunccomment = ptnfunccomment.matcher(wholeline)).find()) {\r
190 //funccomment = mtrfunccomment.group(2);\r
191 //mtrcommentstructure = ptncommentstructure.matcher(funccomment);\r
192 wholeline = mtrfunccomment.replaceFirst("$2$4$3$1$5");\r
193 }\r
1af2e90c 194 */\r
a756211f 195 /*\r
8c4eeeb6 196 // edit func comment\r
197 mtrtempcomment = ptntempcomment.matcher(wholeline);\r
198 while (mtrtempcomment.find()) {\r
199 System.out.println("-----------------------------");\r
200 System.out.println(mtrtempcomment.group());\r
201 System.out.println("-----------------------------");\r
202 }\r
a756211f 203 */\r
8c4eeeb6 204 Common.string2file(wholeline, filepath);\r
fed802b1 205 }\r
206 }\r
207 \r
5ea254f6 208 public static final void fireAt(String path) throws Exception {\r
ac62aa9a 209 //Common.toDoAll(Common.dirCopy_(path), Critic.class.getMethod("critic", String.class), null, null, Common.FILE);\r
210 Common.toDoAll(path, Critic.class.getMethod("critic", String.class), null, null, Common.FILE);\r
5ea254f6 211 //Common.toDoAll(Common.dirCopy_(path), critic, Common.FILE);\r
fed802b1 212 System.out.println("Critic Done");\r
213 }\r
214}\r
8c4eeeb6 215//analyze func comment\r
216/*if (mtrcommentstructure.find()) {\r
217 newcomment.append("/*++\n\n" + mtrcommentstructure.group(1) + "\n\n");\r
218 \r
219 //System.out.println("-------1-------");\r
220 //System.out.println(mtrcommentstructure.group(1));\r
221 \r
222 // arg\r
223 //System.out.println("-------2-------");\r
224 //System.out.println(mtrcommentstructure.group(2));\r
225 mtrinfequation = ptninfequation.matcher(mtrcommentstructure.group(2));\r
226 while (mtrinfequation.find()) {\r
227 newcomment.append("@param " + mtrinfequation.group(1) + " " + mtrinfequation.group(2) + "\n");\r
228 //System.out.println("@param " + mtrinfequation.group(1) + " " + mtrinfequation.group(2));\r
229 }\r
230 newcomment.append("\n");\r
231 // return\r
232 //System.out.println("-------3-------");\r
233 //System.out.println(mtrcommentstructure.group(3));\r
234 mtrinfequation = ptninfequation.matcher(mtrcommentstructure.group(3));\r
235 while (mtrinfequation.find()) {\r
236 newcomment.append("@retval " + mtrinfequation.group(1) + " " + mtrinfequation.group(2) + "\n");\r
237 //System.out.println("@retval " + mtrinfequation.group(1) + " " + mtrinfequation.group(2));\r
238 }\r
239 System.out.println(newcomment);\r
240} else {\r
241 System.out.println("Error: Comment Style Incorrect");\r
242}*/