From 34b4908c3ea331a7c8be4335c582d16b4c4dc68f Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Sat, 18 Feb 2017 15:13:58 +0000 Subject: [PATCH] opt-viewer: Fix syntax highlighting Syntax highlighting has been done line-at-a-time. Done this way, the lexer resets the context at each line, distorting the formatting. This change will render the whole file at once and feed the highlighted text line-at-a-time to be wrapped by the SourceFileRenderer. Leading/trailing newlines were being ignored by Pygments but since each line was rendered in its own row, it didn't matter. This bug was masked by the line-at-a-time algorithm. So now we need to add "stripnl=False" to the CppLexer to change its behavior to match the expectation. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@295546 91177308-0d34-0410-b5e6-96231b3b80d8 --- utils/opt-viewer/opt-viewer.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/utils/opt-viewer/opt-viewer.py b/utils/opt-viewer/opt-viewer.py index f5a20666d36..d56e295425f 100755 --- a/utils/opt-viewer/opt-viewer.py +++ b/utils/opt-viewer/opt-viewer.py @@ -184,18 +184,29 @@ class SourceFileRenderer: '''.format(filename), file=self.stream) self.html_formatter = HtmlFormatter(encoding='utf-8') - self.cpp_lexer = CppLexer() + self.cpp_lexer = CppLexer(stripnl=False) - def render_source_line(self, linenum, line): - html_line = highlight(line, self.cpp_lexer, self.html_formatter) - print(''' + def render_source_lines(self, stream, line_remarks): + file_text = stream.read() + html_highlighted = highlight(file_text, self.cpp_lexer, self.html_formatter) + + # Take off the header and footer, these must be + # reapplied line-wise, within the page structure + html_highlighted = html_highlighted.replace('
', '')
+        html_highlighted = html_highlighted.replace('
', '') + + for (linenum, html_line) in enumerate(html_highlighted.split('\n'), start=1): + print(''' {linenum} -{html_line} +
{html_line}
'''.format(**locals()), file=self.stream) + for remark in line_remarks.get(linenum, []): + self.render_inline_remarks(remark, html_line) + def render_inline_remarks(self, r, line): inlining_context = r.DemangledFunctionName print @@ -237,10 +248,8 @@ class SourceFileRenderer: Source Inline Context ''', file=self.stream) - for (linenum, line) in enumerate(self.source_stream.readlines(), start=1): - self.render_source_line(linenum, line) - for remark in line_remarks.get(linenum, []): - self.render_inline_remarks(remark, line) + self.render_source_lines(self.source_stream, line_remarks) + print(''' -- 2.50.1