Skip to content

Commit 2ef0a51

Browse files
authored
Emit zeroed() for libc structs without default (#84)
1 parent 2ade29a commit 2ef0a51

3 files changed

Lines changed: 76 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

@@ -3090,6 +3091,12 @@ std::string Converter::GetDefaultAsStringFallback(clang::QualType qual_type) {
30903091
return std::move(buf).str();
30913092
}
30923093

3094+
if (auto record = qual_type->getAsRecordDecl()) {
3095+
if (ctx_.getSourceManager().isInSystemHeader(record->getLocation())) {
3096+
return std::format("std::mem::zeroed::<{}>()", ToString(qual_type));
3097+
}
3098+
}
3099+
30933100
return std::format("<{}>::default()", ToString(qual_type));
30943101
}
30953102

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 = 2;
13+
assert(p.fd == -1);
14+
assert(p.events == 0);
15+
assert(p.revents == 2);
16+
17+
struct in_addr ia;
18+
ia.s_addr = 1;
19+
assert(ia.s_addr == 1);
20+
21+
struct tm t;
22+
t.tm_year = 124;
23+
t.tm_mon = 5;
24+
t.tm_mday = 15;
25+
assert(t.tm_year == 124);
26+
assert(t.tm_mon == 5);
27+
assert(t.tm_mday == 15);
28+
29+
struct stat st;
30+
st.st_size = 1024;
31+
assert(st.st_size == 1024);
32+
return 0;
33+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 = 2_i16;
19+
assert!(((((p.fd) == (-1_i32)) as i32) != 0));
20+
assert!(((((p.events as i32) == (0)) as i32) != 0));
21+
assert!(((((p.revents as i32) == (2)) as i32) != 0));
22+
let mut ia: in_addr = std::mem::zeroed::<in_addr>();
23+
ia.s_addr = 1_u32;
24+
assert!(((((ia.s_addr) == (1_u32)) as i32) != 0));
25+
let mut t: tm = std::mem::zeroed::<tm>();
26+
t.tm_year = 124;
27+
t.tm_mon = 5;
28+
t.tm_mday = 15;
29+
assert!(((((t.tm_year) == (124)) as i32) != 0));
30+
assert!(((((t.tm_mon) == (5)) as i32) != 0));
31+
assert!(((((t.tm_mday) == (15)) as i32) != 0));
32+
let mut st: stat = std::mem::zeroed::<stat>();
33+
st.st_size = 1024_i64;
34+
assert!(((((st.st_size) == (1024_i64)) as i32) != 0));
35+
return 0;
36+
}

0 commit comments

Comments
 (0)