mirror of
https://github.com/servo/servo.git
synced 2025-08-03 12:40:06 +01:00
style: Tweak regen_atoms.py.
Specifically, give all the string templates the ''' form, and give them all `_TEMPLATE` suffixes. This requires slightly changing the newline handling. Bug: 1449787 Reviewed-by: emilio
This commit is contained in:
parent
7f8a3530a3
commit
c276c8a341
1 changed files with 43 additions and 34 deletions
|
@ -15,14 +15,6 @@ sys.path.insert(0, os.path.join(os.path.dirname(GECKO_DIR), "properties"))
|
|||
|
||||
import build
|
||||
|
||||
PRELUDE = """
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/* Autogenerated file created by components/style/gecko/regen_atoms.py, DO NOT EDIT DIRECTLY */
|
||||
"""[1:] # NOQA: E501
|
||||
|
||||
|
||||
# Matches lines like `GK_ATOM(foo, "foo", 0x12345678, nsStaticAtom, PseudoElementAtom)`.
|
||||
PATTERN = re.compile('^GK_ATOM\(([^,]*),[^"]*"([^"]*)",\s*(0x[0-9a-f]+),\s*([^,]*),\s*([^)]*)\)',
|
||||
|
@ -144,55 +136,72 @@ class FileAvoidWrite(BytesIO):
|
|||
self.close()
|
||||
|
||||
|
||||
IMPORTS = ("\nuse gecko_bindings::structs::nsStaticAtom;"
|
||||
"\nuse string_cache::Atom;\n\n")
|
||||
PRELUDE = '''
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
ATOM_TEMPLATE = (" #[link_name = \"{link_name}\"]\n"
|
||||
" pub static {name}: *mut {type};")
|
||||
// Autogenerated file created by components/style/gecko/regen_atoms.py.
|
||||
// DO NOT EDIT DIRECTLY
|
||||
'''[1:]
|
||||
|
||||
UNSAFE_STATIC = ("#[inline(always)]\n"
|
||||
"pub unsafe fn atom_from_static(ptr: *mut nsStaticAtom) -> Atom {\n"
|
||||
" Atom::from_static(ptr)\n"
|
||||
"}\n\n")
|
||||
IMPORTS = '''
|
||||
use gecko_bindings::structs::nsStaticAtom;
|
||||
use string_cache::Atom;
|
||||
|
||||
CFG_IF = '''
|
||||
'''
|
||||
|
||||
UNSAFE_STATIC = '''
|
||||
#[inline(always)]
|
||||
pub unsafe fn atom_from_static(ptr: *mut nsStaticAtom) -> Atom {
|
||||
Atom::from_static(ptr)
|
||||
}
|
||||
'''
|
||||
|
||||
ATOM_TEMPLATE = '''
|
||||
#[link_name = \"{link_name}\"]
|
||||
pub static {name}: *mut {type};
|
||||
'''[1:]
|
||||
|
||||
CFG_IF_TEMPLATE = '''
|
||||
cfg_if! {{
|
||||
if #[cfg(not(target_env = "msvc"))] {{
|
||||
extern {{
|
||||
{gnu}
|
||||
{gnu}\
|
||||
}}
|
||||
}} else if #[cfg(target_pointer_width = "64")] {{
|
||||
extern {{
|
||||
{msvc64}
|
||||
{msvc64}\
|
||||
}}
|
||||
}} else {{
|
||||
extern {{
|
||||
{msvc32}
|
||||
{msvc32}\
|
||||
}}
|
||||
}}
|
||||
}}
|
||||
'''
|
||||
|
||||
RULE_TEMPLATE = ('("{atom}") =>\n '
|
||||
'{{{{ '
|
||||
'#[allow(unsafe_code)] #[allow(unused_unsafe)]'
|
||||
'unsafe {{ $crate::string_cache::atom_macro::atom_from_static'
|
||||
'($crate::string_cache::atom_macro::{name} as *mut _) }}'
|
||||
' }}}};')
|
||||
RULE_TEMPLATE = '''
|
||||
("{atom}") =>
|
||||
{{{{
|
||||
#[allow(unsafe_code)] #[allow(unused_unsafe)]
|
||||
unsafe {{ $crate::string_cache::atom_macro::atom_from_static ($crate::string_cache::atom_macro::{name} as *mut _) }}
|
||||
}}}};
|
||||
'''[1:]
|
||||
|
||||
MACRO = '''
|
||||
MACRO_TEMPLATE = '''
|
||||
#[macro_export]
|
||||
macro_rules! atom {{
|
||||
{}
|
||||
{body}\
|
||||
}}
|
||||
'''
|
||||
|
||||
|
||||
def write_atom_macro(atoms, file_name):
|
||||
def get_symbols(func):
|
||||
return '\n'.join([ATOM_TEMPLATE.format(name=atom.ident,
|
||||
link_name=func(atom),
|
||||
type=atom.type()) for atom in atoms])
|
||||
return ''.join([ATOM_TEMPLATE.format(name=atom.ident,
|
||||
link_name=func(atom),
|
||||
type=atom.type()) for atom in atoms])
|
||||
|
||||
with FileAvoidWrite(file_name) as f:
|
||||
f.write(PRELUDE)
|
||||
|
@ -200,17 +209,17 @@ def write_atom_macro(atoms, file_name):
|
|||
|
||||
for ty in sorted(set([atom.type() for atom in atoms])):
|
||||
if ty != "nsStaticAtom":
|
||||
f.write("pub enum {} {{}}\n\n".format(ty))
|
||||
f.write("pub enum {} {{}}\n".format(ty))
|
||||
|
||||
f.write(UNSAFE_STATIC)
|
||||
|
||||
gnu_symbols = get_symbols(Atom.gnu_symbol)
|
||||
msvc32_symbols = get_symbols(Atom.msvc32_symbol)
|
||||
msvc64_symbols = get_symbols(Atom.msvc64_symbol)
|
||||
f.write(CFG_IF.format(gnu=gnu_symbols, msvc32=msvc32_symbols, msvc64=msvc64_symbols))
|
||||
f.write(CFG_IF_TEMPLATE.format(gnu=gnu_symbols, msvc32=msvc32_symbols, msvc64=msvc64_symbols))
|
||||
|
||||
macro_rules = [RULE_TEMPLATE.format(atom=atom.value, name=atom.ident) for atom in atoms]
|
||||
f.write(MACRO.format('\n'.join(macro_rules)))
|
||||
f.write(MACRO_TEMPLATE.format(body=''.join(macro_rules)))
|
||||
|
||||
|
||||
def write_pseudo_elements(atoms, target_filename):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue