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