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