summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorAdam Nemet <anemet@apple.com>2017-12-14 18:42:42 +0000
committerAdam Nemet <anemet@apple.com>2017-12-14 18:42:42 +0000
commit747f7a4defd3b6fb8e327fcd8e219ffd344fb581 (patch)
tree58739fa4ba567829842470ea4f7b48d6e08b7adc /tools
parent18fcbf52eb51eeb6d220eb5a7a7858c42b2d6cf3 (diff)
[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
Diffstat (limited to 'tools')
-rwxr-xr-xtools/opt-viewer/opt-viewer.py22
-rw-r--r--tools/opt-viewer/optrecord.py8
2 files changed, 19 insertions, 11 deletions
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