Backed out changeset f594e6f00208 (bug 1940883) for causing crashes in bug 1941164.
[gecko.git] / toolkit / components / telemetry / build_scripts / gen_histogram_enum.py
blob32a3c82e221ee9e28db16ab048b00489b6474d96
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 # Write out a C++ enum definition whose members are the names of
6 # histograms as well as the following other members:
8 # - HistogramCount
10 # The histograms are defined in files provided as command-line arguments.
12 import sys
14 import buildconfig
15 from mozparsers import parse_histograms
16 from mozparsers.shared_telemetry_utils import ParserError
18 banner = """/* This file is auto-generated, see gen_histogram_enum.py. */
19 """
21 header = """
22 #ifndef mozilla_TelemetryHistogramEnums_h
23 #define mozilla_TelemetryHistogramEnums_h
25 #include <cstdint>
26 #include <type_traits>
28 // X11 defines "Success" which collides with a categorical label.
29 #pragma push_macro("Success")
30 #undef Success
32 namespace mozilla {
33 namespace Telemetry {
34 """
36 footer = """
37 } // namespace mozilla
38 } // namespace Telemetry
40 #pragma pop_macro("Success")
42 #endif // mozilla_TelemetryHistogramEnums_h"""
45 def main(output, *filenames):
46 # Print header.
47 print(banner, file=output)
48 print(header, file=output)
50 # Load the histograms.
51 try:
52 all_histograms = list(parse_histograms.from_files(filenames))
53 except ParserError as ex:
54 print("\nError processing histograms:\n" + str(ex) + "\n")
55 sys.exit(1)
57 # Print the histogram enums.
58 print("enum HistogramID : uint32_t {", file=output)
59 for histogram in all_histograms:
60 if histogram.record_on_os(buildconfig.substs["OS_TARGET"]):
61 print(" %s," % histogram.name(), file=output)
63 print(" HistogramCount,", file=output)
65 print("};", file=output)
67 # Write categorical label enums.
68 categorical = filter(lambda h: h.kind() == "categorical", all_histograms)
69 categorical = filter(
70 lambda h: h.record_on_os(buildconfig.substs["OS_TARGET"]), categorical
72 enums = [("LABELS_" + h.name(), h.labels(), h.name()) for h in categorical]
73 for name, labels, _ in enums:
74 print("\nenum class %s : uint32_t {" % name, file=output)
75 print(" %s" % ",\n ".join(labels), file=output)
76 print("};", file=output)
78 print(
79 "\ntemplate<class T> struct IsCategoricalLabelEnum : std::false_type {};",
80 file=output,
82 for name, _, _ in enums:
83 print(
84 "template<> struct IsCategoricalLabelEnum<%s> : std::true_type {};" % name,
85 file=output,
88 print("\ntemplate<class T> struct CategoricalLabelId {};", file=output)
89 for name, _, id in enums:
90 print(
91 "template<> struct CategoricalLabelId<%s> : "
92 "std::integral_constant<uint32_t, %s> {};" % (name, id),
93 file=output,
96 # Footer.
97 print(footer, file=output)
100 if __name__ == "__main__":
101 main(sys.stdout, *sys.argv[1:])