Skip to content

Commit 4bb7665

Browse files
committed
Keep default arguments the rule declares as parameters
1 parent 1b1e896 commit 4bb7665

5 files changed

Lines changed: 33 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/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)