]> granicus.if.org Git - llvm/commitdiff
[opt-viewer] Support unicode characters in function names
authorAdam Nemet <anemet@apple.com>
Thu, 14 Dec 2017 18:42:42 +0000 (18:42 +0000)
committerAdam Nemet <anemet@apple.com>
Thu, 14 Dec 2017 18:42:42 +0000 (18:42 +0000)
This is a Swift feature.  The output stream for the index page and the source
HTML page is utf-8 now.

The next patch will add the HTML magic to properly render these characters in
the browser.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@320725 91177308-0d34-0410-b5e6-96231b3b80d8

test/tools/opt-viewer/Inputs/unicode-function-name/s.opt.yaml [new file with mode: 0644]
test/tools/opt-viewer/Inputs/unicode-function-name/s.swift [new file with mode: 0644]
test/tools/opt-viewer/Outputs/unicode-function-name/index.html [new file with mode: 0644]
test/tools/opt-viewer/Outputs/unicode-function-name/s.swift.html [new file with mode: 0644]
test/tools/opt-viewer/unicode-function-name.test [new file with mode: 0644]
tools/opt-viewer/opt-viewer.py
tools/opt-viewer/optrecord.py

diff --git a/test/tools/opt-viewer/Inputs/unicode-function-name/s.opt.yaml b/test/tools/opt-viewer/Inputs/unicode-function-name/s.opt.yaml
new file mode 100644 (file)
index 0000000..2659ff2
--- /dev/null
@@ -0,0 +1,22 @@
+--- !Passed
+Pass:            sil-inliner
+Name:            sil.Inlined
+DebugLoc:
+  File:            s.swift
+  Line:            12
+  Column:          16
+Function:        main
+Args:
+  - Callee:          '"s.• infix(_:_:)"'
+    DebugLoc:
+      File:            s.swift
+      Line:            3
+      Column:          6
+  - String:          ' inlined into '
+  - Caller:          '"main"'
+  - String:          ' (cost = '
+  - Cost:            '2'
+  - String:          ', benefit = '
+  - Benefit:         '40'
+  - String:          ')'
+...
diff --git a/test/tools/opt-viewer/Inputs/unicode-function-name/s.swift b/test/tools/opt-viewer/Inputs/unicode-function-name/s.swift
new file mode 100644 (file)
index 0000000..6347cde
--- /dev/null
@@ -0,0 +1,12 @@
+infix operator •: AdditionPrecedence
+
+func • (a: Int, b: Int) -> Int {
+  return a * b
+}
+
+@inline(never)
+func g(a: Int) -> Int{
+  return a + 1
+}
+
+let i = g(a: 1 • 2)
diff --git a/test/tools/opt-viewer/Outputs/unicode-function-name/index.html b/test/tools/opt-viewer/Outputs/unicode-function-name/index.html
new file mode 100644 (file)
index 0000000..3b13be6
--- /dev/null
@@ -0,0 +1,25 @@
+
+<html>
+<head>
+<link rel='stylesheet' type='text/css' href='style.css'>
+</head>
+<body>
+<div class="centered">
+<table>
+<tr>
+<td>Source Location</td>
+<td>Hotness</td>
+<td>Function</td>
+<td>Pass</td>
+</tr>
+
+<tr>
+<td class="column-entry-0"><a href="s.swift.html#L12">s.swift:12:16</a></td>
+<td class="column-entry-0"></td>
+<td class="column-entry-0">main</td>
+<td class="column-entry-green">sil-inliner</td>
+</tr>
+
+</table>
+</body>
+</html>
diff --git a/test/tools/opt-viewer/Outputs/unicode-function-name/s.swift.html b/test/tools/opt-viewer/Outputs/unicode-function-name/s.swift.html
new file mode 100644 (file)
index 0000000..cae6add
--- /dev/null
@@ -0,0 +1,122 @@
+
+<html>
+<head>
+<link rel='stylesheet' type='text/css' href='style.css'>
+</head>
+<body>
+<div class="centered">
+<table class="source">
+<thead>
+<tr>
+<th style="width: 2%">Line</td>
+<th style="width: 3%">Hotness</td>
+<th style="width: 10%">Optimization</td>
+<th style="width: 70%">Source</td>
+<th style="width: 15%">Inline Context</td>
+</tr>
+</thead>
+<tbody>
+
+<tr>
+<td><a name="L1">1</a></td>
+<td></td>
+<td></td>
+<td><div class="highlight"><pre>infix operator •: AdditionPrecedence</pre></div></td>
+</tr>
+
+<tr>
+<td><a name="L2">2</a></td>
+<td></td>
+<td></td>
+<td><div class="highlight"><pre></pre></div></td>
+</tr>
+
+<tr>
+<td><a name="L3">3</a></td>
+<td></td>
+<td></td>
+<td><div class="highlight"><pre>func • (a: Int, b: Int) -> Int {</pre></div></td>
+</tr>
+
+<tr>
+<td><a name="L4">4</a></td>
+<td></td>
+<td></td>
+<td><div class="highlight"><pre>  return a * b</pre></div></td>
+</tr>
+
+<tr>
+<td><a name="L5">5</a></td>
+<td></td>
+<td></td>
+<td><div class="highlight"><pre>}</pre></div></td>
+</tr>
+
+<tr>
+<td><a name="L6">6</a></td>
+<td></td>
+<td></td>
+<td><div class="highlight"><pre></pre></div></td>
+</tr>
+
+<tr>
+<td><a name="L7">7</a></td>
+<td></td>
+<td></td>
+<td><div class="highlight"><pre>@inline(never)</pre></div></td>
+</tr>
+
+<tr>
+<td><a name="L8">8</a></td>
+<td></td>
+<td></td>
+<td><div class="highlight"><pre>func g(a: Int) -> Int{</pre></div></td>
+</tr>
+
+<tr>
+<td><a name="L9">9</a></td>
+<td></td>
+<td></td>
+<td><div class="highlight"><pre>  return a + 1</pre></div></td>
+</tr>
+
+<tr>
+<td><a name="L10">10</a></td>
+<td></td>
+<td></td>
+<td><div class="highlight"><pre>}</pre></div></td>
+</tr>
+
+<tr>
+<td><a name="L11">11</a></td>
+<td></td>
+<td></td>
+<td><div class="highlight"><pre></pre></div></td>
+</tr>
+
+<tr>
+<td><a name="L12">12</a></td>
+<td></td>
+<td></td>
+<td><div class="highlight"><pre>let i = g(a: 1 • 2)</pre></div></td>
+</tr>
+
+<tr>
+<td></td>
+<td></td>
+<td class="column-entry-green">sil-inliner</td>
+<td><pre style="display:inline">               </pre><span class="column-entry-yellow"> <a href="s.swift.html#L3">"s.• infix(_:_:)"</a> inlined into "main" (cost = 2, benefit = 40)&nbsp;</span></td>
+<td class="column-entry-yellow">main</td>
+</tr>
+
+<tr>
+<td><a name="L13">13</a></td>
+<td></td>
+<td></td>
+<td><div class="highlight"><pre></pre></div></td>
+</tr>
+
+</tbody>
+</table>
+</body>
+</html>
diff --git a/test/tools/opt-viewer/unicode-function-name.test b/test/tools/opt-viewer/unicode-function-name.test
new file mode 100644 (file)
index 0000000..f62c024
--- /dev/null
@@ -0,0 +1,7 @@
+RUN: %opt-viewer -s %p/Inputs/unicode-function-name -o %t %p/Inputs/unicode-function-name/s.opt.yaml --no-highlight --demangler=llvm-cxxfilt
+RUN: diff %p/Outputs/unicode-function-name/index.html %t/index.html
+RUN: diff %p/Outputs/unicode-function-name/s.swift.html %t/s.swift.html
+
+# Also test the pygments code path without actually checking the output since
+# that may slightly vary between pygment versions
+RUN: %opt-viewer -s %p/Inputs/unicode-function-name -o %t.unused %p/Inputs/unicode-function-name/s.opt.yaml --demangler=llvm-cxxfilt
index 4c09ef86869adcd262525e35524074904609c116..4483b94fc7fef2cb955f5dcc4107ac1b2fbdd0b5 100755 (executable)
@@ -4,6 +4,7 @@ from __future__ import print_function
 
 import argparse
 import cgi
