Skip to content

Commit af5161b

Browse files
authored
Don't hoist literal arguments of function calls (#180)
They alias with no other argument
1 parent 073103f commit af5161b

213 files changed

Lines changed: 656 additions & 2768 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cpp2rust/converter/converter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1635,7 +1635,7 @@ Converter::CallInfo Converter::CollectCallInfo(clang::CallExpr *expr) {
16351635
: proto->getParamType(i),
16361636
.expr = arg,
16371637
.has_default = function && function->getParamDecl(i)->hasDefaultArg(),
1638-
.kind = Kind::Hoisted,
1638+
.kind = IsLiteral(arg) ? Kind::Inline : Kind::Hoisted,
16391639
};
16401640
bool is_materialize = clang::isa<clang::MaterializeTemporaryExpr>(arg);
16411641
if (is_materialize && ca.param_type->isLValueReferenceType()) {

cpp2rust/converter/converter_lib.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,14 @@ bool IsAsciiStringLiteral(const clang::StringLiteral *str) {
265265
return true;
266266
}
267267

268+
bool IsLiteral(const clang::Expr *expr) {
269+
expr = expr->IgnoreParenImpCasts();
270+
return clang::isa<clang::IntegerLiteral, clang::FloatingLiteral,
271+
clang::StringLiteral, clang::CharacterLiteral,
272+
clang::CXXBoolLiteralExpr, clang::FixedPointLiteral,
273+
clang::ImaginaryLiteral>(expr);
274+
}
275+
268276
bool IsInitExprOfStringLiteral(const clang::InitListExpr *expr) {
269277
auto type = expr->getType();
270278
return expr->getNumInits() == 1 && type->isArrayType() &&

cpp2rust/converter/converter_lib.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ bool IsAsciiStringLiteral(const clang::StringLiteral *str);
7171

7272
bool IsInitExprOfStringLiteral(const clang::InitListExpr *expr);
7373

74+
bool IsLiteral(const clang::Expr *expr);
75+
7476
std::vector<clang::CXXConstructorDecl *>
7577
GetTemplateInstantiatedCtors(clang::CXXRecordDecl *decl);
7678

tests/benchmarks/out/refcount/bfs.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,7 @@ fn main_0() -> i32 {
225225
let pred: Value<Ptr<u32>> = Rc::new(RefCell::new(
226226
({
227227
let _graph: Ptr<Graph> = graph.as_pointer();
228-
let _start_vertex: u32 = 0_u32;
229-
BFS_0(_graph, _start_vertex)
228+
BFS_0(_graph, 0_u32)
230229
}),
231230
));
232231
let i: Value<u32> = Rc::new(RefCell::new(0_u32));

tests/benchmarks/out/refcount/fibonacci.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,5 @@ pub fn main() {
2727
std::process::exit(main_0());
2828
}
2929
fn main_0() -> i32 {
30-
return (({
31-
let _n: u64 = 46_u64;
32-
fib_0(_n)
33-
}) as i32);
30+
return (({ fib_0(46_u64) }) as i32);
3431
}

tests/benchmarks/out/unsafe/bfs.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ unsafe fn main_0() -> i32 {
159159
}
160160
let mut pred: *mut u32 = (unsafe {
161161
let _graph: *const Graph = &graph as *const Graph;
162-
let _start_vertex: u32 = 0_u32;
163-
BFS_0(_graph, _start_vertex)
162+
BFS_0(_graph, 0_u32)
164163
});
165164
let mut i: u32 = 0_u32;
166165
'loop_: while ((i as u64) < (V)) {

tests/benchmarks/out/unsafe/fibonacci.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,5 @@ pub fn main() {
2828
}
2929
}
3030
unsafe fn main_0() -> i32 {
31-
return ((unsafe {
32-
let _n: u64 = 46_u64;
33-
fib_0(_n)
34-
}) as i32);
31+
return ((unsafe { fib_0(46_u64) }) as i32);
3532
}

tests/multi-file/extern_functions/out/refcount/extern_functions.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,7 @@ pub fn main() {
1010
std::process::exit(main_0());
1111
}
1212
fn main_0() -> i32 {
13-
assert!(
14-
(((({
15-
let _x: i32 = 42;
16-
helper_0(_x)
17-
}) == 43) as i32)
18-
!= 0)
19-
);
13+
assert!((((({ helper_0(42,) }) == 43) as i32) != 0));
2014
return 0;
2115
}
2216
pub fn unrelated1_1() -> i32 {

tests/multi-file/extern_functions/out/unsafe/extern_functions.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,7 @@ pub fn main() {
1212
}
1313
}
1414
unsafe fn main_0() -> i32 {
15-
assert!(
16-
((((unsafe {
17-
let _x: i32 = 42;
18-
helper_0(_x)
19-
}) == (43)) as i32)
20-
!= 0)
21-
);
15+
assert!(((((unsafe { helper_0(42,) }) == (43)) as i32) != 0));
2216
return 0;
2317
}
2418
pub unsafe fn unrelated1_1() -> i32 {

tests/multi-file/header_function/out/refcount/header_function.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,7 @@ pub fn main() {
1010
std::process::exit(main_0());
1111
}
1212
fn main_0() -> i32 {
13-
assert!(
14-
(((({
15-
let _x: i32 = 42;
16-
helper_0(_x)
17-
}) == 43) as i32)
18-
!= 0)
19-
);
13+
assert!((((({ helper_0(42,) }) == 43) as i32) != 0));
2014
return 0;
2115
}
2216
pub fn unrelated1_1() -> i32 {

0 commit comments

Comments
 (0)