Skip to content

Commit dfec347

Browse files
committed
Emit zeroed() for libc structs without default
1 parent 5cc91f7 commit dfec347

3 files changed

Lines changed: 85 additions & 0 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <clang/AST/APValue.h>
77
#include <clang/AST/ParentMapContext.h>
8+
#include <clang/Basic/SourceManager.h>
89
#include <llvm/ADT/DenseMap.h>
910
#include <llvm/Support/ConvertUTF.h>
1011

@@ -3073,6 +3074,12 @@ std::string Converter::GetDefaultAsStringFallback(clang::QualType qual_type) {
30733074
return std::move(buf).str();
30743075
}
30753076

3077+
if (auto record = qual_type->getAsRecordDecl()) {
3078+
if (ctx_.getSourceManager().isInSystemHeader(record->getLocation())) {
3079+
return std::format("std::mem::zeroed::<{}>()", ToString(qual_type));
3080+
}
3081+
}
3082+
30763083
return std::format("<{}>::default()", ToString(qual_type));
30773084
}
30783085

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// no-compile: refcount
2+
#include <assert.h>
3+
#include <netinet/in.h>
4+
#include <poll.h>
5+
#include <sys/stat.h>
6+
#include <time.h>
7+
8+
int main() {
9+
struct pollfd p;
10+
p.fd = -1;
11+
p.events = 0;
12+
p.revents = 0;
13+
14+
struct in_addr ia;
15+
ia.s_addr = 0;
16+
assert(ia.s_addr == 0);
17+
18+
struct tm t;
19+
t.tm_year = 124;
20+
t.tm_mon = 5;
21+
t.tm_mday = 15;
22+
assert(t.tm_year == 124);
23+
assert(t.tm_mon == 5);
24+
assert(t.tm_mday == 15);
25+
26+
struct sockaddr_in sa;
27+
sa.sin_family = AF_INET;
28+
sa.sin_port = 8080;
29+
assert(sa.sin_family == AF_INET);
30+
assert(sa.sin_port == 8080);
31+
32+
struct stat st;
33+
st.st_size = 1024;
34+
assert(st.st_size == 1024);
35+
assert(p.fd == -1);
36+
assert(p.events == 0);
37+
return 0;
38+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
8+
use std::rc::Rc;
9+
pub fn main() {
10+
unsafe {
11+
std::process::exit(main_0() as i32);
12+
}
13+
}
14+
unsafe fn main_0() -> i32 {
15+
let mut p: pollfd = std::mem::zeroed::<pollfd>();
16+
p.fd = -1_i32;
17+
p.events = 0_i16;
18+
p.revents = 0_i16;
19+
let mut ia: in_addr = std::mem::zeroed::<in_addr>();
20+
ia.s_addr = 0_u32;
21+
assert!(((((ia.s_addr) == (0_u32)) as i32) != 0));
22+
let mut t: tm = std::mem::zeroed::<tm>();
23+
t.tm_year = 124;
24+
t.tm_mon = 5;
25+
t.tm_mday = 15;
26+
assert!(((((t.tm_year) == (124)) as i32) != 0));
27+
assert!(((((t.tm_mon) == (5)) as i32) != 0));
28+
assert!(((((t.tm_mday) == (15)) as i32) != 0));
29+
let mut sa: sockaddr_in = std::mem::zeroed::<sockaddr_in>();
30+
sa.sin_family = 2_u16;
31+
sa.sin_port = 8080_u16;
32+
assert!(((((sa.sin_family as i32) == (2)) as i32) != 0));
33+
assert!(((((sa.sin_port as i32) == (8080)) as i32) != 0));
34+
let mut st: stat = std::mem::zeroed::<stat>();
35+
st.st_size = 1024_i64;
36+
assert!(((((st.st_size) == (1024_i64)) as i32) != 0));
37+
assert!(((((p.fd) == (-1_i32)) as i32) != 0));
38+
assert!(((((p.events as i32) == (0)) as i32) != 0));
39+
return 0;
40+
}

0 commit comments

Comments
 (0)