Skip to content

Commit

Permalink
citnames: config values are not cloned
Browse files Browse the repository at this point in the history
  • Loading branch information
rizsotto committed Jan 26, 2024
1 parent 78859c5 commit 5654978
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 26 deletions.
6 changes: 3 additions & 3 deletions source/citnames_rs/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub struct Compilation {
// When executable name matches it tries to parse the flags as it would
// be a known compiler, and append the additional flags to the output
// entry if the compiler is recognized.
#[derive(Clone, Debug, Deserialize, PartialEq)]
#[derive(Debug, Deserialize, PartialEq)]
pub struct CompilerToRecognize {
pub executable: PathBuf,
#[serde(default)]
Expand Down Expand Up @@ -90,7 +90,7 @@ impl Default for Format {
// This will act as a filter on the output elements.
// These attributes can be read from the configuration file, and can be
// overridden by command line arguments.
#[derive(Clone, Debug, Deserialize, PartialEq)]
#[derive(Debug, Deserialize, PartialEq)]
pub struct Content {
#[serde(default = "disabled")]
pub include_only_existing_source: bool,
Expand Down Expand Up @@ -122,7 +122,7 @@ fn enabled() -> bool {
}

/// Represents how the duplicate filtering detects duplicate entries.
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
#[derive(Debug, Default, Deserialize, PartialEq)]
#[serde(try_from = "String")]
pub enum DuplicateFilterFields {
FileOnly,
Expand Down
25 changes: 13 additions & 12 deletions source/citnames_rs/src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ use crate::configuration::{Content, DuplicateFilterFields};

pub(crate) type EntryPredicate = Box<dyn FnMut(&Entry) -> bool>;

impl From<Content> for EntryPredicate {
fn from(val: Content) -> Self {
impl From<&Content> for EntryPredicate {
fn from(val: &Content) -> Self {
let source_check = EntryPredicateBuilder::source_check(val.include_only_existing_source);
let paths_to_include = EntryPredicateBuilder::contains(val.paths_to_include);
let paths_to_exclude = EntryPredicateBuilder::contains(val.paths_to_exclude);
let duplicates = EntryPredicateBuilder::duplicates(val.duplicate_filter_fields);
let paths_to_include = EntryPredicateBuilder::contains(val.paths_to_include.as_slice());
let paths_to_exclude = EntryPredicateBuilder::contains(val.paths_to_exclude.as_slice());
let duplicates = EntryPredicateBuilder::duplicates(&val.duplicate_filter_fields);

(!paths_to_exclude & paths_to_include & source_check & duplicates).build()
}
Expand Down Expand Up @@ -61,18 +61,19 @@ impl EntryPredicateBuilder {
}
}

fn contains(paths: Vec<PathBuf>) -> Self {
fn contains(paths: &[PathBuf]) -> Self {
if paths.is_empty() {
EntryPredicateBuilder { predicate_opt: None }
} else {
let paths_copy = paths.to_vec();
let predicate: EntryPredicate = Box::new(move |entry| {
paths.iter().any(|path| { entry.file.starts_with(path) })
paths_copy.iter().any(|path| { entry.file.starts_with(path) })
});
EntryPredicateBuilder { predicate_opt: Some(predicate) }
}
}

fn duplicates(config: DuplicateFilterFields) -> Self {
fn duplicates(config: &DuplicateFilterFields) -> Self {
let hash_function: fn(&Entry) -> u64 = config.into();
let mut have_seen = HashSet::new();

Expand Down Expand Up @@ -158,8 +159,8 @@ impl DuplicateFilterFields {
}
}

impl From<DuplicateFilterFields> for fn(&Entry) -> u64 {
fn from(val: DuplicateFilterFields) -> Self {
impl From<&DuplicateFilterFields> for fn(&Entry) -> u64 {
fn from(val: &DuplicateFilterFields) -> Self {
match val {
DuplicateFilterFields::FileOnly =>
DuplicateFilterFields::hash_source,
Expand Down Expand Up @@ -214,7 +215,7 @@ mod test {
},
];

let sut: EntryPredicate = Content::default().into();
let sut: EntryPredicate = (&Content::default()).into();
let result: Vec<Entry> = input.into_iter().filter(sut).collect();
assert_eq!(expected, result);
}
Expand Down Expand Up @@ -279,7 +280,7 @@ mod test {
},
];

let sut: EntryPredicate = config.into();
let sut: EntryPredicate = (&config).into();
let result: Vec<Entry> = input.into_iter().filter(sut).collect();
assert_eq!(expected, result);
}
Expand Down
2 changes: 1 addition & 1 deletion source/citnames_rs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl Application {
}

fn run(self) -> Result<()> {
let filter: EntryPredicate = self.configuration.output.content.clone().into();
let filter: EntryPredicate = (&self.configuration.output.content).into();
let entries = self.create_entries()?
.inspect(|entry| log::debug!("{:?}", entry))
.filter(filter);
Expand Down
26 changes: 16 additions & 10 deletions source/citnames_rs/src/tools/configured.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,20 @@ use crate::tools::matchers::source::looks_like_a_source_file;
use crate::tools::RecognitionResult::{NotRecognized, Recognized};

pub(crate) struct Configured {
config: CompilerToRecognize,
pub executable: PathBuf,
pub flags_to_add: Vec<String>,
pub flags_to_remove: Vec<String>,
}

impl Configured {
pub(crate) fn new(config: &CompilerToRecognize) -> Box<dyn Tool> {
Box::new(Configured { config: config.clone() })
Box::new(
Configured {
executable: config.executable.clone(),
flags_to_add: config.flags_to_add.clone(),
flags_to_remove: config.flags_to_remove.clone(),
}
)
}

pub(crate) fn from(configs: &[CompilerToRecognize]) -> Box<dyn Tool> {
Expand All @@ -44,13 +52,13 @@ impl Configured {
impl Tool for Configured {
/// Any of the tool recognize the semantic, will be returned as result.
fn recognize(&self, x: &Execution) -> RecognitionResult {
if x.executable == self.config.executable {
if x.executable == self.executable {
let mut flags = vec![];
let mut sources = vec![];

// find sources and filter out requested flags.
for argument in x.arguments.iter().skip(1) {
if self.config.flags_to_remove.contains(argument) {
if self.flags_to_remove.contains(argument) {
continue;
} else if looks_like_a_source_file(argument.as_str()) {
sources.push(PathBuf::from(argument));
Expand All @@ -59,7 +67,7 @@ impl Tool for Configured {
}
}
// extend flags with requested flags.
for flag in &self.config.flags_to_add {
for flag in &self.flags_to_add {
flags.push(flag.clone());
}

Expand Down Expand Up @@ -150,11 +158,9 @@ mod test {

lazy_static! {
static ref SUT: Configured = Configured {
config: CompilerToRecognize {
executable: PathBuf::from("/usr/bin/something"),
flags_to_remove: vec_of_strings!["-I."],
flags_to_add: vec_of_strings!["-Wall"],
}
executable: PathBuf::from("/usr/bin/something"),
flags_to_remove: vec_of_strings!["-I."],
flags_to_add: vec_of_strings!["-Wall"],
};
}
}

0 comments on commit 5654978

Please sign in to comment.