diff options
-rw-r--r-- | test/tools/opt-viewer/Inputs/unicode-function-name/s.opt.yaml | 22 | ||||
-rw-r--r-- | test/tools/opt-viewer/Inputs/unicode-function-name/s.swift | 12 | ||||
-rw-r--r-- | test/tools/opt-viewer/Outputs/unicode-function-name/index.html | 25 | ||||
-rw-r--r-- | test/tools/opt-viewer/Outputs/unicode-function-name/s.swift.html | 122 | ||||
-rw-r--r-- | test/tools/opt-viewer/unicode-function-name.test | 7 | ||||
-rwxr-xr-x | tools/opt-viewer/opt-viewer.py | 22 | ||||
-rw-r--r-- | tools/opt-viewer/optrecord.py | 8 |
7 files changed, 207 insertions, 11 deletions
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 index 00000000000..2659ff2def9 --- /dev/null +++ b/test/tools/opt-viewer/Inputs/unicode-function-name/s.opt.yaml @@ -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 index 00000000000..6347cde51e4 --- /dev/null +++ b/test/tools/opt-viewer/Inputs/unicode-function-name/s.swift @@ -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 index 00000000000..3b13be6965e --- /dev/null +++ b/test/tools/opt-viewer/Outputs/unicode-function-name/index.html @@ -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 index 00000000000..cae6add0481 --- /dev/null +++ b/test/tools/opt-viewer/Outputs/unicode-function-name/s.swift.html @@ -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) </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 index 00000000000..f62c0241985 --- /dev/null +++ b/test/tools/opt-viewer/unicode-function-name.test @@ -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 diff --git a/tools/opt-viewer/opt-viewer.py b/tools/opt-viewer/opt-viewer.py index 4c09ef86869..4483b94fc7f 100755 --- a/tools/opt-viewer/opt-viewer.py +++ b/tools/opt-viewer/opt-viewer.py @@ -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> diff --git a/tools/opt-viewer/optrecord.py b/tools/opt-viewer/optrecord.py index 2256b4dd243..54e79119253 100644 --- a/tools/opt-viewer/optrecord.py +++ b/tools/opt-viewer/optrecord.py @@ -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 |