Skip to content

Commit 0ec5e8a

Browse files
committed
Don't centralize tgt indexes
1 parent 8e66096 commit 0ec5e8a

3 files changed

Lines changed: 13 additions & 31 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -306,20 +306,8 @@ add_custom_command(
306306

307307
add_custom_target("index-cpp-rules" ALL DEPENDS ${CMAKE_BINARY_DIR}/rules_index/c.json ${CMAKE_BINARY_DIR}/rules_index/cpp.json)
308308

309-
set(rust_rules_index_outputs ${CMAKE_BINARY_DIR}/rules_index/index_unsafe.json ${CMAKE_BINARY_DIR}/rules_index/index_refcount.json)
310-
add_custom_command(
311-
OUTPUT ${rust_rules_index_outputs}
312-
COMMAND ${CARGO_ENV_RULE_PREPROCESSOR}
313-
RUSTUP_TOOLCHAIN=${RUST_NIGHTLY_VERSION}
314-
LD_LIBRARY_PATH=${NIGHTLY_SYSROOT}/lib
315-
DYLD_LIBRARY_PATH=${NIGHTLY_SYSROOT}/lib
316-
${PREPROCESSOR_BIN} --merge ${CMAKE_BINARY_DIR}/rules_index ${RULES_IR_DIR}
317-
DEPENDS ${rust_rules_ir_outputs} ${PREPROCESSOR_BIN}
318-
VERBATIM
319-
)
320-
321309
add_custom_target("preprocess-rust-rules" ALL
322-
DEPENDS ${rust_rules_ir_outputs} ${rust_rules_index_outputs})
310+
DEPENDS ${rust_rules_ir_outputs})
323311

324312
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_CLEAN_FILES
325313
"${CMAKE_BINARY_DIR}/libcc2rs-target"

‎cpp2rust/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ target_compile_definitions(cpp2rust_core PUBLIC "-DCLANG_RESOURCE_DIR=\"${LLVM_L
2020
target_compile_definitions(cpp2rust_core PUBLIC "-DCOMPAT_INCLUDE_DIR=\"${CMAKE_CURRENT_SOURCE_DIR}/compat\"")
2121
target_compile_definitions(cpp2rust_core PUBLIC "-DRULES_SRC_DIR=\"${PROJECT_SOURCE_DIR}/rules\"")
2222
target_compile_definitions(cpp2rust_core PUBLIC "-DRULES_INDEX_DIR=\"${CMAKE_BINARY_DIR}/rules_index\"")
23+
target_compile_definitions(cpp2rust_core PUBLIC "-DRULES_IR_DIR=\"${CMAKE_BINARY_DIR}/rules\"")
2324
target_compile_definitions(cpp2rust_core PUBLIC "-DRUST_STABLE_VERSION=\"${RUST_STABLE_VERSION}\"")
2425

2526
if (APPLE)

‎cpp2rust/converter/rules/registry.cpp‎

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,16 @@ std::unordered_multimap<RuleDecls::ExprKey, ExprRuleDecl,
3737
exprs_;
3838
std::unordered_multimap<const void *, TypeRuleDecl> types_;
3939

40-
const llvm::json::Object &unsafeIndex() {
41-
static const llvm::json::Object index = RulesLoader::ReadIndex(
42-
std::filesystem::path(RULES_INDEX_DIR) / "index_unsafe.json");
43-
return index;
44-
}
45-
46-
const llvm::json::Object &refcountIndex() {
47-
static const llvm::json::Object index = RulesLoader::ReadIndex(
48-
std::filesystem::path(RULES_INDEX_DIR) / "index_refcount.json");
49-
return index;
50-
}
51-
52-
const llvm::json::Object *findRule(const llvm::json::Object &index,
53-
const std::string &dir,
40+
const llvm::json::Object *findRule(Model model, const std::string &dir,
5441
const std::string &name) {
55-
auto rules = index.getObject(dir);
56-
return rules ? rules->getObject(name) : nullptr;
42+
static std::unordered_map<std::string, llvm::json::Object> indexes;
43+
auto path = std::filesystem::path(RULES_IR_DIR) / dir /
44+
(model == Model::kRefCount ? "ir_refcount.json" : "ir_unsafe.json");
45+
auto [it, inserted] = indexes.try_emplace(path.string());
46+
if (inserted && std::filesystem::exists(path)) {
47+
it->second = RulesLoader::ReadIndex(path);
48+
}
49+
return it->second.getObject(name);
5750
}
5851

5952
bool pointsToUserType(clang::QualType type) {
@@ -114,9 +107,9 @@ void checkGenerics(const std::string &dir, const std::string &name,
114107

115108
void addRule(clang::ASTContext &ctx, const std::string &dir,
116109
const std::string &name, const clang::NamedDecl *decl) {
117-
auto rule = findRule(unsafeIndex(), dir, name);
110+
auto rule = findRule(Model::kUnsafe, dir, name);
118111
if (model_ == Model::kRefCount) {
119-
if (auto refcount_rule = findRule(refcountIndex(), dir, name)) {
112+
if (auto refcount_rule = findRule(model_, dir, name)) {
120113
rule = refcount_rule;
121114
}
122115
}

0 commit comments

Comments
 (0)