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/.
4
5# Write out a C++ enum definition whose members are the names of
6# histograms as well as the following other members:
7#
8#   - HistogramCount
9#   - HistogramFirstUseCounter
10#   - HistogramLastUseCounter
11#   - HistogramUseCounterCount
12#
13# The histograms are defined in files provided as command-line arguments.
14
15from __future__ import print_function
16
17import histogram_tools
18import itertools
19import sys
20
21banner = """/* This file is auto-generated, see gen-histogram-enum.py.  */
22"""
23
24header = """
25#ifndef mozilla_TelemetryHistogramEnums_h
26#define mozilla_TelemetryHistogramEnums_h
27
28#include "mozilla/TemplateLib.h"
29
30namespace mozilla {
31namespace Telemetry {
32"""
33
34footer = """
35} // namespace mozilla
36} // namespace Telemetry
37#endif // mozilla_TelemetryHistogramEnums_h"""
38
39def main(output, *filenames):
40    # Print header.
41    print(banner, file=output)
42    print(header, file=output)
43
44    # Load the histograms.
45    all_histograms = list(histogram_tools.from_files(filenames))
46    groups = itertools.groupby(all_histograms,
47                               lambda h: h.name().startswith("USE_COUNTER2_"))
48
49    # Print the histogram enums.
50    # Note that histogram_tools.py guarantees that all of the USE_COUNTER2_*
51    # histograms are defined in a contiguous block.  We therefore assume
52    # that there's at most one group for which use_counter_group is true.
53    print("enum ID : uint32_t {", file=output)
54    seen_use_counters = False
55    for (use_counter_group, histograms) in groups:
56        if use_counter_group:
57            seen_use_counters = True
58
59        # The HistogramDUMMY* enum variables are used to make the computation
60        # of Histogram{First,Last}UseCounter easier.  Otherwise, we'd have to
61        # special case the first and last histogram in the group.
62        if use_counter_group:
63            print("  HistogramFirstUseCounter,", file=output)
64            print("  HistogramDUMMY1 = HistogramFirstUseCounter - 1,", file=output)
65
66        for histogram in histograms:
67            cpp_guard = histogram.cpp_guard()
68            if cpp_guard:
69                print("#if defined(%s)" % cpp_guard, file=output)
70            print("  %s," % histogram.name(), file=output)
71            if cpp_guard:
72                print("#endif", file=output)
73
74        if use_counter_group:
75            print("  HistogramDUMMY2,", file=output)
76            print("  HistogramLastUseCounter = HistogramDUMMY2 - 1,", file=output)
77
78    print("  HistogramCount,", file=output)
79    if seen_use_counters:
80        print("  HistogramUseCounterCount = HistogramLastUseCounter - HistogramFirstUseCounter + 1", file=output)
81    else:
82        print("  HistogramFirstUseCounter = 0,", file=output)
83        print("  HistogramLastUseCounter = 0,", file=output)
84        print("  HistogramUseCounterCount = 0", file=output)
85    print("};", file=output)
86
87    # Write categorical label enums.
88    categorical = filter(lambda h: h.kind() == "categorical", all_histograms)
89    enums = [("LABELS_" + h.name(), h.labels(), h.name()) for h in categorical]
90    for name,labels,_ in enums:
91        print("\nenum class %s : uint32_t {" % name, file=output)
92        print("  %s" % ",\n  ".join(labels), file=output)
93        print("};", file=output)
94
95    print("\ntemplate<class T> struct IsCategoricalLabelEnum : FalseType {};", file=output)
96    for name,_,_ in enums:
97        print("template<> struct IsCategoricalLabelEnum<%s> : TrueType {};" % name, file=output)
98
99    print("\ntemplate<class T> struct CategoricalLabelId {};", file=output)
100    for name,_,id in enums:
101        print("template<> struct CategoricalLabelId<%s> : IntegralConstant<uint32_t, %s> {};" % (name, id), file=output)
102
103    # Footer.
104    print(footer, file=output)
105
106if __name__ == '__main__':
107    main(sys.stdout, *sys.argv[1:])
108