Pārlūkot izejas kodu

Add button to show cfg. (#3000)

* Add button to view cfg in OptEditor.

* Show graph directly.
Xiang Li 5 gadi atpakaļ
vecāks
revīzija
fec269169a

+ 44 - 39
tools/clang/tools/dotnetc/EditorForm.cs

@@ -1785,46 +1785,51 @@ namespace MainNs
                     // See file history for a version that inserted the image in-line with graph.
                     // The svg/web browser approach provides zooming and more interactivity.
                     string graphText = s.Text.Substring(s.SelectionStart, closing - s.SelectionStart);
-                    string path = System.IO.Path.GetTempFileName();
-                    string outPath = path + ".svg";
-                    try
+                    ShowDot(graphText);
+                }
+            }
+
+            static public void ShowDot(string graphText)
+            {
+                string path = System.IO.Path.GetTempFileName();
+                string outPath = path + ".svg";
+                try
+                {
+                    System.IO.File.WriteAllText(path, graphText);
+
+                    string svgData = RunDot(path, DotOutFormat.Svg, null);
+                    Form browserForm = new Form();
+
+                    TrackBar zoomControl = new TrackBar();
+                    zoomControl.Minimum = 10;
+                    zoomControl.Maximum = 400;
+                    zoomControl.Value = 100;
+                    zoomControl.Dock = DockStyle.Top;
+                    zoomControl.Text = "zoom";
+
+                    WebBrowser browser = new WebBrowser();
+                    browser.Dock = DockStyle.Fill;
+                    browser.DocumentText = svgData;
+                    browserForm.Controls.Add(browser);
+                    browserForm.Controls.Add(zoomControl);
+                    zoomControl.ValueChanged += (_, __) =>
                     {
-                        System.IO.File.WriteAllText(path, graphText);
-
-                        string svgData = RunDot(path, DotOutFormat.Svg, null);
-                        Form browserForm = new Form();
-
-                        TrackBar zoomControl = new TrackBar();
-                        zoomControl.Minimum = 10;
-                        zoomControl.Maximum = 400;
-                        zoomControl.Value = 100;
-                        zoomControl.Dock = DockStyle.Top;
-                        zoomControl.Text = "zoom";
-
-                        WebBrowser browser = new WebBrowser();
-                        browser.Dock = DockStyle.Fill;
-                        browser.DocumentText = svgData;
-                        browserForm.Controls.Add(browser);
-                        browserForm.Controls.Add(zoomControl);
-                        zoomControl.ValueChanged += (_, __) =>
+                        if (browser.Document != null && browser.Document.DomDocument != null)
                         {
-                            if (browser.Document != null && browser.Document.DomDocument != null)
-                            {
-                                dynamic o = browser.Document.DomDocument;
-                                o.documentElement.style.zoom = String.Format("{0}%", zoomControl.Value);
-                            }
-                        };
-                        browserForm.Text = "graph";
-                        browserForm.Show();
-                    }
-                    catch (DotProgram.CannotFindDotException cfde)
-                    {
-                        MessageBox.Show(cfde.Message, "Unable to find dot.exe", MessageBoxButtons.OK, MessageBoxIcon.Error);
-                    }
-                    finally
-                    {
-                        DeleteIfExists(path);
-                    }
+                            dynamic o = browser.Document.DomDocument;
+                            o.documentElement.style.zoom = String.Format("{0}%", zoomControl.Value);
+                        }
+                    };
+                    browserForm.Text = "graph";
+                    browserForm.Show();
+                }
+                catch (DotProgram.CannotFindDotException cfde)
+                {
+                    MessageBox.Show(cfde.Message, "Unable to find dot.exe", MessageBoxButtons.OK, MessageBoxIcon.Error);
+                }
+                finally
+                {
+                    DeleteIfExists(path);
                 }
             }
 
@@ -1834,7 +1839,7 @@ namespace MainNs
                     System.IO.File.Delete(path);
             }
 
