From f2045db0e9a5068546cf7ac50ec94d91412455bf Mon Sep 17 00:00:00 2001 From: Omar Date: Thu, 14 Aug 2025 10:38:56 +0300 Subject: [PATCH] Add compiler directives to metadata (#139) --- crates/compiler/src/lib.rs | 22 ++++++++++++++++++++ crates/compiler/src/revive_resolc.rs | 3 +++ crates/compiler/src/solc.rs | 10 +++++++++ crates/format/src/metadata.rs | 31 ++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+) diff --git a/crates/compiler/src/lib.rs b/crates/compiler/src/lib.rs index 551831e..4337d35 100644 --- a/crates/compiler/src/lib.rs +++ b/crates/compiler/src/lib.rs @@ -55,6 +55,7 @@ pub struct CompilerInput { pub base_path: Option, pub sources: HashMap, pub libraries: HashMap>, + pub revert_string_handling: Option, } /// The generic compilation output configuration. @@ -91,6 +92,7 @@ where base_path: Default::default(), sources: Default::default(), libraries: Default::default(), + revert_string_handling: Default::default(), }, additional_options: T::Options::default(), } @@ -142,6 +144,14 @@ where self } + pub fn with_revert_string_handling( + mut self, + revert_string_handling: impl Into>, + ) -> Self { + self.input.revert_string_handling = revert_string_handling.into(); + self + } + pub fn with_additional_options(mut self, options: impl Into) -> Self { self.additional_options = options.into(); self @@ -160,3 +170,15 @@ where self.input.clone() } } + +/// Defines how the compiler should handle revert strings. +#[derive( + Clone, Debug, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize, +)] +pub enum RevertString { + #[default] + Default, + Debug, + Strip, + VerboseDebug, +} diff --git a/crates/compiler/src/revive_resolc.rs b/crates/compiler/src/revive_resolc.rs index 0771211..0261de8 100644 --- a/crates/compiler/src/revive_resolc.rs +++ b/crates/compiler/src/revive_resolc.rs @@ -47,6 +47,9 @@ impl SolidityCompiler for Resolc { base_path, sources, libraries, + // TODO: this is currently not being handled since there is no way to pass it into + // resolc. So, we need to go back to this later once it's supported. + revert_string_handling: _, }: CompilerInput, additional_options: Self::Options, ) -> anyhow::Result { diff --git a/crates/compiler/src/solc.rs b/crates/compiler/src/solc.rs index a07cdb9..f714857 100644 --- a/crates/compiler/src/solc.rs +++ b/crates/compiler/src/solc.rs @@ -42,6 +42,7 @@ impl SolidityCompiler for Solc { base_path, sources, libraries, + revert_string_handling, }: CompilerInput, _: Self::Options, ) -> anyhow::Result { @@ -87,6 +88,15 @@ impl SolidityCompiler for Solc { }) .collect(), }, + debug: revert_string_handling.map(|revert_string_handling| DebuggingSettings { + revert_strings: match revert_string_handling { + crate::RevertString::Default => Some(RevertStrings::Default), + crate::RevertString::Debug => Some(RevertStrings::Debug), + crate::RevertString::Strip => Some(RevertStrings::Strip), + crate::RevertString::VerboseDebug => Some(RevertStrings::VerboseDebug), + }, + debug_info: Default::default(), + }), ..Default::default() }, }; diff --git a/crates/format/src/metadata.rs b/crates/format/src/metadata.rs index 7cfb5a4..2a21c73 100644 --- a/crates/format/src/metadata.rs +++ b/crates/format/src/metadata.rs @@ -75,6 +75,12 @@ pub struct Metadata { /// be run of the evm version of the nodes match the evm version specified here. #[serde(skip_serializing_if = "Option::is_none")] pub required_evm_version: Option, + + /// A set of compilation directives that will be passed to the compiler whenever the contracts for + /// the test are being compiled. Note that this differs from the [`Mode`]s in that a [`Mode`] is + /// just a filter for when a test can run whereas this is an instruction to the compiler. + #[serde(skip_serializing_if = "Option::is_none")] + pub compiler_directives: Option, } impl Metadata { @@ -490,6 +496,31 @@ impl From for String { } } +/// A set of compilation directives that will be passed to the compiler whenever the contracts for +/// the test are being compiled. Note that this differs from the [`Mode`]s in that a [`Mode`] is +/// just a filter for when a test can run whereas this is an instruction to the compiler. +/// Defines how the compiler should handle revert strings. +#[derive( + Clone, Debug, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize, +)] +pub struct CompilationDirectives { + /// Defines how the revert strings should be handled. + pub revert_string_handling: Option, +} + +/// Defines how the compiler should handle revert strings. +#[derive( + Clone, Debug, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize, +)] +#[serde(rename_all = "camelCase")] +pub enum RevertString { + #[default] + Default, + Debug, + Strip, + VerboseDebug, +} + #[cfg(test)] mod test { use super::*;