Laplace take over SourceFileReplacer.java

git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@1472 6f19259b-4bc3-4df7-8a09-765794883524
This commit is contained in:
alfred 2006-09-06 04:39:31 +00:00
parent adb6105a5a
commit 2887f99b7c
2 changed files with 209 additions and 167 deletions

View File

@ -178,10 +178,18 @@ public final class Common {
}
public static abstract class Laplace {
public final void transform(String src, String des) throws Exception {
public void transform(String src, String des) throws Exception {
Common.string2file(operation(Common.file2string(src)), des);
}
public void run() {
}
public abstract String operation(String wholeline);
public abstract boolean recognize(String filename);
public abstract String namechange(String oldname);
}
}

View File

@ -20,8 +20,17 @@ import java.util.regex.Pattern;
public final class SourceFileReplacer implements Common.ForDoAll {
private static final SourceFileReplacer SFReplacer = new SourceFileReplacer();
private ModuleInfo mi;
//private static boolean showdetails = true; // set this as default now, may be changed in the future
private static final Set<Common.Laplace> Laplaces = new HashSet<Common.Laplace>();
// these sets are used only for printing log of the changes in current file
private static final Set<r8tor9> filefunc = new HashSet<r8tor9>();
private static final Set<r8tor9> filemacro = new HashSet<r8tor9>();
private static final Set<r8tor9> fileguid = new HashSet<r8tor9>();
private static final Set<r8tor9> fileppi = new HashSet<r8tor9>();
private static final Set<r8tor9> fileprotocol = new HashSet<r8tor9>();
private static final Set<String> filer8only = new HashSet<String>();
//---------------------------------------inner classes---------------------------------------//
private static class r8tor9 {
r8tor9(String r8, String r9) {
r8thing = r8;
@ -31,178 +40,211 @@ public final class SourceFileReplacer implements Common.ForDoAll {
public String r9thing;
}
// these sets are used only for printing log of the changes in current file
private static final Set<r8tor9> filefunc = new HashSet<r8tor9>();
private static final Set<r8tor9> filemacro = new HashSet<r8tor9>();
private static final Set<r8tor9> fileguid = new HashSet<r8tor9>();
private static final Set<r8tor9> fileppi = new HashSet<r8tor9>();
private static final Set<r8tor9> fileprotocol = new HashSet<r8tor9>();
private static final Set<String> filer8only = new HashSet<String>();
// Caution : if there is @ in file , it will be replaced with \n , so is you use Doxygen ... God Bless you!
private final String sourcefilereplace(String wholeline) throws Exception {
boolean addr8 = false;
Pattern pat = Pattern.compile("g?(BS|RT)(\\s*->\\s*)([a-zA-Z_]\\w*)", Pattern.MULTILINE); // ! only two level () bracket allowed !
//Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*[#$]*)(\\s*\\(([^\\(\\)]*(\\([^\\(\\)]*\\))?[^\\(\\)]*)*\\))", Pattern.MULTILINE);
// replace BS -> gBS , RT -> gRT
Matcher mat = pat.matcher(wholeline);
if (mat.find()) { // add a library here
MigrationTool.ui.println("Converting all BS->gBS, RT->gRT");
wholeline = mat.replaceAll("g$1$2$3"); //unknown correctiveness
private class IdleLaplace extends Common.Laplace {
public String operation(String wholeline) {
return wholeline;
}
mat.reset();
while (mat.find()) {
if (mat.group(1).matches("BS")) {
mi.hashrequiredr9libs.add("UefiBootServicesTableLib");
}
if (mat.group(1).matches("RT")) {
mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib");
}
}
// remove EFI_DRIVER_ENTRY_POINT
wholeline = wholeline.replaceAll("(EFI_\\w+_ENTRY_POINT)", MigrationTool.MIGRATIONCOMMENT + " $1");
// start replacing names
String r8thing;
String r9thing;
Iterator<String> it;
// Converting non-locla function
it = mi.hashnonlocalfunc.iterator();
while (it.hasNext()) {
r8thing = it.next();
if (r8thing.matches("EfiInitializeDriverLib")) { //s
mi.hashrequiredr9libs.add("UefiBootServicesTableLib"); //p
mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib"); //e
} else if (r8thing.matches("DxeInitializeDriverLib")) { //c
mi.hashrequiredr9libs.add("UefiBootServicesTableLib"); //i
mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib"); //a
mi.hashrequiredr9libs.add("DxeServicesTableLib"); //l
} else { //
mi.hashrequiredr9libs.add(MigrationTool.db.getR9Lib(r8thing)); // add a library here
}
public boolean recognize(String filename) {
return filename.contains(".h") || filename.contains(".H") || filename.contains(".uni");
}
r8tor9 temp;
if ((r9thing = MigrationTool.db.getR9Func(r8thing)) != null) {
if (!r8thing.equals(r9thing)) {
if (wholeline.contains(r8thing)) {
wholeline = wholeline.replaceAll(r8thing, r9thing);
filefunc.add(new r8tor9(r8thing, r9thing));
Iterator<r8tor9> rt = filefunc.iterator();
while (rt.hasNext()) {
temp = rt.next();
if (MigrationTool.db.r8only.contains(temp.r8thing)) {
filer8only.add(r8thing);
mi.hashr8only.add(r8thing);
addr8 = true;
public String namechange(String oldname) {
if (oldname.contains(".H")) {
return oldname.replaceFirst(".H", ".h");
} else {
return oldname;
}
}
}
private class DxsLaplace extends Common.Laplace {
public String operation(String wholeline) {
if (mi.getModuleType().equals("PEIM")) {
return addincludefile(wholeline, "\\<PeimDepex.h\\>");
} else {
return addincludefile(wholeline, "\\<DxeDepex.h\\>");
}
}
public boolean recognize(String filename) {
return filename.contains(".dxs");
}
public String namechange(String oldname) {
return oldname;
}
}
private class CLaplace extends Common.Laplace {
public String operation(String wholeline) {
boolean addr8 = false;
Pattern pat = Pattern.compile("g?(BS|RT)(\\s*->\\s*)([a-zA-Z_]\\w*)", Pattern.MULTILINE); // ! only two level () bracket allowed !
//Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*[#$]*)(\\s*\\(([^\\(\\)]*(\\([^\\(\\)]*\\))?[^\\(\\)]*)*\\))", Pattern.MULTILINE);
// replace BS -> gBS , RT -> gRT
Matcher mat = pat.matcher(wholeline);
if (mat.find()) { // add a library here
MigrationTool.ui.println("Converting all BS->gBS, RT->gRT");
wholeline = mat.replaceAll("g$1$2$3"); //unknown correctiveness
}
mat.reset();
while (mat.find()) {
if (mat.group(1).matches("BS")) {
mi.hashrequiredr9libs.add("UefiBootServicesTableLib");
}
if (mat.group(1).matches("RT")) {
mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib");
}
}
// remove EFI_DRIVER_ENTRY_POINT
wholeline = wholeline.replaceAll("(EFI_\\w+_ENTRY_POINT)", MigrationTool.MIGRATIONCOMMENT + " $1");
// start replacing names
String r8thing;
String r9thing;
Iterator<String> it;
// Converting non-locla function
it = mi.hashnonlocalfunc.iterator();
while (it.hasNext()) {
r8thing = it.next();
if (r8thing.matches("EfiInitializeDriverLib")) { //s
mi.hashrequiredr9libs.add("UefiBootServicesTableLib"); //p
mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib"); //e
} else if (r8thing.matches("DxeInitializeDriverLib")) { //c
mi.hashrequiredr9libs.add("UefiBootServicesTableLib"); //i
mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib"); //a
mi.hashrequiredr9libs.add("DxeServicesTableLib"); //l
} else { //
mi.hashrequiredr9libs.add(MigrationTool.db.getR9Lib(r8thing)); // add a library here
}
r8tor9 temp;
if ((r9thing = MigrationTool.db.getR9Func(r8thing)) != null) {
if (!r8thing.equals(r9thing)) {
if (wholeline.contains(r8thing)) {
wholeline = wholeline.replaceAll(r8thing, r9thing);
filefunc.add(new r8tor9(r8thing, r9thing));
Iterator<r8tor9> rt = filefunc.iterator();
while (rt.hasNext()) {
temp = rt.next();
if (MigrationTool.db.r8only.contains(temp.r8thing)) {
filer8only.add(r8thing);
mi.hashr8only.add(r8thing);
addr8 = true;
}
}
}
}
}
} //is any of the guids changed?
if (addr8 == true) {
wholeline = addincludefile(wholeline, "\"R8Lib.h\"");
}
} //is any of the guids changed?
if (addr8 == true) {
wholeline = addincludefile(wholeline, "\"R8Lib.h\"");
}
// Converting macro
it = mi.hashnonlocalmacro.iterator();
while (it.hasNext()) { //macros are all assumed MdePkg currently
r8thing = it.next();
//mi.hashrequiredr9libs.add(MigrationTool.db.getR9Lib(r8thing));
if ((r9thing = MigrationTool.db.getR9Macro(r8thing)) != null) {
if (wholeline.contains(r8thing)) {
wholeline = wholeline.replaceAll(r8thing, r9thing);
filemacro.add(new r8tor9(r8thing, r9thing));
// Converting macro
it = mi.hashnonlocalmacro.iterator();
while (it.hasNext()) { //macros are all assumed MdePkg currently
r8thing = it.next();
//mi.hashrequiredr9libs.add(MigrationTool.db.getR9Lib(r8thing));
if ((r9thing = MigrationTool.db.getR9Macro(r8thing)) != null) {
if (wholeline.contains(r8thing)) {
wholeline = wholeline.replaceAll(r8thing, r9thing);
filemacro.add(new r8tor9(r8thing, r9thing));
}
}
}
}
// Converting guid
replaceGuid(wholeline, mi.guid, "guid", fileguid);
replaceGuid(wholeline, mi.ppi, "ppi", fileppi);
replaceGuid(wholeline, mi.protocol, "protocol", fileprotocol);
// Converting guid
replaceGuid(wholeline, mi.guid, "guid", fileguid);
replaceGuid(wholeline, mi.ppi, "ppi", fileppi);
replaceGuid(wholeline, mi.protocol, "protocol", fileprotocol);
// Converting Pei
// First , find all (**PeiServices)-> or (*PeiServices). with arg "PeiServices" , change name and add #%
Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*)(\\s*\\(\\s*PeiServices\\s*,\\s*)", Pattern.MULTILINE);
if (mi.moduletype.contains("PEIM")) {
Matcher mtrpei = ptnpei.matcher(wholeline);
while (mtrpei.find()) { // ! add a library here !
wholeline = mtrpei.replaceAll("PeiServices$1#%$2");
mi.hashrequiredr9libs.add("PeiServicesLib");
// Converting Pei
// First , find all (**PeiServices)-> or (*PeiServices). with arg "PeiServices" , change name and add #%
Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*)(\\s*\\(\\s*PeiServices\\s*,\\s*)", Pattern.MULTILINE);
if (mi.moduletype.contains("PEIM")) {
Matcher mtrpei = ptnpei.matcher(wholeline);
while (mtrpei.find()) { // ! add a library here !
wholeline = mtrpei.replaceAll("PeiServices$1#%$2");
mi.hashrequiredr9libs.add("PeiServicesLib");
}
mtrpei.reset();
if (wholeline.contains("PeiServicesCopyMem")) {
wholeline = wholeline.replaceAll("PeiServicesCopyMem#%", "CopyMem");
mi.hashrequiredr9libs.add("BaseMemoryLib");
}
if (wholeline.contains("PeiServicesSetMem")) {
wholeline = wholeline.replaceAll("PeiServicesSetMem#%", "SetMem");
mi.hashrequiredr9libs.add("BaseMemoryLib");
}
// Second , find all #% to drop the arg "PeiServices"
Pattern ptnpeiarg = Pattern.compile("#%+(\\s*\\(+\\s*)PeiServices\\s*,\\s*", Pattern.MULTILINE);
Matcher mtrpeiarg = ptnpeiarg.matcher(wholeline);
while (mtrpeiarg.find()) {
wholeline = mtrpeiarg.replaceAll("$1");
}
}
mtrpei.reset();
if (wholeline.contains("PeiServicesCopyMem")) {
wholeline = wholeline.replaceAll("PeiServicesCopyMem#%", "CopyMem");
mi.hashrequiredr9libs.add("BaseMemoryLib");
Matcher mtrmac;
mtrmac = Pattern.compile("EFI_IDIV_ROUND\\((.*), (.*)\\)").matcher(wholeline);
if (mtrmac.find()) {
wholeline = mtrmac.replaceAll("\\($1 \\/ $2 \\+ \\(\\(\\(2 \\* \\($1 \\% $2\\)\\) \\< $2\\) \\? 0 \\: 1\\)\\)");
}
if (wholeline.contains("PeiServicesSetMem")) {
wholeline = wholeline.replaceAll("PeiServicesSetMem#%", "SetMem");
mi.hashrequiredr9libs.add("BaseMemoryLib");
mtrmac = Pattern.compile("EFI_MIN\\((.*), (.*)\\)").matcher(wholeline);
if (mtrmac.find()) {
wholeline = mtrmac.replaceAll("\\(\\($1 \\< $2\\) \\? $1 \\: $2\\)");
}
mtrmac = Pattern.compile("EFI_MAX\\((.*), (.*)\\)").matcher(wholeline);
if (mtrmac.find()) {
wholeline = mtrmac.replaceAll("\\(\\($1 \\> $2\\) \\? $1 \\: $2\\)");
}
mtrmac = Pattern.compile("EFI_UINTN_ALIGNED\\((.*)\\)").matcher(wholeline);
if (mtrmac.find()) {
wholeline = mtrmac.replaceAll("\\(\\(\\(UINTN\\) $1\\) \\& \\(sizeof \\(UINTN\\) \\- 1\\)\\)");
}
if (wholeline.contains("EFI_UINTN_ALIGN_MASK")) {
wholeline = wholeline.replaceAll("EFI_UINTN_ALIGN_MASK", "(sizeof (UINTN) - 1)");
}
// Second , find all #% to drop the arg "PeiServices"
Pattern ptnpeiarg = Pattern.compile("#%+(\\s*\\(+\\s*)PeiServices\\s*,\\s*", Pattern.MULTILINE);
Matcher mtrpeiarg = ptnpeiarg.matcher(wholeline);
while (mtrpeiarg.find()) {
wholeline = mtrpeiarg.replaceAll("$1");
show(filefunc, "function");
show(filemacro, "macro");
show(fileguid, "guid");
show(fileppi, "ppi");
show(fileprotocol, "protocol");
if (!filer8only.isEmpty()) {
MigrationTool.ui.println("Converting r8only : " + filer8only);
}
filefunc.clear();
filemacro.clear();
fileguid.clear();
fileppi.clear();
fileprotocol.clear();
filer8only.clear();
return wholeline;
}
Matcher mtrmac;
mtrmac = Pattern.compile("EFI_IDIV_ROUND\\((.*), (.*)\\)").matcher(wholeline);
if (mtrmac.find()) {
wholeline = mtrmac.replaceAll("\\($1 \\/ $2 \\+ \\(\\(\\(2 \\* \\($1 \\% $2\\)\\) \\< $2\\) \\? 0 \\: 1\\)\\)");
}
mtrmac = Pattern.compile("EFI_MIN\\((.*), (.*)\\)").matcher(wholeline);
if (mtrmac.find()) {
wholeline = mtrmac.replaceAll("\\(\\($1 \\< $2\\) \\? $1 \\: $2\\)");
}
mtrmac = Pattern.compile("EFI_MAX\\((.*), (.*)\\)").matcher(wholeline);
if (mtrmac.find()) {
wholeline = mtrmac.replaceAll("\\(\\($1 \\> $2\\) \\? $1 \\: $2\\)");
}
mtrmac = Pattern.compile("EFI_UINTN_ALIGNED\\((.*)\\)").matcher(wholeline);
if (mtrmac.find()) {
wholeline = mtrmac.replaceAll("\\(\\(\\(UINTN\\) $1\\) \\& \\(sizeof \\(UINTN\\) \\- 1\\)\\)");
}
if (wholeline.contains("EFI_UINTN_ALIGN_MASK")) {
wholeline = wholeline.replaceAll("EFI_UINTN_ALIGN_MASK", "(sizeof (UINTN) - 1)");
public boolean recognize(String filename) {
return filename.contains(".c") || filename.contains(".C");
}
show(filefunc, "function");
show(filemacro, "macro");
show(fileguid, "guid");
show(fileppi, "ppi");
show(fileprotocol, "protocol");
if (!filer8only.isEmpty()) {
MigrationTool.ui.println("Converting r8only : " + filer8only);
public String namechange(String oldname) {
if (oldname.contains(".C")) {
return oldname.replaceFirst(".C", ".c");
} else {
return oldname;
}
}
filefunc.clear();
filemacro.clear();
fileguid.clear();
fileppi.clear();
fileprotocol.clear();
filer8only.clear();
return wholeline;
}
//---------------------------------------inner classes---------------------------------------//
private static final String addincludefile(String wholeline, String hfile) {
return wholeline.replaceFirst("(\\*/\\s)", "$1\n#include " + hfile + "\n");
}
private final String convertdxs(String wholeline) {
if (mi.getModuleType().equals("PEIM")) {
return addincludefile(wholeline, "\\<PeimDepex.h\\>");
} else {
return addincludefile(wholeline, "\\<DxeDepex.h\\>");
}
}
private static final void show(Set<r8tor9> hash, String sh) {
Iterator<r8tor9> it = hash.iterator();
r8tor9 temp;
@ -267,31 +309,17 @@ public final class SourceFileReplacer implements Common.ForDoAll {
//-----------------------------------ForDoAll-----------------------------------//
public void run(String filepath) throws Exception {
String outname = null;
String inname = filepath.replace(mi.modulepath + File.separator, "");
String tempinpath = mi.modulepath + File.separator + "temp" + File.separator;
String tempoutpath = MigrationTool.ModuleInfoMap.get(mi) + File.separator + "Migration_" + mi.modulename + File.separator;
if (inname.contains(".c") || inname.contains(".C")) {
if (inname.contains(".C")) {
outname = inname.replaceFirst(".C", ".c");
} else {
outname = inname;
Iterator<Common.Laplace> itLaplace = Laplaces.iterator();
while (itLaplace.hasNext()) {
Common.Laplace lap = itLaplace.next();
if (lap.recognize(inname)) {
MigrationTool.ui.println("\nHandling file: " + inname);
lap.transform(tempinpath + inname, tempoutpath + lap.namechange(inname));
}
MigrationTool.ui.println("\nModifying file: " + inname);
Common.string2file(sourcefilereplace(Common.file2string(tempinpath + inname)), tempoutpath + outname);
} else if (inname.contains(".h") || inname.contains(".H") || inname.contains(".uni")) {
if (inname.contains(".H")) {
outname = inname.replaceFirst(".H", ".h");
} else {
outname = inname;
}
MigrationTool.ui.println("\nCopying file: " + inname);
Common.string2file(Common.file2string(tempinpath + inname), tempoutpath + outname);
} else if (inname.contains(".dxs")) {
outname = inname;
MigrationTool.ui.println("\nModifying file: " + inname);
Common.string2file(convertdxs(Common.file2string(tempinpath + inname)), tempoutpath + outname);
}
}
@ -305,11 +333,17 @@ public final class SourceFileReplacer implements Common.ForDoAll {
}
private final void start() throws Exception {
Laplaces.add(new DxsLaplace());
Laplaces.add(new CLaplace());
Laplaces.add(new IdleLaplace());
Common.toDoAll(mi.localmodulesources, this);
if (!mi.hashr8only.isEmpty()) {
addr8only();
}
Laplaces.clear();
}
public static final void fireAt(ModuleInfo moduleinfo) throws Exception {