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:
10 # The histograms are defined in files provided as command-line arguments.
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. */
22 #ifndef mozilla_TelemetryHistogramEnums_h
23 #define mozilla_TelemetryHistogramEnums_h
26 #include <type_traits>
28 // X11 defines "Success" which collides with a categorical label.
29 #pragma push_macro("Success")
37 } // namespace mozilla
38 } // namespace Telemetry
40 #pragma pop_macro("Success")
42 #endif // mozilla_TelemetryHistogramEnums_h"""
45 def main(output
, *filenames
):
47 print(banner
, file=output
)
48 print(header
, file=output
)
50 # Load the histograms.
52 all_histograms
= list(parse_histograms
.from_files(filenames
))
53 except ParserError
as ex
:
54 print("\nError processing histograms:\n" + str(ex
) + "\n")
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
)
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
)
79 "\ntemplate<class T> struct IsCategoricalLabelEnum : std::false_type {};",
82 for name
, _
, _
in enums
:
84 "template<> struct IsCategoricalLabelEnum<%s> : std::true_type {};" % name
,
88 print("\ntemplate<class T> struct CategoricalLabelId {};", file=output
)
89 for name
, _
, id in enums
:
91 "template<> struct CategoricalLabelId<%s> : "
92 "std::integral_constant<uint32_t, %s> {};" % (name
, id),
97 print(footer
, file=output
)
100 if __name__
== "__main__":
101 main(sys
.stdout
, *sys
.argv
[1:])