Skip to content

Commit

Permalink
fix: add fallback parser for contract names (#229)
Browse files Browse the repository at this point in the history
Fixes after bump foundry-rs/foundry#9433
  • Loading branch information
DaniPopes authored Nov 29, 2024
1 parent 569c3a1 commit 9589a49
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 10 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ cfg-if = "1.0"
dunce = "1.0"
md-5 = "0.10"
memmap2 = "0.9"
once_cell = "1.19"
path-slash = "0.2"
rayon = "1.8"
regex = "1.10"
Expand All @@ -54,7 +53,7 @@ similar-asserts = "1"
solar-parse = { version = "=0.1.0", default-features = false }
svm = { package = "svm-rs", version = "0.5", default-features = false }
tempfile = "3.9"
thiserror = "1"
thiserror = "2"
tracing = "0.1"
walkdir = "2.5"
yansi = "1.0"
Expand Down
2 changes: 0 additions & 2 deletions crates/compilers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ thiserror.workspace = true
path-slash.workspace = true
yansi.workspace = true
solar-parse.workspace = true
once_cell = { workspace = true, optional = true }
futures-util = { workspace = true, optional = true }
tokio = { workspace = true, optional = true }

Expand Down Expand Up @@ -85,7 +84,6 @@ svm-solc = [
"dep:svm-builds",
"dep:sha2",
"foundry-compilers-core/svm-solc",
"dep:once_cell",
]
# Utilities for creating and testing project workspaces.
project-util = [
Expand Down
4 changes: 2 additions & 2 deletions crates/compilers/src/compilers/solc/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ macro_rules! take_solc_installer_lock {
/// we should download.
/// The boolean value marks whether there was an error accessing the release list
#[cfg(feature = "svm-solc")]
pub static RELEASES: once_cell::sync::Lazy<(svm::Releases, Vec<Version>, bool)> =
once_cell::sync::Lazy::new(|| {
pub static RELEASES: std::sync::LazyLock<(svm::Releases, Vec<Version>, bool)> =
std::sync::LazyLock::new(|| {
match serde_json::from_str::<svm::Releases>(svm_builds::RELEASE_LIST_JSON) {
Ok(releases) => {
let sorted_versions = releases.clone().into_versions();
Expand Down
23 changes: 23 additions & 0 deletions crates/compilers/src/resolver/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ impl SolData {
if imports.is_empty() {
imports = capture_imports(content);
}
if contract_names.is_empty() {
utils::RE_CONTRACT_NAMES.captures_iter(content).for_each(|cap| {
contract_names.push(cap[1].to_owned());
});
}
}
let license = content.lines().next().and_then(|line| {
utils::capture_outer_and_inner(
Expand Down Expand Up @@ -313,6 +318,12 @@ mod tests {
assert_eq!(data.version_req, version_req.map(|v| v.parse().unwrap()), "src:\n{src}");
}

#[track_caller]
fn assert_contract_names(names: &[&str], src: &str) {
let data = SolData::parse(src, "test.sol".as_ref());
assert_eq!(data.contract_names, names, "src:\n{src}");
}

#[test]
fn soldata_parsing() {
assert_version(None, "");
Expand All @@ -332,6 +343,18 @@ contract BugReport {
}
"#,
);

assert_contract_names(
&["A", "B69$_", "C_", "$D"],
r#"
contract A {}
library B69$_ {}
abstract contract C_ {} interface $D {}
uint constant x = .1e10;
uint constant y = .1 ether;
"#,
);
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions crates/compilers/tests/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ use foundry_compilers_core::{
error::SolcError,
utils::{self, canonicalize, RuntimeOrHandle},
};
use once_cell::sync::Lazy;
use semver::Version;
use similar_asserts::assert_eq;
use std::{
Expand All @@ -39,10 +38,11 @@ use std::{
io,
path::{Path, PathBuf, MAIN_SEPARATOR},
str::FromStr,
sync::LazyLock,
};
use svm::{platform, Platform};

pub static VYPER: Lazy<Vyper> = Lazy::new(|| {
pub static VYPER: LazyLock<Vyper> = LazyLock::new(|| {
RuntimeOrHandle::new().block_on(async {
#[cfg(target_family = "unix")]
use std::{fs::Permissions, os::unix::fs::PermissionsExt};
Expand Down
1 change: 0 additions & 1 deletion crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ workspace = true
alloy-primitives.workspace = true
cfg-if.workspace = true
dunce.workspace = true
once_cell.workspace = true
path-slash.workspace = true
regex.workspace = true
semver.workspace = true
Expand Down
7 changes: 6 additions & 1 deletion crates/core/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use crate::error::{SolcError, SolcIoError};
use alloy_primitives::{hex, keccak256};
use cfg_if::cfg_if;
use once_cell::sync::Lazy;
use regex::{Match, Regex};
use semver::{Version, VersionReq};
use serde::{de::DeserializeOwned, Serialize};
Expand All @@ -13,6 +12,7 @@ use std::{
io::Write,
ops::Range,
path::{Component, Path, PathBuf},
sync::LazyLock as Lazy,
};
use walkdir::WalkDir;

Expand Down Expand Up @@ -46,6 +46,11 @@ pub static RE_THREE_OR_MORE_NEWLINES: Lazy<Regex> = Lazy::new(|| Regex::new("\n{
pub static RE_VYPER_VERSION: Lazy<Regex> =
Lazy::new(|| Regex::new(r"#(?:pragma version|@version)\s+(?P<version>.+)").unwrap());

/// A regex that matches the contract names in a Solidity file.
pub static RE_CONTRACT_NAMES: Lazy<Regex> = Lazy::new(|| {
Regex::new(r"\b(?:contract|library|abstract\s+contract|interface)\s+([\w$]+)").unwrap()
});

/// Extensions acceptable by solc compiler.
pub const SOLC_EXTENSIONS: &[&str] = &["sol", "yul"];

Expand Down

0 comments on commit 9589a49

Please sign in to comment.