+import codecs
 import errno
 import functools
 from multiprocessing import cpu_count
@@ -51,7 +52,7 @@ class SourceFileRenderer:
             if os.path.exists(fn):
                 existing_filename = fn
 
-        self.stream = open(os.path.join(output_dir, optrecord.html_file_name(filename)), 'w')
+        self.stream = codecs.open(os.path.join(output_dir, optrecord.html_file_name(filename)), 'w', encoding='utf-8')
         if existing_filename:
             self.source_stream = open(existing_filename)
         else:
@@ -69,16 +70,19 @@ class SourceFileRenderer:
         file_text = stream.read()
 
         if args.no_highlight:
-            html_highlighted = file_text
+            html_highlighted = file_text.decode('utf-8')
         else:
             html_highlighted = highlight(
             file_text,
                 self.cpp_lexer,
                 self.html_formatter)
 
-            # On Python 3, pygments.highlight() returns a bytes object, not a str.
-            if sys.version_info >= (3, 0):
-              html_highlighted = html_highlighted.decode('utf-8')
+            # Note that the API is different between Python 2 and 3.  On
+            # Python 3, pygments.highlight() returns a bytes object, so we
+            # have to decode.  On Python 2, the output is str but since we
+            # support unicode characters and the output streams is unicode we
+            # decode too.
+            html_highlighted = html_highlighted.decode('utf-8')
 
             # Take off the header and footer, these must be
             #   reapplied line-wise, within the page structure
