@@ -23,7 +23,10 @@ use solar_parse::{
2323 Parser ,
2424} ;
2525use solar_sema:: { hir:: Arena , ParsingContext } ;
26- use std:: path:: { Path , PathBuf } ;
26+ use std:: {
27+ collections:: HashSet ,
28+ path:: { Path , PathBuf } ,
29+ } ;
2730
2831mod data;
2932mod deps;
@@ -60,62 +63,65 @@ impl Preprocessor<SolcCompiler> for TestOptimizerPreprocessor {
6063 _solc : & SolcCompiler ,
6164 mut input : SolcVersionedInput ,
6265 paths : & ProjectPathsConfig < SolcLanguage > ,
63- ) -> Result < SolcVersionedInput > {
66+ ) -> Result < ( SolcVersionedInput , Option < HashSet < PathBuf > > ) > {
6467 let sources = & mut input. input . sources ;
6568 // Skip if we are not preprocessing any tests or scripts. Avoids unnecessary AST parsing.
6669 if sources. iter ( ) . all ( |( path, _) | !is_test_or_script ( path, paths) ) {
6770 trace ! ( "no tests or sources to preprocess" ) ;
68- return Ok ( input) ;
71+ return Ok ( ( input, None ) ) ;
6972 }
7073
7174 let sess = Session :: builder ( ) . with_buffer_emitter ( Default :: default ( ) ) . build ( ) ;
72- let _ = sess. enter_parallel ( || -> solar_parse:: interface:: Result < ( ) > {
73- let hir_arena = Arena :: new ( ) ;
74- let mut parsing_context = ParsingContext :: new ( & sess) ;
75- // Set remappings into HIR parsing context.
76- for remapping in & paths. remappings {
77- parsing_context
78- . file_resolver
79- . add_import_map ( PathBuf :: from ( & remapping. name ) , PathBuf :: from ( & remapping. path ) ) ;
80- }
81- // Load and parse test and script contracts only (dependencies are automatically
82- // resolved).
83- let preprocessed_paths = sources
84- . into_iter ( )
85- . filter ( |( path, source) | {
86- is_test_or_script ( path, paths) && !source. content . is_empty ( )
87- } )
88- . map ( |( path, _) | path. clone ( ) )
89- . collect_vec ( ) ;
90- parsing_context. load_files ( & preprocessed_paths) ?;
75+ let mocks =
76+ sess. enter_parallel ( || -> solar_parse:: interface:: Result < Option < HashSet < PathBuf > > > {
77+ let hir_arena = Arena :: new ( ) ;
78+ let mut parsing_context = ParsingContext :: new ( & sess) ;
79+ // Set remappings into HIR parsing context.
80+ for remapping in & paths. remappings {
81+ parsing_context. file_resolver . add_import_map (
82+ PathBuf :: from ( & remapping. name ) ,
83+ PathBuf :: from ( & remapping. path ) ,
84+ ) ;
85+ }
86+ // Load and parse test and script contracts only (dependencies are automatically
87+ // resolved).
88+ let preprocessed_paths = sources
89+ . into_iter ( )
90+ . filter ( |( path, source) | {
91+ is_test_or_script ( path, paths) && !source. content . is_empty ( )
92+ } )
93+ . map ( |( path, _) | path. clone ( ) )
94+ . collect_vec ( ) ;
95+ parsing_context. load_files ( & preprocessed_paths) ?;
9196
92- let hir = & parsing_context. parse_and_lower_to_hir ( & hir_arena) ?;
93- // Collect tests and scripts dependencies.
94- let deps = PreprocessorDependencies :: new (
95- & sess,
96- hir,
97- & preprocessed_paths,
98- & paths. paths_relative ( ) . sources ,
99- ) ;
100- // Collect data of source contracts referenced in tests and scripts.
101- let data = collect_preprocessor_data ( & sess, hir, & deps. referenced_contracts ) ;
97+ let hir = & parsing_context. parse_and_lower_to_hir ( & hir_arena) ?;
98+ // Collect tests and scripts dependencies.
99+ let deps = PreprocessorDependencies :: new (
100+ & sess,
101+ hir,
102+ & preprocessed_paths,
103+ & paths. paths_relative ( ) . sources ,
104+ & paths. root ,
105+ ) ;
106+ // Collect data of source contracts referenced in tests and scripts.
107+ let data = collect_preprocessor_data ( & sess, hir, & deps. referenced_contracts ) ;
102108
103- // Extend existing sources with preprocessor deploy helper sources.
104- sources. extend ( create_deploy_helpers ( & data) ) ;
109+ // Extend existing sources with preprocessor deploy helper sources.
110+ sources. extend ( create_deploy_helpers ( & data) ) ;
105111
106- // Generate and apply preprocessor source updates.
107- apply_updates ( sources, remove_bytecode_dependencies ( hir, & deps, & data) ) ;
112+ // Generate and apply preprocessor source updates.
113+ apply_updates ( sources, remove_bytecode_dependencies ( hir, & deps, & data) ) ;
108114
109- Ok ( ( ) )
110- } ) ;
115+ Ok ( Some ( deps . mocks ) )
116+ } ) ;
111117
112118 // Return if any diagnostics emitted during content parsing.
113119 if let Err ( err) = sess. emitted_errors ( ) . unwrap ( ) {
114120 trace ! ( "failed preprocessing {err}" ) ;
115121 return Err ( SolcError :: Message ( err. to_string ( ) ) ) ;
116122 }
117123
118- Ok ( input)
124+ Ok ( ( input, mocks . unwrap_or_default ( ) ) )
119125 }
120126}
121127
@@ -125,18 +131,18 @@ impl Preprocessor<MultiCompiler> for TestOptimizerPreprocessor {
125131 compiler : & MultiCompiler ,
126132 input : <MultiCompiler as Compiler >:: Input ,
127133 paths : & ProjectPathsConfig < MultiCompilerLanguage > ,
128- ) -> Result < <MultiCompiler as Compiler >:: Input > {
134+ ) -> Result < ( <MultiCompiler as Compiler >:: Input , Option < HashSet < PathBuf > > ) > {
129135 match input {
130136 MultiCompilerInput :: Solc ( input) => {
131137 if let Some ( solc) = & compiler. solc {
132138 let paths = paths. clone ( ) . with_language :: < SolcLanguage > ( ) ;
133- let input = self . preprocess ( solc, input, & paths) ?;
134- Ok ( MultiCompilerInput :: Solc ( input) )
139+ let ( input, mocks ) = self . preprocess ( solc, input, & paths) ?;
140+ Ok ( ( MultiCompilerInput :: Solc ( input) , mocks ) )
135141 } else {
136- Ok ( MultiCompilerInput :: Solc ( input) )
142+ Ok ( ( MultiCompilerInput :: Solc ( input) , None ) )
137143 }
138144 }
139- MultiCompilerInput :: Vyper ( input) => Ok ( MultiCompilerInput :: Vyper ( input) ) ,
145+ MultiCompilerInput :: Vyper ( input) => Ok ( ( MultiCompilerInput :: Vyper ( input) , None ) ) ,
140146 }
141147 }
142148}
0 commit comments