]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/MigrationTools/org/tianocore/migration/Critic.java
multiline support for critic
[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 class Critic implements Common.ForDoAll {
19 private static Pattern ptnheadcomment = Pattern.compile("^\\/\\*\\+\\+(.*?)\\-\\-\\*\\/",Pattern.DOTALL);
20 private static Pattern ptnfunccomment = Pattern.compile("([\\w\\d]*\\s*[_\\w][_\\w\\d]*\\s*\\([^\\)\\(]*\\)\\s*)\\/\\*\\+\\+(.*?)\\-\\-\\*\\/(\\s*.*?)([\\{;])",Pattern.DOTALL);
21 //private static Pattern ptncommentstructure = Pattern.compile("\\/\\*\\+\\+\\s*Routine Description:\\s*(.*?)\\s*Arguments:\\s*(.*?)\\s*Returns:\\s*(.*?)\\s*\\-\\-\\*\\/",Pattern.DOTALL);
22 private static Pattern ptninfequation = Pattern.compile("([^\\s]*)\\s*-\\s*(.*)\\s*");
23 private static Matcher mtrinfequation;
24
25 public void toDo(String filepath) throws Exception {
26 if (filepath.contains(".c") || filepath.contains(".h")) {
27 BufferedReader rd = null;
28 String line = null;
29 StringBuffer templine = new StringBuffer();
30 boolean incomment = false;
31 boolean description = false;
32 boolean arguments = false;
33 boolean returns = false;
34 boolean inequation = false;
35
36 System.out.println("Criticing " + filepath);
37 String wholeline = Common.file2string(filepath);
38
39 wholeline = Common.replaceAll(wholeline, ptnheadcomment, "/** @file$1**/");
40 wholeline = Common.replaceAll(wholeline, ptnfunccomment, "/**$2**/$3$1$4");
41 //wholeline = Common.replaceAll(wholeline, ptncommentstructure, "/**\n#%\n$1\n%#\n#%%\n$2\n%%#\n#%%%\n$3\n%%%#\n**/");
42
43 rd = new BufferedReader(new StringReader(wholeline));
44 while ((line = rd.readLine()) != null) {
45 if (line.matches("\\/\\*\\*")) {
46 incomment = true;
47 templine.append(line + "\n");
48 } else if (line.matches("\\*\\*\\/")) {
49 incomment = false;
50 templine.append(line + "\n");
51 } else if (incomment && line.contains("Routine Description:")) {
52 description = true;
53 arguments = false;
54 returns = false;
55 } else if (incomment && line.contains("Arguments:")) {
56 description = false;
57 arguments = true;
58 returns = false;
59 } else if (incomment && line.contains("Returns:")) {
60 description = false;
61 arguments = false;
62 returns = true;
63 } else if (incomment && description) {
64 templine.append(line + "\n");
65 } else if (incomment && arguments) {
66 mtrinfequation = ptninfequation.matcher(line);
67 if (mtrinfequation.find()) {
68 inequation = true;
69 templine.append(" @param " + mtrinfequation.group(1) + " " + mtrinfequation.group(2) + "\n");
70 } else if (inequation && line.trim().length() == 0) {
71 inequation = false;
72 templine.append(line + "\n");
73 } else if (inequation && line.trim().length() != 0) {
74 templine.append("#%#%" + line + "\n");
75 } else {
76 templine.append(line + "\n");
77 }
78 } else if (incomment && returns) {
79 mtrinfequation = ptninfequation.matcher(line);
80 if (mtrinfequation.find()) {
81 inequation = true;
82 templine.append(" @retval " + mtrinfequation.group(1) + " " + mtrinfequation.group(2) + "\n");
83 } else if (inequation && line.trim().length() == 0) {
84 inequation = false;
85 templine.append(line + "\n");
86 } else if (inequation && line.trim().length() != 0) {
87 templine.append("#%#%" + line + "\n");
88 } else {
89 templine.append(line + "\n");
90 }
91 } else {
92 templine.append(line + "\n");
93 }
94 }
95 wholeline = templine.toString();
96 wholeline = wholeline.replaceAll("\n#%#%\\s*", " ");
97
98 /* -----slow edition of replacefirst with stringbuffer-----
99 line.append(wholeline);
100 mtrfunccomment = ptnfunccomment.matcher(line);
101 while (mtrfunccomment.find()) {
102 line.replace(0, line.length()-1, mtrfunccomment.replaceFirst("$2$4$3$1$5"));
103 }
104 */
105 /* -----slow edition of replacefirst with string-----
106 while ((mtrfunccomment = ptnfunccomment.matcher(wholeline)).find()) {
107 //funccomment = mtrfunccomment.group(2);
108 //mtrcommentstructure = ptncommentstructure.matcher(funccomment);
109 wholeline = mtrfunccomment.replaceFirst("$2$4$3$1$5");
110 }
111 */
112 /*
113 // edit func comment
114 mtrtempcomment = ptntempcomment.matcher(wholeline);
115 while (mtrtempcomment.find()) {
116 System.out.println("-----------------------------");
117 System.out.println(mtrtempcomment.group());
118 System.out.println("-----------------------------");
119 }
120 */
121 Common.string2file(wholeline, filepath);
122 }
123 }
124
125 public static void fireAt(String path) throws Exception {
126 Critic critic = new Critic();
127 Common.toDoAll(Common.dirCopy_(path), critic);
128 System.out.println("Critic Done");
129 }
130 }
131 //analyze func comment
132 /*if (mtrcommentstructure.find()) {
133 newcomment.append("/*++\n\n" + mtrcommentstructure.group(1) + "\n\n");
134
135 //System.out.println("-------1-------");
136 //System.out.println(mtrcommentstructure.group(1));
137
138 // arg
139 //System.out.println("-------2-------");
140 //System.out.println(mtrcommentstructure.group(2));
141 mtrinfequation = ptninfequation.matcher(mtrcommentstructure.group(2));
142 while (mtrinfequation.find()) {
143 newcomment.append("@param " + mtrinfequation.group(1) + " " + mtrinfequation.group(2) + "\n");
144 //System.out.println("@param " + mtrinfequation.group(1) + " " + mtrinfequation.group(2));
145 }
146 newcomment.append("\n");
147 // return
148 //System.out.println("-------3-------");
149 //System.out.println(mtrcommentstructure.group(3));
150 mtrinfequation = ptninfequation.matcher(mtrcommentstructure.group(3));
151 while (mtrinfequation.find()) {
152 newcomment.append("@retval " + mtrinfequation.group(1) + " " + mtrinfequation.group(2) + "\n");
153 //System.out.println("@retval " + mtrinfequation.group(1) + " " + mtrinfequation.group(2));
154 }
155 System.out.println(newcomment);
156 } else {
157 System.out.println("Error: Comment Style Incorrect");
158 }*/