1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use proc_macro2::Span;
use syn::Result as ParseResult;
use syn::{
parse::{Parse, ParseStream},
Token,
};
use crate::type_config::TypeConfig;
#[derive(PartialEq, Clone, Debug, Hash)]
pub enum UnsafePolicy {
AllFunctionsSafe,
AllFunctionsUnsafe,
}
impl Parse for UnsafePolicy {
fn parse(input: ParseStream) -> ParseResult<Self> {
if input.parse::<Option<Token![unsafe]>>()?.is_some() {
return Ok(UnsafePolicy::AllFunctionsSafe);
}
let r = match input.parse::<Option<syn::Ident>>()? {
Some(id) => {
if id == "unsafe_ffi" {
Ok(UnsafePolicy::AllFunctionsSafe)
} else {
Err(syn::Error::new(id.span(), "expected unsafe_ffi"))
}
}
None => Ok(UnsafePolicy::AllFunctionsUnsafe),
};
if !input.is_empty() {
return Err(syn::Error::new(
Span::call_site(),
"unexpected tokens within safety directive",
));
}
r
}
}
#[derive(Hash, Debug)]
pub struct IncludeCppConfig {
pub inclusions: Vec<String>,
pub exclude_utilities: bool,
pub unsafe_policy: UnsafePolicy,
pub type_config: TypeConfig,
pub parse_only: bool,
}
impl Parse for IncludeCppConfig {
fn parse(input: ParseStream) -> ParseResult<Self> {
let mut inclusions = Vec::new();
let mut parse_only = false;
let mut exclude_utilities = false;
let mut type_config = TypeConfig::new();
let mut unsafe_policy = UnsafePolicy::AllFunctionsUnsafe;
while !input.is_empty() {
let has_hexathorpe = input.parse::<Option<syn::Token![#]>>()?.is_some();
let ident: syn::Ident = input.parse()?;
if has_hexathorpe {
if ident != "include" {
return Err(syn::Error::new(ident.span(), "expected include"));
}
let hdr: syn::LitStr = input.parse()?;
inclusions.push(hdr.value());
} else {
input.parse::<Option<syn::Token![!]>>()?;
if ident == "generate" || ident == "generate_pod" {
let args;
syn::parenthesized!(args in input);
let generate: syn::LitStr = args.parse()?;
type_config.add_to_allowlist(generate.value());
if ident == "generate_pod" {
type_config.note_pod_request(generate.value());
}
} else if ident == "block" {
let args;
syn::parenthesized!(args in input);
let generate: syn::LitStr = args.parse()?;
type_config.add_to_blocklist(generate.value());
} else if ident == "parse_only" {
parse_only = true;
} else if ident == "exclude_utilities" {
exclude_utilities = true;
} else if ident == "safety" {
let args;
syn::parenthesized!(args in input);
unsafe_policy = args.parse()?;
} else {
return Err(syn::Error::new(
ident.span(),
"expected generate, generate_pod, nested_type, safety or exclude_utilities",
));
}
}
if input.is_empty() {
break;
}
}
if !exclude_utilities {
type_config.add_to_allowlist("make_string".to_string());
}
Ok(IncludeCppConfig {
inclusions,
exclude_utilities,
unsafe_policy,
type_config,
parse_only,
})
}
}
#[cfg(test)]
mod parse_tests {
use crate::config::UnsafePolicy;
use syn::parse_quote;
#[test]
fn test_safety_unsafe() {
let us: UnsafePolicy = parse_quote! {
unsafe
};
assert_eq!(us, UnsafePolicy::AllFunctionsSafe)
}
#[test]
fn test_safety_unsafe_ffi() {
let us: UnsafePolicy = parse_quote! {
unsafe_ffi
};
assert_eq!(us, UnsafePolicy::AllFunctionsSafe)
}
#[test]
fn test_safety_safe() {
let us: UnsafePolicy = parse_quote! {};
assert_eq!(us, UnsafePolicy::AllFunctionsUnsafe)
}
}