Skip to content

Commit

Permalink
Bug 1678416 - Emit input filename in files generated for static prefs…
Browse files Browse the repository at this point in the history
… to make it easier to track backwards to a pref via searchfox r=KrisWright

Differential Revision: https://phabricator.services.mozilla.com/D94794
  • Loading branch information
hotsphink committed Dec 11, 2020
1 parent e7d9dae commit e4825fb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
27 changes: 18 additions & 9 deletions modules/libpref/init/generate_static_pref_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@
"float": "f32",
}

FIRST_LINE = "// This file was generated by generate_static_pref_list.py. DO NOT EDIT."
HEADER_LINE = (
"// This file was generated by generate_static_pref_list.py from {input_filename}."
" DO NOT EDIT."
)

MIRROR_TEMPLATES = {
"never": """\
Expand Down Expand Up @@ -222,19 +225,21 @@ def check_pref_list(pref_list):
prev_pref = pref


def generate_code(pref_list):
def generate_code(pref_list, input_filename):
check_pref_list(pref_list)

first_line = HEADER_LINE.format(input_filename=input_filename)

# The required includes for StaticPrefs_<group>.h.
includes = defaultdict(set)

# StaticPrefList_<group>.h contains all the pref definitions for this
# group.
static_pref_list_group_h = defaultdict(lambda: [FIRST_LINE, ""])
static_pref_list_group_h = defaultdict(lambda: [first_line, ""])

# StaticPrefsCGetters.cpp contains C getters for all the mirrored prefs,
# for use by Rust code.
static_prefs_c_getters_cpp = [FIRST_LINE, ""]
static_prefs_c_getters_cpp = [first_line, ""]

# static_prefs.rs contains C getter declarations and a macro.
static_prefs_rs_decls = []
Expand Down Expand Up @@ -310,7 +315,7 @@ def generate_code(pref_list):

# StaticPrefListAll.h contains one `#include "mozilla/StaticPrefList_X.h`
# line per pref group.
static_pref_list_all_h = [FIRST_LINE, ""]
static_pref_list_all_h = [first_line, ""]
static_pref_list_all_h.extend(
'#include "mozilla/StaticPrefList_{}.h"'.format(group)
for group in sorted(static_pref_list_group_h)
Expand All @@ -319,7 +324,7 @@ def generate_code(pref_list):

# StaticPrefsAll.h contains one `#include "mozilla/StaticPrefs_X.h` line per
# pref group.
static_prefs_all_h = [FIRST_LINE, ""]
static_prefs_all_h = [first_line, ""]
static_prefs_all_h.extend(
'#include "mozilla/StaticPrefs_{}.h"'.format(group)
for group in sorted(static_pref_list_group_h)
Expand All @@ -330,7 +335,7 @@ def generate_code(pref_list):
# used directly by application code.
static_prefs_group_h = defaultdict(list)
for group in sorted(static_pref_list_group_h):
static_prefs_group_h[group] = [FIRST_LINE]
static_prefs_group_h[group] = [first_line]
static_prefs_group_h[group].append(
STATIC_PREFS_GROUP_H_TEMPLATE1.format(group=group)
)
Expand All @@ -344,7 +349,7 @@ def generate_code(pref_list):
)

# static_prefs.rs contains the Rust macro getters.
static_prefs_rs = [FIRST_LINE, "", 'extern "C" {']
static_prefs_rs = [first_line, "", 'extern "C" {']
static_prefs_rs.extend(static_prefs_rs_decls)
static_prefs_rs.extend(["}", "", "#[macro_export]", "macro_rules! pref {"])
static_prefs_rs.extend(static_prefs_rs_macro)
Expand Down Expand Up @@ -382,7 +387,11 @@ def emit_code(fd, pref_list_filename):

try:
pref_list = yaml.safe_load(pp.out.getvalue())
code = generate_code(pref_list)
input_file = os.path.relpath(
pref_list_filename,
os.environ.get("GECKO_PATH", os.environ.get("TOPSRCDIR")),
)
code = generate_code(pref_list, input_file)
except (IOError, ValueError) as e:
print("{}: error:\n {}\n".format(pref_list_filename, e))
sys.exit(1)
Expand Down
18 changes: 9 additions & 9 deletions modules/libpref/test/test_generate_static_pref_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
good[
"static_pref_list_all_h"
] = """\
// This file was generated by generate_static_pref_list.py. DO NOT EDIT.
// This file was generated by generate_static_pref_list.py from (string input). DO NOT EDIT.
#include "mozilla/StaticPrefList_my.h"
#include "mozilla/StaticPrefList_my_dashed.h"
Expand All @@ -108,15 +108,15 @@
good[
"static_prefs_all_h"
] = """\
// This file was generated by generate_static_pref_list.py. DO NOT EDIT.
// This file was generated by generate_static_pref_list.py from (string input). DO NOT EDIT.
#include "mozilla/StaticPrefs_my.h"
#include "mozilla/StaticPrefs_my_dashed.h"
"""

good["static_pref_list_group_h"] = {
"my": """\
// This file was generated by generate_static_pref_list.py. DO NOT EDIT.
// This file was generated by generate_static_pref_list.py from (string input). DO NOT EDIT.
NEVER_PREF("my.bool", bool, false)
Expand Down Expand Up @@ -167,15 +167,15 @@
)
""",
"my_dashed": """\
// This file was generated by generate_static_pref_list.py. DO NOT EDIT.
// This file was generated by generate_static_pref_list.py from (string input). DO NOT EDIT.
NEVER_PREF("my-dashed.atomic.float", AtomicFloat, 0.4455667)
""",
}

good["static_prefs_group_h"] = {
"my": """\
// This file was generated by generate_static_pref_list.py. DO NOT EDIT.
// This file was generated by generate_static_pref_list.py from (string input). DO NOT EDIT.
// Include it to gain access to StaticPrefs::my_*.
#ifndef mozilla_StaticPrefs_my_h
Expand All @@ -194,7 +194,7 @@
good[
"static_prefs_c_getters_cpp"
] = """\
// This file was generated by generate_static_pref_list.py. DO NOT EDIT.
// This file was generated by generate_static_pref_list.py from (string input). DO NOT EDIT.
extern "C" uint32_t StaticPrefs_my_uint() {
return mozilla::StaticPrefs::my_uint();
Expand All @@ -212,7 +212,7 @@
good[
"static_prefs_rs"
] = """\
// This file was generated by generate_static_pref_list.py. DO NOT EDIT.
// This file was generated by generate_static_pref_list.py from (string input). DO NOT EDIT.
extern "C" {
pub fn StaticPrefs_my_uint() -> u32;
Expand Down Expand Up @@ -417,7 +417,7 @@ def test_good(self):
"Test various pieces of good input."
inp = StringIO(good_input)
pref_list = yaml.safe_load(inp)
code = generate_code(pref_list)
code = generate_code(pref_list, "(string input)")

self.assertEqual(good["static_pref_list_all_h"], code["static_pref_list_all_h"])

Expand Down Expand Up @@ -449,7 +449,7 @@ def test_bad(self):
inp = StringIO(input_string)
try:
pref_list = yaml.safe_load(inp)
generate_code(pref_list)
generate_code(pref_list, "(string input")
self.assertEqual(0, 1)
except ValueError as e:
self.assertEqual(str(e), expected)
Expand Down

0 comments on commit e4825fb

Please sign in to comment.