]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/MigrationTools/org/tianocore/migration/Critic.java
modify r8onlylib generate
[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/**$3**/$4$2$5");
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(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 } else if (line.contains("Returns:")) {
77 description = false;
78 arguments = false;
79 returns = true;
80 } else if (description) {
81 if (line.trim().length() != 0) {
82 templine.append(" " + line.trim() + "\n");
83 }
84 } else if (arguments) {
85 mtrcommentequation = ptncommentequation.matcher(line);
86 if (mtrcommentequation.find()) {
87 inequation = true;
88 templine.append(" @param " + 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 if (returns) {
98 mtrcommentequation = ptncommentequation.matcher(line);
99 if (mtrcommentequation.find()) {
100 inequation = true;
101 templine.append(" @retval " + mtrcommentequation.group(1) + " " + mtrcommentequation.group(2) + "\n");
102 } else if (inequation && line.trim().length() == 0) {
103 inequation = false;
104 templine.append(line + "\n");
105 } else if (inequation && line.trim().length() != 0) {
106 templine.append("#%#%" + line + "\n");
107 } else {
108 templine.append(" " + line.trim() + "\n");
109 }
110 }
111 } else {
112 templine.append(line + "\n");
113 }
114 }
115 wholeline = templine.toString();
116 wholeline = wholeline.replaceAll("\n#%#%\\s*", " ");
117 //
118
119 // secend scan
120 int startmax = 0;
121 rd = new BufferedReader(new StringReader(wholeline));
122 while ((line = rd.readLine()) != null) {
123 if (line.matches("\\/\\*\\*")) {
124 incomment = true;
125 templine.append(line + "\n");
126 } else if (line.matches("\\*\\*\\/")) {
127 incomment = false;
128 templine.append(line + "\n");
129 } else if (incomment) {
130 mtrnewcomment = ptnnewcomment.matcher(line);
131 if (mtrnewcomment.find()) {
132 startmax = mtrnewcomment.group(1).length() > startmax ? mtrnewcomment.group(1).length() : startmax;
133 }
134 }
135 }
136 startmax++;
137 //
138
139 // third scan
140 int n = 0;
141 String temp = null;
142 String[] tempcont = null;
143 int count = 0;
144 templine = new StringBuffer();
145 rd = new BufferedReader(new StringReader(wholeline));
146 while ((line = rd.readLine()) != null) {
147 if (line.matches("\\/\\*\\*")) {
148 incomment = true;
149 templine.append(line + "\n");
150 } else if (line.matches("\\*\\*\\/")) {
151 incomment = false;
152 templine.append(line + "\n");
153 } else if (incomment) {
154 mtrnewcomment = ptnnewcomment.matcher(line);
155 if (mtrnewcomment.find()) {
156 n = startmax - mtrnewcomment.group(1).length();
157 templine.append(mtrnewcomment.group(1));
158 while (n-- >= 0) {
159 templine.append(" ");
160 }
161 temp = mtrnewcomment.group(3);
162 tempcont = temp.split(" "); // use \\s+ ?
163
164 count = 0;
165 for (int i = 0; i < tempcont.length; i++) {
166 count += tempcont[i].length();
167 if (count <= (totallinelength - startmax)) {
168 templine.append(tempcont[i] + " ");
169 count += 1;
170 } else {
171 templine.append("\n");
172 n = startmax;
173 while (n-- >= 0) {
174 templine.append(" ");
175 }
176 templine.append(tempcont[i] + " ");
177 count = tempcont[i].length() + 1;
178 }
179 }
180 templine.append("\n");
181 } else {
182 templine.append(line + "\n");
183 }
184 } else {
185 templine.append(line + "\n");
186 }
187 }
188 wholeline = templine.toString();
189 //
190
191 Common.string2file(wholeline, filepath);
192 }
193 }
194
195 public static final void fireAt(String path) throws Exception {
196 //Common.toDoAll(Common.dirCopy_(path), Critic.class.getMethod("critic", String.class), null, null, Common.FILE);
197 Common.toDoAll(path, Critic.class.getMethod("run", String.class), null, null, Common.FILE);
198 //Common.toDoAll(Common.dirCopy_(path), critic, Common.FILE);
199 System.out.println("Critic Done");
200 }
201 }