<commit_before>/* Soot - a J*va Optimization Framework
 * Copyright (C) 2003 <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/*
 * Modified by the Sable Research Group and others 1997-2003.  
 * See the 'credits' file distributed with Soot for the complete list of
 * contributors.  (Soot is distributed at http://www.sable.mcgill.ca/soot)
 */


package soot.util;

import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import soot.Body;
import soot.G;
import soot.Printer;
import soot.Scene;
import soot.Singletons;
import soot.SootClass;
import soot.SootMethod;
import soot.options.Options;
import soot.toolkits.graph.ExceptionalGraph;
import soot.toolkits.graph.DirectedGraph;
import soot.util.cfgcmd.CFGToDotGraph;
import soot.util.dot.DotGraph;


/**
 * The <tt>PhaseDumper</tt> is a debugging aid.  It maintains two
 * lists of phases to be debugged.  If a phase is on the
 * <code>bodyDumpingPhases</code> list, then the intermediate
 * representation of the bodies being manipulated by the phase is
 * dumped before and after the phase is applied.  If a phase is on the
 * <code>cfgDumpingPhases</code> list, then whenever a CFG is
 * constructed during the phase, a dot file is dumped representing the
 * CFG constructed.
 */

public class PhaseDumper {
    // As a minor optimization, we leave these lists null in the
    // case were no phases at all are to be dumped, which is the 
    // most likely case.
    private List bodyDumpingPhases = null;
    private List cfgDumpingPhases = null;

    private class PhaseStack extends ArrayList {
	// We eschew java.util.Stack to avoid synchronization overhead.

	private final static int initialCapacity = 4;
	final static String EMPTY_STACK_PHASE_NAME = "NOPHASE";

	PhaseStack() {
	    super(initialCapacity);
	}

	boolean empty() {
	    return (this.size() == 0);
	}

	String currentPhase() {
	    if (this.size() <= 0) {
		return EMPTY_STACK_PHASE_NAME;
	    } else {
		return (String) this.get(this.size() - 1);
	    }
	}

	String pop() {
	    return (String) this.remove(this.size() - 1);
	}

	String push(String phaseName) {
	    this.add(phaseName);
	    return phaseName;
	}
    }
    private final PhaseStack phaseStack = new PhaseStack(); 
    final static String allWildcard = "ALL";


    public PhaseDumper(Singletons.Global g) {
	if (! Options.v().dump_body().isEmpty()) {
	    bodyDumpingPhases = Options.v().dump_body();
	}
	if (! Options.v().dump_cfg().isEmpty()) {
	    cfgDumpingPhases = Options.v().dump_cfg();
	}
    }


    /**
     * Returns the single instance of <code>PhaseDumper</code>.
     *
     * @return Soot's <code>PhaseDumper</code>.
     */
    public static PhaseDumper v() { 
	return G.v().soot_util_PhaseDumper(); 
    }


    private boolean isBodyDumpingPhase(String phaseName) {
	return ((bodyDumpingPhases != null)
		&& (bodyDumpingPhases.contains(phaseName) || 
		    bodyDumpingPhases.contains(allWildcard)));
    }


    private boolean isCFGDumpingPhase(String phaseName) {
	if (cfgDumpingPhases == null) {
	    return false;
	}
	if (cfgDumpingPhases.contains(allWildcard)) {
	  return true;
	} else {
	  while (true) { // loop exited by "return" or "break".
	    if (cfgDumpingPhases.contains(phaseName)) {
	      return true;
	    }
	    // Go on to check if phaseName is a subphase of a
	    // phase in cfgDumpingPhases.
	    int lastDot = phaseName.lastIndexOf('.');
	    if (lastDot < 0) {
	      break;
	    } else {
	      phaseName = phaseName.substring(0, lastDot);
	    }
	  }
	  return false;
	}
    }


