diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml index 8d020676..9591ca4d 100644 --- a/.github/workflows/format.yml +++ b/.github/workflows/format.yml @@ -23,7 +23,7 @@ jobs: echo "$HOME/.cargo/bin" >> $GITHUB_PATH - name: Check C++ formatting - run: find cpp2rust tests -name '*.cpp' -o -name '*.h' | xargs clang-format --dry-run --Werror + run: find cpp2rust tests -name '*.cpp' -o -name '*.h' -o -name '*.c' | xargs clang-format --dry-run --Werror - name: Check Rust formatting run: | diff --git a/CMakeLists.txt b/CMakeLists.txt index b880ff79..05b17078 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -93,7 +93,7 @@ add_subdirectory(tests) find_program(CLANG_FORMAT NAMES clang-format-22 clang-format) file(GLOB_RECURSE ALL_CXX_SOURCES - cpp2rust/*.cpp cpp2rust/*.h tests/*.cpp) + cpp2rust/*.cpp cpp2rust/*.h tests/*.cpp tests/*.c) add_custom_target("format" COMMAND ${CLANG_FORMAT} -i ${ALL_CXX_SOURCES} diff --git a/cpp2rust/CMakeLists.txt b/cpp2rust/CMakeLists.txt index 4719cd4b..ce30f0a9 100644 --- a/cpp2rust/CMakeLists.txt +++ b/cpp2rust/CMakeLists.txt @@ -13,7 +13,8 @@ target_link_libraries(cpp2rust PRIVATE target_include_directories(cpp2rust PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) target_include_directories(cpp2rust PUBLIC SYSTEM ${CLANG_INCLUDE_DIRS} ${LLVM_INCLUDE_DIRS}) target_compile_definitions(cpp2rust PUBLIC ${LLVM_DEFINITIONS}) -target_compile_definitions(cpp2rust PUBLIC "-DCLANG_EXECUTABLE=\"${CMAKE_CXX_COMPILER}\"") +target_compile_definitions(cpp2rust PUBLIC "-DCLANG_C_COMPILER=\"${CMAKE_C_COMPILER}\"") +target_compile_definitions(cpp2rust PUBLIC "-DCLANG_CXX_COMPILER=\"${CMAKE_CXX_COMPILER}\"") target_compile_definitions(cpp2rust PUBLIC "-DCLANG_RESOURCE_DIR=\"${LLVM_LIBRARY_DIR}/clang/${LLVM_VERSION_MAJOR}\"") target_compile_definitions(cpp2rust PUBLIC "-DCOMPAT_INCLUDE_DIR=\"${CMAKE_CURRENT_SOURCE_DIR}/compat\"") diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 67afae39..b358f553 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -916,9 +916,18 @@ bool Converter::VisitReturnStmt(clang::ReturnStmt *stmt) { return false; } +void Converter::ConvertCondition(clang::Expr *cond) { + if (!cond->getType()->isBooleanType()) { + PushExprKind push(*this, ExprKind::RValue); + Convert(CreateConversionToBool(cond, ctx_)); + return; + } + Convert(cond); +} + bool Converter::VisitIfStmt(clang::IfStmt *stmt) { StrCat(keyword::kIf); - Convert(stmt->getCond()); + ConvertCondition(stmt->getCond()); StrCat(token::kOpenCurlyBracket); Convert(stmt->getThen()); StrCat(token::kCloseCurlyBracket); @@ -938,7 +947,7 @@ bool Converter::VisitIfStmt(clang::IfStmt *stmt) { bool Converter::VisitWhileStmt(clang::WhileStmt *stmt) { StrCat("'loop_:"); StrCat(keyword::kWhile); - Convert(stmt->getCond()); + ConvertCondition(stmt->getCond()); StrCat(token::kOpenCurlyBracket); curr_for_inc_.emplace(nullptr); Convert(stmt->getBody()); @@ -954,7 +963,7 @@ bool Converter::VisitDoStmt(clang::DoStmt *stmt) { Convert(stmt->getBody()); curr_for_inc_.pop(); StrCat(keyword::kIf, token::kNot, token::kOpenParen); - Convert(stmt->getCond()); + ConvertCondition(stmt->getCond()); StrCat(token::kCloseParen, token::kOpenCurlyBracket, keyword::kBreak, token::kSemiColon, token::kCloseCurlyBracket, token::kCloseCurlyBracket); @@ -968,7 +977,7 @@ bool Converter::VisitForStmt(clang::ForStmt *stmt) { if (stmt->getCond() == nullptr) { StrCat("true"); } else { - Convert(stmt->getCond()); + ConvertCondition(stmt->getCond()); } StrCat(token::kOpenCurlyBracket); curr_for_inc_.emplace(stmt->getInc()); @@ -1546,7 +1555,8 @@ bool Converter::VisitFloatingLiteral(clang::FloatingLiteral *expr) { bool Converter::VisitCharacterLiteral(clang::CharacterLiteral *expr) { std::string ch = GetEscapedCharLiteral(expr->getValue()); ch = "'" + std::move(ch) + "'"; - StrCat(token::kOpenParen, ch, keyword::kAs, "u8", token::kCloseParen); + StrCat(token::kOpenParen, ch, keyword::kAs, ToStringBase(expr->getType()), + token::kCloseParen); computed_expr_type_ = ComputedExprType::FreshValue; return false; } @@ -1664,6 +1674,15 @@ bool Converter::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) { Convert(sub_expr); break; case clang::CastKind::CK_IntegralToBoolean: + if (auto binop = clang::dyn_cast( + sub_expr->IgnoreParenImpCasts())) { + // This already produces bool, no need for != 0 + if (binop->isComparisonOp()) { + Convert(sub_expr); + break; + } + } + StrCat(token::kOpenParen); Convert(sub_expr); StrCat(token::kDiff, token::kZero, token::kCloseParen); @@ -2875,12 +2894,12 @@ void Converter::ConvertAssignment(clang::Expr *lhs, clang::Expr *rhs, curr_init_type_.pop(); auto rhs_as_string = ConvertFreshRValue(rhs); - if (isRValue()) { + if (!isVoid()) { StrCat(token::kOpenCurlyBracket); } StrCat(lhs_as_string, assign_operator, rhs_as_string); - if (isRValue()) { + if (!isVoid()) { StrCat(token::kSemiColon, ConvertRValue(lhs), token::kCloseCurlyBracket); } } diff --git a/cpp2rust/converter/converter.h b/cpp2rust/converter/converter.h index 899cc965..bcc0d538 100644 --- a/cpp2rust/converter/converter.h +++ b/cpp2rust/converter/converter.h @@ -112,6 +112,8 @@ class Converter : public clang::RecursiveASTVisitor { virtual bool VisitReturnStmt(clang::ReturnStmt *stmt); + void ConvertCondition(clang::Expr *cond); + virtual bool VisitIfStmt(clang::IfStmt *stmt); virtual bool VisitWhileStmt(clang::WhileStmt *stmt); diff --git a/cpp2rust/converter/converter_lib.cpp b/cpp2rust/converter/converter_lib.cpp index c70ed407..3f515788 100644 --- a/cpp2rust/converter/converter_lib.cpp +++ b/cpp2rust/converter/converter_lib.cpp @@ -654,4 +654,10 @@ bool ContainsVAArgExpr(const clang::Stmt *stmt) { return false; } +clang::Expr *CreateConversionToBool(clang::Expr *expr, clang::ASTContext &ctx) { + return clang::ImplicitCastExpr::Create( + ctx, ctx.BoolTy, clang::CK_IntegralToBoolean, expr, + /*BasePath=*/nullptr, clang::VK_PRValue, clang::FPOptionsOverride()); +} + } // namespace cpp2rust diff --git a/cpp2rust/converter/converter_lib.h b/cpp2rust/converter/converter_lib.h index c0d2a65d..3696d680 100644 --- a/cpp2rust/converter/converter_lib.h +++ b/cpp2rust/converter/converter_lib.h @@ -152,4 +152,6 @@ bool IsBuiltinVaCopy(const clang::CallExpr *expr); bool ContainsVAArgExpr(const clang::Stmt *stmt); +clang::Expr *CreateConversionToBool(clang::Expr *expr, clang::ASTContext &ctx); + } // namespace cpp2rust diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index 7d98708f..714c2ffd 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -188,7 +188,7 @@ bool ConverterRefCount::VisitPointerType(clang::PointerType *type) { PushConversionKind push2(*this, ConversionKind::FullRefCount, pointee_type->isArrayType()); if (pointee_type->isRecordType() && - abstract_structs_.contains(GetID(pointee_type->getAsCXXRecordDecl()))) { + abstract_structs_.contains(GetID(pointee_type->getAsRecordDecl()))) { StrCat("PtrDyn &cxx_flags, - const std::string &rules_dir) { + const std::string &rules_dir, + std::string_view filename) { auto tool_args = getPlatformClangFlags(); tool_args.push_back("-fparse-all-comments"); tool_args.insert(tool_args.end(), cxx_flags.begin(), cxx_flags.end()); @@ -21,7 +22,8 @@ std::string TranspileSrc(std::string_view cc_code, Model model, std::string rs_code; clang::tooling::runToolOnCodeWithArgs( std::make_unique(rs_code, model, true, rules_dir), - cc_code, tool_args, "input.cpp", CLANG_EXECUTABLE); + cc_code, tool_args, filename.ends_with(".c") ? "input.c" : "input.cpp", + filename.ends_with(".c") ? CLANG_C_COMPILER : CLANG_CXX_COMPILER); return rs_code; } diff --git a/cpp2rust/cpp2rust_lib.h b/cpp2rust/cpp2rust_lib.h index 544d72a7..9c87db2e 100644 --- a/cpp2rust/cpp2rust_lib.h +++ b/cpp2rust/cpp2rust_lib.h @@ -12,7 +12,8 @@ namespace cpp2rust { std::string TranspileSrc(std::string_view cc_code, Model model, const std::vector &cxx_flags, - const std::string &rules_dir); + const std::string &rules_dir, + std::string_view filename); std::string TranspileDir(std::string_view build_dir, Model model, const std::string &rules_dir); diff --git a/tests/lit/lit/formats/Cpp2RustTest.py b/tests/lit/lit/formats/Cpp2RustTest.py index 1f0d54da..a9b87106 100644 --- a/tests/lit/lit/formats/Cpp2RustTest.py +++ b/tests/lit/lit/formats/Cpp2RustTest.py @@ -48,7 +48,7 @@ def getTestsInDirectory(self, testSuite, path_in_suite, litConfig, localConfig): source_path = testSuite.getSourcePath(path_in_suite) for filename in os.listdir(source_path): - if filename.endswith('.cpp'): + if filename.endswith('.cpp') or filename.endswith('.c'): for t in self.getTestsForPath(testSuite, path_in_suite + (filename,), litConfig, localConfig): yield t @@ -172,10 +172,11 @@ def fail(str, code = fail_code): elif is_nondet_result: lit.util.executeCommand(rust_bin) else: - cmd = ['clang++', '-O3', '-o', tmp_dir + '/cpp', cc_input] + cc = 'clang' if cc_input.endswith('.c') else 'clang++' + cmd = [cc, '-O3', '-o', tmp_dir + '/cpp', cc_input] _, _, code = lit.util.executeCommand(cmd) if code != 0: - return fail('clang++ failed') + return fail(cc + ' failed') out_cpp, err_cpp, code_cpp = lit.util.executeCommand(tmp_dir + '/cpp') out_rs, err_rs, code_rs = lit.util.executeCommand(rust_bin) diff --git a/tests/unit/expr_as_bool_c.c b/tests/unit/expr_as_bool_c.c new file mode 100644 index 00000000..3a7cc106 --- /dev/null +++ b/tests/unit/expr_as_bool_c.c @@ -0,0 +1,29 @@ +#include +#include + +int main() { + int a = 0; + int b; + + if (b = a) { + } + while ((b = a) != 0) { + } + if (a) { + } + if (a == b) { + } + if (a < b) { + } + + assert(a == b); + assert(!(a = b)); + + bool c; + c = a = b; + c = (b = a) != 0; + c = a; + c = a == b; + c = a < b; + return 0; +} diff --git a/tests/unit/expr_as_bool_cpp.cpp b/tests/unit/expr_as_bool_cpp.cpp new file mode 100644 index 00000000..3a7cc106 --- /dev/null +++ b/tests/unit/expr_as_bool_cpp.cpp @@ -0,0 +1,29 @@ +#include +#include + +int main() { + int a = 0; + int b; + + if (b = a) { + } + while ((b = a) != 0) { + } + if (a) { + } + if (a == b) { + } + if (a < b) { + } + + assert(a == b); + assert(!(a = b)); + + bool c; + c = a = b; + c = (b = a) != 0; + c = a; + c = a == b; + c = a < b; + return 0; +} diff --git a/tests/unit/out/refcount/expr_as_bool.rs b/tests/unit/out/refcount/expr_as_bool.rs new file mode 100644 index 00000000..a257e1b8 --- /dev/null +++ b/tests/unit/out/refcount/expr_as_bool.rs @@ -0,0 +1,22 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::Seek; +use std::io::{Read, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let a: Value = Rc::new(RefCell::new(0)); + let b: Value = >::default(); + if ({ + (*b.borrow_mut()) = (*a.borrow()); + (*b.borrow()) + } != 0) + {} + return 0; +} diff --git a/tests/unit/out/refcount/expr_as_bool_c.rs b/tests/unit/out/refcount/expr_as_bool_c.rs new file mode 100644 index 00000000..c1b22fc5 --- /dev/null +++ b/tests/unit/out/refcount/expr_as_bool_c.rs @@ -0,0 +1,52 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::Seek; +use std::io::{Read, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let a: Value = Rc::new(RefCell::new(0)); + let b: Value = >::default(); + if ({ + (*b.borrow_mut()) = (*a.borrow()); + (*b.borrow()) + } != 0) + {} + 'loop_: while ((({ + (*b.borrow_mut()) = (*a.borrow()); + (*b.borrow()) + }) as i32) + != 0) + {} + if ((*a.borrow()) != 0) {} + if ((*a.borrow()) == (*b.borrow())) {} + if ((*a.borrow()) < (*b.borrow())) {} + assert!(((*a.borrow()) == (*b.borrow()))); + assert!( + (!(({ + (*a.borrow_mut()) = (*b.borrow()); + (*a.borrow()) + }) as i32) + != 0) + ); + let c: Value = >::default(); + (*c.borrow_mut()) = ({ + (*a.borrow_mut()) = (*b.borrow()); + (*a.borrow()) + } != 0); + (*c.borrow_mut()) = ((({ + (*b.borrow_mut()) = (*a.borrow()); + (*b.borrow()) + }) as i32) + != 0); + (*c.borrow_mut()) = ((*a.borrow()) != 0); + (*c.borrow_mut()) = ((*a.borrow()) == (*b.borrow())); + (*c.borrow_mut()) = ((*a.borrow()) < (*b.borrow())); + return 0; +} diff --git a/tests/unit/out/refcount/expr_as_bool_cpp.rs b/tests/unit/out/refcount/expr_as_bool_cpp.rs new file mode 100644 index 00000000..5cf39984 --- /dev/null +++ b/tests/unit/out/refcount/expr_as_bool_cpp.rs @@ -0,0 +1,52 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::Seek; +use std::io::{Read, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let a: Value = Rc::new(RefCell::new(0)); + let b: Value = >::default(); + if ({ + (*b.borrow_mut()) = (*a.borrow()); + (*b.borrow()) + } != 0) + {} + 'loop_: while ((({ + (*b.borrow_mut()) = (*a.borrow()); + (*b.borrow()) + }) as i32) + != 0) + {} + if ((*a.borrow()) != 0) {} + if ((*a.borrow()) == (*b.borrow())) {} + if ((*a.borrow()) < (*b.borrow())) {} + assert!(((*a.borrow()) == (*b.borrow()))); + assert!( + !((({ + (*a.borrow_mut()) = (*b.borrow()); + (*a.borrow()) + }) as i32) + != 0) + ); + let c: Value = >::default(); + (*c.borrow_mut()) = ({ + (*a.borrow_mut()) = (*b.borrow()); + (*a.borrow()) + } != 0); + (*c.borrow_mut()) = ((({ + (*b.borrow_mut()) = (*a.borrow()); + (*b.borrow()) + }) as i32) + != 0); + (*c.borrow_mut()) = ((*a.borrow()) != 0); + (*c.borrow_mut()) = ((*a.borrow()) == (*b.borrow())); + (*c.borrow_mut()) = ((*a.borrow()) < (*b.borrow())); + return 0; +} diff --git a/tests/unit/out/refcount/va_arg_chain.rs b/tests/unit/out/refcount/va_arg_chain.rs index 0dbdeef3..a08b1106 100644 --- a/tests/unit/out/refcount/va_arg_chain.rs +++ b/tests/unit/out/refcount/va_arg_chain.rs @@ -28,7 +28,7 @@ pub fn middle_layer_1(n: i32, ap: VaList) -> i32 { } pub fn top_level_2(n: i32, args: &[VaArg]) -> i32 { let n: Value = Rc::new(RefCell::new(n)); - let ap: Value = Rc::new(RefCell::new(::default())); + let ap: Value = >::default(); (*ap.borrow_mut()) = VaList::new(args); let result: Value = Rc::new(RefCell::new( ({ diff --git a/tests/unit/out/refcount/va_arg_concat.rs b/tests/unit/out/refcount/va_arg_concat.rs index c39321b1..e59f12a2 100644 --- a/tests/unit/out/refcount/va_arg_concat.rs +++ b/tests/unit/out/refcount/va_arg_concat.rs @@ -9,7 +9,7 @@ use std::os::fd::AsFd; use std::rc::{Rc, Weak}; pub fn sum_ints_0(first: i32, args: &[VaArg]) -> i32 { let first: Value = Rc::new(RefCell::new(first)); - let ap: Value = Rc::new(RefCell::new(::default())); + let ap: Value = >::default(); let total: Value = Rc::new(RefCell::new((*first.borrow()))); (*ap.borrow_mut()) = VaList::new(args); let val: Value = >::default(); diff --git a/tests/unit/out/refcount/va_arg_conditional.rs b/tests/unit/out/refcount/va_arg_conditional.rs index 5717a301..f2bdf8ab 100644 --- a/tests/unit/out/refcount/va_arg_conditional.rs +++ b/tests/unit/out/refcount/va_arg_conditional.rs @@ -11,7 +11,7 @@ pub fn conditional_log_0(verbose: i32, fmt: Ptr, args: &[VaArg]) -> i32 { let verbose: Value = Rc::new(RefCell::new(verbose)); let fmt: Value> = Rc::new(RefCell::new(fmt)); if ((*verbose.borrow()) != 0) { - let ap: Value = Rc::new(RefCell::new(::default())); + let ap: Value = >::default(); (*ap.borrow_mut()) = VaList::new(args); let result: Value = Rc::new(RefCell::new(((*ap.borrow_mut()).arg::()).clone())); return (*result.borrow()); diff --git a/tests/unit/out/refcount/va_arg_copy.rs b/tests/unit/out/refcount/va_arg_copy.rs index f5cabeb4..3cc524fe 100644 --- a/tests/unit/out/refcount/va_arg_copy.rs +++ b/tests/unit/out/refcount/va_arg_copy.rs @@ -9,8 +9,8 @@ use std::os::fd::AsFd; use std::rc::{Rc, Weak}; pub fn sum_with_copy_0(count: i32, args: &[VaArg]) -> i32 { let count: Value = Rc::new(RefCell::new(count)); - let ap: Value = Rc::new(RefCell::new(::default())); - let aq: Value = Rc::new(RefCell::new(::default())); + let ap: Value = >::default(); + let aq: Value = >::default(); (*ap.borrow_mut()) = VaList::new(args); (*aq.borrow_mut()) = (*ap.borrow_mut()).clone(); let sum1: Value = Rc::new(RefCell::new(0)); diff --git a/tests/unit/out/refcount/va_arg_forward.rs b/tests/unit/out/refcount/va_arg_forward.rs index 25929e68..257e60d2 100644 --- a/tests/unit/out/refcount/va_arg_forward.rs +++ b/tests/unit/out/refcount/va_arg_forward.rs @@ -20,7 +20,7 @@ pub fn inner_0(count: i32, ap: VaList) -> i32 { } pub fn outer_1(count: i32, args: &[VaArg]) -> i32 { let count: Value = Rc::new(RefCell::new(count)); - let ap: Value = Rc::new(RefCell::new(::default())); + let ap: Value = >::default(); (*ap.borrow_mut()) = VaList::new(args); let result: Value = Rc::new(RefCell::new( ({ diff --git a/tests/unit/out/refcount/va_arg_mixed_int_ptr.rs b/tests/unit/out/refcount/va_arg_mixed_int_ptr.rs index 4cfaedc3..a8a3aaca 100644 --- a/tests/unit/out/refcount/va_arg_mixed_int_ptr.rs +++ b/tests/unit/out/refcount/va_arg_mixed_int_ptr.rs @@ -9,7 +9,7 @@ use std::os::fd::AsFd; use std::rc::{Rc, Weak}; pub fn mixed_args_0(count: i32, args: &[VaArg]) -> i32 { let count: Value = Rc::new(RefCell::new(count)); - let ap: Value = Rc::new(RefCell::new(::default())); + let ap: Value = >::default(); (*ap.borrow_mut()) = VaList::new(args); let total: Value = Rc::new(RefCell::new(0)); let i: Value = Rc::new(RefCell::new(0)); diff --git a/tests/unit/out/refcount/va_arg_mixed_types.rs b/tests/unit/out/refcount/va_arg_mixed_types.rs index c54047df..2dbb4bd2 100644 --- a/tests/unit/out/refcount/va_arg_mixed_types.rs +++ b/tests/unit/out/refcount/va_arg_mixed_types.rs @@ -9,7 +9,7 @@ use std::os::fd::AsFd; use std::rc::{Rc, Weak}; pub fn sum_mixed_0(count: i32, args: &[VaArg]) -> i32 { let count: Value = Rc::new(RefCell::new(count)); - let ap: Value = Rc::new(RefCell::new(::default())); + let ap: Value = >::default(); (*ap.borrow_mut()) = VaList::new(args); let total: Value = Rc::new(RefCell::new(0)); let i: Value = Rc::new(RefCell::new(0)); diff --git a/tests/unit/out/refcount/va_arg_printf.rs b/tests/unit/out/refcount/va_arg_printf.rs index 5fe7f0d8..10078dc7 100644 --- a/tests/unit/out/refcount/va_arg_printf.rs +++ b/tests/unit/out/refcount/va_arg_printf.rs @@ -17,7 +17,7 @@ pub fn logf_impl_0(fmt: Ptr, ap: VaList) -> i32 { } pub fn logf_1(fmt: Ptr, args: &[VaArg]) -> i32 { let fmt: Value> = Rc::new(RefCell::new(fmt)); - let ap: Value = Rc::new(RefCell::new(::default())); + let ap: Value = >::default(); (*ap.borrow_mut()) = VaList::new(args); let result: Value = Rc::new(RefCell::new( ({ diff --git a/tests/unit/out/refcount/va_arg_promotion.rs b/tests/unit/out/refcount/va_arg_promotion.rs index a7065516..7b7f0620 100644 --- a/tests/unit/out/refcount/va_arg_promotion.rs +++ b/tests/unit/out/refcount/va_arg_promotion.rs @@ -9,7 +9,7 @@ use std::os::fd::AsFd; use std::rc::{Rc, Weak}; pub fn test_promotions_0(count: i32, args: &[VaArg]) -> i32 { let count: Value = Rc::new(RefCell::new(count)); - let ap: Value = Rc::new(RefCell::new(::default())); + let ap: Value = >::default(); (*ap.borrow_mut()) = VaList::new(args); let a: Value = Rc::new(RefCell::new(((*ap.borrow_mut()).arg::()).clone())); let b: Value = Rc::new(RefCell::new(((*ap.borrow_mut()).arg::()).clone())); @@ -23,7 +23,7 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let x: Value = Rc::new(RefCell::new(('A' as u8))); + let x: Value = Rc::new(RefCell::new((('A' as i32) as u8))); let y: Value = Rc::new(RefCell::new(10_i16)); let z: Value = Rc::new(RefCell::new(3.0E+0)); assert!( diff --git a/tests/unit/out/refcount/va_arg_snprintf.rs b/tests/unit/out/refcount/va_arg_snprintf.rs index fcd28cbf..2d5ee15f 100644 --- a/tests/unit/out/refcount/va_arg_snprintf.rs +++ b/tests/unit/out/refcount/va_arg_snprintf.rs @@ -11,7 +11,7 @@ pub fn extract_first_0(buf: Ptr, size: i32, fmt: Ptr, args: &[VaArg]) -> let buf: Value> = Rc::new(RefCell::new(buf)); let size: Value = Rc::new(RefCell::new(size)); let fmt: Value> = Rc::new(RefCell::new(fmt)); - let ap: Value = Rc::new(RefCell::new(::default())); + let ap: Value = >::default(); (*ap.borrow_mut()) = VaList::new(args); let n: Value = Rc::new(RefCell::new(((*ap.borrow_mut()).arg::()).clone())); let __rhs = ((*n.borrow()) as u8); @@ -42,6 +42,6 @@ fn main_0() -> i32 { extract_first_0(_buf, _size, _fmt, &[65.into()]) }) == 65) ); - assert!((((*buf.borrow())[(0) as usize] as i32) == (('A' as u8) as i32))); + assert!((((*buf.borrow())[(0) as usize] as i32) == ('A' as i32))); return 0; } diff --git a/tests/unit/out/refcount/va_arg_two_passes.rs b/tests/unit/out/refcount/va_arg_two_passes.rs index d5362b99..4f6a25a4 100644 --- a/tests/unit/out/refcount/va_arg_two_passes.rs +++ b/tests/unit/out/refcount/va_arg_two_passes.rs @@ -9,7 +9,7 @@ use std::os::fd::AsFd; use std::rc::{Rc, Weak}; pub fn sum_then_product_0(first: i32, args: &[VaArg]) -> i32 { let first: Value = Rc::new(RefCell::new(first)); - let ap: Value = Rc::new(RefCell::new(::default())); + let ap: Value = >::default(); let sum: Value = Rc::new(RefCell::new((*first.borrow()))); let product: Value = Rc::new(RefCell::new((*first.borrow()))); (*ap.borrow_mut()) = VaList::new(args); diff --git a/tests/unit/out/unsafe/expr_as_bool.rs b/tests/unit/out/unsafe/expr_as_bool.rs new file mode 100644 index 00000000..25c79dbb --- /dev/null +++ b/tests/unit/out/unsafe/expr_as_bool.rs @@ -0,0 +1,24 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::Seek; +use std::io::{Read, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut a: i32 = 0; + let mut b: i32 = 0_i32; + if ({ + b = a; + b + } != 0) + {} + return 0; +} diff --git a/tests/unit/out/unsafe/expr_as_bool_c.rs b/tests/unit/out/unsafe/expr_as_bool_c.rs new file mode 100644 index 00000000..a136d356 --- /dev/null +++ b/tests/unit/out/unsafe/expr_as_bool_c.rs @@ -0,0 +1,54 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::Seek; +use std::io::{Read, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut a: i32 = 0; + let mut b: i32 = 0_i32; + if ({ + b = a; + b + } != 0) + {} + 'loop_: while ((({ + b = a; + b + }) as i32) + != (0)) + {} + if (a != 0) {} + if ((a) == (b)) {} + if ((a) < (b)) {} + assert!(((a) == (b))); + assert!( + (!(({ + a = b; + a + }) as i32) + != 0) + ); + let mut c: bool = false; + c = ({ + a = b; + a + } != 0); + c = ((({ + b = a; + b + }) as i32) + != (0)); + c = (a != 0); + c = ((a) == (b)); + c = ((a) < (b)); + return 0; +} diff --git a/tests/unit/out/unsafe/expr_as_bool_cpp.rs b/tests/unit/out/unsafe/expr_as_bool_cpp.rs new file mode 100644 index 00000000..a011f145 --- /dev/null +++ b/tests/unit/out/unsafe/expr_as_bool_cpp.rs @@ -0,0 +1,54 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::Seek; +use std::io::{Read, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut a: i32 = 0; + let mut b: i32 = 0_i32; + if ({ + b = a; + b + } != 0) + {} + 'loop_: while ((({ + b = a; + b + }) as i32) + != (0)) + {} + if (a != 0) {} + if ((a) == (b)) {} + if ((a) < (b)) {} + assert!(((a) == (b))); + assert!( + !((({ + a = b; + a + }) as i32) + != 0) + ); + let mut c: bool = false; + c = ({ + a = b; + a + } != 0); + c = ((({ + b = a; + b + }) as i32) + != (0)); + c = (a != 0); + c = ((a) == (b)); + c = ((a) < (b)); + return 0; +} diff --git a/tests/unit/out/unsafe/va_arg_concat.rs b/tests/unit/out/unsafe/va_arg_concat.rs index cddda762..ebd9cdb7 100644 --- a/tests/unit/out/unsafe/va_arg_concat.rs +++ b/tests/unit/out/unsafe/va_arg_concat.rs @@ -11,7 +11,7 @@ pub unsafe fn sum_ints_0(mut first: i32, args: &[VaArg]) -> i32 { let mut ap: VaList = ::default(); let mut total: i32 = first; ap = VaList::new(args); - let mut val: i32 = ::default(); + let mut val: i32 = 0_i32; 'loop_: while ((({ val = ap.arg::(); val diff --git a/tests/unit/out/unsafe/va_arg_promotion.rs b/tests/unit/out/unsafe/va_arg_promotion.rs index 5cef927b..7500a36c 100644 --- a/tests/unit/out/unsafe/va_arg_promotion.rs +++ b/tests/unit/out/unsafe/va_arg_promotion.rs @@ -24,7 +24,7 @@ pub fn main() { } } unsafe fn main_0() -> i32 { - let mut x: u8 = ('A' as u8); + let mut x: u8 = (('A' as i32) as u8); let mut y: i16 = 10_i16; let mut z: f32 = 3.0E+0; assert!( diff --git a/tests/unit/out/unsafe/va_arg_snprintf.rs b/tests/unit/out/unsafe/va_arg_snprintf.rs index 38ccf04e..40d91d60 100644 --- a/tests/unit/out/unsafe/va_arg_snprintf.rs +++ b/tests/unit/out/unsafe/va_arg_snprintf.rs @@ -25,7 +25,7 @@ pub fn main() { } } unsafe fn main_0() -> i32 { - let mut buf: [u8; 64] = [::default(); 64]; + let mut buf: [u8; 64] = [0_u8; 64]; assert!( ((unsafe { let _buf: *mut u8 = buf.as_mut_ptr(); @@ -43,6 +43,6 @@ unsafe fn main_0() -> i32 { extract_first_0(_buf, _size, _fmt, &[65.into()]) }) == (65)) ); - assert!(((buf[(0) as usize] as i32) == (('A' as u8) as i32))); + assert!(((buf[(0) as usize] as i32) == ('A' as i32))); return 0; } diff --git a/tests/unit/out/unsafe/va_arg_two_passes.rs b/tests/unit/out/unsafe/va_arg_two_passes.rs index 0ebe33ca..93f2df7c 100644 --- a/tests/unit/out/unsafe/va_arg_two_passes.rs +++ b/tests/unit/out/unsafe/va_arg_two_passes.rs @@ -12,7 +12,7 @@ pub unsafe fn sum_then_product_0(mut first: i32, args: &[VaArg]) -> i32 { let mut sum: i32 = first; let mut product: i32 = first; ap = VaList::new(args); - let mut val: i32 = ::default(); + let mut val: i32 = 0_i32; 'loop_: while ((({ val = ap.arg::(); val diff --git a/tests/unit/va_arg_chain.c b/tests/unit/va_arg_chain.c index 5b627395..c7233384 100644 --- a/tests/unit/va_arg_chain.c +++ b/tests/unit/va_arg_chain.c @@ -8,9 +8,7 @@ int extract_nth(int n, va_list ap) { return va_arg(ap, int); } -int middle_layer(int n, va_list ap) { - return extract_nth(n, ap); -} +int middle_layer(int n, va_list ap) { return extract_nth(n, ap); } int top_level(int n, ...) { va_list ap; diff --git a/tests/unit/va_arg_promotion.c b/tests/unit/va_arg_promotion.c index 7b74bbef..54b38526 100644 --- a/tests/unit/va_arg_promotion.c +++ b/tests/unit/va_arg_promotion.c @@ -6,11 +6,11 @@ int test_promotions(int count, ...) { va_start(ap, count); // char and short are promoted to int by the caller - int a = va_arg(ap, int); // was passed as char 'A' (65) - int b = va_arg(ap, int); // was passed as short 10 + int a = va_arg(ap, int); // was passed as char 'A' (65) + int b = va_arg(ap, int); // was passed as short 10 // float is promoted to double by the caller - double c = va_arg(ap, double); // was passed as float 3.0 + double c = va_arg(ap, double); // was passed as float 3.0 va_end(ap); diff --git a/tests/unit/va_arg_struct_ctx.c b/tests/unit/va_arg_struct_ctx.cpp similarity index 100% rename from tests/unit/va_arg_struct_ctx.c rename to tests/unit/va_arg_struct_ctx.cpp