Turn to Regex in ModuleReader.java
[mirror_edk2.git] / Tools / Source / MigrationTools / org / tianocore / migration / ModuleInfo.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.io.*;
16 import java.util.*;
17 import java.util.regex.*;
18
19 /*
20 Class ModuleInfo is built for scanning the source files, it contains all the needed
21 information and all the temporary data.
22 */
23 public class ModuleInfo {
24 ModuleInfo(String modulepath, UI ui, Database db) throws Exception {
25 this.modulepath = modulepath;
26 this.ui = ui;
27 this.db = db;
28 moduleScan();
29 }
30
31 private String modulepath = null;
32 private Database db = null;
33 private UI ui = null;
34
35 public String modulename = null;
36 public String guidvalue = null;
37 public String moduletype = null;
38 public String entrypoint = null;
39
40 public Set<String> localmodulesources = new HashSet<String>(); //contains both .c and .h
41 public Set<String> preprocessedccodes = new HashSet<String>();
42 public Set<String> msaorinf = new HashSet<String>(); //only a little, hash may be too big for this
43
44 public Set<String> hashfuncc = new HashSet<String>();
45 public Set<String> hashfuncd = new HashSet<String>();
46 public Set<String> hashnonlocalfunc = new HashSet<String>();
47 public Set<String> hashnonlocalmacro = new HashSet<String>();
48 public Set<String> hashEFIcall = new HashSet<String>();
49 public Set<String> hashr8only = new HashSet<String>();
50
51 public Set<String> hashrequiredr9libs = new HashSet<String>(); // hashrequiredr9libs is now all added in SourceFileReplacer
52 public Set<String> guid = new HashSet<String>();
53 public Set<String> protocol = new HashSet<String>();
54 public Set<String> ppi = new HashSet<String>();
55
56 private static String migrationcomment = "//%$//";
57
58 private void dirScan(String subpath) throws Exception {
59 String[] list = new File(modulepath + File.separator + subpath).list(); // if no sub , separator need?
60 File test;
61
62 for (int i = 0 ; i < list.length ; i++) {
63 test = new File(modulepath + File.separator + subpath + list[i]);
64 if (test.isDirectory()) {
65 if (list[i].contains("result") || list[i].contains("temp")) {
66 } else {
67 dirScan(subpath + list[i] + File.separator);
68 }
69 } else {
70 if (list[i].contains(".c") || list[i].contains(".C") || list[i].contains(".h") ||
71 list[i].contains(".H") || list[i].contains(".dxs") || list[i].contains(".uni")) {
72 localmodulesources.add(subpath + list[i]);
73 } else if (list[i].contains(".inf") || list[i].contains(".msa")) {
74 msaorinf.add(subpath + list[i]);
75 }
76 }
77 }
78 }
79
80 private void moduleScan() throws Exception {
81 dirScan("");
82 String filename = null;
83 if (msaorinf.isEmpty()) {
84 ui.println("No INF nor MSA file found!");
85 System.exit(0);
86 } else {
87 filename = ui.choose("Found .inf or .msa file in the module\nChoose one Please", msaorinf.toArray());
88 }
89 ModuleReader mr = new ModuleReader(modulepath, this, db, ui);
90 if (filename.contains(".inf")) {
91 mr.readInf(filename);
92 } else if (filename.contains(".msa")) {
93 mr.readMsa(filename);
94 }
95
96 CommentOutNonLocalHFile();
97 parsePreProcessedSourceCode();
98
99 new SourceFileReplacer(modulepath, this, db, ui).flush(); // some adding library actions are taken here,so it must be put before "MsaWriter"
100
101 // show result
102 if (ui.yesOrNo("Parse of the Module Information has completed. View details?")) {
103 ui.println("\nModule Information : ");
104 ui.println("Entrypoint : " + entrypoint);
105 show(protocol, "Protocol : ");
106 show(ppi, "Ppi : ");
107 show(guid, "Guid : ");
108 show(hashfuncc, "call : ");
109 show(hashfuncd, "def : ");
110 show(hashEFIcall, "EFIcall : ");
111 show(hashnonlocalmacro, "macro : ");
112 show(hashnonlocalfunc, "nonlocal : ");
113 show(hashr8only, "hashr8only : ");
114 }
115
116 new MsaWriter(modulepath, this, db).flush();
117
118 // remove temp directory
119 //File tempdir = new File(modulepath + File.separator + "temp");
120 //System.out.println("Deleting Dir");
121 //if (tempdir.exists()) tempdir.d;
122
123 ui.println("Errors Left : " + db.error);
124 ui.println("Complete!");
125 ui.println("Your R9 module was placed here: " + modulepath + File.separator + "result");
126 ui.println("Your logfile was placed here: " + modulepath);
127 }
128
129 private void show(Set<String> hash, String show) {
130 ui.println(show + hash.size());
131 ui.println(hash);
132 }
133
134 // add '//' to all non-local include lines
135 private void CommentOutNonLocalHFile() throws IOException {
136 BufferedReader rd;
137 String line;
138 String curFile;
139 PrintWriter outfile;
140
141 Pattern ptninclude = Pattern.compile("[\"<](.*[.]h)[\">]");
142 Matcher mtrinclude;
143
144 Iterator<String> ii = localmodulesources.iterator();
145 while ( ii.hasNext() ) {
146 curFile = ii.next();
147 rd = new BufferedReader(new FileReader(modulepath + File.separator + curFile));
148 Common.ensureDir(modulepath + File.separator + "temp" + File.separator + curFile);
149 outfile = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "temp" + File.separator + curFile)));
150 while ((line = rd.readLine()) != null) {
151 if (line.contains("#include")) {
152 mtrinclude = ptninclude.matcher(line);
153 if (mtrinclude.find() && localmodulesources.contains(mtrinclude.group(1))) {
154 } else {
155 line = migrationcomment + line;
156 }
157 }
158 outfile.append(line + '\n');
159 }
160 outfile.flush();
161 outfile.close();
162 }
163 }
164
165 private void parsePreProcessedSourceCode() throws Exception {
166 //Cl cl = new Cl(modulepath);
167 //cl.execute("Fat.c");
168 //cl.generateAll(preprocessedccodes);
169 //
170 //System.out.println("Note!!!! The CL is not implemented now , pls do it manually!!! RUN :");
171 //System.out.println("cl " + modulepath + "\\temp\\*.c" + " -P");
172 //String[] list = new File(modulepath + File.separator + "temp").list(); // without CL , add
173 BufferedReader rd = null;
174 String ifile = null;
175 String line = null;
176 String temp = null;
177
178 Iterator<String> ii = localmodulesources.iterator();
179 while (ii.hasNext()) {
180 temp = ii.next();
181 if (temp.contains(".c")) {
182 preprocessedccodes.add(temp);
183 }
184 }
185
186 ii = preprocessedccodes.iterator();
187
188 Pattern patefifuncc = Pattern.compile("g?(BS|RT)\\s*->\\s*([a-zA-Z_]\\w*)",Pattern.MULTILINE);
189 Pattern patentrypoint = Pattern.compile("EFI_([A-Z]*)_ENTRY_POINT\\s*\\(([^\\(\\)]*)\\)",Pattern.MULTILINE);
190 Matcher matguid;
191 Matcher matfuncc;
192 Matcher matfuncd;
193 Matcher matenclosereplace;
194 Matcher matefifuncc;
195 Matcher matentrypoint;
196 Matcher matmacro;
197
198 while (ii.hasNext()) {
199 StringBuffer wholefile = new StringBuffer();
200 ifile = ii.next();
201 rd = new BufferedReader(new FileReader(modulepath + File.separator + "temp" + File.separator + ifile));
202 while ((line = rd.readLine()) != null) {
203 wholefile.append(line + '\n');
204 }
205 line = wholefile.toString();
206
207 // if this is a Pei phase module , add these library class to .msa
208 matentrypoint = patentrypoint.matcher(line);
209 if (matentrypoint.find()) {
210 entrypoint = matentrypoint.group(2);
211 if (matentrypoint.group(1).matches("PEIM")) {
212 hashrequiredr9libs.add("PeimEntryPoint");
213 } else {
214 hashrequiredr9libs.add("UefiDriverEntryPoint");
215 }
216 }
217
218 // find guid
219 matguid = Guid.ptnguid.matcher(line); // several ways to implement this , which one is faster ? :
220 while (matguid.find()) { // 1.currently , find once , then call to identify which is it
221 if ((temp = Guid.register(matguid, this, db)) != null) { // 2.use 3 different matchers , search 3 times to find each
222 //matguid.appendReplacement(result, db.getR9Guidname(temp)); // search the database for all 3 kinds of guids , high cost
223 }
224 }
225 //matguid.appendTail(result);
226 //line = result.toString();
227
228 // find EFI call in form of '->' , many 'gUnicodeCollationInterface->' like things are not changed
229 // This item is not simply replaced , special operation is required.
230 matefifuncc = patefifuncc.matcher(line);
231 while (matefifuncc.find()) {
232 hashEFIcall.add(matefifuncc.group(2));
233 }
234
235 // find function call
236 matfuncc = Func.ptnfuncc.matcher(line);
237 while (matfuncc.find()) {
238 if ((temp = Func.register(matfuncc, this, db)) != null) {
239 //ui.println(ifile + " dofunc " + temp);
240 //matfuncc.appendReplacement(result, db.getR9Func(temp));
241 }
242 }
243 //matfuncc.appendTail(result);
244 //line = result.toString();
245
246 // find macro
247 matmacro = Macro.ptntmacro.matcher(line);
248 while (matmacro.find()) {
249 if ((temp = Macro.register(matmacro, this, db)) != null) {
250 }
251 }
252
253 // find function definition
254 // replace all {} to @
255 while ((matenclosereplace = Func.ptnbrace.matcher(line)).find()) {
256 line = matenclosereplace.replaceAll("@");
257 }
258
259 matfuncd = Func.ptnfuncd.matcher(line);
260 while (matfuncd.find()) {
261 if ((temp = Func.register(matfuncd, this, db)) != null) {
262 }
263 }
264 }
265
266 // op on hash
267 Iterator<String> funcci = hashfuncc.iterator();
268 while (funcci.hasNext()) {
269 if (!hashfuncd.contains(temp = funcci.next()) && !hashEFIcall.contains(temp)) {
270 hashnonlocalfunc.add(temp); // this set contains both changed and not changed items
271 }
272 }
273 }
274
275 public static void main(String[] args) throws Exception {
276 FirstPanel.init();
277 }
278 }