    private static java.io.File makeDirectoryIfMissing(Body b) 
	throws java.io.IOException {
	StringBuffer buf = 
	    new StringBuffer(soot.SourceLocator.v().getOutputDir());
	buf.append(File.separatorChar);
	String className = b.getMethod().getDeclaringClass().getName();
	buf.append(className);
	buf.append(File.separatorChar);
	buf.append(b.getMethod().getSubSignature());
	java.io.File dir = new java.io.File(buf.toString());
	if (dir.exists()) {
	    if (! dir.isDirectory()) {<commit_msg>Changed PhaseDumper to not try to create invalid folder names (<, >)<commit_after>/* Soot - a J*va Optimization Framework
 * Copyright (C) 2003 <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/*
 * Modified by the Sable Research Group and others 1997-2003.  
 * See the 'credits' file distributed with Soot for the complete list of
 * contributors.  (Soot is distributed at http://www.sable.mcgill.ca/soot)
 */


package soot.util;

import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import soot.Body;
import soot.G;
import soot.Printer;
import soot.Scene;
import soot.Singletons;
import soot.SootClass;
import soot.SootMethod;
import soot.options.Options;
import soot.toolkits.graph.ExceptionalGraph;
import soot.toolkits.graph.DirectedGraph;
import soot.util.cfgcmd.CFGToDotGraph;
import soot.util.dot.DotGraph;


/**
 * The <tt>PhaseDumper</tt> is a debugging aid.  It maintains two
 * lists of phases to be debugged.  If a phase is on the
 * <code>bodyDumpingPhases</code> list, then the intermediate
 * representation of the bodies being manipulated by the phase is
 * dumped before and after the phase is applied.  If a phase is on the
 * <code>cfgDumpingPhases</code> list, then whenever a CFG is
 * constructed during the phase, a dot file is dumped representing the
 * CFG constructed.
 */

public class PhaseDumper {
    // As a minor optimization, we leave these lists null in the
    // case were no phases at all are to be dumped, which is the 
    // most likely case.
    private List bodyDumpingPhases = null;
    private List cfgDumpingPhases = null;

    private class PhaseStack extends ArrayList {
	// We eschew java.util.Stack to avoid synchronization overhead.

	private final static int initialCapacity = 4;
	final static String EMPTY_STACK_PHASE_NAME = "NOPHASE";

	PhaseStack() {
	    super(initialCapacity);
	}

	boolean empty() {
	    return (this.size() == 0);
	}

	String currentPhase() {
	    if (this.size() <= 0) {
		return EMPTY_STACK_PHASE_NAME;
	    } else {
		return (String) this.get(this.size() - 1);
	    }
	}

	String pop() {
	    return (String) this.remove(this.size() - 1);
	}

	String push(String phaseName) {
	    this.add(phaseName);
	    return phaseName;
	}
    }
    private final PhaseStack phaseStack = new PhaseStack(); 
    final static String allWildcard = "ALL";


    public PhaseDumper(Singletons.Global g) {
	if (! Options.v().dump_body().isEmpty()) {
	    bodyDumpingPhases = Options.v().dump_body();
	}
	if (! Options.v().dump_cfg().isEmpty()) {
	    cfgDumpingPhases = Options.v().dump_cfg();
	}
    }


    /**
     * Returns the single instance of <code>PhaseDumper</code>.
     *
     * @return Soot's <code>PhaseDumper</code>.
     */
    public static PhaseDumper v() { 
	return G.v().soot_util_PhaseDumper(); 
    }


    private boolean isBodyDumpingPhase(String phaseName) {
	return ((bodyDumpingPhases != null)
		&& (bodyDumpingPhases.contains(phaseName) || 
		    bodyDumpingPhases.contains(allWildcard)));
    }


    private boolean isCFGDumpingPhase(String phaseName) {
	if (cfgDumpingPhases == null) {
	    return false;
	}
	if (cfgDumpingPhases.contains(allWildcard)) {
	  return true;
	} else {
	  while (true) { // loop exited by "return" or "break".
	    if (cfgDumpingPhases.contains(phaseName)) {
	      return true;
	    }
	    // Go on to check if phaseName is a subphase of a
	    // phase in cfgDumpingPhases.
	    int lastDot = phaseName.lastIndexOf('.');
	    if (lastDot < 0) {
	      break;
	    } else {
	      phaseName = phaseName.substring(0, lastDot);
	    }
	  }
	  return false;
	}
    }


    private static java.io.File makeDirectoryIfMissing(Body b) 
	throws java.io.IOException {
	StringBuffer buf = 
	    new StringBuffer(soot.SourceLocator.v().getOutputDir());
	buf.append(File.separatorChar);
	String className = b.getMethod().getDeclaringClass().getName();
	buf.append(className);
	buf.append(File.separatorChar);
	buf.append(b.getMethod().getSubSignature().replace('<', '[').replace('>', ']'));
	java.io.File dir = new java.io.File(buf.toString());
	if (dir.exists()) {
	    if (! dir.isDirectory()) {
{'fraction_non_alphanumeric': 0.10808299783214617, 'fraction_numerical': 0.0072261794157117785, 'mean_word_length': 3.4399633363886344, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1185577', 'n_tokens_mistral': 3171, 'n_tokens_neox': 2907, 'n_words': 963}