Skip to content

Commit cfe44a5

Browse files
Copilotnunoplopes
andauthored
Revert Map to return std::string; empty string signals pattern not found
Agent-Logs-Url: https://github.com/Cpp2Rust/cpp2rust/sessions/763ac366-fc96-4bcf-80e8-94f1db4602e2 Co-authored-by: nunoplopes <2998477+nunoplopes@users.noreply.github.com>
1 parent 73d9cc6 commit cfe44a5

5 files changed

Lines changed: 21 additions & 19 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ bool Converter::Convert(clang::QualType qual_type) {
6565
return false;
6666
}
6767

68-
if (auto mapped = Mapper::Map(qual_type);
69-
mapped && *mapped != ignore_rule_type_) {
70-
StrCat(*mapped);
68+
if (Mapper::Contains(qual_type) &&
69+
Mapper::Map(qual_type) != ignore_rule_type_) {
70+
StrCat(Mapper::Map(qual_type));
7171
return false;
7272
}
7373

@@ -76,7 +76,7 @@ bool Converter::Convert(clang::QualType qual_type) {
7676
}
7777

7878
bool Converter::ConvertMappedType(clang::QualType qual_type) {
79-
std::string type_as_string = Mapper::Map(qual_type).value_or(std::string{});
79+
std::string type_as_string = Mapper::Map(qual_type);
8080
if (type_as_string == ignore_rule_type_) {
8181
return false;
8282
}
@@ -551,7 +551,7 @@ static bool recordDerivesCopy(const clang::RecordDecl *decl) {
551551
for (auto f : decl->fields()) {
552552
// Records that contain std::vector, std::array, std::string or anything
553553
// that is translated to Vec<>, do not derive Copy
554-
auto mapped = Mapper::Map(f->getType()).value_or(std::string{});
554+
auto mapped = Mapper::Map(f->getType());
555555
if (mapped.starts_with("Vec<")) {
556556
return false;
557557
}
@@ -1105,7 +1105,7 @@ bool Converter::VisitCXXForRangeStmtMap(clang::CXXForRangeStmt *stmt) {
11051105
auto loop_var_name = GetNamedDeclAsString(loop_var);
11061106

11071107
StrCat("'loop_:");
1108-
auto map_type = Mapper::Map(stmt->getRangeInit()->getType()).value_or(std::string{});
1108+
auto map_type = Mapper::Map(stmt->getRangeInit()->getType());
11091109
StrCat(keyword::kFor, loop_var_name, keyword::kIn,
11101110
"UnsafeMapIterator::begin(&");
11111111
Convert(stmt->getRangeInit());
@@ -1234,7 +1234,7 @@ bool Converter::GetFmtArg(clang::Expr *arg, std::string &fmt,
12341234
fmt_width.erase(0, fmt_width.find_first_not_of(' '));
12351235
fmt_width.erase(fmt_width.find_last_not_of(' ') + 1);
12361236
} else if (!arg->getType()->isCharType() &&
1237-
Mapper::Map(arg->getType()).value_or(std::string{}) != "Vec<u8>") {
1237+
Mapper::Map(arg->getType()) != "Vec<u8>") {
12381238
fmt += ("{:" + fmt_width + fmt_trait + "}");
12391239
fmt_width.clear(); // Reset setw after first usage
12401240
arg_str = ToString(arg);
@@ -1251,7 +1251,7 @@ bool Converter::GetFmtArg(clang::Expr *arg, std::string &fmt,
12511251
bool Converter::GetRawArg(clang::Expr *arg, std::string &raw_args) {
12521252
if (arg->getType()->isCharType()) {
12531253
raw_args += "(&[" + ToString(arg) + "]";
1254-
} else if (Mapper::Map(arg->getType()).value_or(std::string{}) == "Vec<u8>") {
1254+
} else if (Mapper::Map(arg->getType()) == "Vec<u8>") {
12551255
PushExprKind push(*this, ExprKind::RValue);
12561256
std::string str = ToString(arg);
12571257
raw_args += "(&(" + str + ")[..(" + str + ").len() - 1]";
@@ -1630,7 +1630,7 @@ std::string Converter::getIntegerLiteral(clang::IntegerLiteral *expr,
16301630
}
16311631

16321632
bool Converter::VisitIntegerLiteral(clang::IntegerLiteral *expr) {
1633-
StrCat(getIntegerLiteral(expr, Mapper::Map(expr->getType()).value_or(std::string{}) != "i32"));
1633+
StrCat(getIntegerLiteral(expr, Mapper::Map(expr->getType()) != "i32"));
16341634
computed_expr_type_ = ComputedExprType::FreshValue;
16351635
return false;
16361636
}
@@ -2672,7 +2672,7 @@ bool Converter::VisitEnumDecl(clang::EnumDecl *decl) {
26722672
}
26732673
Mapper::AddRuleForUserDefinedType(decl);
26742674
StrCat("#[derive(Clone, Copy, PartialEq, Debug, Default)]");
2675-
StrCat(std::format("enum {}", Mapper::Map(ctx_.getCanonicalTagType(decl)).value_or(std::string{})));
2675+
StrCat(std::format("enum {}", Mapper::Map(ctx_.getCanonicalTagType(decl))));
26762676
StrCat("{");
26772677
bool first_enumerator = true;
26782678
for (auto e : decl->enumerators()) {

cpp2rust/converter/converter_lib.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -567,15 +567,18 @@ bool HasReceiver(clang::Expr *expr) {
567567
std::optional<IteratorCategory>
568568
GetStrongestIteratorCategory(clang::QualType type) {
569569
type = type.getNonReferenceType().getUnqualifiedType();
570-
auto mapped = Mapper::Map(type);
571-
if (!mapped) {
570+
if (!Mapper::Contains(type)) {
572571
return std::nullopt;
573572
}
574573
if (Mapper::MapsToRefcountPointer(type)) {
575574
return IteratorCategory::Contiguous;
576575
}
577-
if (mapped->starts_with("RefcountMapIter<") ||
578-
mapped->starts_with("UnsafeMapIterator<")) {
576+
auto mapped = Mapper::Map(type);
577+
if (mapped.empty()) {
578+
return std::nullopt;
579+
}
580+
if (mapped.starts_with("RefcountMapIter<") ||
581+
mapped.starts_with("UnsafeMapIterator<")) {
579582
return IteratorCategory::Bidirectional;
580583
}
581584
return std::nullopt;

cpp2rust/converter/mapper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,15 +612,15 @@ std::string InstantiateTemplate(const clang::Expr *expr,
612612
return instantiateTgt(types_map, text);
613613
}
614614

615-
std::optional<std::string> Map(clang::QualType qual_type) {
615+
std::string Map(clang::QualType qual_type) {
616616
if (auto it = search(qual_type); it != types_.end()) {
617617
auto types_map = matchTemplate(it->first, ToString(qual_type)).value();
618618
for (auto &kv : types_map) {
619619
kv.second = mapTypeStringRecursive(kv.second);
620620
}
621621
return instantiateTgt(types_map, it->second.type_info.type);
622622
}
623-
return std::nullopt;
623+
return {};
624624
}
625625

626626
bool MapsToPointer(clang::QualType qual_type) {

cpp2rust/converter/mapper.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <clang/AST/Type.h>
99

1010
#include <filesystem>
11-
#include <optional>
1211
#include <string>
1312
#include <unordered_map>
1413

@@ -30,7 +29,7 @@ class PushASTContext {
3029
bool Contains(clang::QualType qual_type);
3130
bool Contains(const clang::Expr *expr);
3231

33-
std::optional<std::string> Map(clang::QualType qual_type);
32+
std::string Map(clang::QualType qual_type);
3433
const TranslationRule::ExprTgt *GetExprTgt(const clang::Expr *expr);
3534
std::string MapFunctionName(const clang::FunctionDecl *decl);
3635
std::string InstantiateTemplate(const clang::Expr *expr,

cpp2rust/converter/models/converter_refcount.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ static bool IsBoxedType(std::string_view type) {
3636
}
3737

3838
static bool IsBoxedType(clang::QualType type) {
39-
return IsBoxedType(Mapper::Map(type.getUnqualifiedType()).value_or(std::string{}));
39+
return IsBoxedType(Mapper::Map(type.getUnqualifiedType()));
4040
}
4141

4242
static bool NeedsMutAccess(const clang::CXXMethodDecl *method,

0 commit comments

Comments
 (0)