11//! Source file parsing.
22use ignore:: Walk ;
33use itertools:: Itertools ;
4+ use log:: debug;
45use proc_macro2:: { Delimiter , Group } ;
56use rayon:: iter:: { IntoParallelIterator , ParallelIterator } ;
67use std:: {
@@ -185,6 +186,7 @@ pub fn parse_input(
185186 inputs
186187 . into_par_iter ( )
187188 . map ( |parser_input| {
189+ debug ! ( "Parsing file {:?}" , parser_input. file_path) ;
188190 // Performance nit: we don't need to clone in the error case;
189191 // map_err is taking unconditional ownership unnecessarily
190192 let content = std:: fs:: read_to_string ( & parser_input. file_path ) . map_err ( |err| {
@@ -285,7 +287,8 @@ pub fn parse(
285287) -> Result < Option < ParsedData > , ParseErrorSet > {
286288 // We will only produce output for files that contain the `#[typeshare]`
287289 // attribute, so this is a quick and easy performance win
288- if !source_code. contains ( "#[typeshare" ) {
290+ if !source_code. contains ( "typeshare" ) {
291+ debug ! ( "No typeshare found in file" ) ;
289292 return Ok ( None ) ;
290293 }
291294
@@ -296,7 +299,6 @@ pub fn parse(
296299 . map_err ( |err| ParseError :: new ( & err. span ( ) , ParseErrorKind :: SynError ( err) ) ) ?;
297300
298301 import_visitor. visit_file ( & file_contents) ;
299-
300302 import_visitor. parsed_data ( ) . map ( Some )
301303}
302304
@@ -690,12 +692,16 @@ fn parse_const_expr(e: &Expr) -> Result<RustConstExpr, ParseError> {
690692
691693// Helpers
692694
693- /// Checks the given attrs for `#[typeshare]`
695+ /// Checks the given attrs for `#[typeshare]` or `#[cfg_attr(<cond>, typeshare)]`
694696pub ( crate ) fn has_typeshare_annotation ( attrs : & [ syn:: Attribute ] ) -> bool {
697+ let check_cfg_attr = |attr| {
698+ get_meta_items ( attr, "cfg_attr" ) . iter ( ) . any ( |item| {
699+ matches ! ( item, Meta :: Path ( path) if path. segments. iter( ) . any( |segment| segment. ident == TYPESHARE ) )
700+ } )
701+ } ;
695702 attrs
696703 . iter ( )
697- . flat_map ( |attr| attr. path ( ) . segments . clone ( ) )
698- . any ( |segment| segment. ident == TYPESHARE )
704+ . any ( |attr| attr. path ( ) . is_ident ( TYPESHARE ) || check_cfg_attr ( attr) )
699705}
700706
701707pub ( crate ) fn serde_rename_all ( attrs : & [ syn:: Attribute ] ) -> Option < String > {
@@ -1096,4 +1102,10 @@ mod test_get_decorators {
10961102 ) ;
10971103 assert_eq ! ( decorators. type_override_for_lang( "kotlin" ) , None ) ;
10981104 }
1105+
1106+ #[ test]
1107+ fn test_cfg_attr ( ) {
1108+ let attr = parse_attr ( r#"#[cfg_attr(feature = "typeshare", typeshare)]"# ) ;
1109+ assert ! ( has_typeshare_annotation( & attr) ) ;
1110+ }
10991111}
0 commit comments