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