Skip to content

Commit 76f525f

Browse files
committed
codegen: carry decl and func with containerinfo
1 parent 10f4751 commit 76f525f

File tree

11 files changed

+176
-192
lines changed

11 files changed

+176
-192
lines changed

src/Common.h

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,54 @@ extern "C" {
2525

2626
constexpr int oidMagicId = 0x01DE8;
2727

28-
struct ContainerInfo;
28+
#define LIST_OF_CONTAINER_TYPES \
29+
X(UNKNOWN_TYPE) \
30+
X(ARRAY_TYPE) \
31+
X(SMALL_VEC_TYPE) \
32+
X(SET_TYPE) \
33+
X(UNORDERED_SET_TYPE) \
34+
X(SEQ_TYPE) \
35+
X(LIST_TYPE) \
36+
X(STD_MAP_TYPE) \
37+
X(STD_UNORDERED_MAP_TYPE) \
38+
X(MAP_SEQ_TYPE) \
39+
X(BY_MULTI_QRT_TYPE) \
40+
X(F14_MAP) \
41+
X(F14_SET) \
42+
X(FEED_QUICK_HASH_SET) \
43+
X(FEED_QUICK_HASH_MAP) \
44+
X(RADIX_TREE_TYPE) \
45+
X(PAIR_TYPE) \
46+
X(STRING_TYPE) \
47+
X(FOLLY_IOBUF_TYPE) \
48+
X(FOLLY_IOBUFQUEUE_TYPE) \
49+
X(FB_STRING_TYPE) \
50+
X(UNIQ_PTR_TYPE) \
51+
X(SHRD_PTR_TYPE) \
52+
X(FB_HASH_MAP_TYPE) \
53+
X(FB_HASH_SET_TYPE) \
54+
X(FOLLY_OPTIONAL_TYPE) \
55+
X(OPTIONAL_TYPE) \
56+
X(TRY_TYPE) \
57+
X(REF_WRAPPER_TYPE) \
58+
X(SORTED_VEC_SET_TYPE) \
59+
X(REPEATED_FIELD_TYPE) \
60+
X(CAFFE2_BLOB_TYPE) \
61+
X(MULTI_MAP_TYPE) \
62+
X(FOLLY_SMALL_HEAP_VECTOR_MAP) \
63+
X(CONTAINER_ADAPTER_TYPE) \
64+
X(MICROLIST_TYPE) \
65+
X(ENUM_MAP_TYPE) \
66+
X(BOOST_BIMAP_TYPE) \
67+
X(STD_VARIANT_TYPE) \
68+
X(THRIFT_ISSET_TYPE) \
69+
X(WEAK_PTR_TYPE)
70+
71+
enum ContainerTypeEnum {
72+
#define X(name) name,
73+
LIST_OF_CONTAINER_TYPES
74+
#undef X
75+
};
2976

3077
struct RootInfo {
3178
std::string varName;
@@ -47,8 +94,9 @@ struct DrgnClassMemberInfo {
4794

4895
struct TypeHierarchy {
4996
std::map<struct drgn_type*, std::vector<DrgnClassMemberInfo>> classMembersMap;
50-
std::map<struct drgn_type*,
51-
std::pair<ContainerInfo, std::vector<struct drgn_qualified_type>>>
97+
std::map<
98+
struct drgn_type*,
99+
std::pair<ContainerTypeEnum, std::vector<struct drgn_qualified_type>>>
52100
containerTypeMap;
53101
std::map<struct drgn_type*, struct drgn_type*> typedefMap;
54102
std::map<std::string, size_t> sizeMap;

src/ContainerInfo.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
#include <map>
2222

23+
namespace fs = std::filesystem;
24+
2325
ContainerTypeEnum containerTypeEnumFromStr(std::string& str) {
2426
static const std::map<std::string, ContainerTypeEnum> nameMap = {
2527
#define X(name) {#name, name},
@@ -128,6 +130,30 @@ std::unique_ptr<ContainerInfo> ContainerInfo::loadFromFile(
128130
std::optional<size_t> underlyingContainerIndex =
129131
(*info)["underlyingContainerIndex"].value<size_t>();
130132

133+
toml::table* codegen = container["codegen"].as_table();
134+
if (!codegen) {
135+
LOG(ERROR) << "a container info file requires an `codegen` table";
136+
return nullptr;
137+
}
138+
139+
std::string decl;
140+
if (std::optional<std::string> str =
141+
(*codegen)["decl"].value<std::string>()) {
142+
decl = std::move(*str);
143+
} else {
144+
LOG(ERROR) << "`codegen.decl` is a required field";
145+
return nullptr;
146+
}
147+
148+
std::string func;
149+
if (std::optional<std::string> str =
150+
(*codegen)["func"].value<std::string>()) {
151+
func = std::move(*str);
152+
} else {
153+
LOG(ERROR) << "`codegen.func` is a required field";
154+
return nullptr;
155+
}
156+
131157
return std::unique_ptr<ContainerInfo>(new ContainerInfo{
132158
std::move(typeName),
133159
std::move(matcher),
@@ -138,5 +164,10 @@ std::unique_ptr<ContainerInfo> ContainerInfo::loadFromFile(
138164
std::move(replaceTemplateParamIndex),
139165
allocatorIndex,
140166
underlyingContainerIndex,
167+
168+
{
169+
std::move(decl),
170+
std::move(func),
171+
},
141172
});
142173
}

src/ContainerInfo.h

Lines changed: 39 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,63 +17,45 @@
1717
#include <filesystem>
1818
#include <optional>
1919
#include <regex>
20+
#include <set>
2021
#include <string>
2122
#include <vector>
2223

23-
namespace fs = std::filesystem;
24+
#include "Common.h"
2425

25-
#define LIST_OF_CONTAINER_TYPES \
26-
X(UNKNOWN_TYPE) \
27-
X(ARRAY_TYPE) \
28-
X(SMALL_VEC_TYPE) \
29-
X(SET_TYPE) \
30-
X(UNORDERED_SET_TYPE) \
31-
X(SEQ_TYPE) \
32-
X(LIST_TYPE) \
33-
X(STD_MAP_TYPE) \
34-
X(STD_UNORDERED_MAP_TYPE) \
35-
X(MAP_SEQ_TYPE) \
36-
X(BY_MULTI_QRT_TYPE) \
37-
X(F14_MAP) \
38-
X(F14_SET) \
39-
X(FEED_QUICK_HASH_SET) \
40-
X(FEED_QUICK_HASH_MAP) \
41-
X(RADIX_TREE_TYPE) \
42-
X(PAIR_TYPE) \
43-
X(STRING_TYPE) \
44-
X(FOLLY_IOBUF_TYPE) \
45-
X(FOLLY_IOBUFQUEUE_TYPE) \
46-
X(FB_STRING_TYPE) \
47-
X(UNIQ_PTR_TYPE) \
48-
X(SHRD_PTR_TYPE) \
49-
X(FB_HASH_MAP_TYPE) \
50-
X(FB_HASH_SET_TYPE) \
51-
X(FOLLY_OPTIONAL_TYPE) \
52-
X(OPTIONAL_TYPE) \
53-
X(TRY_TYPE) \
54-
X(REF_WRAPPER_TYPE) \
55-
X(SORTED_VEC_SET_TYPE) \
56-
X(REPEATED_FIELD_TYPE) \
57-
X(CAFFE2_BLOB_TYPE) \
58-
X(MULTI_MAP_TYPE) \
59-
X(FOLLY_SMALL_HEAP_VECTOR_MAP) \
60-
X(CONTAINER_ADAPTER_TYPE) \
61-
X(MICROLIST_TYPE) \
62-
X(ENUM_MAP_TYPE) \
63-
X(BOOST_BIMAP_TYPE) \
64-
X(STD_VARIANT_TYPE) \
65-
X(THRIFT_ISSET_TYPE) \
66-
X(WEAK_PTR_TYPE)
67-
68-
enum ContainerTypeEnum {
69-
#define X(name) name,
70-
LIST_OF_CONTAINER_TYPES
71-
#undef X
72-
};
7326
ContainerTypeEnum containerTypeEnumFromStr(std::string& str);
7427
const char* containerTypeEnumToStr(ContainerTypeEnum ty);
7528

7629
struct ContainerInfo {
30+
struct Codegen {
31+
std::string decl;
32+
std::string func;
33+
};
34+
35+
ContainerInfo(const ContainerInfo&) = delete;
36+
ContainerInfo& operator=(const ContainerInfo& other) = delete;
37+
38+
ContainerInfo() = default;
39+
ContainerInfo(std::string typeName_, std::regex matcher_,
40+
std::optional<size_t> numTemplateParams_,
41+
ContainerTypeEnum ctype_, std::string header_,
42+
std::vector<std::string> ns_,
43+
std::vector<size_t> replaceTemplateParamIndex_,
44+
std::optional<size_t> allocatorIndex_,
45+
std::optional<size_t> underlyingContainerIndex_,
46+
ContainerInfo::Codegen codegen_)
47+
: typeName(std::move(typeName_)),
48+
matcher(std::move(matcher_)),
49+
numTemplateParams(numTemplateParams_),
50+
ctype(ctype_),
51+
header(std::move(header_)),
52+
ns(std::move(ns_)),
53+
replaceTemplateParamIndex(std::move(replaceTemplateParamIndex_)),
54+
allocatorIndex(allocatorIndex_),
55+
underlyingContainerIndex(underlyingContainerIndex_),
56+
codegen(std::move(codegen_)) {
57+
}
58+
7759
std::string typeName;
7860
std::regex matcher;
7961
std::optional<size_t> numTemplateParams;
@@ -86,9 +68,16 @@ struct ContainerInfo {
8668
// adapter
8769
std::optional<size_t> underlyingContainerIndex{};
8870

89-
static std::unique_ptr<ContainerInfo> loadFromFile(const fs::path& path);
71+
Codegen codegen;
72+
73+
static std::unique_ptr<ContainerInfo> loadFromFile(
74+
const std::filesystem::path& path);
9075

9176
bool operator<(const ContainerInfo& rhs) const {
9277
return (typeName < rhs.typeName);
9378
}
9479
};
80+
81+
using ContainerInfoRefSet =
82+
std::set<std::reference_wrapper<const ContainerInfo>,
83+
std::less<ContainerInfo>>;

src/FuncGen.cpp

Lines changed: 8 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "FuncGen.h"
1717

1818
#include <glog/logging.h>
19-
#include <toml++/toml.h>
2019

2120
#include <boost/format.hpp>
2221
#include <map>
@@ -322,27 +321,14 @@ void FuncGen::DefineTopLevelGetSizeSmartPtr(std::string& testCode,
322321
}
323322

324323
bool FuncGen::DeclareGetSizeFuncs(std::string& testCode,
325-
const std::set<ContainerInfo>& containerInfo,
324+
const ContainerInfoRefSet& containerInfo,
326325
bool chaseRawPointers) {
327-
for (auto& cInfo : containerInfo) {
326+
for (const ContainerInfo& cInfo : containerInfo) {
328327
std::string ctype = cInfo.typeName;
329328
ctype = ctype.substr(0, ctype.find("<", 0));
330329

331-
if (!typeToFuncMap.contains(cInfo.ctype)) {
332-
LOG(ERROR) << "attempted to use container `"
333-
<< containerTypeEnumToStr(cInfo.ctype)
334-
<< "` for which a declaration was not provided";
335-
return false;
336-
}
337-
338-
auto& func = typeToDeclMap[cInfo.ctype];
339-
boost::format fmt;
340-
fmt = boost::format(func) % ctype;
341-
/*if (cInfo.ctype == STRING_TYPE) {
342-
fmt = boost::format(func);
343-
} else {
344-
fmt = boost::format(func) % ctype;
345-
}*/
330+
auto& decl = cInfo.codegen.decl;
331+
boost::format fmt = boost::format(decl) % ctype;
346332
testCode.append(fmt.str());
347333
}
348334

@@ -359,28 +345,14 @@ bool FuncGen::DeclareGetSizeFuncs(std::string& testCode,
359345
}
360346

361347
bool FuncGen::DefineGetSizeFuncs(std::string& testCode,
362-
const std::set<ContainerInfo>& containerInfo,
348+
const ContainerInfoRefSet& containerInfo,
363349
bool chaseRawPointers) {
364-
for (auto& cInfo : containerInfo) {
350+
for (const ContainerInfo& cInfo : containerInfo) {
365351
std::string ctype = cInfo.typeName;
366352
ctype = ctype.substr(0, ctype.find("<", 0));
367353

368-
if (!typeToFuncMap.contains(cInfo.ctype)) {
369-
LOG(ERROR) << "attempted to use container `"
370-
<< containerTypeEnumToStr(cInfo.ctype)
371-
<< "` for which a definition was not provided";
372-
return false;
373-
}
374-
auto& func = typeToFuncMap[cInfo.ctype];
375-
376-
boost::format fmt;
377-
fmt = boost::format(func) % ctype;
378-
/*if (cInfo.ctype == STRING_TYPE) {
379-
fmt = boost::format(func);
380-
} else {
381-
fmt = boost::format(func) % ctype;
382-
}*/
383-
354+
auto& func = cInfo.codegen.func;
355+
boost::format fmt = boost::format(func) % ctype;
384356
testCode.append(fmt.str());
385357
}
386358

@@ -422,38 +394,3 @@ void FuncGen::DeclareGetContainer(std::string& testCode) {
422394
)";
423395
testCode.append(func);
424396
}
425-
426-
bool FuncGen::RegisterContainer(ContainerTypeEnum ctype, const fs::path& path) {
427-
toml::table container;
428-
try {
429-
container = toml::parse_file(std::string(path));
430-
} catch (const toml::parse_error& ex) {
431-
LOG(ERROR) << "FuncGen::RegisterContainer: " << path << " : "
432-
<< ex.description();
433-
return false;
434-
}
435-
436-
toml::table* codegen = container["codegen"].as_table();
437-
if (!codegen) {
438-
LOG(ERROR) << "a container info file requires an `codegen` table";
439-
return false;
440-
}
441-
442-
if (std::optional<std::string> str =
443-
(*codegen)["decl"].value<std::string>()) {
444-
typeToDeclMap.emplace(ctype, std::move(*str));
445-
} else {
446-
LOG(ERROR) << "`codegen.decl` is a required field";
447-
return false;
448-
}
449-
450-
if (std::optional<std::string> str =
451-
(*codegen)["func"].value<std::string>()) {
452-
typeToFuncMap.emplace(ctype, std::move(*str));
453-
} else {
454-
LOG(ERROR) << "`codegen.func` is a required field";
455-
return false;
456-
}
457-
458-
return true;
459-
}

src/FuncGen.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ namespace fs = std::filesystem;
2525

2626
class FuncGen {
2727
public:
28-
bool RegisterContainer(ContainerTypeEnum, const fs::path& path);
29-
3028
static void DeclareStoreData(std::string& testCode);
3129
static void DefineStoreData(std::string& testCode);
3230

@@ -40,10 +38,10 @@ class FuncGen {
4038
static void DefineEncodeDataSize(std::string& testCode);
4139

4240
bool DeclareGetSizeFuncs(std::string& testCode,
43-
const std::set<ContainerInfo>& containerInfo,
41+
const ContainerInfoRefSet& containerInfo,
4442
bool chaseRawPointers);
4543
bool DefineGetSizeFuncs(std::string& testCode,
46-
const std::set<ContainerInfo>& containerInfo,
44+
const ContainerInfoRefSet& containerInfo,
4745
bool chaseRawPointers);
4846

4947
static void DeclareGetContainer(std::string& testCode);
@@ -67,8 +65,4 @@ class FuncGen {
6765

6866
static void DefineGetSizeTypedValueFunc(std::string& testCode,
6967
const std::string& ctype);
70-
71-
private:
72-
std::map<ContainerTypeEnum, std::string> typeToDeclMap;
73-
std::map<ContainerTypeEnum, std::string> typeToFuncMap;
7468
};

0 commit comments

Comments
 (0)