blob: c96c1ca27acb67d2e148488924ec698011eaedb9 [file] [log] [blame]
Manuel Klimekde063382012-08-27 18:49:12 +00001#!/usr/bin/env python
2# A tool to parse ASTMatchers.h and update the documentation in
3# ../LibASTMatchersReference.html automatically. Run from the
4# directory in which this file is located to update the docs.
5
6import collections
7import re
Serge Gueltonc177c3a2018-12-19 13:46:13 +00008try:
9 from urllib.request import urlopen
10except ImportError:
11 from urllib2 import urlopen
Manuel Klimekde063382012-08-27 18:49:12 +000012
13MATCHERS_FILE = '../../include/clang/ASTMatchers/ASTMatchers.h'
14
15# Each matcher is documented in one row of the form:
16# result | name | argA
17# The subsequent row contains the documentation and is hidden by default,
18# becoming visible via javascript when the user clicks the matcher name.
19TD_TEMPLATE="""
Manuel Klimek8bad9472012-09-07 13:10:32 +000020<tr><td>%(result)s</td><td class="name" onclick="toggle('%(id)s')"><a name="%(id)sAnchor">%(name)s</a></td><td>%(args)s</td></tr>
Manuel Klimekde063382012-08-27 18:49:12 +000021<tr><td colspan="4" class="doc" id="%(id)s"><pre>%(comment)s</pre></td></tr>
22"""
23
24# We categorize the matchers into these three categories in the reference:
25node_matchers = {}
26narrowing_matchers = {}
27traversal_matchers = {}
28
29# We output multiple rows per matcher if the matcher can be used on multiple
30# node types. Thus, we need a new id per row to control the documentation
31# pop-up. ids[name] keeps track of those ids.
32ids = collections.defaultdict(int)
33
34# Cache for doxygen urls we have already verified.
35doxygen_probes = {}
36
37def esc(text):
38 """Escape any html in the given text."""
39 text = re.sub(r'&', '&amp;', text)
40 text = re.sub(r'<', '&lt;', text)
41 text = re.sub(r'>', '&gt;', text)
42 def link_if_exists(m):
43 name = m.group(1)
Sylvestre Ledrubc5c3f52018-11-04 17:02:00 +000044 url = 'https://clang.llvm.org/doxygen/classclang_1_1%s.html' % name
Manuel Klimekde063382012-08-27 18:49:12 +000045 if url not in doxygen_probes:
46 try:
Serge Gueltonc0ebe772018-12-18 08:36:33 +000047 print('Probing %s...' % url)
Serge Gueltonc177c3a2018-12-19 13:46:13 +000048 urlopen(url)
Manuel Klimekde063382012-08-27 18:49:12 +000049 doxygen_probes[url] = True
50 except:
51 doxygen_probes[url] = False
52 if doxygen_probes[url]:
Aaron Ballman672dde22016-01-22 23:15:00 +000053 return r'Matcher&lt;<a href="%s">%s</a>&gt;' % (url, name)
Manuel Klimekde063382012-08-27 18:49:12 +000054 else:
55 return m.group(0)
56 text = re.sub(
57 r'Matcher&lt;([^\*&]+)&gt;', link_if_exists, text)
58 return text
59
60def extract_result_types(comment):
61 """Extracts a list of result types from the given comment.
62
63 We allow annotations in the comment of the matcher to specify what
64 nodes a matcher can match on. Those comments have the form:
65 Usable as: Any Matcher | (Matcher<T1>[, Matcher<t2>[, ...]])
66
67 Returns ['*'] in case of 'Any Matcher', or ['T1', 'T2', ...].
68 Returns the empty list if no 'Usable as' specification could be
69 parsed.
70 """
71 result_types = []
72 m = re.search(r'Usable as: Any Matcher[\s\n]*$', comment, re.S)
73 if m:
74 return ['*']
75 while True:
76 m = re.match(r'^(.*)Matcher<([^>]+)>\s*,?[\s\n]*$', comment, re.S)
77 if not m:
78 if re.search(r'Usable as:\s*$', comment):
79 return result_types
80 else:
81 return None
82 result_types += [m.group(2)]
83 comment = m.group(1)
84
85def strip_doxygen(comment):
86 """Returns the given comment without \-escaped words."""
87 # If there is only a doxygen keyword in the line, delete the whole line.
88 comment = re.sub(r'^\\[^\s]+\n', r'', comment, flags=re.M)
Aaron Ballmanc35724c2016-01-21 15:18:25 +000089
90 # If there is a doxygen \see command, change the \see prefix into "See also:".
91 # FIXME: it would be better to turn this into a link to the target instead.
92 comment = re.sub(r'\\see', r'See also:', comment)
93
Manuel Klimekde063382012-08-27 18:49:12 +000094 # Delete the doxygen command and the following whitespace.
95 comment = re.sub(r'\\[^\s]+\s+', r'', comment)
96 return comment
97
98def unify_arguments(args):
99 """Gets rid of anything the user doesn't care about in the argument list."""
100 args = re.sub(r'internal::', r'', args)
Benjamin Kramerae7ff382018-01-17 16:50:14 +0000101 args = re.sub(r'extern const\s+(.*)&', r'\1 ', args)
Manuel Klimekde063382012-08-27 18:49:12 +0000102 args = re.sub(r'&', r' ', args)
103 args = re.sub(r'(^|\s)M\d?(\s)', r'\1Matcher<*>\2', args)
104 return args
105
106def add_matcher(result_type, name, args, comment, is_dyncast=False):
107 """Adds a matcher to one of our categories."""
108 if name == 'id':
109 # FIXME: Figure out whether we want to support the 'id' matcher.
110 return
111 matcher_id = '%s%d' % (name, ids[name])
112 ids[name] += 1
113 args = unify_arguments(args)
114 matcher_html = TD_TEMPLATE % {
115 'result': esc('Matcher<%s>' % result_type),
116 'name': name,
117 'args': esc(args),
118 'comment': esc(strip_doxygen(comment)),
119 'id': matcher_id,
120 }
121 if is_dyncast:
122 node_matchers[result_type + name] = matcher_html
123 # Use a heuristic to figure out whether a matcher is a narrowing or
124 # traversal matcher. By default, matchers that take other matchers as
125 # arguments (and are not node matchers) do traversal. We specifically
126 # exclude known narrowing matchers that also take other matchers as
127 # arguments.
128 elif ('Matcher<' not in args or
129 name in ['allOf', 'anyOf', 'anything', 'unless']):
Manuel Klimek03892692014-02-24 10:40:22 +0000130 narrowing_matchers[result_type + name + esc(args)] = matcher_html
Manuel Klimekde063382012-08-27 18:49:12 +0000131 else:
Manuel Klimek03892692014-02-24 10:40:22 +0000132 traversal_matchers[result_type + name + esc(args)] = matcher_html
Manuel Klimekde063382012-08-27 18:49:12 +0000133
134def act_on_decl(declaration, comment, allowed_types):
135 """Parse the matcher out of the given declaration and comment.
136
137 If 'allowed_types' is set, it contains a list of node types the matcher
138 can match on, as extracted from the static type asserts in the matcher
139 definition.
140 """
141 if declaration.strip():
142 # Node matchers are defined by writing:
143 # VariadicDynCastAllOfMatcher<ResultType, ArgumentType> name;
Manuel Klimekcdd5c232013-01-09 09:38:21 +0000144 m = re.match(r""".*Variadic(?:DynCast)?AllOfMatcher\s*<
145 \s*([^\s,]+)\s*(?:,
146 \s*([^\s>]+)\s*)?>
Manuel Klimekde063382012-08-27 18:49:12 +0000147 \s*([^\s;]+)\s*;\s*$""", declaration, flags=re.X)
148 if m:
149 result, inner, name = m.groups()
Manuel Klimekcdd5c232013-01-09 09:38:21 +0000150 if not inner:
151 inner = result
Manuel Klimekde063382012-08-27 18:49:12 +0000152 add_matcher(result, name, 'Matcher<%s>...' % inner,
153 comment, is_dyncast=True)
154 return
155
Benjamin Kramerae7ff382018-01-17 16:50:14 +0000156 # Special case of type matchers:
157 # AstTypeMatcher<ArgumentType> name
158 m = re.match(r""".*AstTypeMatcher\s*<
159 \s*([^\s>]+)\s*>
160 \s*([^\s;]+)\s*;\s*$""", declaration, flags=re.X)
Manuel Klimekcdd5c232013-01-09 09:38:21 +0000161 if m:
162 inner, name = m.groups()
163 add_matcher('Type', name, 'Matcher<%s>...' % inner,
164 comment, is_dyncast=True)
Manuel Klimekdba64f12013-07-25 06:05:50 +0000165 # FIXME: re-enable once we have implemented casting on the TypeLoc
166 # hierarchy.
167 # add_matcher('TypeLoc', '%sLoc' % name, 'Matcher<%sLoc>...' % inner,
168 # comment, is_dyncast=True)
Manuel Klimekcdd5c232013-01-09 09:38:21 +0000169 return
170
Benjamin Kramerae7ff382018-01-17 16:50:14 +0000171 # Parse the various matcher definition macros.
172 m = re.match(""".*AST_TYPE(LOC)?_TRAVERSE_MATCHER(?:_DECL)?\(
Manuel Klimekcdd5c232013-01-09 09:38:21 +0000173 \s*([^\s,]+\s*),
Samuel Benzaquen79656e12013-07-15 19:25:06 +0000174 \s*(?:[^\s,]+\s*),
Benjamin Kramer57dd9bd2015-03-07 20:38:15 +0000175 \s*AST_POLYMORPHIC_SUPPORTED_TYPES\(([^)]*)\)
Manuel Klimekcdd5c232013-01-09 09:38:21 +0000176 \)\s*;\s*$""", declaration, flags=re.X)
177 if m:
Manuel Klimek5d093282015-08-14 11:47:51 +0000178 loc, name, results = m.groups()[0:3]
Samuel Benzaquen79656e12013-07-15 19:25:06 +0000179 result_types = [r.strip() for r in results.split(',')]
180
181 comment_result_types = extract_result_types(comment)
182 if (comment_result_types and
183 sorted(result_types) != sorted(comment_result_types)):
184 raise Exception('Inconsistent documentation for: %s' % name)
Manuel Klimekcdd5c232013-01-09 09:38:21 +0000185 for result_type in result_types:
186 add_matcher(result_type, name, 'Matcher<Type>', comment)
Stephen Kelly7b79fb42018-10-09 08:24:18 +0000187 # if loc:
188 # add_matcher('%sLoc' % result_type, '%sLoc' % name, 'Matcher<TypeLoc>',
189 # comment)
Manuel Klimekcdd5c232013-01-09 09:38:21 +0000190 return
191
Samuel Benzaquenc6f2c9b2013-06-21 15:51:31 +0000192 m = re.match(r"""^\s*AST_POLYMORPHIC_MATCHER(_P)?(.?)(?:_OVERLOAD)?\(
193 \s*([^\s,]+)\s*,
Benjamin Kramer57dd9bd2015-03-07 20:38:15 +0000194 \s*AST_POLYMORPHIC_SUPPORTED_TYPES\(([^)]*)\)
Samuel Benzaquenc6f2c9b2013-06-21 15:51:31 +0000195 (?:,\s*([^\s,]+)\s*
196 ,\s*([^\s,]+)\s*)?
197 (?:,\s*([^\s,]+)\s*
198 ,\s*([^\s,]+)\s*)?
199 (?:,\s*\d+\s*)?
200 \)\s*{\s*$""", declaration, flags=re.X)
201
202 if m:
Manuel Klimek5d093282015-08-14 11:47:51 +0000203 p, n, name, results = m.groups()[0:4]
204 args = m.groups()[4:]
Samuel Benzaquenc6f2c9b2013-06-21 15:51:31 +0000205 result_types = [r.strip() for r in results.split(',')]
206 if allowed_types and allowed_types != result_types:
207 raise Exception('Inconsistent documentation for: %s' % name)
208 if n not in ['', '2']:
209 raise Exception('Cannot parse "%s"' % declaration)
210 args = ', '.join('%s %s' % (args[i], args[i+1])
211 for i in range(0, len(args), 2) if args[i])
212 for result_type in result_types:
213 add_matcher(result_type, name, args, comment)
214 return
215
Samuel Benzaquena0839352014-03-10 15:40:23 +0000216 m = re.match(r"""^\s*AST_MATCHER_FUNCTION(_P)?(.?)(?:_OVERLOAD)?\(
217 (?:\s*([^\s,]+)\s*,)?
218 \s*([^\s,]+)\s*
219 (?:,\s*([^\s,]+)\s*
220 ,\s*([^\s,]+)\s*)?
221 (?:,\s*([^\s,]+)\s*
222 ,\s*([^\s,]+)\s*)?
223 (?:,\s*\d+\s*)?
224 \)\s*{\s*$""", declaration, flags=re.X)
225 if m:
226 p, n, result, name = m.groups()[0:4]
227 args = m.groups()[4:]
228 if n not in ['', '2']:
229 raise Exception('Cannot parse "%s"' % declaration)
230 args = ', '.join('%s %s' % (args[i], args[i+1])
231 for i in range(0, len(args), 2) if args[i])
232 add_matcher(result, name, args, comment)
233 return
234
Samuel Benzaquenc6f2c9b2013-06-21 15:51:31 +0000235 m = re.match(r"""^\s*AST_MATCHER(_P)?(.?)(?:_OVERLOAD)?\(
Manuel Klimekde063382012-08-27 18:49:12 +0000236 (?:\s*([^\s,]+)\s*,)?
237 \s*([^\s,]+)\s*
Samuel Benzaquena4076ea2016-05-04 20:45:00 +0000238 (?:,\s*([^,]+)\s*
Manuel Klimekde063382012-08-27 18:49:12 +0000239 ,\s*([^\s,]+)\s*)?
240 (?:,\s*([^\s,]+)\s*
241 ,\s*([^\s,]+)\s*)?
Manuel Klimek4feac282013-02-06 20:36:22 +0000242 (?:,\s*\d+\s*)?
Benjamin Kramer8bf200a2018-01-17 23:14:49 +0000243 \)\s*{""", declaration, flags=re.X)
Manuel Klimekde063382012-08-27 18:49:12 +0000244 if m:
Samuel Benzaquenc6f2c9b2013-06-21 15:51:31 +0000245 p, n, result, name = m.groups()[0:4]
246 args = m.groups()[4:]
Manuel Klimekde063382012-08-27 18:49:12 +0000247 if not result:
248 if not allowed_types:
249 raise Exception('Did not find allowed result types for: %s' % name)
250 result_types = allowed_types
251 else:
252 result_types = [result]
253 if n not in ['', '2']:
254 raise Exception('Cannot parse "%s"' % declaration)
255 args = ', '.join('%s %s' % (args[i], args[i+1])
256 for i in range(0, len(args), 2) if args[i])
257 for result_type in result_types:
258 add_matcher(result_type, name, args, comment)
259 return
260
Samuel Benzaquenbd7d8872013-08-16 16:19:42 +0000261 # Parse ArgumentAdapting matchers.
262 m = re.match(
Benjamin Kramerae7ff382018-01-17 16:50:14 +0000263 r"""^.*ArgumentAdaptingMatcherFunc<.*>\s*
264 ([a-zA-Z]*);$""",
Samuel Benzaquenbd7d8872013-08-16 16:19:42 +0000265 declaration, flags=re.X)
266 if m:
267 name = m.groups()[0]
268 add_matcher('*', name, 'Matcher<*>', comment)
269 return
270
Samuel Benzaquen922bef42016-02-22 21:13:02 +0000271 # Parse Variadic functions.
272 m = re.match(
Samuel Benzaquena4076ea2016-05-04 20:45:00 +0000273 r"""^.*internal::VariadicFunction\s*<\s*([^,]+),\s*([^,]+),\s*[^>]+>\s*
Benjamin Kramerae7ff382018-01-17 16:50:14 +0000274 ([a-zA-Z]*);$""",
Samuel Benzaquen922bef42016-02-22 21:13:02 +0000275 declaration, flags=re.X)
276 if m:
277 result, arg, name = m.groups()[:3]
278 add_matcher(result, name, '%s, ..., %s' % (arg, arg), comment)
279 return
280
Samuel Benzaquen85ec25d2013-08-27 15:11:16 +0000281 # Parse Variadic operator matchers.
282 m = re.match(
Benjamin Kramerae7ff382018-01-17 16:50:14 +0000283 r"""^.*VariadicOperatorMatcherFunc\s*<\s*([^,]+),\s*([^\s]+)\s*>\s*
284 ([a-zA-Z]*);$""",
Samuel Benzaquen85ec25d2013-08-27 15:11:16 +0000285 declaration, flags=re.X)
286 if m:
Manuel Klimek4f8f8902014-02-24 10:28:36 +0000287 min_args, max_args, name = m.groups()[:3]
288 if max_args == '1':
289 add_matcher('*', name, 'Matcher<*>', comment)
290 return
Benjamin Kramerae7ff382018-01-17 16:50:14 +0000291 elif max_args == 'std::numeric_limits<unsigned>::max()':
Manuel Klimek4f8f8902014-02-24 10:28:36 +0000292 add_matcher('*', name, 'Matcher<*>, ..., Matcher<*>', comment)
293 return
Samuel Benzaquen85ec25d2013-08-27 15:11:16 +0000294
Samuel Benzaquenbd7d8872013-08-16 16:19:42 +0000295
Manuel Klimekde063382012-08-27 18:49:12 +0000296 # Parse free standing matcher functions, like:
297 # Matcher<ResultType> Name(Matcher<ArgumentType> InnerMatcher) {
298 m = re.match(r"""^\s*(.*)\s+
299 ([^\s\(]+)\s*\(
300 (.*)
301 \)\s*{""", declaration, re.X)
302 if m:
303 result, name, args = m.groups()
304 args = ', '.join(p.strip() for p in args.split(','))
Manuel Klimekcdd5c232013-01-09 09:38:21 +0000305 m = re.match(r'.*\s+internal::(Bindable)?Matcher<([^>]+)>$', result)
Manuel Klimekde063382012-08-27 18:49:12 +0000306 if m:
Manuel Klimekcdd5c232013-01-09 09:38:21 +0000307 result_types = [m.group(2)]
Manuel Klimekde063382012-08-27 18:49:12 +0000308 else:
309 result_types = extract_result_types(comment)
310 if not result_types:
311 if not comment:
312 # Only overloads don't have their own doxygen comments; ignore those.
Serge Gueltonc0ebe772018-12-18 08:36:33 +0000313 print('Ignoring "%s"' % name)
Manuel Klimekde063382012-08-27 18:49:12 +0000314 else:
Serge Gueltonc0ebe772018-12-18 08:36:33 +0000315 print('Cannot determine result type for "%s"' % name)
Manuel Klimekde063382012-08-27 18:49:12 +0000316 else:
317 for result_type in result_types:
318 add_matcher(result_type, name, args, comment)
319 else:
Serge Gueltonc0ebe772018-12-18 08:36:33 +0000320 print('*** Unparsable: "' + declaration + '" ***')
Manuel Klimekde063382012-08-27 18:49:12 +0000321
322def sort_table(matcher_type, matcher_map):
323 """Returns the sorted html table for the given row map."""
324 table = ''
325 for key in sorted(matcher_map.keys()):
326 table += matcher_map[key] + '\n'
327 return ('<!-- START_%(type)s_MATCHERS -->\n' +
328 '%(table)s' +
329 '<!--END_%(type)s_MATCHERS -->') % {
330 'type': matcher_type,
331 'table': table,
332 }
333
334# Parse the ast matchers.
335# We alternate between two modes:
336# body = True: We parse the definition of a matcher. We need
337# to parse the full definition before adding a matcher, as the
338# definition might contain static asserts that specify the result
339# type.
340# body = False: We parse the comments and declaration of the matcher.
341comment = ''
342declaration = ''
343allowed_types = []
344body = False
345for line in open(MATCHERS_FILE).read().splitlines():
346 if body:
347 if line.strip() and line[0] == '}':
348 if declaration:
349 act_on_decl(declaration, comment, allowed_types)
350 comment = ''
351 declaration = ''
352 allowed_types = []
353 body = False
354 else:
355 m = re.search(r'is_base_of<([^,]+), NodeType>', line)
356 if m and m.group(1):
357 allowed_types += [m.group(1)]
358 continue
359 if line.strip() and line.lstrip()[0] == '/':
Aaron Ballman94f3e742018-12-11 19:30:49 +0000360 comment += re.sub(r'^/+\s?', '', line) + '\n'
Manuel Klimekde063382012-08-27 18:49:12 +0000361 else:
362 declaration += ' ' + line
363 if ((not line.strip()) or
364 line.rstrip()[-1] == ';' or
Samuel Benzaquen85ec25d2013-08-27 15:11:16 +0000365 (line.rstrip()[-1] == '{' and line.rstrip()[-3:] != '= {')):
Manuel Klimekde063382012-08-27 18:49:12 +0000366 if line.strip() and line.rstrip()[-1] == '{':
367 body = True
368 else:
369 act_on_decl(declaration, comment, allowed_types)
370 comment = ''
371 declaration = ''
372 allowed_types = []
373
374node_matcher_table = sort_table('DECL', node_matchers)
375narrowing_matcher_table = sort_table('NARROWING', narrowing_matchers)
376traversal_matcher_table = sort_table('TRAVERSAL', traversal_matchers)
377
378reference = open('../LibASTMatchersReference.html').read()
379reference = re.sub(r'<!-- START_DECL_MATCHERS.*END_DECL_MATCHERS -->',
Yury Gribov75118f52016-02-18 15:43:56 +0000380 node_matcher_table, reference, flags=re.S)
Manuel Klimekde063382012-08-27 18:49:12 +0000381reference = re.sub(r'<!-- START_NARROWING_MATCHERS.*END_NARROWING_MATCHERS -->',
Yury Gribov75118f52016-02-18 15:43:56 +0000382 narrowing_matcher_table, reference, flags=re.S)
Manuel Klimekde063382012-08-27 18:49:12 +0000383reference = re.sub(r'<!-- START_TRAVERSAL_MATCHERS.*END_TRAVERSAL_MATCHERS -->',
Yury Gribov75118f52016-02-18 15:43:56 +0000384 traversal_matcher_table, reference, flags=re.S)
Manuel Klimekde063382012-08-27 18:49:12 +0000385
Benjamin Kramer611d33a2015-11-20 07:46:19 +0000386with open('../LibASTMatchersReference.html', 'wb') as output:
Manuel Klimekde063382012-08-27 18:49:12 +0000387 output.write(reference)
388