[llvm] r295546 - opt-viewer: Fix syntax highlighting
Brian Cain via llvm-commits
llvm-commits at lists.llvm.org
Sat Feb 18 07:13:59 PST 2017
Author: bcain
Date: Sat Feb 18 09:13:58 2017
New Revision: 295546
URL: http://llvm.org/viewvc/llvm-project?rev=295546&view=rev
Log:
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.
Modified:
llvm/trunk/utils/opt-viewer/opt-viewer.py
Modified: llvm/trunk/utils/opt-viewer/opt-viewer.py
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/opt-viewer/opt-viewer.py?rev=295546&r1=295545&r2=295546&view=diff
==============================================================================
--- llvm/trunk/utils/opt-viewer/opt-viewer.py (original)
+++ llvm/trunk/utils/opt-viewer/opt-viewer.py Sat Feb 18 09:13:58 2017
@@ -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('<div class="highlight"><pre>', '')
+ html_highlighted = html_highlighted.replace('</pre></div>', '')
+
+ for (linenum, html_line) in enumerate(html_highlighted.split('\n'), start=1):
+ print('''
<tr>
<td><a name=\"L{linenum}\">{linenum}</a></td>
<td></td>
<td></td>
-<td>{html_line}</td>
+<td><div class="highlight"><pre>{html_line}</pre></div></td>
</tr>'''.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:
<td>Source</td>
<td>Inline Context</td>
</tr>''', 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('''
</table>
</body>
More information about the llvm-commits
mailing list