-            internal string RunDot(string inputFile, DotOutFormat format, string outFile)
+            internal static string RunDot(string inputFile, DotOutFormat format, string outFile)
             {
                 DotProgram program = new DotProgram();
                 program.InFilePath = inputFile;

+ 13 - 0
tools/clang/tools/dotnetc/OptEditorForm.Designer.cs

@@ -39,6 +39,7 @@
             this.ApplyChangesButton = new System.Windows.Forms.Button();
             this.CopyContainerButton = new System.Windows.Forms.Button();
             this.btnSaveAll = new System.Windows.Forms.Button();
+            this.btnViewCFGOnly = new System.Windows.Forms.Button();
             this.LogBox = new System.Windows.Forms.RichTextBox();
             this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
             ((System.ComponentModel.ISupportInitialize)(this.TopContainer)).BeginInit();
@@ -120,6 +121,7 @@
             this.flowLayoutPanel1.Controls.Add(this.ApplyChangesButton);
             this.flowLayoutPanel1.Controls.Add(this.CopyContainerButton);
             this.flowLayoutPanel1.Controls.Add(this.btnSaveAll);
+            this.flowLayoutPanel1.Controls.Add(this.btnViewCFGOnly);
             this.flowLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Top;
             this.flowLayoutPanel1.Location = new System.Drawing.Point(0, 0);
             this.flowLayoutPanel1.Name = "flowLayoutPanel1";
@@ -192,6 +194,16 @@
             this.btnSaveAll.UseVisualStyleBackColor = true;
             this.btnSaveAll.Click += new System.EventHandler(this.btnSaveAll_Click);
             // 
+            // btnViewCFGOnly
+            // 
+            this.btnViewCFGOnly.Location = new System.Drawing.Point(515, 3);
+            this.btnViewCFGOnly.Name = "btnViewCFGOnly";
+            this.btnViewCFGOnly.Size = new System.Drawing.Size(105, 26);
+            this.btnViewCFGOnly.TabIndex = 6;
+            this.btnViewCFGOnly.Text = "ViewCFGOnly";
+            this.btnViewCFGOnly.UseVisualStyleBackColor = true;
+            this.btnViewCFGOnly.Click += new System.EventHandler(this.btnViewCFGOnly_Click);
+            // 
             // LogBox
             // 
             this.LogBox.Dock = System.Windows.Forms.DockStyle.Fill;
@@ -240,5 +252,6 @@
         private System.Windows.Forms.Button CopyContainerButton;
         private System.Windows.Forms.Button btnSaveAll;
         private System.Windows.Forms.SaveFileDialog saveFileDialog1;
+        private System.Windows.Forms.Button btnViewCFGOnly;
     }
 }

+ 25 - 0
tools/clang/tools/dotnetc/OptEditorForm.cs

@@ -166,6 +166,31 @@ namespace MainNs
                 System.IO.File.WriteAllLines(fullName, Section.Lines);
             }
         }
+
+        private void btnViewCFGOnly_Click(object sender, EventArgs e)
+        {
+            if (PassesListBox.SelectedIndex == -1)
+            {
+                MessageBox.Show("Select a pass first");
+                return;
+            }
+            TextSection section = (TextSection)PassesListBox.SelectedItem;
+
+            var source = EditorForm.CreateBlobForText(this.Library, section.Text);
+            source = this.Library.GetBlobAstUf8(source);
+
+            string[] options = new string [1];
+            options[0] = "-view-cfg-only";
+            EditorForm.OptimizeResult opt = EditorForm.RunOptimize(this.Library, options, source);
+            if (!opt.Succeeded)
+            {
+                MessageBox.Show("Failed to optimize: " + opt.ResultText);
+                return;
+            }
+
+            string dotText = opt.ResultText.Substring(opt.ResultText.IndexOf("digraph"));
+            EditorForm.LogContextMenuHelper.ShowDot(dotText);
+        }
     }
 
     public class TextSection