Enhancing Critic

git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@1296 6f19259b-4bc3-4df7-8a09-765794883524
This commit is contained in:
alfred 2006-08-16 12:09:16 +00:00
parent e537d5c130
commit 8c4eeeb6a5
2 changed files with 109 additions and 6 deletions

View File

@ -1,3 +1,15 @@
/** @file
Copyright (c) 2006, Intel Corporation
All rights reserved. This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
package org.tianocore.migration;
import java.io.*;
@ -5,6 +17,8 @@ import java.util.regex.*;
import java.util.*;
public class Common {
public static Pattern ptnseparate = Pattern.compile("(.*)\\\\([^\\\\]*)");
public static String file2string(String filename) throws Exception {
BufferedReader rd = new BufferedReader(new FileReader(filename));
StringBuffer wholefile = new StringBuffer();
@ -16,11 +30,8 @@ public class Common {
}
public static void ensureDir(String objFileWhole) {
Pattern ptnseparate = Pattern.compile("(.*)\\\\[^\\\\]*");
Matcher mtrseparate;
File tempdir;
mtrseparate = ptnseparate.matcher(objFileWhole);
Matcher mtrseparate = ptnseparate.matcher(objFileWhole);
if (mtrseparate.find()) {
tempdir = new File(mtrseparate.group(1));
if (!tempdir.exists()) tempdir.mkdirs();
@ -51,6 +62,29 @@ public class Common {
return filelist;
}
public static String dirCopy_(String src) throws Exception {
Matcher mtrseparate = Common.ptnseparate.matcher(src);
if (mtrseparate.find()) {
dirCopy(src, mtrseparate.group(1) + File.separator + "_" + mtrseparate.group(2));
}
return mtrseparate.group(1) + File.separator + "_" + mtrseparate.group(2);
}
public static void dirCopy(String src, String des) throws Exception {
String[] list = new File(src).list();
File test;
for (int i = 0 ; i < list.length ; i++) {
test = new File(src + File.separator + list[i]);
if (test.isDirectory()) {
dirCopy(src + File.separator + list[i], des + File.separator + list[i]);
} else {
ensureDir(des + File.separator + list[i]);
string2file(file2string(src + File.separator + list[i]), des + File.separator + list[i]);
}
}
}
public static void toDoAll(String path, ForDoAll fda) throws Exception { // filter of file type can be done in toDo
String[] list = new File(path).list();

View File

@ -1,3 +1,15 @@
/** @file
Copyright (c) 2006, Intel Corporation
All rights reserved. This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
package org.tianocore.migration;
import java.util.regex.*;
@ -14,20 +26,77 @@ public class Critic implements Common.ForDoAll {
private static Pattern ptnheadcomment = Pattern.compile("^\\/\\*\\+\\+(.*?)\\-\\-\\*\\/",Pattern.DOTALL);
private static Matcher mtrheadcomment;
private static Pattern ptnfunccomment = Pattern.compile("([\\w\\d]*\\s*[_\\w][_\\w\\d]*\\s*\\([^\\)\\(]*\\)\\s*)(\\/\\*\\+\\+.*?)(\\-\\-\\*\\/\\s*)(.*?)([\\{;])",Pattern.DOTALL);
private static Matcher mtrfunccomment;
private static Pattern ptncommentstructure = Pattern.compile("Routine Description:\\s*(\\w.*?\\w)\\s*Arguments:(\\s*\\w.*?\\w\\s*)Returns:(\\s*\\w.*?\\w\\s*)&%",Pattern.DOTALL);
private static Matcher mtrcommentstructure;
private static Pattern ptntempcomment = Pattern.compile("\\/\\*\\+\\+(.*?)\\-\\-\\*\\/\\s*[\\w\\d]*\\s*[_\\w][_\\w\\d]*\\s*\\([^\\)\\(]*\\)",Pattern.DOTALL);
private static Matcher mtrtempcomment;
private static Pattern ptninfequation = Pattern.compile("([^\\s]*)\\s*-\\s*(\\w.*\\w)");
private static Matcher mtrinfequation;
public void toDo(String filepath) throws Exception {
String funccomment = null;
if (filepath.contains(".c") || filepath.contains(".h")) {
System.out.println("Criticing " + filepath);
String wholeline = Common.file2string(filepath);
// find head comment
mtrheadcomment = ptnheadcomment.matcher(wholeline);
if (mtrheadcomment.find()) { //as we find only the head comment here, use 'if' not 'while'
wholeline = mtrheadcomment.replaceFirst("/** @file$1**/");
Common.string2file(wholeline, filepath + "_");
}
// find func comment
mtrfunccomment = ptnfunccomment.matcher(wholeline);
while (mtrfunccomment.find()) {
funccomment = mtrfunccomment.group(2) + "&%";
mtrcommentstructure = ptncommentstructure.matcher(funccomment);
wholeline = mtrfunccomment.replaceAll("$2$4$3$1$5");
}
// edit func comment
mtrtempcomment = ptntempcomment.matcher(wholeline);
while (mtrtempcomment.find()) {
System.out.println("-----------------------------");
System.out.println(mtrtempcomment.group());
System.out.println("-----------------------------");
}
Common.string2file(wholeline, filepath);
}
}
public static void fireAt(String path) throws Exception {
Common.toDoAll(path, new Critic());
Critic critic = new Critic();
Common.toDoAll(Common.dirCopy_(path), critic);
System.out.println("Critic Done");
}
}
//analyze func comment
/*if (mtrcommentstructure.find()) {
newcomment.append("/*++\n\n" + mtrcommentstructure.group(1) + "\n\n");
//System.out.println("-------1-------");
//System.out.println(mtrcommentstructure.group(1));
// arg
//System.out.println("-------2-------");
//System.out.println(mtrcommentstructure.group(2));
mtrinfequation = ptninfequation.matcher(mtrcommentstructure.group(2));
while (mtrinfequation.find()) {
newcomment.append("@param " + mtrinfequation.group(1) + " " + mtrinfequation.group(2) + "\n");
//System.out.println("@param " + mtrinfequation.group(1) + " " + mtrinfequation.group(2));
}
newcomment.append("\n");
// return
//System.out.println("-------3-------");
//System.out.println(mtrcommentstructure.group(3));
mtrinfequation = ptninfequation.matcher(mtrcommentstructure.group(3));
while (mtrinfequation.find()) {
newcomment.append("@retval " + mtrinfequation.group(1) + " " + mtrinfequation.group(2) + "\n");
//System.out.println("@retval " + mtrinfequation.group(1) + " " + mtrinfequation.group(2));
}
System.out.println(newcomment);
} else {
System.out.println("Error: Comment Style Incorrect");
}*/