Skip to content

Commit 95aa4f4

Browse files
committed
Replace GetNameOfScalarTypedef with kPreserve flag in ToString
1 parent 1bbe918 commit 95aa4f4

5 files changed

Lines changed: 26 additions & 26 deletions

File tree

cpp2rust/converter/converter_lib.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -753,17 +753,6 @@ 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-
767756
bool IsBuiltinVaEnd(const clang::CallExpr *expr) {
768757
if (auto *fn = expr->getDirectCallee()) {
769758
return fn->getBuiltinID() == clang::Builtin::BI__builtin_va_end;

cpp2rust/converter/converter_lib.h

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

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

162-
std::string GetNameOfScalarTypedef(clang::QualType qual_type);
163-
164162
bool IsBuiltinVaStart(const clang::CallExpr *expr);
165163

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

cpp2rust/converter/mapper.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -403,15 +403,17 @@ TranslationRule::ExprRule *search(const clang::Expr *expr) {
403403

404404
std::pair<TranslationRule::TypeRule *, std::vector<std::optional<std::string>>>
405405
search(clang::QualType qual_type) {
406-
if (auto name = GetNameOfScalarTypedef(qual_type); !name.empty()) {
407-
auto res = search(types_, name, GetTypeMapKey(name));
408-
if (res.first) {
409-
log() << "search type " << name
410-
<< ", result: " << res.first->type_info.type << '\n';
411-
return res;
412-
}
406+
auto sugared = ToString(qual_type, ScalarSugar::kPreserve);
407+
if (auto res = search(types_, sugared, GetTypeMapKey(sugared)); res.first) {
408+
log() << "search type " << sugared
409+
<< ", result: " << res.first->type_info.type << '\n';
410+
return res;
413411
}
414412
auto type = ToString(qual_type);
413+
if (type == sugared) {
414+
log() << "search type " << type << ", result: None\n";
415+
return {};
416+
}
415417
auto res = search(types_, type, GetTypeMapKey(type));
416418
log() << "search type " << type
417419
<< ", result: " << (res.first ? res.first->type_info.type : "None")
@@ -742,9 +744,17 @@ void AddRuleForUserDefinedType(clang::NamedDecl *decl) {
742744
}
743745
}
744746

745-
std::string ToString(clang::QualType qual_type) {
747+
std::string ToString(clang::QualType qual_type, ScalarSugar sugar) {
746748
assert(ctx_);
747749

750+
if (sugar == ScalarSugar::kPreserve) {
751+
if (const auto *typedef_type = qual_type->getAs<clang::TypedefType>()) {
752+
if (qual_type.getCanonicalType()->isBuiltinType()) {
753+
return typedef_type->getDecl()->getNameAsString();
754+
}
755+
}
756+
}
757+
748758
if (auto cxx_record_decl = qual_type->getAsCXXRecordDecl()) {
749759
if (cxx_record_decl->isLambda()) {
750760
return ToString(cxx_record_decl->getLambdaCallOperator());

cpp2rust/converter/mapper.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@ bool ParamIsPointer(const clang::Expr *expr, unsigned index);
3838
bool MapsToPointer(clang::QualType qual_type);
3939
bool MapsToRefcountPointer(clang::QualType qual_type);
4040

41-
std::string ToString(clang::QualType qual_type);
41+
enum class ScalarSugar {
42+
kDesugar,
43+
kPreserve,
44+
};
45+
46+
std::string ToString(clang::QualType qual_type,
47+
ScalarSugar sugar = ScalarSugar::kDesugar);
4248
std::string ToString(const clang::Expr *expr);
4349
std::string ToString(const clang::NamedDecl *decl);
4450

cpp2rust/cpp_rule_preprocessor.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,7 @@ class Callback : public clang::ast_matchers::MatchFinder::MatchCallback {
114114
type = lookupType(tdecl);
115115
}
116116
}
117-
auto src = GetNameOfScalarTypedef(type);
118-
if (src.empty()) {
119-
src = Mapper::ToString(type);
120-
}
117+
auto src = Mapper::ToString(type, Mapper::ScalarSugar::kPreserve);
121118
out_.try_emplace(var->getQualifiedNameAsString(), std::move(src));
122119
return;
123120
}

0 commit comments

Comments
 (0)