]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Source/MigrationTools/org/tianocore/migration/Critic.java
enhance ForDoAll
[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
6f55be9f 29 public static final void run(String filepath) throws Exception {\r
7deec148 30 if (MigrationTool.doCritic) { // this is left here to set an example for future structure\r
6f55be9f 31 critic(filepath);\r
32 }\r
33 }\r
34 \r
35 private static final void critic(String filepath) throws Exception {\r
fed802b1 36 if (filepath.contains(".c") || filepath.contains(".h")) {\r
3637381b 37 BufferedReader rd = null;\r
38 String line = null;\r
39 StringBuffer templine = new StringBuffer();\r
1af2e90c 40 boolean incomment = false;\r
63406ead 41\r
8c4eeeb6 42 System.out.println("Criticing " + filepath);\r
fed802b1 43 String wholeline = Common.file2string(filepath);\r
63406ead 44\r
45 wholeline = wholeline.replaceAll("\t", " ");\r
a756211f 46 wholeline = Common.replaceAll(wholeline, ptnheadcomment, "/** @file$1**/");\r
63406ead 47 wholeline = Common.replaceAll(wholeline, ptnfunccomment, "$1/**$3**/$4$2$5");\r
3637381b 48 //wholeline = Common.replaceAll(wholeline, ptncommentstructure, "/**\n#%\n$1\n%#\n#%%\n$2\n%%#\n#%%%\n$3\n%%%#\n**/");\r
5ea254f6 49\r
50 // first scan\r
51 boolean description = false;\r
52 boolean arguments = false;\r
53 boolean returns = false;\r
54 boolean inequation = false;\r
3637381b 55 rd = new BufferedReader(new StringReader(wholeline));\r
56 while ((line = rd.readLine()) != null) {\r
1af2e90c 57 if (line.matches("\\/\\*\\*")) {\r
58 incomment = true;\r
969afc26 59 description = false;\r
60 arguments = false;\r
61 returns = false;\r
3637381b 62 templine.append(line + "\n");\r
1af2e90c 63 } else if (line.matches("\\*\\*\\/")) {\r
64 incomment = false;\r
65 templine.append(line + "\n");\r
8a5cdc3f 66 } else if (incomment) {\r
67 if (line.contains("Routine Description:")) {\r
68 description = true;\r
69 arguments = false;\r
70 returns = false;\r
71 } else if (line.contains("Arguments:")) {\r
72 description = false;\r
73 arguments = true;\r
74 returns = false;\r
75 } else if (line.contains("Returns:")) {\r
76 description = false;\r
77 arguments = false;\r
78 returns = true;\r
79 } else if (description) {\r
80 if (line.trim().length() != 0) {\r
81 templine.append(" " + line.trim() + "\n");\r
82 }\r
83 } else if (arguments) {\r
84 mtrcommentequation = ptncommentequation.matcher(line);\r
85 if (mtrcommentequation.find()) {\r
86 inequation = true;\r
87 templine.append(" @param " + mtrcommentequation.group(1) + " " + mtrcommentequation.group(2) + "\n");\r
88 } else if (inequation && line.trim().length() == 0) {\r
89 inequation = false;\r
90 templine.append(line + "\n");\r
91 } else if (inequation && line.trim().length() != 0) {\r
92 templine.append("#%#%" + line + "\n");\r
93 } else {\r
94 templine.append(" " + line.trim() + "\n");\r
95 }\r
96 } else if (returns) {\r
97 mtrcommentequation = ptncommentequation.matcher(line);\r
98 if (mtrcommentequation.find()) {\r
99 inequation = true;\r
100 templine.append(" @retval " + mtrcommentequation.group(1) + " " + mtrcommentequation.group(2) + "\n");\r
101 } else if (inequation && line.trim().length() == 0) {\r
102 inequation = false;\r
103 templine.append(line + "\n");\r
104 } else if (inequation && line.trim().length() != 0) {\r
105 templine.append("#%#%" + line + "\n");\r
106 } else {\r
107 templine.append(" " + line.trim() + "\n");\r
108 }\r
3637381b 109 }\r
3637381b 110 } else {\r
111 templine.append(line + "\n");\r
112 }\r
113 }\r
1af2e90c 114 wholeline = templine.toString();\r
7934aee7 115 wholeline = wholeline.replaceAll("\n#%#%\\s*", " ");\r
5ea254f6 116 //\r
117 \r
118 // secend scan\r
119 int startmax = 0;\r
120 rd = new BufferedReader(new StringReader(wholeline));\r
121 while ((line = rd.readLine()) != null) {\r
122 if (line.matches("\\/\\*\\*")) {\r
123 incomment = true;\r
124 templine.append(line + "\n");\r
125 } else if (line.matches("\\*\\*\\/")) {\r
126 incomment = false;\r
127 templine.append(line + "\n");\r
128 } else if (incomment) {\r
129 mtrnewcomment = ptnnewcomment.matcher(line);\r
130 if (mtrnewcomment.find()) {\r
131 startmax = mtrnewcomment.group(1).length() > startmax ? mtrnewcomment.group(1).length() : startmax;\r
132 }\r
133 }\r
134 }\r
135 startmax++;\r
136 //\r
137 \r
138 // third scan\r
139 int n = 0;\r
140 String temp = null;\r
141 String[] tempcont = null;\r
142 int count = 0;\r
143 templine = new StringBuffer();\r
144 rd = new BufferedReader(new StringReader(wholeline));\r
145 while ((line = rd.readLine()) != null) {\r
146 if (line.matches("\\/\\*\\*")) {\r
147 incomment = true;\r
148 templine.append(line + "\n");\r
149 } else if (line.matches("\\*\\*\\/")) {\r
150 incomment = false;\r
151 templine.append(line + "\n");\r
152 } else if (incomment) {\r
153 mtrnewcomment = ptnnewcomment.matcher(line);\r
154 if (mtrnewcomment.find()) {\r
155 n = startmax - mtrnewcomment.group(1).length();\r
156 templine.append(mtrnewcomment.group(1));\r
157 while (n-- >= 0) {\r
158 templine.append(" ");\r
159 }\r
160 temp = mtrnewcomment.group(3);\r
161 tempcont = temp.split(" "); // use \\s+ ?\r
162 \r
163 count = 0;\r
164 for (int i = 0; i < tempcont.length; i++) {\r
165 count += tempcont[i].length();\r
166 if (count <= (totallinelength - startmax)) {\r
167 templine.append(tempcont[i] + " ");\r
168 count += 1;\r
169 } else {\r
170 templine.append("\n");\r
171 n = startmax;\r
172 while (n-- >= 0) {\r
173 templine.append(" ");\r
174 }\r
175 templine.append(tempcont[i] + " ");\r
176 count = tempcont[i].length() + 1;\r
177 }\r
178 }\r
179 templine.append("\n");\r
180 } else {\r
181 templine.append(line + "\n");\r
182 }\r
183 } else {\r
184 templine.append(line + "\n");\r
185 }\r
186 }\r
187 wholeline = templine.toString();\r
188 //\r
6f55be9f 189\r
8c4eeeb6 190 Common.string2file(wholeline, filepath);\r
fed802b1 191 }\r
192 }\r
193 \r
5ea254f6 194 public static final void fireAt(String path) throws Exception {\r
ac62aa9a 195 //Common.toDoAll(Common.dirCopy_(path), Critic.class.getMethod("critic", String.class), null, null, Common.FILE);\r
6f55be9f 196 Common.toDoAll(path, Critic.class.getMethod("run", String.class), null, null, Common.FILE);\r
5ea254f6 197 //Common.toDoAll(Common.dirCopy_(path), critic, Common.FILE);\r
fed802b1 198 System.out.println("Critic Done");\r
199 }\r
6f55be9f 200}