Skip to content
Closed
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
2 changes: 1 addition & 1 deletion cpp2rust/compat/platform_flags.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
static inline std::vector<std::string> getPlatformClangBeginFlags() {
std::vector<std::string> flags = {
"-resource-dir=" CLANG_RESOURCE_DIR,
"-I" COMPAT_INCLUDE_DIR,
"-isystem" COMPAT_INCLUDE_DIR,
"-D_FORTIFY_SOURCE=0",
};
#ifdef MACOS_SDK_PATH
Expand Down
2 changes: 1 addition & 1 deletion cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ bool Converter::Convert(clang::Decl *decl) { return TraverseDecl(decl); }

bool Converter::VisitTranslationUnitDecl(clang::TranslationUnitDecl *decl) {
for (auto *child : decl->decls()) {
if (IsConvertibleDecl(child) &&
if (IsUserDefinedDecl(child) &&
(IsInMainFile(child) || !decl_ids_.contains(GetID(child)))) {
Convert(child);
}
Expand Down
17 changes: 16 additions & 1 deletion cpp2rust/converter/converter_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ bool IsInMainFile(const clang::Decl *decl) {
return src_mgr.isInMainFile(src_mgr.getExpansionLoc(loc));
}

bool IsConvertibleDecl(const clang::Decl *decl) {
bool IsUserDefinedDecl(const clang::Decl *decl) {
const auto &ctx = decl->getASTContext();
const auto &src_mgr = ctx.getSourceManager();
const auto src_loc = decl->getLocation();
Expand All @@ -138,6 +138,21 @@ bool IsConvertibleDecl(const clang::Decl *decl) {
!src_mgr.isInSystemMacro(src_loc);
}

bool RefersToUserDefinedDecl(const clang::Expr *expr) {
expr = expr->IgnoreParenImpCasts();
const clang::Decl *decl = nullptr;
if (const auto *call = llvm::dyn_cast<clang::CallExpr>(expr)) {
decl = call->getDirectCallee();
} else if (const auto *ref = llvm::dyn_cast<clang::DeclRefExpr>(expr)) {
decl = ref->getDecl();
} else if (const auto *member = llvm::dyn_cast<clang::MemberExpr>(expr)) {
decl = member->getMemberDecl();
} else if (const auto *ctor = llvm::dyn_cast<clang::CXXConstructExpr>(expr)) {
decl = ctor->getConstructor();
}
return decl && IsUserDefinedDecl(decl);
}

bool IsUnsignedArithOp(const clang::BinaryOperator *expr) {
clang::QualType lhs_type;
clang::QualType rhs_type;
Expand Down
4 changes: 3 additions & 1 deletion cpp2rust/converter/converter_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ bool IsComparisonWithNullOp(const clang::BinaryOperator *expr);

bool IsInMainFile(const clang::Decl *decl);

bool IsConvertibleDecl(const clang::Decl *decl);
bool IsUserDefinedDecl(const clang::Decl *decl);

bool RefersToUserDefinedDecl(const clang::Expr *expr);

bool IsUnsignedArithOp(const clang::BinaryOperator *expr);

Expand Down
6 changes: 5 additions & 1 deletion cpp2rust/converter/mapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,9 @@ search(std::unordered_multimap<std::string, T> &map, const std::string &txt,
}

TranslationRule::ExprRule *search(const clang::Expr *expr) {
if (RefersToUserDefinedDecl(expr)) {
return nullptr;
}
auto qualified_name = ToString(expr);
auto [rule, subs] =
search(exprs_, qualified_name, GetExprMapKey(qualified_name));
Expand Down Expand Up @@ -598,7 +601,8 @@ const TranslationRule::ExprRule *GetExprRule(const clang::Expr *expr) {

std::string MapFunctionName(const clang::FunctionDecl *decl) {
assert(decl);
if (exprs_.contains(GetExprMapKey(ToString(decl)))) {
if (!IsUserDefinedDecl(decl) &&
exprs_.contains(GetExprMapKey(ToString(decl)))) {
return std::format("libcc2rs::{}_{}", decl->getNameAsString(),
model_ == Model::kRefCount ? "refcount" : "unsafe");
}
Expand Down
Loading
Loading