[PATCH] D30132: opt-viewer: Fix syntax highlighting

Brian Cain via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 17 20:32:07 PST 2017


bcain created this revision.

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.


Repository:
  rL LLVM

https://reviews.llvm.org/D30132

Files:
  utils/opt-viewer/opt-viewer.py


Index: utils/opt-viewer/opt-viewer.py
===================================================================
--- utils/opt-viewer/opt-viewer.py
+++ utils/opt-viewer/opt-viewer.py
@@ -184,18 +184,29 @@
             '''.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 @@
 <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>


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D30132.89009.patch
Type: text/x-patch
Size: 2018 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170218/e215ff1c/attachment.bin>


More information about the llvm-commits mailing list