Skip to content

Commit 1473070

Browse files
committed
utilities: annotation: Clean-up deprecated parsing and getter.
1 parent 0fb5d07 commit 1473070

File tree

6 files changed

+87
-106
lines changed

6 files changed

+87
-106
lines changed

src/bindgen/ir/annotation.rs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5+
use std::borrow::Cow;
56
use std::collections::hash_map::Entry;
67
use std::collections::HashMap;
78
use std::str::FromStr;
@@ -38,6 +39,12 @@ pub struct AnnotationSet {
3839
pub deprecated: Option<String>,
3940
}
4041

42+
pub enum DeprecatedNoteKind {
43+
Function,
44+
Struct,
45+
Enum,
46+
}
47+
4148
impl AnnotationSet {
4249
pub fn new() -> AnnotationSet {
4350
AnnotationSet {
@@ -55,25 +62,32 @@ impl AnnotationSet {
5562
self.must_use && config.language != Language::Cython
5663
}
5764

58-
pub(crate) fn deprecated_note(&self, config: &Config) -> Option<&str> {
65+
pub(crate) fn deprecated_note<'c>(
66+
&self,
67+
config: &'c Config,
68+
kind: DeprecatedNoteKind,
69+
) -> Option<Cow<'c, str>> {
70+
let note = self.deprecated.as_deref()?;
71+
5972
if config.language == Language::Cython {
6073
return None;
6174
}
6275

63-
self.deprecated.as_deref()
64-
}
65-
66-
pub(crate) fn format_deprecated_note(
67-
&self,
68-
format_without_note: Option<&str>,
69-
format_with_note: Option<&str>,
70-
note: &str,
71-
) -> Option<String> {
7276
if note.is_empty() {
73-
return format_without_note.map(|x| x.to_string());
77+
return Some(Cow::Borrowed(match kind {
78+
DeprecatedNoteKind::Enum => config.enumeration.deprecated.as_deref()?,
79+
DeprecatedNoteKind::Function => config.function.deprecated.as_deref()?,
80+
DeprecatedNoteKind::Struct => config.structure.deprecated.as_deref()?,
81+
}));
82+
}
83+
84+
let format = match kind {
85+
DeprecatedNoteKind::Enum => &config.enumeration.deprecated_with_note,
86+
DeprecatedNoteKind::Function => &config.function.deprecated_with_note,
87+
DeprecatedNoteKind::Struct => &config.structure.deprecated_with_note,
7488
}
75-
format_with_note
76-
.map(|format_with_note| format_with_note.replace("{}", format!("{:?}", note).as_str()))
89+
.as_ref()?;
90+
Some(Cow::Owned(format.replace("{}", &format!("{:?}", note))))
7791
}
7892

7993
pub fn load(attrs: &[syn::Attribute]) -> Result<AnnotationSet, String> {

src/bindgen/ir/enumeration.rs

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ use crate::bindgen::config::{Config, Language};
1010
use crate::bindgen::declarationtyperesolver::DeclarationTypeResolver;
1111
use crate::bindgen::dependencies::Dependencies;
1212
use crate::bindgen::ir::{
13-
AnnotationSet, AnnotationValue, Cfg, ConditionWrite, Documentation, Field, GenericArgument,
14-
GenericParams, GenericPath, Item, ItemContainer, Literal, Path, Repr, ReprStyle, Struct,
15-
ToCondition, Type,
13+
AnnotationSet, AnnotationValue, Cfg, ConditionWrite, DeprecatedNoteKind, Documentation, Field,
14+
GenericArgument, GenericParams, GenericPath, Item, ItemContainer, Literal, Path, Repr,
15+
ReprStyle, Struct, ToCondition, Type,
1616
};
1717
use crate::bindgen::library::Library;
1818
use crate::bindgen::mangle;
@@ -754,14 +754,11 @@ impl Enum {
754754
// If we need to specify size, then we have no choice but to create a typedef,
755755
// so `config.style` is not respected.
756756
write!(out, "enum");
757-
if let Some(note) = self.annotations.deprecated_note(config) {
758-
if let Some(note) = self.annotations.format_deprecated_note(
759-
config.enumeration.deprecated.as_deref(),
760-
config.enumeration.deprecated_with_note.as_deref(),
761-
note,
762-
) {
763-
write!(out, " {}", note);
764-
}
757+
if let Some(note) = self
758+
.annotations
759+
.deprecated_note(config, DeprecatedNoteKind::Enum)
760+
{
761+
write!(out, " {}", note);
765762
}
766763
write!(out, " {}", tag_name);
767764

@@ -779,14 +776,11 @@ impl Enum {
779776
out.write("typedef ");
780777
}
781778
out.write("enum");
782-
if let Some(note) = self.annotations.deprecated_note(config) {
783-
if let Some(note) = self.annotations.format_deprecated_note(
784-
config.enumeration.deprecated.as_deref(),
785-
config.enumeration.deprecated_with_note.as_deref(),
786-
note,
787-
) {
788-
write!(out, " {}", note);
789-
}
779+
if let Some(note) = self
780+
.annotations
781+
.deprecated_note(config, DeprecatedNoteKind::Enum)
782+
{
783+
write!(out, " {}", note);
790784
}
791785
if config.style.generate_tag() {
792786
write!(out, " {}", tag_name);
@@ -806,14 +800,11 @@ impl Enum {
806800
}
807801
}
808802

809-
if let Some(note) = self.annotations.deprecated_note(config) {
810-
if let Some(note) = self.annotations.format_deprecated_note(
811-
config.structure.deprecated.as_deref(),
812-
config.structure.deprecated_with_note.as_deref(),
813-
note,
814-
) {
815-
write!(out, " {}", note);
816-
}
803+
if let Some(note) = self
804+
.annotations
805+
.deprecated_note(config, DeprecatedNoteKind::Enum)
806+
{
807+
write!(out, " {}", note);
817808
}
818809

819810
write!(out, " {}", tag_name);
@@ -894,14 +885,11 @@ impl Enum {
894885
}
895886
}
896887

897-
if let Some(note) = self.annotations.deprecated_note(config) {
898-
if let Some(note) = self.annotations.format_deprecated_note(
899-
config.enumeration.deprecated.as_deref(),
900-
config.enumeration.deprecated_with_note.as_deref(),
901-
note,
902-
) {
903-
write!(out, " {} ", note);
904-
}
888+
if let Some(note) = self
889+
.annotations
890+
.deprecated_note(config, DeprecatedNoteKind::Struct)
891+
{
892+
write!(out, " {} ", note);
905893
}
906894

907895
if config.language != Language::C || config.style.generate_tag() {

src/bindgen/ir/function.rs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ use crate::bindgen::config::{Config, Language, Layout};
1212
use crate::bindgen::declarationtyperesolver::DeclarationTypeResolver;
1313
use crate::bindgen::dependencies::Dependencies;
1414
use crate::bindgen::ir::{
15-
AnnotationSet, Cfg, ConditionWrite, Documentation, GenericPath, Path, ToCondition, Type,
15+
AnnotationSet, Cfg, ConditionWrite, DeprecatedNoteKind, Documentation, GenericPath, Path,
16+
ToCondition, Type,
1617
};
1718
use crate::bindgen::library::Library;
1819
use crate::bindgen::monomorph::Monomorphs;
@@ -241,14 +242,11 @@ impl Source for Function {
241242
write!(out, "{} ", anno);
242243
}
243244
}
244-
if let Some(note) = func.annotations.deprecated_note(config) {
245-
if let Some(note) = func.annotations.format_deprecated_note(
246-
config.function.deprecated.as_deref(),
247-
config.function.deprecated_with_note.as_deref(),
248-
note,
249-
) {
250-
write!(out, "{} ", note);
251-
}
245+
if let Some(note) = func
246+
.annotations
247+
.deprecated_note(config, DeprecatedNoteKind::Function)
248+
{
249+
write!(out, "{} ", note);
252250
}
253251
}
254252
cdecl::write_func(out, func, Layout::Horizontal, config);
@@ -293,15 +291,12 @@ impl Source for Function {
293291
out.new_line();
294292
}
295293
}
296-
if let Some(note) = func.annotations.deprecated_note(config) {
297-
if let Some(note) = func.annotations.format_deprecated_note(
298-
config.function.deprecated.as_deref(),
299-
config.function.deprecated_with_note.as_deref(),
300-
note,
301-
) {
302-
write!(out, "{}", note);
303-
out.new_line();
304-
}
294+
if let Some(note) = func
295+
.annotations
296+
.deprecated_note(config, DeprecatedNoteKind::Function)
297+
{
298+
write!(out, "{}", note);
299+
out.new_line();
305300
}
306301
}
307302
cdecl::write_func(out, func, Layout::Vertical, config);

src/bindgen/ir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub mod ty;
2020
pub mod typedef;
2121
pub mod union;
2222

23-
pub use self::annotation::{AnnotationSet, AnnotationValue};
23+
pub use self::annotation::{AnnotationSet, AnnotationValue, DeprecatedNoteKind};
2424
pub use self::cfg::*;
2525
pub use self::constant::*;
2626
pub use self::documentation::Documentation;

src/bindgen/ir/structure.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ use crate::bindgen::config::{Config, Language, LayoutConfig};
1010
use crate::bindgen::declarationtyperesolver::DeclarationTypeResolver;
1111
use crate::bindgen::dependencies::Dependencies;
1212
use crate::bindgen::ir::{
13-
AnnotationSet, Cfg, ConditionWrite, Constant, Documentation, Field, GenericArgument,
14-
GenericParams, Item, ItemContainer, Path, Repr, ReprAlign, ReprStyle, ToCondition, Type,
15-
Typedef,
13+
AnnotationSet, Cfg, ConditionWrite, Constant, DeprecatedNoteKind, Documentation, Field,
14+
GenericArgument, GenericParams, Item, ItemContainer, Path, Repr, ReprAlign, ReprStyle,
15+
ToCondition, Type, Typedef,
1616
};
1717
use crate::bindgen::library::Library;
1818
use crate::bindgen::mangle;
@@ -455,14 +455,11 @@ impl Source for Struct {
455455
write!(out, " {}", anno);
456456
}
457457
}
458-
if let Some(note) = self.annotations.deprecated_note(config) {
459-
if let Some(note) = self.annotations.format_deprecated_note(
460-
config.structure.deprecated.as_deref(),
461-
config.structure.deprecated_with_note.as_deref(),
462-
note,
463-
) {
464-
write!(out, " {}", note);
465-
}
458+
if let Some(note) = self
459+
.annotations
460+
.deprecated_note(config, DeprecatedNoteKind::Struct)
461+
{
462+
write!(out, " {}", note);
466463
}
467464

468465
if config.language != Language::C || config.style.generate_tag() {

src/bindgen/utilities.rs

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -139,47 +139,34 @@ pub trait SynAttributeHelpers {
139139

140140
// #[deprecated]
141141
if attrs.has_attr_word("deprecated") {
142-
return Some("".to_string());
142+
return Some(String::new());
143143
}
144144

145145
// #[deprecated(note = "")]
146-
if let Some(attr) = attrs.iter().find(|attr| {
146+
let attr = attrs.iter().find(|attr| {
147147
if let Ok(syn::Meta::List(list)) = attr.parse_meta() {
148148
list.path.is_ident("deprecated")
149149
} else {
150150
false
151151
}
152-
}) {
153-
let args: syn::punctuated::Punctuated<syn::MetaNameValue, Token![,]> =
154-
match attr.parse_args_with(syn::punctuated::Punctuated::parse_terminated) {
155-
Ok(args) => args,
156-
Err(_) => {
157-
warn!("couldn't parse deprecated attribute");
158-
return None;
159-
}
160-
};
161-
162-
let lit = match args
163-
.iter()
164-
.find(|arg| arg.path.is_ident("note"))
165-
.map(|arg| &arg.lit)
166-
{
167-
Some(lit) => lit,
168-
None => {
169-
warn!("couldn't parse deprecated attribute: no `note` field");
152+
})?;
153+
154+
let args: syn::punctuated::Punctuated<syn::MetaNameValue, Token![,]> =
155+
match attr.parse_args_with(syn::punctuated::Punctuated::parse_terminated) {
156+
Ok(args) => args,
157+
Err(_) => {
158+
warn!("couldn't parse deprecated attribute");
170159
return None;
171160
}
172161
};
173162

174-
return if let syn::Lit::Str(lit) = lit {
175-
Some(lit.value())
176-
} else {
177-
warn!("deprecated attribute must be a string");
178-
None
179-
};
163+
let arg = args.iter().find(|arg| arg.path.is_ident("note"))?;
164+
if let syn::Lit::Str(ref lit) = arg.lit {
165+
Some(lit.value())
166+
} else {
167+
warn!("deprecated attribute must be a string");
168+
None
180169
}
181-
182-
None
183170
}
184171

185172
fn is_no_mangle(&self) -> bool {

0 commit comments

Comments
 (0)