Skip to content

Commit 4c6f232

Browse files
JakeHillionfacebook-github-bot
authored andcommitted
containers: add required features (#374)
Summary: Adds the option for required features to container definitions. These cause the container not to be passed to `DrgnParser` if that feature is not enabled during code generation. The thrift isset type does not currently work with `tree-builder-v2` and only provides benefit with `capture-thrift-isset`. This change makes sure the container is ignored if it won't be useful, allowing code generation under `tree-builder-v2`. Test Plan: - CI Differential Revision: D49960512 Pulled By: JakeHillion
1 parent 593a141 commit 4c6f232

File tree

8 files changed

+46
-2
lines changed

8 files changed

+46
-2
lines changed

oi/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,20 @@ target_link_libraries(symbol_service
2626
dw
2727
)
2828

29+
add_library(features Features.cpp)
30+
target_link_libraries(features glog::glog)
31+
2932
add_library(container_info
3033
ContainerInfo.cpp
3134
)
3235
target_link_libraries(container_info
36+
features
3337
glog::glog
3438
toml
3539
)
3640

3741
add_library(codegen
3842
CodeGen.cpp
39-
Features.cpp
4043
FuncGen.cpp
4144
OICodeGen.cpp
4245
)

oi/CodeGen.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,10 @@ bool CodeGen::codegenFromDrgn(struct drgn_type* drgnType, std::string& code) {
11191119

11201120
void CodeGen::registerContainer(const fs::path& path) {
11211121
auto info = std::make_unique<ContainerInfo>(path);
1122+
if (info->requiredFeatures != (config_.features & info->requiredFeatures)) {
1123+
VLOG(1) << "Skipping container (feature conflict): " << info->typeName;
1124+
return;
1125+
}
11221126
VLOG(1) << "Registered container: " << info->typeName;
11231127
containerInfos_.emplace_back(std::move(info));
11241128
}

oi/ContainerInfo.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,19 @@ ContainerInfo::ContainerInfo(const fs::path& path) {
242242

243243
underlyingContainerIndex = info["underlying_container_index"].value<size_t>();
244244

245+
if (toml::array* arr = info["required_features"].as_array()) {
246+
arr->for_each([&](auto&& el) {
247+
if constexpr (toml::is_string<decltype(el)>) {
248+
oi::detail::Feature f = oi::detail::featureFromStr(*el);
249+
if (f == oi::detail::Feature::UnknownFeature) {
250+
LOG(WARNING) << "unknown feature in container config: " << el;
251+
return;
252+
}
253+
requiredFeatures[f] = true;
254+
}
255+
});
256+
}
257+
245258
if (!container["codegen"].is_table()) {
246259
throw ContainerInfoError(
247260
path, "a container info file requires a `codegen` table");

oi/ContainerInfo.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <vector>
2323

2424
#include "oi/ContainerTypeEnum.h"
25+
#include "oi/Features.h"
2526

2627
ContainerTypeEnum containerTypeEnumFromStr(std::string& str);
2728
const char* containerTypeEnumToStr(ContainerTypeEnum ty);
@@ -94,6 +95,7 @@ struct ContainerInfo {
9495
std::optional<size_t> underlyingContainerIndex{};
9596
std::vector<size_t> stubTemplateParams{};
9697
bool captureKeys = false;
98+
oi::detail::FeatureSet requiredFeatures;
9799

98100
Codegen codegen;
99101

oi/EnumBitset.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,26 @@ class EnumBitset {
4747
return bitset.none();
4848
}
4949

50+
bool operator==(const EnumBitset<T, N>& that) const {
51+
return bitset == that.bitset;
52+
}
5053
EnumBitset<T, N>& operator|=(const EnumBitset<T, N>& that) {
5154
bitset |= that.bitset;
5255
return *this;
5356
}
57+
EnumBitset<T, N>& operator&=(const EnumBitset<T, N>& that) {
58+
bitset &= that.bitset;
59+
return *this;
60+
}
5461

5562
private:
5663
BitsetType bitset;
5764
};
65+
66+
template <typename T, size_t N>
67+
EnumBitset<T, N> operator&(const EnumBitset<T, N>& lhs,
68+
const EnumBitset<T, N>& rhs) {
69+
auto out = lhs;
70+
out &= rhs;
71+
return out;
72+
}

test/integration/thrift_isset.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,6 @@ namespace cpp2 {
213213
]}]'''
214214

215215
[cases.no_capture]
216-
oil_skip = 'oil does not support thrift isset yet' # https://github.com/facebookexperimental/object-introspection/issues/296
217216
param_types = ["const cpp2::MyThriftStructBoxed&"]
218217
setup = '''
219218
cpp2::MyThriftStructBoxed ret;

types/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ This document describes the format of the container definition files contained i
2222
Only used for container adapters. Points OI to the template parameter
2323
representing the underlying container to be measured.
2424

25+
- `required_features`
26+
27+
A set of feature names such as `tree-builder-v2` which must be enabled for
28+
this container description to be included. Currently only supported with
29+
CodeGen v2 as that's their only use case and an implementation for CodeGen v1
30+
would be untested.
31+
2532
### codegen
2633
- `decl`
2734

types/thrift_isset_type.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
type_name = "apache::thrift::detail::isset_bitset"
33
ctype = "THRIFT_ISSET_TYPE"
44
header = "thrift/lib/cpp2/gen/module_types_h.h"
5+
required_features = ["capture-thrift-isset"]
56

67
# Old:
78
typeName = "apache::thrift::detail::isset_bitset<"

0 commit comments

Comments
 (0)