Skip to content

Commit 073bf1d

Browse files
committed
[cc] minor cleanups
1 parent 3f51c22 commit 073bf1d

File tree

3 files changed

+176
-3
lines changed

3 files changed

+176
-3
lines changed

cc/arch/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ pub fn get_arch_macros(target: &Target) -> Vec<(&'static str, Option<&'static st
3030
let mut macros = vec![
3131
// Common architecture macros based on type sizes
3232
("__CHAR_BIT__", Some("8")),
33-
// All our targets are 64-bit
34-
("__LP64__", Some("1")),
35-
("_LP64", Some("1")),
3633
("__SIZEOF_POINTER__", Some("8")),
3734
// Type sizes
3835
("__SIZEOF_SHORT__", Some("2")),
@@ -44,6 +41,12 @@ pub fn get_arch_macros(target: &Target) -> Vec<(&'static str, Option<&'static st
4441
("__CHAR_UNSIGNED__", char_unsigned),
4542
];
4643

44+
// LP64 macros only on LP64 targets (Unix), not on LLP64 (Windows)
45+
if target.long_width == 64 {
46+
macros.push(("__LP64__", Some("1")));
47+
macros.push(("_LP64", Some("1")));
48+
}
49+
4750
// Architecture-specific
4851
match target.arch {
4952
Arch::X86_64 => {

cc/tests/features/has_feature.rs

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
//
2+
// Copyright (c) 2024 Jeff Garzik
3+
//
4+
// This file is part of the posixutils-rs project covered under
5+
// the MIT License. For the full license text, please see the LICENSE
6+
// file in the root directory of this project.
7+
// SPDX-License-Identifier: MIT
8+
//
9+
// Tests for __has_builtin, __has_attribute, __has_feature, __has_extension
10+
//
11+
12+
use crate::common::compile_and_run;
13+
14+
/// Test __has_builtin for supported builtins
15+
#[test]
16+
fn has_builtin_supported() {
17+
let code = r#"
18+
int main(void) {
19+
// Test supported builtins return 1
20+
#if !__has_builtin(__builtin_constant_p)
21+
return 1;
22+
#endif
23+
24+
#if !__has_builtin(__builtin_types_compatible_p)
25+
return 2;
26+
#endif
27+
28+
#if !__has_builtin(__builtin_bswap16)
29+
return 3;
30+
#endif
31+
32+
#if !__has_builtin(__builtin_bswap32)
33+
return 4;
34+
#endif
35+
36+
#if !__has_builtin(__builtin_bswap64)
37+
return 5;
38+
#endif
39+
40+
#if !__has_builtin(__builtin_alloca)
41+
return 6;
42+
#endif
43+
44+
return 0;
45+
}
46+
"#;
47+
assert_eq!(compile_and_run("has_builtin_supported", code), 0);
48+
}
49+
50+
/// Test __has_builtin for unsupported builtins
51+
#[test]
52+
fn has_builtin_unsupported() {
53+
let code = r#"
54+
int main(void) {
55+
// Test unsupported/unknown builtins return 0
56+
#if __has_builtin(__builtin_nonexistent_thing)
57+
return 1;
58+
#endif
59+
60+
#if __has_builtin(__builtin_xyz_unknown)
61+
return 2;
62+
#endif
63+
64+
return 0;
65+
}
66+
"#;
67+
assert_eq!(compile_and_run("has_builtin_unsupported", code), 0);
68+
}
69+
70+
/// Test __has_attribute always returns 0 (not yet implemented)
71+
#[test]
72+
fn has_attribute_returns_zero() {
73+
let code = r#"
74+
int main(void) {
75+
// __has_attribute currently returns 0 for all attributes
76+
#if __has_attribute(unused)
77+
return 1;
78+
#endif
79+
80+
#if __has_attribute(noreturn)
81+
return 2;
82+
#endif
83+
84+
#if __has_attribute(packed)
85+
return 3;
86+
#endif
87+
88+
return 0;
89+
}
90+
"#;
91+
assert_eq!(compile_and_run("has_attribute_zero", code), 0);
92+
}
93+
94+
/// Test __has_feature always returns 0 (not yet implemented)
95+
#[test]
96+
fn has_feature_returns_zero() {
97+
let code = r#"
98+
int main(void) {
99+
// __has_feature currently returns 0 for all features
100+
#if __has_feature(c_alignas)
101+
return 1;
102+
#endif
103+
104+
#if __has_feature(c_static_assert)
105+
return 2;
106+
#endif
107+
108+
#if __has_feature(c_generic_selections)
109+
return 3;
110+
#endif
111+
112+
return 0;
113+
}
114+
"#;
115+
assert_eq!(compile_and_run("has_feature_zero", code), 0);
116+
}
117+
118+
/// Test __has_extension always returns 0 (not yet implemented)
119+
#[test]
120+
fn has_extension_returns_zero() {
121+
let code = r#"
122+
int main(void) {
123+
// __has_extension currently returns 0 for all extensions
124+
#if __has_extension(c_alignas)
125+
return 1;
126+
#endif
127+
128+
#if __has_extension(attribute_deprecated_with_message)
129+
return 2;
130+
#endif
131+
132+
return 0;
133+
}
134+
"#;
135+
assert_eq!(compile_and_run("has_extension_zero", code), 0);
136+
}
137+
138+
/// Test __has_builtin in complex preprocessor expressions
139+
#[test]
140+
fn has_builtin_complex_expressions() {
141+
let code = r#"
142+
int main(void) {
143+
// Test in logical AND
144+
#if __has_builtin(__builtin_constant_p) && __has_builtin(__builtin_bswap32)
145+
int ok1 = 1;
146+
#else
147+
return 1;
148+
#endif
149+
150+
// Test in logical OR
151+
#if __has_builtin(__builtin_nonexistent) || __has_builtin(__builtin_alloca)
152+
int ok2 = 1;
153+
#else
154+
return 2;
155+
#endif
156+
157+
// Test negation
158+
#if !__has_builtin(__builtin_nonexistent)
159+
int ok3 = 1;
160+
#else
161+
return 3;
162+
#endif
163+
164+
// Use the variables to avoid warnings
165+
return ok1 + ok2 + ok3 - 3;
166+
}
167+
"#;
168+
assert_eq!(compile_and_run("has_builtin_complex", code), 0);
169+
}

cc/tests/features/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ mod alloca;
1313
mod bswap;
1414
mod constant_p;
1515
mod debug;
16+
mod has_feature;
1617
mod storage;
1718
mod types_compatible;
1819
mod varargs;

0 commit comments

Comments
 (0)