]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Source/MigrationTools/org/tianocore/migration/Critic.java
modify r8onlylib generate
[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
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
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
63406ead 48 wholeline = Common.replaceAll(wholeline, ptnfunccomment, "$1/**$3**/$4$2$5");\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
108 templine.append(" " + line.trim() + "\n");\r
109 }\r
3637381b 110 }\r
3637381b 111 } else {\r
112 templine.append(line + "\n");\r
113 }\r
114 }\r
1af2e90c 115 wholeline = templine.toString();\r
7934aee7 116 wholeline = wholeline.replaceAll("\n#%#%\\s*", " ");\r
5ea254f6 117 //\r
118 \r
119 // secend scan\r
120 int startmax = 0;\r
121 rd = new BufferedReader(new StringReader(wholeline));\r
122 while ((line = rd.readLine()) != null) {\r
123 if (line.matches("\\/\\*\\*")) {\r
124 incomment = true;\r
125 templine.append(line + "\n");\r
126 } else if (line.matches("\\*\\*\\/")) {\r
127 incomment = false;\r
128 templine.append(line + "\n");\r
129 } else if (incomment) {\r
130 mtrnewcomment = ptnnewcomment.matcher(line);\r
131 if (mtrnewcomment.find()) {\r
132 startmax = mtrnewcomment.group(1).length() > startmax ? mtrnewcomment.group(1).length() : startmax;\r
133 }\r
134 }\r
135 }\r
136 startmax++;\r
137 //\r
138 \r
139 // third scan\r
140 int n = 0;\r
141 String temp = null;\r
142 String[] tempcont = null;\r
143 int count = 0;\r
144 templine = new StringBuffer();\r
145 rd = new BufferedReader(new StringReader(wholeline));\r
146 while ((line = rd.readLine()) != null) {\r
147 if (line.matches("\\/\\*\\*")) {\r
148 incomment = true;\r
149 templine.append(line + "\n");\r
150 } else if (line.matches("\\*\\*\\/")) {\r
151 incomment = false;\r
152 templine.append(line + "\n");\r
153 } else if (incomment) {\r
154 mtrnewcomment = ptnnewcomment.matcher(line);\r
155 if (mtrnewcomment.find()) {\r
156 n = startmax - mtrnewcomment.group(1).length();\r
157 templine.append(mtrnewcomment.group(1));\r
158 while (n-- >= 0) {\r
159 templine.append(" ");\r
160 }\r
161 temp = mtrnewcomment.group(3);\r
162 tempcont = temp.split(" "); // use \\s+ ?\r
163 \r
164 count = 0;\r
165 for (int i = 0; i < tempcont.length; i++) {\r
166 count += tempcont[i].length();\r
167 if (count <= (totallinelength - startmax)) {\r
168 templine.append(tempcont[i] + " ");\r
169 count += 1;\r
170 } else {\r
171 templine.append("\n");\r
172 n = startmax;\r
173 while (n-- >= 0) {\r
174 templine.append(" ");\r
175 }\r
176 templine.append(tempcont[i] + " ");\r
177 count = tempcont[i].length() + 1;\r
178 }\r
179 }\r
180 templine.append("\n");\r
181 } else {\r
182 templine.append(line + "\n");\r
183 }\r
184 } else {\r
185 templine.append(line + "\n");\r
186 }\r
187 }\r
188 wholeline = templine.toString();\r
189 //\r
6f55be9f 190\r
8c4eeeb6 191 Common.string2file(wholeline, filepath);\r
fed802b1 192 }\r
193 }\r
194 \r
5ea254f6 195 public static final void fireAt(String path) throws Exception {\r
ac62aa9a 196 //Common.toDoAll(Common.dirCopy_(path), Critic.class.getMethod("critic", String.class), null, null, Common.FILE);\r
6f55be9f 197 Common.toDoAll(path, Critic.class.getMethod("run", String.class), null, null, Common.FILE);\r
5ea254f6 198 //Common.toDoAll(Common.dirCopy_(path), critic, Common.FILE);\r
fed802b1 199 System.out.println("Critic Done");\r
200 }\r
6f55be9f 201}