| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
# Copyright (C) 2023-2025 Free Software Foundation, Inc. |
| 4 |
# |
| 5 |
# Script to regenerate attr-urls.def from generated HTML. |
| 6 |
# |
| 7 |
# This file is part of GCC. |
| 8 |
# |
| 9 |
# GCC is free software; you can redistribute it and/or modify it under |
| 10 |
# the terms of the GNU General Public License as published by the Free |
| 11 |
# Software Foundation; either version 3, or (at your option) any later |
| 12 |
# version. |
| 13 |
# |
| 14 |
# GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 15 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 16 |
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 17 |
# for more details. |
| 18 |
# |
| 19 |
# You should have received a copy of the GNU General Public License |
| 20 |
# along with GCC; see the file COPYING3. If not see |
| 21 |
# <http://www.gnu.org/licenses/>. */ |
| 22 |
|
| 23 |
DESCRIPTION = """ |
| 24 |
Parses the generated HTML (from "make html") to locate anchors |
| 25 |
for attributes, and generates a gcc/attr-urls.def file in the source tree, |
| 26 |
giving URLs for each attribute, where it can. |
| 27 |
|
| 28 |
Usage (from build/gcc subdirectory): |
| 29 |
../../src/gcc/regenerate-attr-urls.py HTML/gcc-15.0.0/ ../../src |
| 30 |
|
| 31 |
To run unit tests: |
| 32 |
../../src/gcc/regenerate-attr-urls.py HTML/gcc-15.0.0/ ../../src --unit-test |
| 33 |
""" |
| 34 |
|
| 35 |
import argparse |
| 36 |
import json |
| 37 |
import os |
| 38 |
from pathlib import Path |
| 39 |
from pprint import pprint |
| 40 |
import sys |
| 41 |
import re |
| 42 |
import unittest |
| 43 |
|
| 44 |
class Index: |
| 45 |
def __init__(self): |
| 46 |
self.entries = [] |
| 47 |
self.entries_by_kind = {} |
| 48 |
|
| 49 |
def add_entry(self, url_suffix, name, kind, extra_text, verbose=False): |
| 50 |
#if len(self.entries) > 5: |
| 51 |
# return |
| 52 |
self.entries.append( (url_suffix, name, kind, extra_text) ) |
| 53 |
|
| 54 |
if kind in self.entries_by_kind: |
| 55 |
by_kind = self.entries_by_kind[kind] |
| 56 |
else: |
| 57 |
by_kind = [] |
| 58 |
self.entries_by_kind[kind] = by_kind |
| 59 |
by_kind.append( (name, url_suffix, extra_text) ) |
| 60 |
|
| 61 |
def parse_attribute_index(self, input_filename, verbose=False): |
| 62 |
with open(input_filename) as f: |
| 63 |
for line in f: |
| 64 |
self.parse_html_line_attribute_index(line, verbose) |
| 65 |
|
| 66 |
def parse_html_line_attribute_index(self, line, verbose=False): |
| 67 |
if verbose: |
| 68 |
print(repr(line)) |
| 69 |
|
| 70 |
# Update for this in the GCC website's bin/preprocess process_html_file: |
| 71 |
# | sed -e 's/_002d/-/g' -e 's/_002a/*/g' \ |
| 72 |
line = line.replace('_002d', '-') |
| 73 |
line = line.replace('_002a', '*') |
| 74 |
|
| 75 |
# e.g. <a href="Common-Function-Attributes.html#index-access-function-attribute"><code>access</code> function attribute</a> |
| 76 |
# e.g. <a href="MIPS-Function-Attributes.html#index-nocompression-function-attribute_002c-MIPS"><code class="code">nocompression</code> function attribute, MIPS</a> |
| 77 |
m = re.search(r'<a href="([\S]+)"><code[^>]*>([\S]+)</code> (\S+) attribute([^<]*)</a>', line) |
| 78 |
if not m: |
| 79 |
return |
| 80 |
if verbose: |
| 81 |
print(m.groups()) |
| 82 |
|
| 83 |
url_suffix, name, kind, extra_text = m.groups() |
| 84 |
|
| 85 |
if extra_text.startswith(', '): |
| 86 |
extra_text = extra_text[2:] |
| 87 |
|
| 88 |
# Reject anchors where the name contains a paren |
| 89 |
# e.g. 'target("3dnowa")': |
| 90 |
if '(' in name: |
| 91 |
return |
| 92 |
|
| 93 |
self.add_entry(url_suffix, name, kind, extra_text) |
| 94 |
|
| 95 |
def generate_file(self, dstpath): |
| 96 |
with open(dstpath, 'w') as outf: |
| 97 |
self.write_file(outf) |
| 98 |
|
| 99 |
def write_file(self, outf): |
| 100 |
outf.write("/* Autogenerated by regenerate-attr-urls.py. */\n\n") |
| 101 |
|
| 102 |
for kind in sorted(self.entries_by_kind.keys()): |
| 103 |
by_kind = self.entries_by_kind[kind] |
| 104 |
outf.write("const attr_url_entry %s_attrs[] = {\n" % kind) |
| 105 |
for name, url_suffix, extra_text in sorted(self.entries_by_kind[kind]): |
| 106 |
outf.write(' { "%s", "gcc/%s", "%s", %i},\n' |
| 107 |
% (name, url_suffix, extra_text, len(name))) |
| 108 |
outf.write("};\n\n") |
| 109 |
|
| 110 |
outf.write('static const struct attr_url_table {\n') |
| 111 |
outf.write(' const attr_url_entry *m_table;\n') |
| 112 |
outf.write(' const size_t m_table_sz;\n') |
| 113 |
outf.write('} attr_url_tables[] = {\n') |
| 114 |
for kind in sorted(self.entries_by_kind.keys()): |
| 115 |
outf.write(" { %s_attrs, ARRAY_SIZE (%s_attrs) },\n" % (kind, kind)) |
| 116 |
outf.write("};\n") |
| 117 |
|
| 118 |
INDEX_REL_PATH = 'gcc/Concept-and-Symbol-Index.html' |
| 119 |
|
| 120 |
class TestParsingIndex(unittest.TestCase): |
| 121 |
def test_function_attribute(self): |
| 122 |
index = Index() |
| 123 |
index.parse_html_line_attribute_index('<a href="Common-Function-Attributes.html#index-access-function-attribute"><code>access</code> function attribute</a>') |
| 124 |
self.assertEqual(index.entries, [('Common-Function-Attributes.html#index-access-function-attribute', |
| 125 |
'access', |
| 126 |
'function', |
| 127 |
'')]) |
| 128 |
|
| 129 |
def test_function_attribute_with_target(self): |
| 130 |
index = Index() |
| 131 |
index.parse_html_line_attribute_index('<a href="MIPS-Function-Attributes.html#index-nocompression-function-attribute_002c-MIPS"><code class="code">nocompression</code> function attribute, MIPS</a>') |
| 132 |
self.assertEqual(index.entries, [('MIPS-Function-Attributes.html#index-nocompression-function-attribute_002c-MIPS', |
| 133 |
'nocompression', |
| 134 |
'function', |
| 135 |
'MIPS')]) |
| 136 |
|
| 137 |
def test_reject_parens(self): |
| 138 |
index = Index() |
| 139 |
index.parse_html_line_attribute_index('<a href="x86-Function-Attributes.html#index-target_0028_00223dnow_0022_0029-function-attribute_002c-x86"><code>target("3dnow")</code> function attribute, x86</a>') |
| 140 |
self.assertEqual(len(index.entries), 0) |
| 141 |
|
| 142 |
def test_type_attribute(self): |
| 143 |
index = Index() |
| 144 |
index.parse_html_line_attribute_index('<a href="Common-Type-Attributes.html#index-aligned-type-attribute"><code>aligned</code> type attribute</a>') |
| 145 |
self.assertEqual(index.entries, [('Common-Type-Attributes.html#index-aligned-type-attribute', |
| 146 |
'aligned', |
| 147 |
'type', |
| 148 |
'')]) |
| 149 |
|
| 150 |
def test_enumerator_attribute(self): |
| 151 |
index = Index() |
| 152 |
index.parse_html_line_attribute_index('<a href="Enumerator-Attributes.html#index-deprecated-enumerator-attribute"><code>deprecated</code> enumerator attribute</a>') |
| 153 |
self.assertEqual(index.entries, [('Enumerator-Attributes.html#index-deprecated-enumerator-attribute', |
| 154 |
'deprecated', |
| 155 |
'enumerator', |
| 156 |
'')]) |
| 157 |
def test_label_attribute(self): |
| 158 |
index = Index() |
| 159 |
index.parse_html_line_attribute_index('<a href="Label-Attributes.html#index-cold-label-attribute"><code>cold</code> label attribute</a>') |
| 160 |
self.assertEqual(index.entries, [('Label-Attributes.html#index-cold-label-attribute', |
| 161 |
'cold', |
| 162 |
'label', |
| 163 |
'')]) |
| 164 |
|
| 165 |
def test_statement_attribute(self): |
| 166 |
index = Index() |
| 167 |
index.parse_html_line_attribute_index('<a href="Statement-Attributes.html#index-assume-statement-attribute"><code>assume</code> statement attribute</a>') |
| 168 |
self.assertEqual(index.entries, [('Statement-Attributes.html#index-assume-statement-attribute', |
| 169 |
'assume', |
| 170 |
'statement', |
| 171 |
'')]) |
| 172 |
|
| 173 |
def test_variable_attribute(self): |
| 174 |
index = Index() |
| 175 |
index.parse_html_line_attribute_index('<a href="AVR-Variable-Attributes.html#index-absdata-variable-attribute_002c-AVR"><code>absdata</code> variable attribute, AVR</a>') |
| 176 |
self.assertEqual(index.entries, [('AVR-Variable-Attributes.html#index-absdata-variable-attribute_002c-AVR', |
| 177 |
'absdata', |
| 178 |
'variable', |
| 179 |
'AVR')]) |
| 180 |
|
| 181 |
def test_parse_attribute_index(self): |
| 182 |
index = Index() |
| 183 |
index.parse_attribute_index(INPUT_HTML_PATH / INDEX_REL_PATH) |
| 184 |
self.assertEqual(index.entries_by_kind['enumerator'][0], |
| 185 |
('deprecated', |
| 186 |
'Enumerator-Attributes.html#index-deprecated-enumerator-attribute', |
| 187 |
'')) |
| 188 |
self.assertEqual(index.entries_by_kind['label'][0], |
| 189 |
('cold', 'Label-Attributes.html#index-cold-label-attribute', '')) |
| 190 |
|
| 191 |
def main(args): |
| 192 |
index = Index() |
| 193 |
index.parse_attribute_index(args.base_html_dir / INDEX_REL_PATH) |
| 194 |
dstpath = args.src_gcc_dir / 'gcc' / 'attr-urls.def' |
| 195 |
index.generate_file(dstpath) |
| 196 |
|
| 197 |
if __name__ == '__main__': |
| 198 |
parser = argparse.ArgumentParser(description=DESCRIPTION, |
| 199 |
formatter_class=argparse.RawDescriptionHelpFormatter) |
| 200 |
parser.add_argument('base_html_dir', type=Path) |
| 201 |
parser.add_argument('src_gcc_dir', type=Path) |
| 202 |
parser.add_argument('--unit-test', action='store_true') |
| 203 |
args = parser.parse_args() |
| 204 |
|
| 205 |
if args.unit_test: |
| 206 |
INPUT_HTML_PATH = args.base_html_dir |
| 207 |
unittest.main(argv=[sys.argv[0], '-v']) |
| 208 |
else: |
| 209 |
main(args) |