Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/debugtools/DDR_Autoblob/src/com/ibm/j9ddr/autoblob/StripGarbage.java
6007 views
1
/*******************************************************************************
2
* Copyright (c) 2010, 2019 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
21
*******************************************************************************/
22
package com.ibm.j9ddr.autoblob;
23
24
import java.io.File;
25
import java.io.FileReader;
26
import java.io.FileWriter;
27
import java.io.Reader;
28
import java.io.Writer;
29
import java.util.ArrayList;
30
import java.util.regex.Matcher;
31
import java.util.regex.Pattern;
32
33
/**
34
* Removes non-ANSI garbage from pre-processed C/C++
35
*
36
* Takes foo.i as an input, and produces foo.ic as an output.
37
* @author andhall
38
*
39
*/
40
public class StripGarbage
41
{
42
/**
43
* @param args
44
*/
45
public static void main(String[] args) throws Exception
46
{
47
for (String filename : args) {
48
System.out.println("Cleaning " + filename);
49
File input = new File(filename);
50
File output = new File(filename + "c");
51
52
//Slurp the entire file into a buffer
53
StringBuilder builder = new StringBuilder();
54
Reader reader = new FileReader(input);
55
char[] buffer = new char[4096];
56
57
try {
58
int read;
59
60
do {
61
read = reader.read(buffer);
62
63
if (read != -1) {
64
builder.append(buffer,0,read);
65
}
66
} while (read != -1);
67
68
} finally {
69
reader.close();
70
}
71
72
String cleaned = stripGarbage(builder.toString());
73
74
Writer writer = new FileWriter(output);
75
try {
76
writer.write(cleaned);
77
writer.close();
78
} finally {
79
writer.close();
80
}
81
}
82
}
83
84
private final static Pattern DECLSPEC = Pattern.compile("_+declspec\\(.*?\\)", Pattern.DOTALL + Pattern.MULTILINE);
85
private final static Pattern ASM = Pattern.compile("_+asm\\s+\\{.*?\\}", Pattern.DOTALL + Pattern.MULTILINE);
86
private final static Pattern ASM2 = Pattern.compile("_*asm\\s*(?:volatile)?\\(.*?\\);", Pattern.DOTALL + Pattern.MULTILINE);
87
private final static Pattern ATTRIBUTE = Pattern.compile("__attribute__\\s*[(]{2}.*?[)]{2}", Pattern.DOTALL + Pattern.MULTILINE);
88
private final static Pattern TRAILING_BACKSLASH = Pattern.compile("\\\\+\\s*$", Pattern.MULTILINE);
89
private final static Pattern PRAGMA = Pattern.compile("^#pragma .*$", Pattern.MULTILINE);
90
private final static String C_MULTILINE_DELIM = "\\";
91
private final static char C_MULTILINE_DELIM_CHAR = '\\';
92
93
private static String stripGarbage(String contents)
94
{
95
String working = contents;
96
97
working = removeMultiLineGarbage(working, PRAGMA);
98
99
//Windows
100
working = working.replaceAll("_+cdecl", "");
101
working = working.replaceAll("_+stdcall", "");
102
working = DECLSPEC.matcher(working).replaceAll("");
103
working = ASM.matcher(working).replaceAll("");
104
working = TRAILING_BACKSLASH.matcher(working).replaceAll("");
105
working = working.replaceAll("__forceinline","");
106
working = working.replaceAll("\u000c", "");
107
working = working.replaceAll("WINAPI", "");
108
109
//gcc
110
working = working.replaceAll("__restrict","");
111
working = working.replaceAll("__const","");
112
working = working.replaceAll("__attribute__\\s*[(]{2}.*[)]{2}", "");
113
working = working.replaceAll("__asm__[^;]+", "");
114
working = ATTRIBUTE.matcher(working).replaceAll("");
115
working = working.replaceAll("(<?=\\s)_+inline","");
116
working = working.replaceAll("__extension__","");
117
working = ASM2.matcher(working).replaceAll("");
118
119
return working;
120
}
121
122
/**
123
* Removes multi-line or single sections from the supplied data. It assumes that the data is continued over multiple lines by use of the \ character.
124
* @param data string to scan matches for
125
* @param pattern regex describing the start of the match
126
* @param blockID string describing how a multi-line block is identified e.g. { or \
127
* @param terminator string describing the termination of a the multi-line match (note this is currently not a reg-ex)
128
* @return the altered data or if no matches were found the unchanged data
129
*/
130
private static String removeMultiLineGarbage(String data, Pattern pattern) {
131
ArrayList<Region> regions = new ArrayList<Region>();
132
Matcher matcher = pattern.matcher(data);
133
while(matcher.find()) {
134
String declaration = matcher.group();
135
if(declaration.endsWith(C_MULTILINE_DELIM)) {
136
//multi-line declaration, locate first line that does not end with a \
137
int pos = data.indexOf('\n', matcher.start());
138
int offset = 1;
139
if(pos != -1) {
140
if('\r' == data.charAt(pos - 1)) {
141
//adjust for running on windows
142
offset++;
143
}
144
}
145
while((pos != -1) && (C_MULTILINE_DELIM_CHAR == data.charAt(pos - offset))) {
146
pos = data.indexOf('\n', ++pos);
147
}
148
if(-1 == pos) {
149
String msg = String.format("Warning : unmatched end of multi-line declaration for %s starting at position %d", pattern.toString(), matcher.start());
150
System.err.println(msg);
151
} else {
152
regions.add(new Region(matcher.start(), ++pos));
153
}
154
} else {
155
//single line declaration
156
regions.add(new Region(matcher.start(), matcher.end()));
157
}
158
}
159
160
if(regions.size() != 0) {
161
//now remove all the specified regions from the string
162
StringBuilder temp = new StringBuilder();
163
int index = 0;
164
for(Region region : regions) {
165
temp.append(data.substring(index, region.getStart()));
166
index = region.getEnd();
167
}
168
temp.append(data.substring(index)); //copy remaining text
169
return temp.toString();
170
} else {
171
//return data unchanged
172
return data;
173
}
174
}
175
176
private static class Region {
177
private final int start;
178
private final int end;
179
180
public Region(int start, int end) {
181
this.start = start;
182
this.end = end;
183
}
184
185
public int getStart() {
186
return start;
187
}
188
189
public int getEnd() {
190
return end;
191
}
192
193
194
}
195
196
}
197
198