17
17
#include < list>
18
18
#include < map>
19
19
#include < shared_mutex>
20
+ #include < variant>
20
21
#include < vector>
21
22
22
23
#include " ExclusiveAccess.h"
@@ -122,7 +123,9 @@ struct InfoTreeNode {
122
123
static constexpr uint64_t IndentSize = 4 ;
123
124
124
125
std::string Key;
125
- std::string Value;
126
+ struct None {};
127
+ using VariantType = std::variant<uint64_t , std::string, bool , None>;
128
+ VariantType Value;
126
129
std::string Units;
127
130
// Need to specify a default value number of elements here as `InfoTreeNode`'s
128
131
// size is unknown. This is a vector (rather than a Key->Value map) since:
@@ -131,31 +134,31 @@ struct InfoTreeNode {
131
134
// * The same key can appear multiple times
132
135
std::unique_ptr<llvm::SmallVector<InfoTreeNode, 8 >> Children;
133
136
134
- InfoTreeNode () : InfoTreeNode(" " , " " , " " ) {}
135
- InfoTreeNode (std::string Key, std::string Value, std::string Units)
137
+ InfoTreeNode () : InfoTreeNode(" " , None{} , " " ) {}
138
+ InfoTreeNode (std::string Key, VariantType Value, std::string Units)
136
139
: Key(Key), Value(Value), Units(Units) {}
137
140
138
141
// / Add a new info entry as a child of this node. The entry requires at least
139
142
// / a key string in \p Key. The value in \p Value is optional and can be any
140
143
// / type that is representable as a string. The units in \p Units is optional
141
144
// / and must be a string.
142
- template <typename T = std::string >
145
+ template <typename T = None >
143
146
InfoTreeNode *add (std::string Key, T Value = T(),
144
147
const std::string &Units = std::string()) {
145
148
assert (!Key.empty () && " Invalid info key" );
146
149
147
150
if (!Children)
148
151
Children = std::make_unique<llvm::SmallVector<InfoTreeNode, 8 >>();
149
152
150
- std::string ValueStr ;
151
- if constexpr (std::is_same_v<T, bool >)
152
- ValueStr = Value ? " Yes " : " No " ;
153
+ VariantType ValueVariant ;
154
+ if constexpr (std::is_same_v<T, bool > || std::is_same_v<T, None> )
155
+ ValueVariant = Value;
153
156
else if constexpr (std::is_arithmetic_v<T>)
154
- ValueStr = std::to_string (Value);
157
+ ValueVariant = static_cast < uint64_t > (Value);
155
158
else
156
- ValueStr = Value;
159
+ ValueVariant = std::string{ Value} ;
157
160
158
- return &Children->emplace_back (Key, ValueStr , Units);
161
+ return &Children->emplace_back (Key, ValueVariant , Units);
159
162
}
160
163
161
164
std::optional<InfoTreeNode *> get (StringRef Key) {
@@ -184,8 +187,23 @@ struct InfoTreeNode {
184
187
MaxKeySize - (Key.size () + KeyIndentSize) + IndentSize;
185
188
186
189
llvm::outs () << std::string (KeyIndentSize, ' ' ) << Key
187
- << std::string (ValIndentSize, ' ' ) << Value
188
- << (Units.empty () ? " " : " " ) << Units << " \n " ;
190
+ << std::string (ValIndentSize, ' ' );
191
+ std::visit (
192
+ [](auto &&V) {
193
+ using T = std::decay_t <decltype (V)>;
194
+ if constexpr (std::is_same_v<T, std::string>)
195
+ llvm::outs () << V;
196
+ else if constexpr (std::is_same_v<T, bool >)
197
+ llvm::outs () << (V ? " Yes" : " No" );
198
+ else if constexpr (std::is_same_v<T, uint64_t >)
199
+ llvm::outs () << V;
200
+ else if constexpr (std::is_same_v<T, None>) {
201
+ // Do nothing
202
+ } else
203
+ static_assert (false , " doPrint visit not exhaustive" );
204
+ },
205
+ Value);
206
+ llvm::outs () << (Units.empty () ? " " : " " ) << Units << " \n " ;
189
207
}
190
208
191
209
// Print children
0 commit comments