Skip to content

Commit ec950d5

Browse files
committed
Add support for typedef'ed rules
1 parent 19ad74a commit ec950d5

4 files changed

Lines changed: 38 additions & 12 deletions

File tree

cpp2rust/converter/converter_lib.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,17 @@ bool IsBuiltinVaStart(const clang::CallExpr *expr) {
753753
return false;
754754
}
755755

756+
std::string GetNameOfScalarTypedef(clang::QualType qual_type) {
757+
qual_type = qual_type.getNonReferenceType();
758+
const auto *typedef_type =
759+
llvm::dyn_cast<clang::TypedefType>(qual_type.getTypePtr());
760+
if (!typedef_type || !qual_type.getCanonicalType()->isBuiltinType()) {
761+
return {};
762+
}
763+
std::string name = typedef_type->getDecl()->getNameAsString();
764+
return qual_type.isConstQualified() ? "const " + name : name;
765+
}
766+
756767
bool IsBuiltinVaEnd(const clang::CallExpr *expr) {
757768
if (auto *fn = expr->getDirectCallee()) {
758769
return fn->getBuiltinID() == clang::Builtin::BI__builtin_va_end;

cpp2rust/converter/converter_lib.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ std::string GetClassName(clang::QualType type);
159159

160160
bool IsVaListType(clang::QualType type);
161161

162+
std::string GetNameOfScalarTypedef(clang::QualType qual_type);
163+
162164
bool IsBuiltinVaStart(const clang::CallExpr *expr);
163165

164166
bool IsBuiltinVaEnd(const clang::CallExpr *expr);

cpp2rust/converter/mapper.cpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -400,12 +400,22 @@ TranslationRule::ExprRule *search(const clang::Expr *expr) {
400400
return rule;
401401
}
402402

403-
TranslationRule::TypeRule *search(clang::QualType qual_type) {
403+
std::pair<TranslationRule::TypeRule *, std::vector<std::optional<std::string>>>
404+
search(clang::QualType qual_type) {
405+
if (auto name = GetNameOfScalarTypedef(qual_type); !name.empty()) {
406+
auto res = search(types_, name, GetTypeMapKey(name));
407+
if (res.first) {
408+
log() << "search type " << name
409+
<< ", result: " << res.first->type_info.type << '\n';
410+
return res;
411+
}
412+
}
404413
auto type = ToString(qual_type);
405-
auto [rule, subs] = search(types_, type, GetTypeMapKey(type));
414+
auto res = search(types_, type, GetTypeMapKey(type));
406415
log() << "search type " << type
407-
<< ", result: " << (rule ? rule->type_info.type : "None") << '\n';
408-
return rule;
416+
<< ", result: " << (res.first ? res.first->type_info.type : "None")
417+
<< '\n';
418+
return res;
409419
}
410420

411421
void addRulesFromDirectory(const std::filesystem::path &dir, Model model) {
@@ -579,7 +589,7 @@ PushASTContext::PushASTContext(clang::ASTContext &ctx) : prev_(ctx_) {
579589
PushASTContext::~PushASTContext() { ctx_ = prev_; }
580590

581591
bool Contains(clang::QualType qual_type) {
582-
return search(qual_type) != nullptr;
592+
return search(qual_type).first != nullptr;
583593
}
584594

585595
bool Contains(const clang::Expr *expr) { return search(expr) != nullptr; }
@@ -614,8 +624,7 @@ std::string InstantiateTemplate(const clang::Expr *expr, unsigned n) {
614624
}
615625

616626
std::string Map(clang::QualType qual_type) {
617-
auto type_str = ToString(qual_type);
618-
auto [rule, subs] = search(types_, type_str, GetTypeMapKey(type_str));
627+
auto [rule, subs] = search(qual_type);
619628
if (rule) {
620629
for (auto &ty : subs) {
621630
if (ty) {
@@ -628,8 +637,7 @@ std::string Map(clang::QualType qual_type) {
628637
}
629638

630639
std::string MapInitializer(clang::QualType qual_type) {
631-
auto type_str = ToString(qual_type);
632-
auto [rule, subs] = search(types_, type_str, GetTypeMapKey(type_str));
640+
auto [rule, subs] = search(qual_type);
633641
if (rule && !rule->initializer.empty()) {
634642
for (auto &ty : subs) {
635643
if (ty) {
@@ -642,12 +650,12 @@ std::string MapInitializer(clang::QualType qual_type) {
642650
}
643651

644652
bool MapsToPointer(clang::QualType qual_type) {
645-
auto rule = search(qual_type);
653+
auto rule = search(qual_type).first;
646654
return rule && rule->type_info.is_pointer();
647655
}
648656

649657
bool MapsToRefcountPointer(clang::QualType qual_type) {
650-
auto rule = search(qual_type);
658+
auto rule = search(qual_type).first;
651659
return rule && rule->type_info.is_refcount_pointer;
652660
}
653661

cpp2rust/cpp_rule_preprocessor.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <string>
2727

2828
#include "compat/platform_flags.h"
29+
#include "converter/converter_lib.h"
2930
#include "converter/mapper.h"
3031

3132
namespace fs = std::filesystem;
@@ -113,7 +114,11 @@ class Callback : public clang::ast_matchers::MatchFinder::MatchCallback {
113114
} else {
114115
type = var->getUnderlyingType();
115116
}
116-
out_.try_emplace(var->getQualifiedNameAsString(), Mapper::ToString(type));
117+
auto src = GetNameOfScalarTypedef(type);
118+
if (src.empty()) {
119+
src = Mapper::ToString(type);
120+
}
121+
out_.try_emplace(var->getQualifiedNameAsString(), std::move(src));
117122
return;
118123
}
119124

0 commit comments

Comments
 (0)