@@ -86,7 +90,7 @@ class SourceFileRenderer:
             html_highlighted = html_highlighted.replace('</pre></div>', '')
 
         for (linenum, html_line) in enumerate(html_highlighted.split('\n'), start=1):
-            print('''
+            print(u'''
 <tr>
 <td><a name=\"L{linenum}\">{linenum}</a></td>
 <td></td>
@@ -111,7 +115,7 @@ class SourceFileRenderer:
         indent = line[:max(r.Column, 1) - 1]
         indent = re.sub('\S', ' ', indent)
 
-        print('''
+        print(u'''
 <tr>
 <td></td>
 <td>{r.RelativeHotness}</td>
@@ -153,12 +157,12 @@ class SourceFileRenderer:
 
 class IndexRenderer:
     def __init__(self, output_dir, should_display_hotness):
-        self.stream = open(os.path.join(output_dir, 'index.html'), 'w')
+        self.stream = codecs.open(os.path.join(output_dir, 'index.html'), 'w', encoding='utf-8')
         self.should_display_hotness = should_display_hotness
 
     def render_entry(self, r, odd):
         escaped_name = cgi.escape(r.DemangledFunctionName)
-        print('''
+        print(u'''
 <tr>
 <td class=\"column-entry-{odd}\"><a href={r.Link}>{r.DebugLocString}</a></td>
 <td class=\"column-entry-{odd}\">{r.RelativeHotness}</td>
index 2256b4dd24316b2c6882a154da0b33b649b4aa66..54e7911925318e23ce7fe6d06ca6c91db6a0d5a4 100644 (file)
@@ -79,7 +79,11 @@ class Remark(yaml.YAMLObject):
     def _reduce_memory(self):
         self.Pass = intern(self.Pass)
         self.Name = intern(self.Name)
-        self.Function = intern(self.Function)
+        try:
+            # Can't intern unicode strings.
+            self.Function = intern(self.Function)
+        except:
+            pass
 
         def _reduce_memory_dict(old_dict):
             new_dict = dict()
@@ -156,7 +160,7 @@ class Remark(yaml.YAMLObject):
 
         if dl and key != 'Caller':
             dl_dict = dict(list(dl))
-            return "<a href={}>{}</a>".format(
+            return u"<a href={}>{}</a>".format(
                 make_link(dl_dict['File'], dl_dict['Line']), value)
         else:
             return value