Skip to content

Commit 633db6f

Browse files
committed
Don't map user-defined expressions
1 parent a54bbfd commit 633db6f

5 files changed

Lines changed: 26 additions & 5 deletions

File tree

cpp2rust/compat/platform_flags.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
static inline std::vector<std::string> getPlatformClangBeginFlags() {
1010
std::vector<std::string> flags = {
1111
"-resource-dir=" CLANG_RESOURCE_DIR,
12-
"-I" COMPAT_INCLUDE_DIR,
12+
"-isystem" COMPAT_INCLUDE_DIR,
1313
"-D_FORTIFY_SOURCE=0",
1414
};
1515
#ifdef MACOS_SDK_PATH

cpp2rust/converter/converter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ bool Converter::Convert(clang::Decl *decl) { return TraverseDecl(decl); }
276276

277277
bool Converter::VisitTranslationUnitDecl(clang::TranslationUnitDecl *decl) {
278278
for (auto *child : decl->decls()) {
279-
if (IsConvertibleDecl(child) &&
279+
if (IsUserDefinedDecl(child) &&
280280
(IsInMainFile(child) || !decl_ids_.contains(GetID(child)))) {
281281
Convert(child);
282282
}

cpp2rust/converter/converter_lib.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ bool IsInMainFile(const clang::Decl *decl) {
129129
return src_mgr.isInMainFile(src_mgr.getExpansionLoc(loc));
130130
}
131131

132-
bool IsConvertibleDecl(const clang::Decl *decl) {
132+
bool IsUserDefinedDecl(const clang::Decl *decl) {
133133
const auto &ctx = decl->getASTContext();
134134
const auto &src_mgr = ctx.getSourceManager();
135135
const auto src_loc = decl->getLocation();
@@ -138,6 +138,21 @@ bool IsConvertibleDecl(const clang::Decl *decl) {
138138
!src_mgr.isInSystemMacro(src_loc);
139139
}
140140

141+
bool RefersToUserDefinedDecl(const clang::Expr *expr) {
142+
expr = expr->IgnoreParenImpCasts();
143+
const clang::Decl *decl = nullptr;
144+
if (const auto *call = llvm::dyn_cast<clang::CallExpr>(expr)) {
145+
decl = call->getDirectCallee();
146+
} else if (const auto *ref = llvm::dyn_cast<clang::DeclRefExpr>(expr)) {
147+
decl = ref->getDecl();
148+
} else if (const auto *member = llvm::dyn_cast<clang::MemberExpr>(expr)) {
149+
decl = member->getMemberDecl();
150+
} else if (const auto *ctor = llvm::dyn_cast<clang::CXXConstructExpr>(expr)) {
151+
decl = ctor->getConstructor();
152+
}
153+
return decl && IsUserDefinedDecl(decl);
154+
}
155+
141156
bool IsUnsignedArithOp(const clang::BinaryOperator *expr) {
142157
clang::QualType lhs_type;
143158
clang::QualType rhs_type;

cpp2rust/converter/converter_lib.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ bool IsComparisonWithNullOp(const clang::BinaryOperator *expr);
3939

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

42-
bool IsConvertibleDecl(const clang::Decl *decl);
42+
bool IsUserDefinedDecl(const clang::Decl *decl);
43+
44+
bool RefersToUserDefinedDecl(const clang::Expr *expr);
4345

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

cpp2rust/converter/mapper.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,9 @@ search(std::unordered_multimap<std::string, T> &map, const std::string &txt,
385385
}
386386

387387
TranslationRule::ExprRule *search(const clang::Expr *expr) {
388+
if (RefersToUserDefinedDecl(expr)) {
389+
return nullptr;
390+
}
388391
auto qualified_name = ToString(expr);
389392
auto [rule, subs] =
390393
search(exprs_, qualified_name, GetExprMapKey(qualified_name));
@@ -598,7 +601,8 @@ const TranslationRule::ExprRule *GetExprRule(const clang::Expr *expr) {
598601

599602
std::string MapFunctionName(const clang::FunctionDecl *decl) {
600603
assert(decl);
601-
if (exprs_.contains(GetExprMapKey(ToString(decl)))) {
604+
if (!IsUserDefinedDecl(decl) &&
605+
exprs_.contains(GetExprMapKey(ToString(decl)))) {
602606
return std::format("libcc2rs::{}_{}", decl->getNameAsString(),
603607
model_ == Model::kRefCount ? "refcount" : "unsafe");
604608
}

0 commit comments

Comments
 (0)