Skip to content

Commit 5625910

Browse files
committed
edits
1 parent 8220bff commit 5625910

2 files changed

Lines changed: 18 additions & 22 deletions

File tree

cpp2rust/converter/mapper.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ void addRulesFromDirectory(const std::filesystem::path &dir, Model model) {
436436
}
437437
} else if (auto *type =
438438
std::get_if<TranslationRule::TypeRule>(&rule.tgt)) {
439-
types_.emplace(GetMapKey(type->src), std::move(*type));
439+
types_.emplace(GetMapKey(rule.src), std::move(*type));
440440
}
441441
}
442442
}
@@ -648,17 +648,13 @@ std::string Map(clang::QualType qual_type) {
648648
}
649649

650650
bool MapsToPointer(clang::QualType qual_type) {
651-
if (auto rule = search(qual_type)) {
652-
return rule->type_info.is_pointer();
653-
}
654-
return false;
651+
auto rule = search(qual_type);
652+
return rule && rule->type_info.is_pointer();
655653
}
656654

657655
bool MapsToRefcountPointer(clang::QualType qual_type) {
658-
if (auto rule = search(qual_type)) {
659-
return rule->type_info.is_refcount_pointer;
660-
}
661-
return false;
656+
auto rule = search(qual_type);
657+
return rule && rule->type_info.is_refcount_pointer;
662658
}
663659

664660
bool ReturnsPointer(const clang::Expr *expr) {

cpp2rust/converter/translation_rule.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,15 @@ class Callback : public clang::ast_matchers::MatchFinder::MatchCallback {
9797
} else {
9898
type = var->getUnderlyingType();
9999
}
100-
out_.at(var->getQualifiedNameAsString()).src = Mapper::ToString(type);
100+
auto &rule = out_.at(var->getQualifiedNameAsString());
101+
rule.src = Mapper::ToString(type);
102+
get<TypeRule>(rule.tgt).src = rule.src;
101103
return;
102104
}
103105

104106
if (auto func = R.Nodes.getNodeAs<clang::FunctionDecl>("func")) {
105107
auto sym = func->getQualifiedNameAsString();
106-
auto add = [&](std::string src) { out_.at(sym).src = std::move(src); };
108+
auto add = [&](std::string &&src) { out_.at(sym).src = std::move(src); };
107109

108110
if (const auto *fcall = R.Nodes.getNodeAs<clang::CallExpr>("fcall")) {
109111
if (fcall->getDirectCallee()) {
@@ -837,10 +839,8 @@ ExprTgt ParseExprTgtJSON(const llvm::json::Object &obj) {
837839
return ir;
838840
}
839841

840-
TypeRule ParseTypeRuleJSON(const std::string &src,
841-
const llvm::json::Object &obj) {
842+
TypeRule ParseTypeRuleJSON(const llvm::json::Object &obj) {
842843
TypeRule rule;
843-
rule.src = src;
844844
if (auto init = obj.getString("init"))
845845
rule.initializer = init->str();
846846
rule.type_info = ParseTypeInfoJSON(obj);
@@ -883,7 +883,7 @@ void LoadTgtFromIR(RuleMap &rules, const std::filesystem::path &json_path) {
883883
rule.tgt = ParseExprTgtJSON(*obj);
884884
std::get<ExprTgt>(rule.tgt).validate(json_path.string() + ':' + name);
885885
} else if (name[0] == 't') {
886-
rule.tgt = ParseTypeRuleJSON(name, *obj);
886+
rule.tgt = ParseTypeRuleJSON(*obj);
887887
} else {
888888
continue;
889889
}
@@ -1024,15 +1024,15 @@ void ExprTgt::validate(const std::string &context) const {
10241024
}
10251025

10261026
RuleMap Load(const std::filesystem::path &path, Model model) {
1027-
auto dir = path.parent_path();
1028-
auto unsafe_ir_path = dir / "ir_unsafe.json";
1029-
auto refcount_ir_path = dir / "ir_refcount.json";
1030-
10311027
RuleMap rules;
1032-
LoadTgtFromIR(rules, unsafe_ir_path);
1028+
auto dir = path.parent_path();
1029+
LoadTgtFromIR(rules, dir / "ir_unsafe.json");
10331030

1034-
if (model == Model::kRefCount && std::filesystem::exists(refcount_ir_path)) {
1035-
LoadTgtFromIR(rules, refcount_ir_path);
1031+
if (model == Model::kRefCount) {
1032+
auto refcount_ir_path = dir / "ir_refcount.json";
1033+
if (std::filesystem::exists(refcount_ir_path)) {
1034+
LoadTgtFromIR(rules, refcount_ir_path);
1035+
}
10361036
}
10371037

10381038
LoadSrc(rules, path);

0 commit comments

Comments
 (0)