changeset 2692:79590d6b4a7c

IdealGraphPrinterObserver, GraphvizPrinterObserver: replace possibly invalid characters in file names
author Peter Hofer <peter.hofer@jku.at>
date Wed, 18 May 2011 11:50:58 +0200
parents 4149feada801
children c4201554beeb
files graal/GraalCompiler/src/com/sun/c1x/debug/GraphvizPrinterObserver.java graal/GraalCompiler/src/com/sun/c1x/debug/IdealGraphPrinter.java graal/GraalCompiler/src/com/sun/c1x/debug/IdealGraphPrinterObserver.java
diffstat 3 files changed, 34 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/graal/GraalCompiler/src/com/sun/c1x/debug/GraphvizPrinterObserver.java	Wed May 18 11:27:10 2011 +0200
+++ b/graal/GraalCompiler/src/com/sun/c1x/debug/GraphvizPrinterObserver.java	Wed May 18 11:50:58 2011 +0200
@@ -23,6 +23,7 @@
 package com.sun.c1x.debug;
 
 import java.io.*;
+import java.util.regex.*;
 
 import com.oracle.graal.graph.*;
 import com.oracle.graal.graph.vis.*;
@@ -38,6 +39,8 @@
  */
 public class GraphvizPrinterObserver implements CompilationObserver {
 
+    private static final Pattern INVALID_CHAR = Pattern.compile("[^A-Za-z0-9_.-]");
+
     private final boolean pdf;
     private int n;
 
@@ -59,7 +62,9 @@
             String name = event.getMethod().holder().name();
             name = name.substring(1, name.length() - 1).replace('/', '.');
             name = name + "." + event.getMethod().name();
+
             String filename = name + "_" + (n++) + "_" + event.getLabel();
+            filename = INVALID_CHAR.matcher(filename).replaceAll("_");
 
             OutputStream out = null;
             try {
--- a/graal/GraalCompiler/src/com/sun/c1x/debug/IdealGraphPrinter.java	Wed May 18 11:27:10 2011 +0200
+++ b/graal/GraalCompiler/src/com/sun/c1x/debug/IdealGraphPrinter.java	Wed May 18 11:50:58 2011 +0200
@@ -63,20 +63,33 @@
     }
 
     /**
-     * Starts a new graph document containing a single group of graphs with the given name, short name and byte code
-     * index (BCI) as properties.
+     * Starts a new graph document.
      */
-    public void begin(String name, String shortName, int bci) {
-        stream.println("<graphDocument><group>");
+    public void begin() {
+        stream.println("<graphDocument>");
+    }
+
+    /**
+     * Starts a new group of graphs with the given name, short name and method byte code index (BCI) as properties.
+     */
+    public void beginGroup(String name, String shortName, int bci) {
+        stream.println("<group>");
         stream.printf(" <properties><p name='name'>%s</p></properties>%n", escape(name));
         stream.printf(" <method name='%s' shortName='%s' bci='%d'/>%n", escape(name), escape(shortName), bci);
     }
 
     /**
-     * Finishes the graph document.
+     * Ends the current group.
+     */
+    public void endGroup() {
+        stream.println("</group>");
+    }
+
+    /**
+     * Finishes the graph document and flushes the output stream.
      */
     public void end() {
-        stream.println("</group></graphDocument>");
+        stream.println("</graphDocument>");
         stream.flush();
     }
 
--- a/graal/GraalCompiler/src/com/sun/c1x/debug/IdealGraphPrinterObserver.java	Wed May 18 11:27:10 2011 +0200
+++ b/graal/GraalCompiler/src/com/sun/c1x/debug/IdealGraphPrinterObserver.java	Wed May 18 11:50:58 2011 +0200
@@ -23,6 +23,7 @@
 package com.sun.c1x.debug;
 
 import java.io.*;
+import java.util.regex.*;
 
 import com.oracle.graal.graph.*;
 import com.sun.c1x.*;
@@ -32,11 +33,13 @@
 /**
  * Observes compilation events and uses {@link IdealGraphPrinter} to generate a graph representation that can be
  * inspected with the <a href="http://kenai.com/projects/igv">Ideal Graph Visualizer</a>.
- * 
+ *
  * @author Peter Hofer
  */
 public class IdealGraphPrinterObserver implements CompilationObserver {
 
+    private static final Pattern INVALID_CHAR = Pattern.compile("[^A-Za-z0-9_.-]");
+
     private IdealGraphPrinter printer;
     private OutputStream stream;
 
@@ -48,7 +51,10 @@
             String name = event.getMethod().holder().name();
             name = name.substring(1, name.length() - 1).replace('/', '.');
             name = name + "." + event.getMethod().name();
+
             String filename = name + ".igv.xml";
+            filename = INVALID_CHAR.matcher(filename).replaceAll("_");
+
             try {
                 stream = new FileOutputStream(filename);
                 printer = new IdealGraphPrinter(stream);
@@ -57,7 +63,8 @@
                     printer.addOmittedClass(FrameState.class);
                 }
 
-                printer.begin(name, name, -1);
+                printer.begin();
+                printer.beginGroup(name, name, -1);
             } catch (IOException e) {
                 e.printStackTrace();
             }
@@ -76,6 +83,7 @@
     public void compilationFinished(CompilationEvent event) {
         if (printer != null) {
             try {
+                printer.endGroup();
                 printer.end();
                 stream.close();
             } catch (IOException e) {