Skip to content

Commit e485118

Browse files
authored
Keep default arguments the rule declares as parameters (#559)
1 parent 017008d commit e485118

7 files changed

Lines changed: 84 additions & 4 deletions

File tree

‎cpp2rust/converter/rules/expr_matcher.cpp‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ clang::Expr *getObject(clang::ASTContext &ctx, clang::Expr *object) {
143143
}
144144

145145
std::vector<clang::Expr *> getOperands(clang::ASTContext &ctx,
146-
const clang::Expr *use) {
146+
const clang::Expr *use,
147+
unsigned arity = 0) {
147148
auto expr = const_cast<clang::Expr *>(RuleDecls::SkipImplicit(use));
148149
while (auto cast = llvm::dyn_cast<clang::ExplicitCastExpr>(expr)) {
149150
expr =
@@ -182,7 +183,7 @@ std::vector<clang::Expr *> getOperands(clang::ASTContext &ctx,
182183
->IgnoreParenImpCasts();
183184
operands.push_back(getObject(ctx, base));
184185
}
185-
while (!operands.empty() &&
186+
while (operands.size() > arity &&
186187
llvm::isa<clang::CXXDefaultArgExpr>(operands.back())) {
187188
operands.pop_back();
188189
}
@@ -298,7 +299,7 @@ void checkCallable(clang::Sema &sema, const clang::Expr *use,
298299
}
299300
RuleDecls::Trial trial(sema);
300301
auto loc = use->getBeginLoc();
301-
auto operands = getOperands(sema.Context, use);
302+
auto operands = getOperands(sema.Context, use, rule->getNumParams());
302303
clang::OverloadCandidateSet set(loc, clang::OverloadCandidateSet::CSK_Normal);
303304
auto found = clang::DeclAccessPair::make(rule, clang::AS_public);
304305
if (auto tmpl = rule->getDescribedFunctionTemplate()) {
@@ -347,7 +348,8 @@ MatchResult matchTemplate(clang::Sema &sema, const clang::Expr *use,
347348
auto &ctx = sema.Context;
348349
auto explicit_args = getResultArgs(sema, rule, use);
349350
auto specialization =
350-
deduce(sema, rule, explicit_args, getOperands(ctx, use));
351+
deduce(sema, rule, explicit_args,
352+
getOperands(ctx, use, rule->getTemplatedDecl()->getNumParams()));
351353
if (!specialization) {
352354
return std::nullopt;
353355
}

‎rules/string/src.cpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,7 @@ std::string &f29(std::string &dst, const std::string &src) { return dst = src; }
8383
std::string &f30(std::string &dst, std::string &&src) {
8484
return dst = std::move(src);
8585
}
86+
87+
std::size_t f32(const std::string &a0, const char *a1, std::size_t a2) {
88+
return a0.find(a1, a2);
89+
}

‎rules/string/tgt_refcount.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,12 @@ fn f30(a0: Ptr<Vec<i8>>, a1: &mut Vec<i8>) {
202202
fn f31(a0: Vec<i8>, a1: Ptr<i8>) -> bool {
203203
a1.with_c_str(|__s| a0[..a0.len().saturating_sub(1)] != *__s)
204204
}
205+
206+
fn f32(a0: Vec<i8>, a1: Ptr<i8>, a2: usize) -> usize {
207+
let __hay = &a0[..a0.len().saturating_sub(1)];
208+
a1.with_c_str(|__needle| {
209+
(a2..=__hay.len().saturating_sub(__needle.len()))
210+
.find(|&__i| __hay[__i..].starts_with(__needle))
211+
.unwrap_or(usize::MAX)
212+
})
213+
}

‎rules/string/tgt_unsafe.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,11 @@ unsafe fn f31(a0: Vec<libc::c_char>, a1: *const libc::c_char) -> bool {
202202
std::slice::from_raw_parts(s, (0..).take_while(|&i| *s.add(i) != 0).count() + 1).to_vec()
203203
}
204204
}
205+
206+
unsafe fn f32(a0: Vec<libc::c_char>, a1: *const libc::c_char, a2: usize) -> usize {
207+
let __hay = &a0[..a0.len() - 1];
208+
let __needle = ::std::slice::from_raw_parts(a1, libc::strlen(a1));
209+
(a2..=__hay.len().saturating_sub(__needle.len()))
210+
.find(|&__i| __hay[__i..].starts_with(__needle))
211+
.unwrap_or(usize::MAX)
212+
}

‎tests/unit/out/refcount/string.rs‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,33 @@ fn main_0() -> i32 {
292292
.unwrap_or(usize::MAX)
293293
});
294294
assert!((pos == ((-1_i64 as u64) as usize)));
295+
pos = {
296+
let __hay = &result[..result.len().saturating_sub(1)];
297+
Ptr::<i8>::from_string_literal(b"f").with_c_str(|__needle| {
298+
(0_usize..=__hay.len().saturating_sub(__needle.len()))
299+
.find(|&__i| __hay[__i..].starts_with(__needle))
300+
.unwrap_or(usize::MAX)
301+
})
302+
};
303+
assert!((pos == 5_usize));
304+
pos = {
305+
let __hay = &result[..result.len().saturating_sub(1)];
306+
Ptr::<i8>::from_string_literal(b"oo").with_c_str(|__needle| {
307+
(0_usize..=__hay.len().saturating_sub(__needle.len()))
308+
.find(|&__i| __hay[__i..].starts_with(__needle))
309+
.unwrap_or(usize::MAX)
310+
})
311+
};
312+
assert!((pos == 6_usize));
313+
pos = {
314+
let __hay = &result[..result.len().saturating_sub(1)];
315+
Ptr::<i8>::from_string_literal(b"x").with_c_str(|__needle| {
316+
(0_usize..=__hay.len().saturating_sub(__needle.len()))
317+
.find(|&__i| __hay[__i..].starts_with(__needle))
318+
.unwrap_or(usize::MAX)
319+
})
320+
};
321+
assert!((pos == ((-1_i64 as u64) as usize)));
295322
let string_to_cast: Value<Vec<i8>> = Rc::new(RefCell::new({
296323
let mut __bytes = Ptr::<i8>::from_string_literal(b"cast").to_c_bytes();
297324
__bytes.push(0);

‎tests/unit/out/unsafe/string.rs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,30 @@ unsafe fn main_0() -> i32 {
312312
None => usize::MAX,
313313
};
314314
assert!(((pos) == ((-1_i64 as u64) as usize)));
315+
pos = {
316+
let __hay = &result[..result.len() - 1];
317+
let __needle = ::std::slice::from_raw_parts(c"f".as_ptr(), libc::strlen(c"f".as_ptr()));
318+
(0_usize..=__hay.len().saturating_sub(__needle.len()))
319+
.find(|&__i| __hay[__i..].starts_with(__needle))
320+
.unwrap_or(usize::MAX)
321+
};
322+
assert!(((pos) == (5_usize)));
323+
pos = {
324+
let __hay = &result[..result.len() - 1];
325+
let __needle = ::std::slice::from_raw_parts(c"oo".as_ptr(), libc::strlen(c"oo".as_ptr()));
326+
(0_usize..=__hay.len().saturating_sub(__needle.len()))
327+
.find(|&__i| __hay[__i..].starts_with(__needle))
328+
.unwrap_or(usize::MAX)
329+
};
330+
assert!(((pos) == (6_usize)));
331+
pos = {
332+
let __hay = &result[..result.len() - 1];
333+
let __needle = ::std::slice::from_raw_parts(c"x".as_ptr(), libc::strlen(c"x".as_ptr()));
334+
(0_usize..=__hay.len().saturating_sub(__needle.len()))
335+
.find(|&__i| __hay[__i..].starts_with(__needle))
336+
.unwrap_or(usize::MAX)
337+
};
338+
assert!(((pos) == ((-1_i64 as u64) as usize)));
315339
let mut string_to_cast: Vec<libc::c_char> = {
316340
let s = c"cast".as_ptr();
317341
std::slice::from_raw_parts(s, (0..).take_while(|&i| *s.add(i) != 0).count() + 1).to_vec()

‎tests/unit/string.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ int main() {
125125
assert(pos == 7);
126126
pos = result.find_last_of("x");
127127
assert(pos == -1ul);
128+
pos = result.find("f");
129+
assert(pos == 5);
130+
pos = result.find("oo");
131+
assert(pos == 6);
132+
pos = result.find("x");
133+
assert(pos == -1ul);
128134
std::string string_to_cast = "cast";
129135
[[maybe_unused]] std::uint8_t *output_data =
130136
reinterpret_cast<std::uint8_t *>(&string_to_cast[0]);

0 commit comments

Comments
 (0)