2222#include < filesystem>
2323#include < format>
2424#include < fstream>
25- #include < memory>
2625#include < string>
2726#include < unordered_set>
2827#include < vector>
@@ -34,11 +33,6 @@ namespace fs = std::filesystem;
3433
3534namespace cpp2rust {
3635
37- struct RuleCtx {
38- fs::path path;
39- std::string name;
40- };
41-
4236std::string ClassOf (clang::QualType type) {
4337 while (type->isPointerType () || type->isReferenceType ()) {
4438 type = type->getPointeeType ();
@@ -220,13 +214,13 @@ std::string TypeKey(const clang::TypedefNameDecl *rule) {
220214 return ClassOf (type);
221215}
222216
223- std::string CreateIncFile (const RuleCtx &dir , bool is_c,
224- const std::string &name, const std::string &text) {
217+ std::string WrapRule (const std::string &dir_name , bool is_c,
218+ const std::string &name, const std::string &text) {
225219 if (is_c) {
226220 return std::format (" #define {0} cpp2rust_rules_{1}_{0}\n {2}#undef {0}\n " ,
227- name, dir. name , text);
221+ name, dir_name , text);
228222 }
229- return std::format (" namespace cpp2rust_rules_{} {{\n {}}}\n " , dir. name , text);
223+ return std::format (" namespace cpp2rust_rules_{} {{\n {}}}\n " , dir_name , text);
230224}
231225
232226void AddRule (llvm::json::Object &rules, std::string key,
@@ -238,7 +232,7 @@ void AddRule(llvm::json::Object &rules, std::string key,
238232 slot.getAsArray ()->push_back (std::move (rule));
239233}
240234
241- void IndexRuleFile (clang::ASTContext &ctx, const RuleCtx &dir ,
235+ void IndexRuleFile (clang::ASTContext &ctx, const std::string &dir_name ,
242236 const fs::path &src_path, llvm::json::Object &rules) {
243237 bool is_c = !ctx.getLangOpts ().CPlusPlus ;
244238 auto &sm = ctx.getSourceManager ();
@@ -255,7 +249,7 @@ void IndexRuleFile(clang::ASTContext &ctx, const RuleCtx &dir,
255249 if (!(number.consume_front (" f" ) || number.consume_front (" t" )) ||
256250 number.empty () || !llvm::all_of (number, llvm::isDigit)) {
257251 llvm::errs () << " ERROR: declaration '" << name << " ' in rule dir "
258- << dir. path . string () << " is not a rule\n " ;
252+ << dir_name << " is not a rule\n " ;
259253 std::exit (EXIT_FAILURE );
260254 }
261255 auto alias = llvm::dyn_cast<clang::TypedefNameDecl>(decl);
@@ -268,16 +262,16 @@ void IndexRuleFile(clang::ASTContext &ctx, const RuleCtx &dir,
268262 }
269263 if (key.empty ()) {
270264 llvm::errs () << " ERROR: cannot derive the key of rule '" << name
271- << " ' in rule dir " << dir. path . string () << ' \n ' ;
265+ << " ' in rule dir " << dir_name << ' \n ' ;
272266 std::exit (EXIT_FAILURE );
273267 }
274268 auto range = clang::tooling::getExtendedRange (*decl, clang::tok::semi, ctx);
275269 auto text = std::format (
276270 " #line {} \" {}\"\n {}\n " , sm.getSpellingLineNumber (range.getBegin ()),
277271 src_path.string (), clang::tooling::getText (range, ctx).str ());
278- llvm::json::Object rule{{" text" , CreateIncFile (dir , is_c, name, text)}};
272+ llvm::json::Object rule{{" text" , WrapRule (dir_name , is_c, name, text)}};
279273 if (!is_c) {
280- rule[" namespace" ] = " cpp2rust_rules_" + dir. name ;
274+ rule[" namespace" ] = " cpp2rust_rules_" + dir_name ;
281275 }
282276 if (auto required = RequiredClasses (decl, alias); !required.empty ()) {
283277 rule[" requires" ] = llvm::json::Array (required);
@@ -286,7 +280,7 @@ void IndexRuleFile(clang::ASTContext &ctx, const RuleCtx &dir,
286280 }
287281}
288282
289- void Index (const fs::path &src_path, bool is_c, const RuleCtx &dir ,
283+ void Index (const fs::path &src_path, bool is_c, const std::string &dir_name ,
290284 const std::vector<std::string> &cxx_flags,
291285 llvm::json::Object &rules) {
292286 auto flags = getPlatformClangBeginFlags ();
@@ -309,7 +303,7 @@ void Index(const fs::path &src_path, bool is_c, const RuleCtx &dir,
309303 llvm::errs () << " ERROR: cannot parse " << src_path.string () << ' \n ' ;
310304 std::exit (EXIT_FAILURE );
311305 }
312- IndexRuleFile (ast->getASTContext (), dir , src_path, rules);
306+ IndexRuleFile (ast->getASTContext (), dir_name , src_path, rules);
313307}
314308
315309void WriteJson (const fs::path &path, llvm::json::Object object) {
@@ -363,10 +357,8 @@ void IndexRules(const fs::path &rules_dir, const fs::path &index_dir,
363357 }
364358 llvm::errs () << " Indexing " << src_path.string () << " as " << lang
365359 << ' \n ' ;
366- RuleCtx dir;
367- dir.path = rule_dir.filename ();
368- dir.name = rule_dir.filename ().string ();
369- Index (src_path, is_c, dir, ReadCXXFlags (rule_dir), rules);
360+ Index (src_path, is_c, rule_dir.filename ().string (),
361+ ReadCXXFlags (rule_dir), rules);
370362 }
371363 }
372364 WriteJson (index_dir / (std::string (lang) + " .json" ),
0 commit comments