Skip to content

Commit a48b275

Browse files
committed
fix: correctly create Session and ParsingContext from solc input
1 parent a9341af commit a48b275

File tree

3 files changed

+52
-22
lines changed

3 files changed

+52
-22
lines changed

crates/compilers/src/compilers/solc/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ pub struct SolcVersionedInput {
113113
#[serde(flatten)]
114114
pub input: SolcInput,
115115
#[serde(flatten)]
116-
cli_settings: CliSettings,
116+
pub cli_settings: CliSettings,
117117
}
118118

119119
impl CompilerInput for SolcVersionedInput {

crates/compilers/src/preprocessor/mod.rs

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,36 +44,27 @@ impl Preprocessor<SolcCompiler> for TestOptimizerPreprocessor {
4444
paths: &ProjectPathsConfig<SolcLanguage>,
4545
mocks: &mut HashSet<PathBuf>,
4646
) -> Result<()> {
47-
let sources = &mut input.input.sources;
4847
// Skip if we are not preprocessing any tests or scripts. Avoids unnecessary AST parsing.
49-
if sources.iter().all(|(path, _)| !paths.is_test_or_script(path)) {
48+
if !input.input.sources.iter().any(|(path, _)| paths.is_test_or_script(path)) {
5049
trace!("no tests or sources to preprocess");
5150
return Ok(());
5251
}
5352

54-
let sess = Session::builder().with_buffer_emitter(Default::default()).build();
53+
let sess = solar_session_from_solc(input);
5554
let _ = sess.enter_parallel(|| -> solar_parse::interface::Result {
5655
// Set up the parsing context with the project paths.
57-
let mut parsing_context = ParsingContext::new(&sess);
58-
parsing_context.file_resolver.set_current_dir(&paths.root);
59-
for remapping in &paths.remappings {
60-
parsing_context.file_resolver.add_import_remapping(
61-
solar_sema::interface::config::ImportRemapping {
62-
context: remapping.context.clone().unwrap_or_default(),
63-
prefix: remapping.name.clone(),
64-
path: remapping.path.clone(),
65-
},
66-
);
67-
}
68-
parsing_context.file_resolver.add_include_paths(paths.include_paths.iter().cloned());
56+
let mut parsing_context = solar_pcx_from_solc_no_sources(&sess, input, paths);
6957

7058
// Add the sources into the context.
59+
// Include all sources in the source map so as to not re-load them from disk, but only
60+
// parse and preprocess tests and scripts.
7161
let mut preprocessed_paths = vec![];
62+
let sources = &mut input.input.sources;
7263
for (path, source) in sources.iter() {
73-
if paths.is_test_or_script(path) {
74-
if let Ok(src_file) =
75-
sess.source_map().new_source_file(path.clone(), source.content.as_str())
76-
{
64+
if let Ok(src_file) =
65+
sess.source_map().new_source_file(path.clone(), source.content.as_str())
66+
{
67+
if paths.is_test_or_script(path) {
7768
parsing_context.add_file(src_file);
7869
preprocessed_paths.push(path.clone());
7970
}
@@ -134,6 +125,45 @@ impl Preprocessor<MultiCompiler> for TestOptimizerPreprocessor {
134125
}
135126
}
136127

128+
fn solar_session_from_solc(solc: &SolcVersionedInput) -> Session {
129+
use solar_parse::interface::config;
130+
131+
Session::builder()
132+
.with_buffer_emitter(Default::default())
133+
.opts(config::Opts {
134+
language: match solc.input.language {
135+
SolcLanguage::Solidity => config::Language::Solidity,
136+
SolcLanguage::Yul => config::Language::Yul,
137+
_ => unimplemented!(),
138+
},
139+
140+
// TODO: ...
141+
/*
142+
evm_version: solc.input.settings.evm_version,
143+
*/
144+
..Default::default()
145+
})
146+
.build()
147+
}
148+
149+
fn solar_pcx_from_solc_no_sources<'sess>(
150+
sess: &'sess Session,
151+
solc: &SolcVersionedInput,
152+
paths: &ProjectPathsConfig<impl crate::Language>,
153+
) -> ParsingContext<'sess> {
154+
let mut pcx = ParsingContext::new(sess);
155+
pcx.file_resolver.set_current_dir(solc.cli_settings.base_path.as_ref().unwrap_or(&paths.root));
156+
for remapping in &paths.remappings {
157+
pcx.file_resolver.add_import_remapping(solar_sema::interface::config::ImportRemapping {
158+
context: remapping.context.clone().unwrap_or_default(),
159+
prefix: remapping.name.clone(),
160+
path: remapping.path.clone(),
161+
});
162+
}
163+
pcx.file_resolver.add_include_paths(solc.cli_settings.include_paths.iter().cloned());
164+
pcx
165+
}
166+
137167
pub(crate) fn interface_repr_hash(content: &str, path: &Path) -> Option<String> {
138168
let src = interface_repr(content, path).ok()?;
139169
Some(foundry_compilers_artifacts::Source::content_hash_of(&src))

crates/compilers/src/resolver/parse.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ impl SolData {
7272
ast::ItemKind::Import(import) => {
7373
let path = import.path.value.to_string();
7474
let aliases = match &import.items {
75-
ast::ImportItems::Plain(None) | ast::ImportItems::Glob(None) => &[][..],
75+
ast::ImportItems::Plain(None) => &[][..],
7676
ast::ImportItems::Plain(Some(alias))
77-
| ast::ImportItems::Glob(Some(alias)) => &[(*alias, None)][..],
77+
| ast::ImportItems::Glob(alias) => &[(*alias, None)][..],
7878
ast::ImportItems::Aliases(aliases) => aliases,
7979
};
8080
let sol_import = SolImport::new(PathBuf::from(path)).set_aliases(

0 commit comments

Comments
 (0)