From 747f7a4defd3b6fb8e327fcd8e219ffd344fb581 Mon Sep 17 00:00:00 2001 From: Adam Nemet Date: Thu, 14 Dec 2017 18:42:42 +0000 Subject: [opt-viewer] Support unicode characters in function names 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 --- tools/opt-viewer/opt-viewer.py | 22 +++++++++++++--------- tools/opt-viewer/optrecord.py | 8 ++++++-- 2 files changed, 19 insertions(+), 11 deletions(-) (limited to 'tools') 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('', '') for (linenum, html_line) in enumerate(html_highlighted.split('\n'), start=1): - print(''' + print(u''' {linenum} @@ -111,7 +115,7 @@ class SourceFileRenderer: indent = line[:max(r.Column, 1) - 1] indent = re.sub('\S', ' ', indent) - print(''' + print(u''' {r.RelativeHotness} @@ -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''' {r.DebugLocString} {r.RelativeHotness} 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 "{}".format( + return u"{}".format( make_link(dl_dict['File'], dl_dict['Line']), value) else: return value -- cgit v1.2.3