Skip to content

Commit

Permalink
style: Tweak regen_atoms.py.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nnethercote authored and emilio committed Oct 9, 2018
1 parent 7f8a353 commit c276c8a
Showing 1 changed file with 43 additions and 34 deletions.
77 changes: 43 additions & 34 deletions components/style/gecko/regen_atoms.py
Expand Up @@ -15,14 +15,6 @@

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*([^)]*)\)',
Expand Down Expand Up @@ -144,73 +136,90 @@ def __exit__(self, type, value, traceback):
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/. */
// Autogenerated file created by components/style/gecko/regen_atoms.py.
// DO NOT EDIT DIRECTLY
'''[1:]

IMPORTS = '''
use gecko_bindings::structs::nsStaticAtom;
use string_cache::Atom;
ATOM_TEMPLATE = (" #[link_name = \"{link_name}\"]\n"
" pub static {name}: *mut {type};")
'''

UNSAFE_STATIC = '''
#[inline(always)]
pub unsafe fn atom_from_static(ptr: *mut nsStaticAtom) -> Atom {
Atom::from_static(ptr)
}
'''

UNSAFE_STATIC = ("#[inline(always)]\n"
"pub unsafe fn atom_from_static(ptr: *mut nsStaticAtom) -> Atom {\n"
" Atom::from_static(ptr)\n"
"}\n\n")
ATOM_TEMPLATE = '''
#[link_name = \"{link_name}\"]
pub static {name}: *mut {type};
'''[1:]

CFG_IF = '''
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)
f.write(IMPORTS)

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):
Expand Down

0 comments on commit c276c8a

Please sign in to comment.