Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <clang/AST/APValue.h>
#include <clang/AST/ParentMapContext.h>
#include <clang/Basic/SourceManager.h>
#include <llvm/ADT/DenseMap.h>
#include <llvm/Support/ConvertUTF.h>

Expand Down Expand Up @@ -3067,6 +3068,12 @@ std::string Converter::GetDefaultAsStringFallback(clang::QualType qual_type) {
return std::move(buf).str();
}

if (auto record = qual_type->getAsRecordDecl()) {
if (ctx_.getSourceManager().isInSystemHeader(record->getLocation())) {
return std::format("std::mem::zeroed::<{}>()", ToString(qual_type));
}
}

return std::format("<{}>::default()", ToString(qual_type));
}

Expand Down
33 changes: 33 additions & 0 deletions tests/unit/libc_struct_without_default.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// no-compile: refcount
#include <assert.h>
#include <netinet/in.h>
#include <poll.h>
#include <sys/stat.h>
#include <time.h>

int main() {
struct pollfd p;
p.fd = -1;
p.events = 0;
p.revents = 2;
assert(p.fd == -1);
assert(p.events == 0);
assert(p.revents == 2);

struct in_addr ia;
ia.s_addr = 1;
assert(ia.s_addr == 1);

struct tm t;
t.tm_year = 124;
t.tm_mon = 5;
t.tm_mday = 15;
assert(t.tm_year == 124);
assert(t.tm_mon == 5);
assert(t.tm_mday == 15);

struct stat st;
st.st_size = 1024;
assert(st.st_size == 1024);
return 0;
}
36 changes: 36 additions & 0 deletions tests/unit/out/unsafe/libc_struct_without_default.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
extern crate libc;
use libc::*;
extern crate libcc2rs;
use libcc2rs::*;
use std::collections::BTreeMap;
use std::io::{Read, Seek, 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 p: pollfd = std::mem::zeroed::<pollfd>();
p.fd = -1_i32;
p.events = 0_i16;
p.revents = 2_i16;
assert!(((((p.fd) == (-1_i32)) as i32) != 0));
assert!(((((p.events as i32) == (0)) as i32) != 0));
assert!(((((p.revents as i32) == (2)) as i32) != 0));
let mut ia: in_addr = std::mem::zeroed::<in_addr>();
ia.s_addr = 1_u32;
assert!(((((ia.s_addr) == (1_u32)) as i32) != 0));
let mut t: tm = std::mem::zeroed::<tm>();
t.tm_year = 124;
t.tm_mon = 5;
t.tm_mday = 15;
assert!(((((t.tm_year) == (124)) as i32) != 0));
assert!(((((t.tm_mon) == (5)) as i32) != 0));
assert!(((((t.tm_mday) == (15)) as i32) != 0));
let mut st: stat = std::mem::zeroed::<stat>();
st.st_size = 1024_i64;
assert!(((((st.st_size) == (1024_i64)) as i32) != 0));
return 0;
}
Loading