Skip to content

Commit ad94840

Browse files
committed
[opt-viewer] Add syntax highlighting
Uses pygments. llvm-svn: 286532
1 parent 0f94d97 commit ad94840

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

llvm/utils/opt-viewer/opt-viewer.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
import os.path
1313
import subprocess
1414
import shutil
15+
from pygments import highlight
16+
from pygments.lexers.c_cpp import CppLexer
17+
from pygments.formatters import HtmlFormatter
1518

1619
parser = argparse.ArgumentParser(description=desc)
1720
parser.add_argument('yaml_files', nargs='+')
@@ -114,14 +117,17 @@ class SourceFileRenderer:
114117
def __init__(self, filename):
115118
self.source_stream = open(filename)
116119
self.stream = open(os.path.join(args.output_dir, SourceFileRenderer.html_file_name(filename)), 'w')
120+
self.html_formatter = HtmlFormatter()
121+
self.cpp_lexer = CppLexer()
117122

118123
def render_source_line(self, linenum, line):
124+
html_line = highlight(line, self.cpp_lexer, self.html_formatter)
119125
print('''
120126
<tr>
121127
<td><a name=\"L{linenum}\">{linenum}</a></td>
122128
<td></td>
123129
<td></td>
124-
<td><pre>{line}</pre></td>
130+
<td>{html_line}</td>
125131
</tr>'''.format(**locals()), file=self.stream)
126132

127133
def render_inline_remarks(self, r):

llvm/utils/opt-viewer/style.css

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,70 @@ td:first-child {
123123
td:last-child {
124124
border-right: none;
125125
}
126+
127+
/* Generated with pygmentize -S colorful -f html >> style.css */
128+
129+
.hll { background-color: #ffffcc }
130+
.c { color: #888888 } /* Comment */
131+
.err { color: #FF0000; background-color: #FFAAAA } /* Error */
132+
.k { color: #008800; font-weight: bold } /* Keyword */
133+
.o { color: #333333 } /* Operator */
134+
.ch { color: #888888 } /* Comment.Hashbang */
135+
.cm { color: #888888 } /* Comment.Multiline */
136+
.cp { color: #557799 } /* Comment.Preproc */
137+
.cpf { color: #888888 } /* Comment.PreprocFile */
138+
.c1 { color: #888888 } /* Comment.Single */
139+
.cs { color: #cc0000; font-weight: bold } /* Comment.Special */
140+
.gd { color: #A00000 } /* Generic.Deleted */
141+
.ge { font-style: italic } /* Generic.Emph */
142+
.gr { color: #FF0000 } /* Generic.Error */
143+
.gh { color: #000080; font-weight: bold } /* Generic.Heading */
144+
.gi { color: #00A000 } /* Generic.Inserted */
145+
.go { color: #888888 } /* Generic.Output */
146+
.gp { color: #c65d09; font-weight: bold } /* Generic.Prompt */
147+
.gs { font-weight: bold } /* Generic.Strong */
148+
.gu { color: #800080; font-weight: bold } /* Generic.Subheading */
149+
.gt { color: #0044DD } /* Generic.Traceback */
150+
.kc { color: #008800; font-weight: bold } /* Keyword.Constant */
151+
.kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
152+
.kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
153+
.kp { color: #003388; font-weight: bold } /* Keyword.Pseudo */
154+
.kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
155+
.kt { color: #333399; font-weight: bold } /* Keyword.Type */
156+
.m { color: #6600EE; font-weight: bold } /* Literal.Number */
157+
.s { background-color: #fff0f0 } /* Literal.String */
158+
.na { color: #0000CC } /* Name.Attribute */
159+
.nb { color: #007020 } /* Name.Builtin */
160+
.nc { color: #BB0066; font-weight: bold } /* Name.Class */
161+
.no { color: #003366; font-weight: bold } /* Name.Constant */
162+
.nd { color: #555555; font-weight: bold } /* Name.Decorator */
163+
.ni { color: #880000; font-weight: bold } /* Name.Entity */
164+
.ne { color: #FF0000; font-weight: bold } /* Name.Exception */
165+
.nf { color: #0066BB; font-weight: bold } /* Name.Function */
166+
.nl { color: #997700; font-weight: bold } /* Name.Label */
167+
.nn { color: #0e84b5; font-weight: bold } /* Name.Namespace */
168+
.nt { color: #007700 } /* Name.Tag */
169+
.nv { color: #996633 } /* Name.Variable */
170+
.ow { color: #000000; font-weight: bold } /* Operator.Word */
171+
.w { color: #bbbbbb } /* Text.Whitespace */
172+
.mb { color: #6600EE; font-weight: bold } /* Literal.Number.Bin */
173+
.mf { color: #6600EE; font-weight: bold } /* Literal.Number.Float */
174+
.mh { color: #005588; font-weight: bold } /* Literal.Number.Hex */
175+
.mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
176+
.mo { color: #4400EE; font-weight: bold } /* Literal.Number.Oct */
177+
.sb { background-color: #fff0f0 } /* Literal.String.Backtick */
178+
.sc { color: #0044DD } /* Literal.String.Char */
179+
.sd { color: #DD4422 } /* Literal.String.Doc */
180+
.s2 { background-color: #fff0f0 } /* Literal.String.Double */
181+
.se { color: #666666; font-weight: bold; background-color: #fff0f0 } /* Literal.String.Escape */
182+
.sh { background-color: #fff0f0 } /* Literal.String.Heredoc */
183+
.si { background-color: #eeeeee } /* Literal.String.Interpol */
184+
.sx { color: #DD2200; background-color: #fff0f0 } /* Literal.String.Other */
185+
.sr { color: #000000; background-color: #fff0ff } /* Literal.String.Regex */
186+
.s1 { background-color: #fff0f0 } /* Literal.String.Single */
187+
.ss { color: #AA6600 } /* Literal.String.Symbol */
188+
.bp { color: #007020 } /* Name.Builtin.Pseudo */
189+
.vc { color: #336699 } /* Name.Variable.Class */
190+
.vg { color: #dd7700; font-weight: bold } /* Name.Variable.Global */
191+
.vi { color: #3333BB } /* Name.Variable.Instance */
192+
.il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */

0 commit comments

Comments
 (0)