Skip to content

Commit b5eb086

Browse files
committed
Remove namespace wrapping from index
1 parent e715899 commit b5eb086

2 files changed

Lines changed: 18 additions & 13 deletions

File tree

‎cpp2rust/converter/rules/rules_loader.cpp‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include <cassert>
1919
#include <filesystem>
20+
#include <format>
2021
#include <ranges>
2122
#include <string>
2223
#include <unordered_set>
@@ -283,6 +284,18 @@ const llvm::json::Object &CxxIndex() {
283284
return index;
284285
}
285286

287+
std::string WrapRule(const llvm::json::Object &rule, bool is_cxx) {
288+
auto dir = rule.getString("dir")->str();
289+
auto text = rule.getString("text")->str();
290+
if (!is_cxx) {
291+
return std::format("#define {0} cpp2rust_rules_{1}_{0}\n{2}#undef {0}\n",
292+
rule.getString("name")->str(), dir, text);
293+
}
294+
return std::format(
295+
"namespace cpp2rust_rules {{\nnamespace cpp2rust_rules_{} {{\n{}}}\n}}\n",
296+
dir, text);
297+
}
298+
286299
std::string BuildRulesEpilogue(const std::unordered_set<std::string> &keys,
287300
bool is_cxx) {
288301
const auto &index = is_cxx ? CxxIndex() : CIndex();
@@ -306,7 +319,7 @@ std::string BuildRulesEpilogue(const std::unordered_set<std::string> &keys,
306319
})) {
307320
continue;
308321
}
309-
out += *rule->getString("text");
322+
out += WrapRule(*rule, is_cxx);
310323
}
311324
}
312325
return out;

‎cpp2rust/rule_preprocessing/cpp_rule_indexer.cpp‎

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -217,17 +217,6 @@ std::string TypeKey(const clang::TypedefNameDecl *rule) {
217217
return ClassOf(type);
218218
}
219219

220-
std::string WrapRule(const std::string &dir_name, bool is_c,
221-
const std::string &name, const std::string &text) {
222-
if (is_c) {
223-
return std::format("#define {0} cpp2rust_rules_{1}_{0}\n{2}#undef {0}\n",
224-
name, dir_name, text);
225-
}
226-
return std::format(
227-
"namespace cpp2rust_rules {{\nnamespace cpp2rust_rules_{} {{\n{}}}\n}}\n",
228-
dir_name, text);
229-
}
230-
231220
void AddRule(llvm::json::Object &rules, std::string key,
232221
llvm::json::Value rule) {
233222
auto &slot = rules[llvm::json::ObjectKey(std::move(key))];
@@ -272,7 +261,10 @@ void IndexRuleFile(clang::ASTContext &ctx, const std::string &dir_name,
272261
}
273262
auto range = clang::tooling::getExtendedRange(*decl, clang::tok::semi, ctx);
274263
auto text = clang::tooling::getText(range, ctx).str() + '\n';
275-
llvm::json::Object rule{{"text", WrapRule(dir_name, is_c, name, text)}};
264+
llvm::json::Object rule{{"dir", dir_name}, {"text", text}};
265+
if (is_c) {
266+
rule["name"] = name;
267+
}
276268
if (auto required = RequiredClasses(decl, alias); !required.empty()) {
277269
rule["requires"] = llvm::json::Array(required);
278270
}

0 commit comments

Comments
 (0)