Skip to content

Commit 5377589

Browse files
committed
chore: move preprocessor to foundry
1 parent 93b3c3f commit 5377589

File tree

9 files changed

+102
-871
lines changed

9 files changed

+102
-871
lines changed

crates/compilers/src/cache.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::{
44
buildinfo::RawBuildInfo,
55
compilers::{Compiler, CompilerSettings, Language},
66
output::Builds,
7-
preprocessor::interface_repr_hash,
87
resolver::GraphEdges,
98
ArtifactFile, ArtifactOutput, Artifacts, ArtifactsMap, Graph, OutputContext, Project,
109
ProjectPaths, ProjectPathsConfig, SourceCompilationKind,
@@ -26,6 +25,9 @@ use std::{
2625
time::{Duration, UNIX_EPOCH},
2726
};
2827

28+
mod iface;
29+
use iface::interface_repr_hash;
30+
2931
/// ethers-rs format version
3032
///
3133
/// `ethers-solc` uses a different format version id, but the actual format is consistent with

crates/compilers/src/compile/project.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,9 @@ use std::{
126126
pub(crate) type VersionedSources<'a, L, S> = HashMap<L, Vec<(Version, Sources, (&'a str, &'a S))>>;
127127

128128
/// Invoked before the actual compiler invocation and can override the input.
129-
/// Updates the list of identified cached mocks (if any) to be stored in cache and returns
130-
/// preprocessed compiler input.
129+
///
130+
/// Updates the list of identified cached mocks (if any) to be stored in cache and updates the
131+
/// compiler input.
131132
pub trait Preprocessor<C: Compiler>: Debug {
132133
fn preprocess(
133134
&self,

crates/compilers/src/config.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,23 @@ impl<L> ProjectPathsConfig<L> {
250250
Self::dapptools(&std::env::current_dir().map_err(|err| SolcError::io(err, "."))?)
251251
}
252252

253-
pub(crate) fn is_test_or_script(&self, path: &Path) -> bool {
253+
/// Returns true if the given path is a test or script file.
254+
pub fn is_test_or_script(&self, path: &Path) -> bool {
255+
self.is_test(path) || self.is_script(path)
256+
}
257+
258+
/// Returns true if the given path is a test file.
259+
pub fn is_test(&self, path: &Path) -> bool {
254260
path_starts_with_rooted(path, &self.tests, &self.root)
255-
|| path_starts_with_rooted(path, &self.scripts, &self.root)
256261
}
257262

258-
pub(crate) fn is_source_file(&self, path: &Path) -> bool {
263+
/// Returns true if the given path is a script file.
264+
pub fn is_script(&self, path: &Path) -> bool {
265+
path_starts_with_rooted(path, &self.scripts, &self.root)
266+
}
267+
268+
/// Returns true if the given path is a test or script file.
269+
pub fn is_source_file(&self, path: &Path) -> bool {
259270
!self.is_test_or_script(path)
260271
}
261272

@@ -692,6 +703,26 @@ impl ProjectPaths {
692703
.collect();
693704
self
694705
}
706+
707+
/// Returns true if the given path is a test or script file.
708+
pub fn is_test_or_script(&self, path: &Path) -> bool {
709+
self.is_test(path) || self.is_script(path)
710+
}
711+
712+
/// Returns true if the given path is a test file.
713+
pub fn is_test(&self, path: &Path) -> bool {
714+
path.starts_with(&self.tests)
715+
}
716+
717+
/// Returns true if the given path is a script file.
718+
pub fn is_script(&self, path: &Path) -> bool {
719+
path.starts_with(&self.scripts)
720+
}
721+
722+
/// Returns true if the given path is a test or script file.
723+
pub fn is_source_file(&self, path: &Path) -> bool {
724+
!self.is_test_or_script(path)
725+
}
695726
}
696727

697728
impl Default for ProjectPaths {

crates/compilers/src/lib.rs

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ pub use resolver::Graph;
2424
pub mod compilers;
2525
pub use compilers::*;
2626

27-
pub mod preprocessor;
28-
2927
mod compile;
3028
pub use compile::{
3129
output::{AggregatedCompilerOutput, ProjectCompileOutput},
@@ -41,8 +39,9 @@ pub use filter::{FileFilter, SparseOutputFilter, TestFileFilter};
4139
pub mod report;
4240

4341
/// Updates to be applied to the sources.
44-
/// source_path -> (start, end, new_value)
45-
pub(crate) type Updates = HashMap<PathBuf, BTreeSet<(usize, usize, String)>>;
42+
///
43+
/// `source_path -> (start, end, new_value)`
44+
pub type Updates = HashMap<PathBuf, BTreeSet<(usize, usize, String)>>;
4645

4746
/// Utilities for creating, mocking and testing of (temporary) projects
4847
#[cfg(feature = "project-util")]
@@ -65,6 +64,8 @@ use foundry_compilers_core::error::{Result, SolcError, SolcIoError};
6564
use output::sources::{VersionedSourceFile, VersionedSourceFiles};
6665
use project::ProjectCompiler;
6766
use semver::Version;
67+
use solar_parse::Parser;
68+
use solar_sema::interface::{diagnostics::EmittedDiagnostics, source_map::FileName, Session};
6869
use solc::SolcSettings;
6970
use std::{
7071
collections::{BTreeMap, BTreeSet, HashMap, HashSet},
@@ -891,7 +892,7 @@ fn rebase_path(base: &Path, path: &Path) -> PathBuf {
891892
}
892893

893894
/// Utility function to apply a set of updates to provided sources.
894-
fn apply_updates(sources: &mut Sources, updates: Updates) {
895+
pub fn apply_updates(sources: &mut Sources, updates: Updates) {
895896
for (path, source) in sources {
896897
if let Some(updates) = updates.get(path) {
897898
source.content = Arc::new(replace_source_content(
@@ -904,20 +905,42 @@ fn apply_updates(sources: &mut Sources, updates: Updates) {
904905

905906
/// Utility function to change source content ranges with provided updates.
906907
/// Assumes that the updates are sorted.
907-
fn replace_source_content<'a>(
908-
source: &str,
909-
updates: impl IntoIterator<Item = (Range<usize>, &'a str)>,
908+
pub fn replace_source_content<'a>(
909+
source: impl Into<String>,
910+
updates: impl IntoIterator<Item = (Range<usize>, impl AsRef<str>)>,
910911
) -> String {
911912
let mut offset = 0;
912-
let mut content = source.as_bytes().to_vec();
913+
let mut content = source.into();
913914
for (range, new_value) in updates {
914915
let update_range = utils::range_by_offset(&range, offset);
915-
916-
content.splice(update_range.start..update_range.end, new_value.bytes());
916+
let new_value = new_value.as_ref();
917+
content.replace_range(update_range.clone(), new_value);
917918
offset += new_value.len() as isize - (update_range.end - update_range.start) as isize;
918919
}
920+
content
921+
}
919922

920-
String::from_utf8(content).unwrap()
923+
pub(crate) fn parse_one_source<R>(
924+
content: &str,
925+
path: &Path,
926+
f: impl FnOnce(solar_sema::ast::SourceUnit<'_>) -> R,
927+
) -> Result<R, EmittedDiagnostics> {
928+
let sess = Session::builder().with_buffer_emitter(Default::default()).build();
929+
let res = sess.enter(|| -> solar_parse::interface::Result<_> {
930+
let arena = solar_parse::ast::Arena::new();
931+
let filename = FileName::Real(path.to_path_buf());
932+
let mut parser = Parser::from_source_code(&sess, &arena, filename, content.to_string())?;
933+
let ast = parser.parse_file().map_err(|e| e.emit())?;
934+
Ok(f(ast))
935+
});
936+
937+
// Return if any diagnostics emitted during content parsing.
938+
if let Err(err) = sess.emitted_errors().unwrap() {
939+
trace!("failed parsing {path:?}:\n{err}");
940+
return Err(err);
941+
}
942+
943+
Ok(res.unwrap())
921944
}
922945

923946
#[cfg(test)]

crates/compilers/src/preprocessor/data.rs

Lines changed: 0 additions & 200 deletions
This file was deleted.

0 commit comments

Comments
 (0)