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