1//===- DwarfTransformer.h ---------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_DEBUGINFO_GSYM_OUTPUTAGGREGATOR_H
10#define LLVM_DEBUGINFO_GSYM_OUTPUTAGGREGATOR_H
11
12#include "llvm/ADT/StringRef.h"
13#include "llvm/DebugInfo/GSYM/ExtractRanges.h"
14
15#include <map>
16#include <string>
17
18namespace llvm {
19
20class raw_ostream;
21
22namespace gsym {
23
24class OutputAggregator {
25protected:
26 // A std::map is preferable over an llvm::StringMap for presenting results
27 // in a predictable order.
28 std::map<std::string, unsigned> Aggregation;
29 raw_ostream *Out;
30
31public:
32 OutputAggregator(raw_ostream *out) : Out(out) {}
33
34 size_t GetNumCategories() const { return Aggregation.size(); }
35
36 void Report(StringRef s, std::function<void(raw_ostream &o)> detailCallback) {
37 Aggregation[std::string(s)]++;
38 if (GetOS())
39 detailCallback(*Out);
40 }
41
42 void EnumerateResults(
43 std::function<void(StringRef, unsigned)> handleCounts) const {
44 for (auto &&[name, count] : Aggregation)
45 handleCounts(name, count);
46 }
47
48 raw_ostream *GetOS() const { return Out; }
49
50 // You can just use the stream, and if it's null, nothing happens.
51 // Don't do a lot of stuff like this, but it's convenient for silly stuff.
52 // It doesn't work with things that have custom insertion operators, though.
53 template <typename T> OutputAggregator &operator<<(T &&value) {
54 if (Out != nullptr)
55 *Out << value;
56 return *this;
57 }
58
59 // For multi-threaded usage, we can collect stuff in another aggregator,
60 // then merge it in here
61 void Merge(const OutputAggregator &other) {
62 for (auto &&[name, count] : other.Aggregation) {
63 auto it = Aggregation.find(x: name);
64 if (it == Aggregation.end())
65 Aggregation.emplace(args: name, args: count);
66 else
67 it->second += count;
68 }
69 }
70};
71
72} // namespace gsym
73} // namespace llvm
74
75#endif // LLVM_DEBUGINFO_GSYM_OUTPUTAGGREGATOR_H
76

source code of llvm/include/llvm/DebugInfo/GSYM/OutputAggregator.h