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