Skip to content

Commit e94f57a

Browse files
Use CommonAttrs everywhere
1 parent fac4c0d commit e94f57a

File tree

11 files changed

+43
-48
lines changed

11 files changed

+43
-48
lines changed

crates/cxx-qt-gen/bat

Lines changed: 0 additions & 1 deletion
This file was deleted.

crates/cxx-qt-gen/src/generator/cpp/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,10 @@ impl GeneratedCppBlocks {
7272
.iter()
7373
.filter_map(|qobject| {
7474
// Skip if the cfg attributes are not resolved to true
75-
match try_eval_attributes(opt.cfg_evaluator.as_ref(), &qobject.declaration.common_attrs.cfgs)
76-
{
75+
match try_eval_attributes(
76+
opt.cfg_evaluator.as_ref(),
77+
&qobject.declaration.common_attrs.cfgs,
78+
) {
7779
Ok(true) => {
7880
Some(GeneratedCppQObject::from(qobject, &parser.type_names, opt))
7981
}

crates/cxx-qt-gen/src/generator/rust/externcxxqt.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ use crate::{
77
generator::rust::{fragment::GeneratedRustFragment, signals::generate_rust_signal},
88
naming::TypeNames,
99
parser::externcxxqt::ParsedExternCxxQt,
10-
syntax::path::path_compare_str,
1110
};
1211
use quote::quote;
13-
use syn::{parse_quote, Attribute, Item, Result};
12+
use syn::{parse_quote, Item, Result};
1413

1514
impl GeneratedRustFragment {
1615
pub fn from_extern_cxx_qt(
@@ -60,19 +59,9 @@ impl GeneratedRustFragment {
6059
#[cxx_name = #cxx_name]
6160
}
6261
};
63-
// TODO! Can we make extract_docs return references, and then use here?
64-
let cfgs: Vec<&Attribute> = obj
65-
.declaration
66-
.attrs
67-
.iter()
68-
.filter(|attr| path_compare_str(attr.meta.path(), &["cfg"]))
69-
.collect();
70-
let docs: Vec<&Attribute> = obj
71-
.declaration
72-
.attrs
73-
.iter()
74-
.filter(|attr| path_compare_str(attr.meta.path(), &["doc"]))
75-
.collect();
62+
// TODO: (cfg everywhere) Can we make extract_docs return references, and then use here?
63+
let cfgs = obj.common_attrs.cfgs.iter().collect::<Vec<_>>();
64+
let docs = obj.common_attrs.docs.iter().collect::<Vec<_>>();
7665
qobject_items.push(parse_quote! {
7766
#namespace
7867
#cxx_name

crates/cxx-qt-gen/src/generator/rust/qobject.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//
44
// SPDX-License-Identifier: MIT OR Apache-2.0
55
use crate::generator::structuring::StructuredQObject;
6+
use crate::parser::CommonAttrs;
67
use crate::{
78
generator::{
89
naming::{namespace::NamespaceName, qobject::QObjectNames},
@@ -15,7 +16,7 @@ use crate::{
1516
naming::TypeNames,
1617
};
1718
use quote::quote;
18-
use syn::{parse_quote, Attribute, Result};
19+
use syn::{parse_quote, Result};
1920

2021
impl GeneratedRustFragment {
2122
pub fn from_qobject(
@@ -28,7 +29,7 @@ impl GeneratedRustFragment {
2829
let namespace_idents = NamespaceName::from(qobject);
2930

3031
let mut generated = vec![
31-
generate_qobject_definitions(&qobject_names, &qobject.common_attrs.cfgs, &qobject.common_attrs.docs)?,
32+
generate_qobject_definitions(&qobject_names, &qobject.common_attrs)?,
3233
generate_rust_properties(
3334
&qobject.properties,
3435
&qobject_names,
@@ -87,9 +88,11 @@ impl GeneratedRustFragment {
8788
/// Generate the C++ and Rust CXX definitions for the QObject
8889
fn generate_qobject_definitions(
8990
qobject_idents: &QObjectNames,
90-
cfgs: &[Attribute],
91-
docs: &[Attribute],
91+
common_attrs: &CommonAttrs,
9292
) -> Result<GeneratedRustFragment> {
93+
let docs = &common_attrs.docs;
94+
let cfgs = &common_attrs.cfgs;
95+
9396
let cpp_class_name_rust = &qobject_idents.name.rust_unqualified();
9497
let cpp_class_name_cpp = &qobject_idents.name.cxx_unqualified();
9598

crates/cxx-qt-gen/src/parser/externcxxqt.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//
44
// SPDX-License-Identifier: MIT OR Apache-2.0
55

6+
use crate::parser::CommonAttrs;
67
use crate::{
78
parser::{
89
externqobject::ParsedExternQObject, require_attributes, signals::ParsedSignal,
@@ -11,10 +12,8 @@ use crate::{
1112
syntax::{attribute::attribute_get_path, expr::expr_to_string},
1213
};
1314
use syn::{
14-
spanned::Spanned, Error, ForeignItem, ForeignItemFn, Ident, ItemForeignMod, Result,
15-
Token,
15+
spanned::Spanned, Error, ForeignItem, ForeignItemFn, Ident, ItemForeignMod, Result, Token,
1616
};
17-
use crate::parser::CommonAttrs;
1817

1918
/// Representation of an extern "C++Qt" block
2019
#[derive(Default)]
@@ -39,10 +38,10 @@ impl ParsedExternCxxQt {
3938
module_ident: &Ident,
4039
parent_namespace: Option<&str>,
4140
) -> Result<Self> {
42-
// TODO: support cfg on foreign mod blocks
41+
// TODO: (cfg everywhere) support cfg on foreign mod blocks
4342
let (attrs, common_attrs) = require_attributes(
4443
&foreign_mod.attrs,
45-
&["namespace", "doc", "auto_cxx_name", "auto_rust_name"],
44+
&["doc", "namespace", "auto_cxx_name", "auto_rust_name"],
4645
)?;
4746

4847
let auto_case = CaseConversion::from_attrs(&attrs)?;
@@ -72,7 +71,7 @@ impl ParsedExternCxxQt {
7271
ForeignItem::Type(foreign_ty) => {
7372
// Test that there is a #[qobject] attribute on any type
7473
//
75-
// TODO: what happens to any docs here?
74+
// TODO: (cfg everywhere) what happens to any docs here?
7675
if attribute_get_path(&foreign_ty.attrs, &["qobject"]).is_some() {
7776
let extern_ty =
7877
ParsedExternQObject::parse(foreign_ty, module_ident, parent_namespace)?;

crates/cxx-qt-gen/src/parser/externqobject.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//
44
// SPDX-License-Identifier: MIT OR Apache-2.0
55
use crate::naming::Name;
6-
use crate::parser::{parse_base_type, require_attributes, CaseConversion};
6+
use crate::parser::{parse_base_type, require_attributes, CaseConversion, CommonAttrs};
77
use syn::{ForeignItemType, Ident, Result};
88

99
/// A representation of a QObject to be generated in an extern C++ block
@@ -14,15 +14,17 @@ pub struct ParsedExternQObject {
1414
pub declaration: ForeignItemType,
1515
/// The base class of the struct
1616
pub base_class: Option<Ident>,
17+
/// Common attrs for this object
18+
pub common_attrs: CommonAttrs,
1719
}
1820

1921
impl ParsedExternQObject {
2022
const ALLOWED_ATTRS: [&'static str; 7] = [
23+
"cfg",
24+
"doc",
2125
"cxx_name",
2226
"rust_name",
2327
"namespace",
24-
"cfg",
25-
"doc",
2628
"qobject",
2729
"base",
2830
];
@@ -32,7 +34,8 @@ impl ParsedExternQObject {
3234
module_ident: &Ident,
3335
parent_namespace: Option<&str>,
3436
) -> Result<ParsedExternQObject> {
35-
let (attributes, _common_attrs) = require_attributes(&ty.attrs, &Self::ALLOWED_ATTRS)?;
37+
// TODO: (cfg everywhere) should these have docs kept with them in the generated code?
38+
let (attributes, common_attrs) = require_attributes(&ty.attrs, &Self::ALLOWED_ATTRS)?;
3639

3740
let base_class = parse_base_type(&attributes)?;
3841

@@ -46,6 +49,7 @@ impl ParsedExternQObject {
4649
)?,
4750
declaration: ty,
4851
base_class,
52+
common_attrs,
4953
})
5054
}
5155
}

crates/cxx-qt-gen/src/parser/externrustqt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl ParsedExternRustQt {
3737
module_ident: &Ident,
3838
parent_namespace: Option<&str>,
3939
) -> Result<Self> {
40-
// TODO: support cfg on foreign mod blocks
40+
// TODO: (cfg everywhere) support cfg on foreign mod blocks
4141
let (attrs, _common_attrs) = require_attributes(
4242
&foreign_mod.attrs,
4343
&["namespace", "auto_cxx_name", "auto_rust_name"],

crates/cxx-qt-gen/src/parser/inherit.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@ pub struct ParsedInheritedMethod {
1919

2020
impl ParsedInheritedMethod {
2121
const ALLOWED_ATTRS: [&'static str; 6] = [
22+
"cfg",
23+
"doc",
2224
"cxx_name",
2325
"rust_name",
2426
"qinvokable",
25-
"doc",
2627
"inherit",
27-
"cfg",
2828
];
2929

3030
pub fn parse(method: ForeignItemFn, auto_case: CaseConversion) -> Result<Self> {
3131
let (_attrs, common_attrs) = require_attributes(&method.attrs, &Self::ALLOWED_ATTRS)?;
3232

3333
Ok(Self {
3434
method_fields: MethodFields::parse(method, auto_case)?,
35-
common_attrs
35+
common_attrs,
3636
})
3737
}
3838

crates/cxx-qt-gen/src/parser/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ pub fn extract_cfgs(attrs: &[Attribute]) -> Vec<Attribute> {
104104
/// Iterate the attributes of the method to extract Doc attributes (doc comments are parsed as this)
105105
pub fn extract_docs(attrs: &[Attribute]) -> Vec<Attribute> {
106106
extract_attr(attrs, "doc")
107-
108107
}
109108

110109
/// Splits a path by :: separators e.g. "cxx_qt::bridge" becomes ["cxx_qt", "bridge"]
@@ -139,8 +138,7 @@ pub fn require_attributes<'a>(
139138
.position(|string| path_compare_str(attr.meta.path(), &split_path(string)));
140139
if let Some(index) = index {
141140
output.insert(allowed[index], attr); // Doesn't error on duplicates
142-
}
143-
else {
141+
} else {
144142
return Err(Error::new(
145143
attr.span(),
146144
format!(

crates/cxx-qt-gen/src/parser/qobject.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
//
44
// SPDX-License-Identifier: MIT OR Apache-2.0
55

6+
use crate::parser::CommonAttrs;
67
use crate::{
78
naming::Name,
8-
parser::{property::ParsedQProperty, require_attributes, parse_base_type, CaseConversion},
9+
parser::{parse_base_type, property::ParsedQProperty, require_attributes, CaseConversion},
910
syntax::{expr::expr_to_string, foreignmod::ForeignTypeIdentAlias, path::path_compare_str},
1011
};
1112
#[cfg(test)]
1213
use quote::format_ident;
1314
use syn::{Attribute, Error, Ident, Meta, Result};
14-
use crate::parser::CommonAttrs;
1515

1616
/// Metadata for registering QML element
1717
#[derive(Clone, Debug, Default, Eq, PartialEq)]
@@ -47,11 +47,11 @@ pub struct ParsedQObject {
4747

4848
impl ParsedQObject {
4949
const ALLOWED_ATTRS: [&'static str; 11] = [
50+
"cfg",
51+
"doc",
5052
"cxx_name",
5153
"rust_name",
5254
"namespace",
53-
"cfg",
54-
"doc",
5555
"qobject",
5656
"base",
5757
"qml_element",
@@ -76,7 +76,7 @@ impl ParsedQObject {
7676
common_attrs: CommonAttrs {
7777
docs: vec![],
7878
cfgs: vec![],
79-
}
79+
},
8080
}
8181
}
8282

@@ -87,7 +87,8 @@ impl ParsedQObject {
8787
module: &Ident,
8888
auto_case: CaseConversion,
8989
) -> Result<Self> {
90-
let (attributes, common_attrs) = require_attributes(&declaration.attrs, &Self::ALLOWED_ATTRS)?;
90+
let (attributes, common_attrs) =
91+
require_attributes(&declaration.attrs, &Self::ALLOWED_ATTRS)?;
9192

9293
let has_qobject_macro = attributes.contains_key("qobject");
9394

@@ -125,7 +126,7 @@ impl ParsedQObject {
125126
properties,
126127
qml_metadata,
127128
has_qobject_macro,
128-
common_attrs
129+
common_attrs,
129130
})
130131
}
131132

0 commit comments

Comments
 (0)