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