Skip to content

Commit

Permalink
Merge pull request #997 from dtolnay/cfg
Browse files Browse the repository at this point in the history
Store parsed cfg attributes into syntax tree
  • Loading branch information
dtolnay committed Jan 18, 2022
2 parents d8cec78 + 2476aff commit 246b995
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 13 deletions.
2 changes: 2 additions & 0 deletions gen/src/nested.rs
Expand Up @@ -52,6 +52,7 @@ fn sort_by_inner_namespace(apis: Vec<&Api>, depth: usize) -> NamespaceEntries {
mod tests {
use super::NamespaceEntries;
use crate::syntax::attrs::OtherAttrs;
use crate::syntax::cfg::CfgExpr;
use crate::syntax::namespace::Namespace;
use crate::syntax::{Api, Doc, ExternType, ForeignName, Lang, Lifetimes, Pair};
use proc_macro2::{Ident, Span};
Expand Down Expand Up @@ -128,6 +129,7 @@ mod tests {
fn make_api(ns: Option<&str>, ident: &str) -> Api {
let ns = ns.map_or(Namespace::ROOT, |ns| syn::parse_str(ns).unwrap());
Api::CxxType(ExternType {
cfg: CfgExpr::Unconditional,
lang: Lang::Rust,
doc: Doc::new(),
derives: Vec::new(),
Expand Down
3 changes: 3 additions & 0 deletions macro/src/expand.rs
@@ -1,5 +1,6 @@
use crate::syntax::atom::Atom::*;
use crate::syntax::attrs::{self, OtherAttrs};
use crate::syntax::cfg::CfgExpr;
use crate::syntax::file::Module;
use crate::syntax::instantiate::{ImplKey, NamedImplKey};
use crate::syntax::qualified::QualifiedName;
Expand All @@ -19,11 +20,13 @@ use syn::{parse_quote, punctuated, Generics, Lifetime, Result, Token};
pub fn bridge(mut ffi: Module) -> Result<TokenStream> {
let ref mut errors = Errors::new();

let mut cfg = CfgExpr::Unconditional;
let mut doc = Doc::new();
let attrs = attrs::parse(
errors,
mem::take(&mut ffi.attrs),
attrs::Parser {
cfg: Some(&mut cfg),
doc: Some(&mut doc),
..Default::default()
},
Expand Down
13 changes: 8 additions & 5 deletions syntax/attrs.rs
@@ -1,3 +1,4 @@
use crate::syntax::cfg::CfgExpr;
use crate::syntax::namespace::Namespace;
use crate::syntax::report::Errors;
use crate::syntax::Atom::{self, *};
Expand Down Expand Up @@ -27,6 +28,7 @@ use syn::{parenthesized, token, Attribute, Error, LitStr, Path, Result, Token};
//
#[derive(Default)]
pub struct Parser<'a> {
pub cfg: Option<&'a mut CfgExpr>,
pub doc: Option<&'a mut Doc>,
pub derives: Option<&'a mut Vec<Derive>>,
pub repr: Option<&'a mut Option<Atom>>,
Expand Down Expand Up @@ -128,11 +130,12 @@ pub fn parse(cx: &mut Errors, attrs: Vec<Attribute>, mut parser: Parser) -> Othe
} else if attr.path.is_ident("cfg") {
match cfg::parse_attribute.parse2(attr.tokens.clone()) {
Ok(cfg_expr) => {
// TODO
let _ = cfg_expr;
cx.error(&attr, "support for cfg attribute is not implemented yet");
passthrough_attrs.push(attr);
continue;
if let Some(cfg) = &mut parser.cfg {
cfg.merge(cfg_expr);
cx.error(&attr, "support for cfg attribute is not implemented yet");
passthrough_attrs.push(attr);
continue;
}
}
Err(err) => {
cx.push(err);
Expand Down
16 changes: 16 additions & 0 deletions syntax/cfg.rs
@@ -1,14 +1,30 @@
use proc_macro2::Ident;
use std::mem;
use syn::parse::{Error, ParseStream, Result};
use syn::{parenthesized, token, LitStr, Token};

#[derive(Clone)]
pub enum CfgExpr {
Unconditional,
Eq(Ident, Option<String>),
All(Vec<CfgExpr>),
Any(Vec<CfgExpr>),
Not(Box<CfgExpr>),
}

impl CfgExpr {
pub fn merge(&mut self, expr: CfgExpr) {
if let CfgExpr::Unconditional = self {
*self = expr;
} else if let CfgExpr::All(list) = self {
list.push(expr);
} else {
let prev = mem::replace(self, CfgExpr::Unconditional);
*self = CfgExpr::All(vec![prev, expr]);
}
}
}

pub fn parse_attribute(input: ParseStream) -> Result<CfgExpr> {
let content;
parenthesized!(content in input);
Expand Down
5 changes: 5 additions & 0 deletions syntax/impls.rs
Expand Up @@ -8,12 +8,14 @@ use std::ops::{Deref, DerefMut};
impl PartialEq for Include {
fn eq(&self, other: &Self) -> bool {
let Include {
cfg: _,
path,
kind,
begin_span: _,
end_span: _,
} = self;
let Include {
cfg: _,
path: path2,
kind: kind2,
begin_span: _,
Expand Down Expand Up @@ -335,6 +337,7 @@ impl PartialEq for Signature {
&& args.len() == args2.len()
&& args.iter().zip(args2).all(|(arg, arg2)| {
let Var {
cfg: _,
doc: _,
attrs: _,
visibility: _,
Expand All @@ -343,6 +346,7 @@ impl PartialEq for Signature {
ty,
} = arg;
let Var {
cfg: _,
doc: _,
attrs: _,
visibility: _,
Expand Down Expand Up @@ -372,6 +376,7 @@ impl Hash for Signature {
receiver.hash(state);
for arg in args {
let Var {
cfg: _,
doc: _,
attrs: _,
visibility: _,
Expand Down
11 changes: 10 additions & 1 deletion syntax/mod.rs
Expand Up @@ -2,7 +2,7 @@

pub mod atom;
pub mod attrs;
mod cfg;
pub mod cfg;
pub mod check;
pub mod derive;
mod discriminant;
Expand Down Expand Up @@ -31,6 +31,7 @@ pub mod types;
mod visit;

use self::attrs::OtherAttrs;
use self::cfg::CfgExpr;
use self::namespace::Namespace;
use self::parse::kw;
use self::symbol::Symbol;
Expand Down Expand Up @@ -60,6 +61,7 @@ pub enum Api {
}

pub struct Include {
pub cfg: CfgExpr,
pub path: String,
pub kind: IncludeKind,
pub begin_span: Span,
Expand All @@ -76,6 +78,7 @@ pub enum IncludeKind {
}

pub struct ExternType {
pub cfg: CfgExpr,
pub lang: Lang,
pub doc: Doc,
pub derives: Vec<Derive>,
Expand All @@ -91,6 +94,7 @@ pub struct ExternType {
}

pub struct Struct {
pub cfg: CfgExpr,
pub doc: Doc,
pub derives: Vec<Derive>,
pub attrs: OtherAttrs,
Expand All @@ -103,6 +107,7 @@ pub struct Struct {
}

pub struct Enum {
pub cfg: CfgExpr,
pub doc: Doc,
pub derives: Vec<Derive>,
pub attrs: OtherAttrs,
Expand Down Expand Up @@ -130,6 +135,7 @@ pub enum EnumRepr {
}

pub struct ExternFn {
pub cfg: CfgExpr,
pub lang: Lang,
pub doc: Doc,
pub attrs: OtherAttrs,
Expand All @@ -141,6 +147,7 @@ pub struct ExternFn {
}

pub struct TypeAlias {
pub cfg: CfgExpr,
pub doc: Doc,
pub derives: Vec<Derive>,
pub attrs: OtherAttrs,
Expand Down Expand Up @@ -183,6 +190,7 @@ pub struct Signature {
}

pub struct Var {
pub cfg: CfgExpr,
pub doc: Doc,
pub attrs: OtherAttrs,
pub visibility: Token![pub],
Expand All @@ -205,6 +213,7 @@ pub struct Receiver {
}

pub struct Variant {
pub cfg: CfgExpr,
pub doc: Doc,
pub attrs: OtherAttrs,
pub name: Pair,
Expand Down

0 comments on commit 246b995

Please sign in to comment.