Skip to content

Commit 7643fa1

Browse files
committed
Always include common headers
1 parent 4a6e673 commit 7643fa1

2 files changed

Lines changed: 7 additions & 10 deletions

File tree

‎cpp2rust/converter/rules/rules_loader.cpp‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,13 @@ const llvm::json::Object &CxxIndex() {
283283

284284
std::string BuildRulesEpilogue(const std::unordered_set<std::string> &keys,
285285
bool is_cxx) {
286-
std::string out;
287286
const auto &index = is_cxx ? CxxIndex() : CIndex();
288287
auto common = index.getString("common");
288+
std::string out;
289+
if (!common->empty()) {
290+
out += "namespace cpp2rust_rules {\n" + common->str() + "}\n";
291+
}
289292
auto rules = index.getObject("rules");
290-
std::unordered_set<std::string> namespaces;
291293
for (const auto &key : keys) {
292294
auto entries = rules->getArray(key);
293295
if (!entries) {
@@ -302,10 +304,6 @@ std::string BuildRulesEpilogue(const std::unordered_set<std::string> &keys,
302304
})) {
303305
continue;
304306
}
305-
if (auto ns = rule->getString("namespace");
306-
ns && namespaces.insert(ns->str()).second) {
307-
out += "namespace " + ns->str() + " {\n" + common->str() + "}\n";
308-
}
309307
out += *rule->getString("text");
310308
}
311309
}

‎cpp2rust/rule_preprocessing/cpp_rule_indexer.cpp‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,9 @@ std::string WrapRule(const std::string &dir_name, bool is_c,
223223
return std::format("#define {0} cpp2rust_rules_{1}_{0}\n{2}#undef {0}\n",
224224
name, dir_name, text);
225225
}
226-
return std::format("namespace cpp2rust_rules_{} {{\n{}}}\n", dir_name, text);
226+
return std::format(
227+
"namespace cpp2rust_rules {{\nnamespace cpp2rust_rules_{} {{\n{}}}\n}}\n",
228+
dir_name, text);
227229
}
228230

229231
void AddRule(llvm::json::Object &rules, std::string key,
@@ -271,9 +273,6 @@ void IndexRuleFile(clang::ASTContext &ctx, const std::string &dir_name,
271273
auto range = clang::tooling::getExtendedRange(*decl, clang::tok::semi, ctx);
272274
auto text = clang::tooling::getText(range, ctx).str() + '\n';
273275
llvm::json::Object rule{{"text", WrapRule(dir_name, is_c, name, text)}};
274-
if (!is_c) {
275-
rule["namespace"] = "cpp2rust_rules_" + dir_name;
276-
}
277276
if (auto required = RequiredClasses(decl, alias); !required.empty()) {
278277
rule["requires"] = llvm::json::Array(required);
279278
}

0 commit comments

Comments
 (0)