Skip to content

Commit 91740aa

Browse files
grandizzyDaniPopes
andauthored
fix(inspect): add flag to strip ir comments (foundry-rs#9825)
* fix(inspect): do not strip comments when yul print * Update crates/forge/bin/cmd/inspect.rs Co-authored-by: DaniPopes <[email protected]> --------- Co-authored-by: DaniPopes <[email protected]>
1 parent 24d18e2 commit 91740aa

File tree

2 files changed

+59
-7
lines changed

2 files changed

+59
-7
lines changed

crates/forge/bin/cmd/inspect.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,15 @@ pub struct InspectArgs {
3636
/// All build arguments are supported
3737
#[command(flatten)]
3838
build: BuildOpts,
39+
40+
/// Whether to remove comments when inspecting `ir` and `irOptimized` artifact fields.
41+
#[arg(long, short, help_heading = "Display options")]
42+
pub strip_yul_comments: bool,
3943
}
4044

4145
impl InspectArgs {
4246
pub fn run(self) -> Result<()> {
43-
let Self { contract, field, build } = self;
47+
let Self { contract, field, build, strip_yul_comments } = self;
4448

4549
trace!(target: "forge", ?field, ?contract, "running forge inspect");
4650

@@ -106,10 +110,10 @@ impl InspectArgs {
106110
print_json(&artifact.devdoc)?;
107111
}
108112
ContractArtifactField::Ir => {
109-
print_yul(artifact.ir.as_deref())?;
113+
print_yul(artifact.ir.as_deref(), strip_yul_comments)?;
110114
}
111115
ContractArtifactField::IrOptimized => {
112-
print_yul(artifact.ir_optimized.as_deref())?;
116+
print_yul(artifact.ir_optimized.as_deref(), strip_yul_comments)?;
113117
}
114118
ContractArtifactField::Metadata => {
115119
print_json(&artifact.metadata)?;
@@ -532,15 +536,19 @@ fn print_json_str(obj: &impl serde::Serialize, key: Option<&str>) -> Result<()>
532536
Ok(())
533537
}
534538

535-
fn print_yul(yul: Option<&str>) -> Result<()> {
539+
fn print_yul(yul: Option<&str>, strip_comments: bool) -> Result<()> {
536540
let Some(yul) = yul else {
537541
eyre::bail!("Could not get IR output");
538542
};
539543

540544
static YUL_COMMENTS: LazyLock<Regex> =
541-
LazyLock::new(|| Regex::new(r"(///.*\n\s*)|(\s*/\*\*.*\*/)").unwrap());
545+
LazyLock::new(|| Regex::new(r"(///.*\n\s*)|(\s*/\*\*.*?\*/)").unwrap());
542546

543-
sh_println!("{}", YUL_COMMENTS.replace_all(yul, ""))?;
547+
if strip_comments {
548+
sh_println!("{}", YUL_COMMENTS.replace_all(yul, ""))?;
549+
} else {
550+
sh_println!("{yul}")?;
551+
}
544552

545553
Ok(())
546554
}

crates/forge/tests/cli/cmd.rs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3062,7 +3062,51 @@ Compiler run successful!
30623062
// checks `forge inspect <contract> irOptimized works
30633063
forgetest_init!(can_inspect_ir_optimized, |_prj, cmd| {
30643064
cmd.args(["inspect", TEMPLATE_CONTRACT, "irOptimized"]);
3065-
cmd.assert_success();
3065+
cmd.assert_success().stdout_eq(str![[r#"
3066+
/// @use-src 0:"src/Counter.sol"
3067+
object "Counter_21" {
3068+
code {
3069+
{
3070+
/// @src 0:65:257 "contract Counter {..."
3071+
mstore(64, memoryguard(0x80))
3072+
...
3073+
"#]]);
3074+
3075+
// check inspect with strip comments
3076+
cmd.forge_fuse().args(["inspect", TEMPLATE_CONTRACT, "irOptimized", "-s"]);
3077+
cmd.assert_success().stdout_eq(str![[r#"
3078+
object "Counter_21" {
3079+
code {
3080+
{
3081+
mstore(64, memoryguard(0x80))
3082+
if callvalue()
3083+
...
3084+
"#]]);
3085+
});
3086+
3087+
// checks `forge inspect <contract> irOptimized works
3088+
forgetest_init!(can_inspect_ir, |_prj, cmd| {
3089+
cmd.args(["inspect", TEMPLATE_CONTRACT, "ir"]);
3090+
cmd.assert_success().stdout_eq(str![[r#"
3091+
3092+
/// @use-src 0:"src/Counter.sol"
3093+
object "Counter_21" {
3094+
code {
3095+
/// @src 0:65:257 "contract Counter {..."
3096+
mstore(64, memoryguard(128))
3097+
...
3098+
"#]]);
3099+
3100+
// check inspect with strip comments
3101+
cmd.forge_fuse().args(["inspect", TEMPLATE_CONTRACT, "ir", "-s"]);
3102+
cmd.assert_success().stdout_eq(str![[r#"
3103+
3104+
object "Counter_21" {
3105+
code {
3106+
mstore(64, memoryguard(128))
3107+
if callvalue() { revert_error_ca66f745a3ce8ff40e2ccaf1ad45db7774001b90d25810abd9040049be7bf4bb() }
3108+
...
3109+
"#]]);
30663110
});
30673111

30683112
// checks forge bind works correctly on the default project

0 commit comments

Comments
 (0)