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:
Nicholas Nethercote 2018-10-02 08:43:37 +10:00 committed by Emilio Cobos Álvarez
parent 7f8a3530a3
commit c276c8a341
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C

View file

@ -15,14 +15,6 @@ sys.path.insert(0, os.path.join(os.path.dirname(GECKO_DIR), "properties"))
import build 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)`. # Matches lines like `GK_ATOM(foo, "foo", 0x12345678, nsStaticAtom, PseudoElementAtom)`.
PATTERN = re.compile('^GK_ATOM\(([^,]*),[^"]*"([^"]*)",\s*(0x[0-9a-f]+),\s*([^,]*),\s*([^)]*)\)', PATTERN = re.compile('^GK_ATOM\(([^,]*),[^"]*"([^"]*)",\s*(0x[0-9a-f]+),\s*([^,]*),\s*([^)]*)\)',
@ -144,55 +136,72 @@ class FileAvoidWrite(BytesIO):
self.close() self.close()
IMPORTS = ("\nuse gecko_bindings::structs::nsStaticAtom;" PRELUDE = '''
"\nuse string_cache::Atom;\n\n") /* 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" // Autogenerated file created by components/style/gecko/regen_atoms.py.
" pub static {name}: *mut {type};") // DO NOT EDIT DIRECTLY
'''[1:]
UNSAFE_STATIC = ("#[inline(always)]\n" IMPORTS = '''
"pub unsafe fn atom_from_static(ptr: *mut nsStaticAtom) -> Atom {\n" use gecko_bindings::structs::nsStaticAtom;
" Atom::from_static(ptr)\n" use string_cache::Atom;
"}\n\n")
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! {{ cfg_if! {{
if #[cfg(not(target_env = "msvc"))] {{ if #[cfg(not(target_env = "msvc"))] {{
extern {{ extern {{
{gnu} {gnu}\
}} }}
}} else if #[cfg(target_pointer_width = "64")] {{ }} else if #[cfg(target_pointer_width = "64")] {{
extern {{ extern {{
{msvc64} {msvc64}\
}} }}
}} else {{ }} else {{
extern {{ extern {{
{msvc32} {msvc32}\
}} }}
}} }}
}} }}
''' '''
RULE_TEMPLATE = ('("{atom}") =>\n ' RULE_TEMPLATE = '''
'{{{{ ' ("{atom}") =>
'#[allow(unsafe_code)] #[allow(unused_unsafe)]' {{{{
'unsafe {{ $crate::string_cache::atom_macro::atom_from_static' #[allow(unsafe_code)] #[allow(unused_unsafe)]
'($crate::string_cache::atom_macro::{name} as *mut _) }}' unsafe {{ $crate::string_cache::atom_macro::atom_from_static ($crate::string_cache::atom_macro::{name} as *mut _) }}
' }}}};') }}}};
'''[1:]
MACRO = ''' MACRO_TEMPLATE = '''
#[macro_export] #[macro_export]
macro_rules! atom {{ macro_rules! atom {{
{} {body}\
}} }}
''' '''
def write_atom_macro(atoms, file_name): def write_atom_macro(atoms, file_name):
def get_symbols(func): def get_symbols(func):
return '\n'.join([ATOM_TEMPLATE.format(name=atom.ident, return ''.join([ATOM_TEMPLATE.format(name=atom.ident,
link_name=func(atom), link_name=func(atom),
type=atom.type()) for atom in atoms]) type=atom.type()) for atom in atoms])
with FileAvoidWrite(file_name) as f: with FileAvoidWrite(file_name) as f:
f.write(PRELUDE) 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])): for ty in sorted(set([atom.type() for atom in atoms])):
if ty != "nsStaticAtom": if ty != "nsStaticAtom":
f.write("pub enum {} {{}}\n\n".format(ty)) f.write("pub enum {} {{}}\n".format(ty))
f.write(UNSAFE_STATIC) f.write(UNSAFE_STATIC)
gnu_symbols = get_symbols(Atom.gnu_symbol) gnu_symbols = get_symbols(Atom.gnu_symbol)
msvc32_symbols = get_symbols(Atom.msvc32_symbol) msvc32_symbols = get_symbols(Atom.msvc32_symbol)
msvc64_symbols = get_symbols(Atom.msvc64_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] 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): def write_pseudo_elements(atoms, target_filename):