Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions cpp2rust/converter/converter_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,17 @@ bool IsBuiltinVaStart(const clang::CallExpr *expr) {
return false;
}

std::string GetNameOfScalarTypedef(clang::QualType qual_type) {
qual_type = qual_type.getNonReferenceType();
const auto *typedef_type =
llvm::dyn_cast<clang::TypedefType>(qual_type.getTypePtr());
if (!typedef_type || !qual_type.getCanonicalType()->isBuiltinType()) {
return {};
}
std::string name = typedef_type->getDecl()->getNameAsString();
return qual_type.isConstQualified() ? "const " + name : name;
}

bool IsBuiltinVaEnd(const clang::CallExpr *expr) {
if (auto *fn = expr->getDirectCallee()) {
return fn->getBuiltinID() == clang::Builtin::BI__builtin_va_end;
Expand Down
2 changes: 2 additions & 0 deletions cpp2rust/converter/converter_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ std::string GetClassName(clang::QualType type);

bool IsVaListType(clang::QualType type);

std::string GetNameOfScalarTypedef(clang::QualType qual_type);

bool IsBuiltinVaStart(const clang::CallExpr *expr);

bool IsBuiltinVaEnd(const clang::CallExpr *expr);
Expand Down
46 changes: 33 additions & 13 deletions cpp2rust/converter/mapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <clang/Lex/Lexer.h>
#include <llvm/Support/ThreadPool.h>

#include <cstdlib>
#include <format>
#include <optional>
#include <regex>
Expand Down Expand Up @@ -400,12 +401,22 @@ TranslationRule::ExprRule *search(const clang::Expr *expr) {
return rule;
}

TranslationRule::TypeRule *search(clang::QualType qual_type) {
std::pair<TranslationRule::TypeRule *, std::vector<std::optional<std::string>>>
search(clang::QualType qual_type) {
if (auto name = GetNameOfScalarTypedef(qual_type); !name.empty()) {
auto res = search(types_, name, GetTypeMapKey(name));
if (res.first) {
log() << "search type " << name
<< ", result: " << res.first->type_info.type << '\n';
return res;
}
}
auto type = ToString(qual_type);
auto [rule, subs] = search(types_, type, GetTypeMapKey(type));
auto res = search(types_, type, GetTypeMapKey(type));
log() << "search type " << type
<< ", result: " << (rule ? rule->type_info.type : "None") << '\n';
return rule;
<< ", result: " << (res.first ? res.first->type_info.type : "None")
<< '\n';
return res;
}

void addRulesFromDirectory(const std::filesystem::path &dir, Model model) {
Expand All @@ -424,7 +435,18 @@ void addRulesFromDirectory(const std::filesystem::path &dir, Model model) {
exprs_.emplace(GetExprMapKey(rule.src), std::move(rule));
}
for (auto &[_, rule] : type_rules) {
types_.emplace(GetTypeMapKey(rule.src), std::move(rule));
auto key = GetTypeMapKey(rule.src);
auto [begin, end] = types_.equal_range(key);
for (auto it = begin; it != end; ++it) {
if (it->second.src == rule.src) {
llvm::errs() << "ERROR: duplicate type rule for C++ type '"
<< rule.src << "': maps to both '"
<< it->second.type_info.type << "' and '"
<< rule.type_info.type << "'\n";
std::exit(EXIT_FAILURE);
}
}
types_.emplace(std::move(key), std::move(rule));
}
}
}
Expand Down Expand Up @@ -579,7 +601,7 @@ PushASTContext::PushASTContext(clang::ASTContext &ctx) : prev_(ctx_) {
PushASTContext::~PushASTContext() { ctx_ = prev_; }

bool Contains(clang::QualType qual_type) {
return search(qual_type) != nullptr;
return search(qual_type).first != nullptr;
}

bool Contains(const clang::Expr *expr) { return search(expr) != nullptr; }
Expand Down Expand Up @@ -614,8 +636,7 @@ std::string InstantiateTemplate(const clang::Expr *expr, unsigned n) {
}

std::string Map(clang::QualType qual_type) {
auto type_str = ToString(qual_type);
auto [rule, subs] = search(types_, type_str, GetTypeMapKey(type_str));
auto [rule, subs] = search(qual_type);
if (rule) {
for (auto &ty : subs) {
if (ty) {
Expand All @@ -628,8 +649,7 @@ std::string Map(clang::QualType qual_type) {
}

std::string MapInitializer(clang::QualType qual_type) {
auto type_str = ToString(qual_type);
auto [rule, subs] = search(types_, type_str, GetTypeMapKey(type_str));
auto [rule, subs] = search(qual_type);
if (rule && !rule->initializer.empty()) {
for (auto &ty : subs) {
if (ty) {
Expand All @@ -642,12 +662,12 @@ std::string MapInitializer(clang::QualType qual_type) {
}

bool MapsToPointer(clang::QualType qual_type) {
auto rule = search(qual_type);
auto rule = search(qual_type).first;
return rule && rule->type_info.is_pointer();
}

bool MapsToRefcountPointer(clang::QualType qual_type) {
auto rule = search(qual_type);
auto rule = search(qual_type).first;
return rule && rule->type_info.is_refcount_pointer;
}

Expand Down Expand Up @@ -921,8 +941,8 @@ void LoadTranslationRules(Model model, clang::ASTContext &ctx,
}
translation_rules_loaded_ = true;

addRulesFromDirectory(rules_dir, model);
addBuiltinTypes(model);
addRulesFromDirectory(rules_dir, model);

#if 0
for (auto &[src, rule] : exprs_) {
Expand Down
7 changes: 6 additions & 1 deletion cpp2rust/cpp_rule_preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <string>

#include "compat/platform_flags.h"
#include "converter/converter_lib.h"
#include "converter/mapper.h"

namespace fs = std::filesystem;
Expand Down Expand Up @@ -113,7 +114,11 @@ class Callback : public clang::ast_matchers::MatchFinder::MatchCallback {
} else {
type = var->getUnderlyingType();
}
out_.try_emplace(var->getQualifiedNameAsString(), Mapper::ToString(type));
auto src = GetNameOfScalarTypedef(type);
if (src.empty()) {
src = Mapper::ToString(type);
}
out_.try_emplace(var->getQualifiedNameAsString(), std::move(src));
return;
}

Expand Down
2 changes: 1 addition & 1 deletion rule-preprocessor/src/semantic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn build_rustc_args(crate_root: &Path) -> Vec<String> {
args.push("-L".to_string());
args.push(format!("dependency={}", deps.display()));

for dep in &["libcc2rs", "libc", "brotli_sys"] {
for dep in &["libcc2rs", "libc", "brotli_sys", "rustls_ffi"] {
if let Some(rlib) = find_rlib(deps.as_path(), dep) {
args.push("--extern".to_string());
args.push(format!("{}={}", dep, rlib.display()));
Expand Down
1 change: 1 addition & 0 deletions rules/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ path = "src/lib.rs"
libc = "0.2.148"
libcc2rs = { version = "0.1.0", path = "../libcc2rs" }
brotli-sys = "0.3"
rustls-ffi = { version = "0.15.3", default-features = false }
4 changes: 4 additions & 0 deletions rules/cstdlib/src.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ void f9(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *)) {
return qsort(base, nmemb, size, compar);
}

char *f10(const char *path, char *resolved_path) {
return realpath(path, resolved_path);
}
4 changes: 4 additions & 0 deletions rules/cstdlib/tgt_unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,7 @@ unsafe fn f9(
>(a3 as *const ())),
)
}

unsafe fn f10(a0: *const u8, a1: *mut u8) -> *mut u8 {
libc::realpath(a0 as *const i8, a1 as *mut i8) as *mut u8
}
Loading
Loading