Skip to content

Commit 06c548e

Browse files
committed
fix #497
1 parent 5f8c24e commit 06c548e

File tree

2 files changed

+86
-77
lines changed

2 files changed

+86
-77
lines changed

src/generate/register.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ pub fn fields(
494494

495495
for v in &variants {
496496
let pc = &v.pc;
497-
let sc = &v.sc;
497+
let sc = &v.nksc;
498498

499499
let is_variant = Ident::new(
500500
&if sc.to_string().starts_with('_') {
@@ -762,6 +762,7 @@ fn unsafety(write_constraint: Option<&WriteConstraint>, width: u32) -> Option<Id
762762
struct Variant {
763763
doc: String,
764764
pc: Ident,
765+
nksc: Ident,
765766
sc: Ident,
766767
value: u64,
767768
}
@@ -779,13 +780,16 @@ impl Variant {
779780
anyhow!("EnumeratedValue {} has no `<value>` field", ev.name)
780781
})?);
781782

783+
let nksc = ev.name.to_sanitized_not_keyword_snake_case();
784+
let sc = util::sanitize_keyword(nksc.clone());
782785
Ok(Variant {
783786
doc: ev
784787
.description
785788
.clone()
786789
.unwrap_or_else(|| format!("`{:b}`", value)),
787790
pc: Ident::new(&ev.name.to_sanitized_upper_case(), span),
788-
sc: Ident::new(&ev.name.to_sanitized_snake_case(), span),
791+
nksc: Ident::new(&nksc, span),
792+
sc: Ident::new(&sc, span),
789793
value,
790794
})
791795
})

src/util.rs

Lines changed: 80 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -47,91 +47,96 @@ pub trait ToSanitizedUpperCase {
4747
}
4848

4949
pub trait ToSanitizedSnakeCase {
50-
fn to_sanitized_snake_case(&self) -> Cow<str>;
50+
fn to_sanitized_not_keyword_snake_case(&self) -> Cow<str>;
51+
fn to_sanitized_snake_case(&self) -> Cow<str> {
52+
let s = self.to_sanitized_not_keyword_snake_case();
53+
sanitize_keyword(s)
54+
}
5155
}
5256

5357
impl ToSanitizedSnakeCase for str {
54-
fn to_sanitized_snake_case(&self) -> Cow<str> {
55-
macro_rules! keywords {
56-
($s:expr, $($kw:ident),+,) => {
57-
Cow::from(match &$s.to_lowercase()[..] {
58-
$(stringify!($kw) => concat!(stringify!($kw), "_")),+,
59-
_ => return Cow::from($s.to_snake_case())
60-
})
61-
}
62-
}
63-
58+
fn to_sanitized_not_keyword_snake_case(&self) -> Cow<str> {
6459
let s = self.replace(BLACKLIST_CHARS, "");
65-
6660
match s.chars().next().unwrap_or('\0') {
6761
'0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' => {
6862
Cow::from(format!("_{}", s.to_snake_case()))
6963
}
70-
_ => {
71-
keywords! {
72-
s,
73-
abstract,
74-
alignof,
75-
as,
76-
async,
77-
await,
78-
become,
79-
box,
80-
break,
81-
const,
82-
continue,
83-
crate,
84-
do,
85-
else,
86-
enum,
87-
extern,
88-
false,
89-
final,
90-
fn,
91-
for,
92-
if,
93-
impl,
94-
in,
95-
let,
96-
loop,
97-
macro,
98-
match,
99-
mod,
100-
move,
101-
mut,
102-
offsetof,
103-
override,
104-
priv,
105-
proc,
106-
pub,
107-
pure,
108-
ref,
109-
return,
110-
self,
111-
sizeof,
112-
static,
113-
struct,
114-
super,
115-
trait,
116-
true,
117-
try,
118-
type,
119-
typeof,
120-
unsafe,
121-
unsized,
122-
use,
123-
virtual,
124-
where,
125-
while,
126-
yield,
127-
set_bit,
128-
clear_bit,
129-
bit,
130-
bits,
131-
}
132-
}
64+
_ => Cow::from(s.to_snake_case()),
65+
}
66+
}
67+
}
68+
69+
pub fn sanitize_keyword<'a>(s: Cow<'a, str>) -> Cow<'a, str> {
70+
macro_rules! keywords {
71+
($s:expr, $($kw:ident),+,) => {
72+
Cow::from(match &$s.to_lowercase()[..] {
73+
$(stringify!($kw) => concat!(stringify!($kw), "_")),+,
74+
_ => return s,
75+
})
13376
}
13477
}
78+
79+
keywords! {
80+
s,
81+
abstract,
82+
alignof,
83+
as,
84+
async,
85+
await,
86+
become,
87+
box,
88+
break,
89+
const,
90+
continue,
91+
crate,
92+
do,
93+
else,
94+
enum,
95+
extern,
96+
false,
97+
final,
98+
fn,
99+
for,
100+
if,
101+
impl,
102+
in,
103+
let,
104+
loop,
105+
macro,
106+
match,
107+
mod,
108+
move,
109+
mut,
110+
offsetof,
111+
override,
112+
priv,
113+
proc,
114+
pub,
115+
pure,
116+
ref,
117+
return,
118+
self,
119+
sizeof,
120+
static,
121+
struct,
122+
super,
123+
trait,
124+
true,
125+
try,
126+
type,
127+
typeof,
128+
unsafe,
129+
unsized,
130+
use,
131+
virtual,
132+
where,
133+
while,
134+
yield,
135+
set_bit,
136+
clear_bit,
137+
bit,
138+
bits,
139+
}
135140
}
136141

137142
impl ToSanitizedUpperCase for str {

0 commit comments

Comments
 (0)