Path: blob/master/debugtools/DDR_Autoblob/src/com/ibm/j9ddr/autoblob/StripGarbage.java
6007 views
/*******************************************************************************1* Copyright (c) 2010, 2019 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* 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-exception20*******************************************************************************/21package com.ibm.j9ddr.autoblob;2223import java.io.File;24import java.io.FileReader;25import java.io.FileWriter;26import java.io.Reader;27import java.io.Writer;28import java.util.ArrayList;29import java.util.regex.Matcher;30import java.util.regex.Pattern;3132/**33* Removes non-ANSI garbage from pre-processed C/C++34*35* Takes foo.i as an input, and produces foo.ic as an output.36* @author andhall37*38*/39public class StripGarbage40{41/**42* @param args43*/44public static void main(String[] args) throws Exception45{46for (String filename : args) {47System.out.println("Cleaning " + filename);48File input = new File(filename);49File output = new File(filename + "c");5051//Slurp the entire file into a buffer52StringBuilder builder = new StringBuilder();53Reader reader = new FileReader(input);54char[] buffer = new char[4096];5556try {57int read;5859do {60read = reader.read(buffer);6162if (read != -1) {63builder.append(buffer,0,read);64}65} while (read != -1);6667} finally {68reader.close();69}7071String cleaned = stripGarbage(builder.toString());7273Writer writer = new FileWriter(output);74try {75writer.write(cleaned);76writer.close();77} finally {78writer.close();79}80}81}8283private final static Pattern DECLSPEC = Pattern.compile("_+declspec\\(.*?\\)", Pattern.DOTALL + Pattern.MULTILINE);84private final static Pattern ASM = Pattern.compile("_+asm\\s+\\{.*?\\}", Pattern.DOTALL + Pattern.MULTILINE);85private final static Pattern ASM2 = Pattern.compile("_*asm\\s*(?:volatile)?\\(.*?\\);", Pattern.DOTALL + Pattern.MULTILINE);86private final static Pattern ATTRIBUTE = Pattern.compile("__attribute__\\s*[(]{2}.*?[)]{2}", Pattern.DOTALL + Pattern.MULTILINE);87private final static Pattern TRAILING_BACKSLASH = Pattern.compile("\\\\+\\s*$", Pattern.MULTILINE);88private final static Pattern PRAGMA = Pattern.compile("^#pragma .*$", Pattern.MULTILINE);89private final static String C_MULTILINE_DELIM = "\\";90private final static char C_MULTILINE_DELIM_CHAR = '\\';9192private static String stripGarbage(String contents)93{94String working = contents;9596working = removeMultiLineGarbage(working, PRAGMA);9798//Windows99working = working.replaceAll("_+cdecl", "");100working = working.replaceAll("_+stdcall", "");101working = DECLSPEC.matcher(working).replaceAll("");102working = ASM.matcher(working).replaceAll("");103working = TRAILING_BACKSLASH.matcher(working).replaceAll("");104working = working.replaceAll("__forceinline","");105working = working.replaceAll("\u000c", "");106working = working.replaceAll("WINAPI", "");107108//gcc109working = working.replaceAll("__restrict","");110working = working.replaceAll("__const","");111working = working.replaceAll("__attribute__\\s*[(]{2}.*[)]{2}", "");112working = working.replaceAll("__asm__[^;]+", "");113working = ATTRIBUTE.matcher(working).replaceAll("");114working = working.replaceAll("(<?=\\s)_+inline","");115working = working.replaceAll("__extension__","");116working = ASM2.matcher(working).replaceAll("");117118return working;119}120121/**122* 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.123* @param data string to scan matches for124* @param pattern regex describing the start of the match125* @param blockID string describing how a multi-line block is identified e.g. { or \126* @param terminator string describing the termination of a the multi-line match (note this is currently not a reg-ex)127* @return the altered data or if no matches were found the unchanged data128*/129private static String removeMultiLineGarbage(String data, Pattern pattern) {130ArrayList<Region> regions = new ArrayList<Region>();131Matcher matcher = pattern.matcher(data);132while(matcher.find()) {133String declaration = matcher.group();134if(declaration.endsWith(C_MULTILINE_DELIM)) {135//multi-line declaration, locate first line that does not end with a \136int pos = data.indexOf('\n', matcher.start());137int offset = 1;138if(pos != -1) {139if('\r' == data.charAt(pos - 1)) {140//adjust for running on windows141offset++;142}143}144while((pos != -1) && (C_MULTILINE_DELIM_CHAR == data.charAt(pos - offset))) {145pos = data.indexOf('\n', ++pos);146}147if(-1 == pos) {148String msg = String.format("Warning : unmatched end of multi-line declaration for %s starting at position %d", pattern.toString(), matcher.start());149System.err.println(msg);150} else {151regions.add(new Region(matcher.start(), ++pos));152}153} else {154//single line declaration155regions.add(new Region(matcher.start(), matcher.end()));156}157}158159if(regions.size() != 0) {160//now remove all the specified regions from the string161StringBuilder temp = new StringBuilder();162int index = 0;163for(Region region : regions) {164temp.append(data.substring(index, region.getStart()));165index = region.getEnd();166}167temp.append(data.substring(index)); //copy remaining text168return temp.toString();169} else {170//return data unchanged171return data;172}173}174175private static class Region {176private final int start;177private final int end;178179public Region(int start, int end) {180this.start = start;181this.end = end;182}183184public int getStart() {185return start;186}187188public int getEnd() {189return end;190}191192193}194195}196197198