Skip to content

Commit ea46fca

Browse files
committed
rust: ignored semantic is part of recognition
1 parent 9987aec commit ea46fca

File tree

9 files changed

+131
-112
lines changed

9 files changed

+131
-112
lines changed

rust/bear/src/output/mod.rs

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ impl OutputWriter {
4646
}
4747

4848
/// Implements the main logic of the output writer.
49-
pub fn run(&self, meanings: impl Iterator<Item = semantic::Meaning>) -> anyhow::Result<()> {
49+
pub fn run(
50+
&self,
51+
meanings: impl Iterator<Item = semantic::CompilerCall>,
52+
) -> anyhow::Result<()> {
5053
let entries = meanings.flat_map(|value| {
5154
into_entries(value).unwrap_or_else(|error| {
5255
log::error!(
@@ -127,38 +130,34 @@ impl OutputWriter {
127130
}
128131
}
129132

130-
pub fn into_entries(value: semantic::Meaning) -> Result<Vec<Entry>, anyhow::Error> {
131-
match value {
132-
semantic::Meaning::Compiler {
133-
compiler,
134-
working_dir,
135-
passes,
136-
} => {
137-
let entries = passes
138-
.iter()
139-
.flat_map(|pass| -> Result<Entry, anyhow::Error> {
140-
match pass {
141-
semantic::CompilerPass::Preprocess => {
142-
Err(anyhow!("preprocess pass should not show up in results"))
143-
}
144-
semantic::CompilerPass::Compile {
145-
source,
146-
output,
147-
flags,
148-
} => Ok(Entry {
149-
file: into_abspath(source.clone(), working_dir.as_path())?,
150-
directory: working_dir.clone(),
151-
output: into_abspath_opt(output.clone(), working_dir.as_path())?,
152-
arguments: into_arguments(&compiler, source, output, flags)?,
153-
}),
154-
}
155-
})
156-
.collect();
133+
pub fn into_entries(value: semantic::CompilerCall) -> Result<Vec<Entry>, anyhow::Error> {
134+
let semantic::CompilerCall {
135+
compiler,
136+
working_dir,
137+
passes,
138+
} = value;
139+
let entries = passes
140+
.iter()
141+
.flat_map(|pass| -> Result<Entry, anyhow::Error> {
142+
match pass {
143+
semantic::CompilerPass::Preprocess => {
144+
Err(anyhow!("preprocess pass should not show up in results"))
145+
}
146+
semantic::CompilerPass::Compile {
147+
source,
148+
output,
149+
flags,
150+
} => Ok(Entry {
151+
file: into_abspath(source.clone(), working_dir.as_path())?,
152+
directory: working_dir.clone(),
153+
output: into_abspath_opt(output.clone(), working_dir.as_path())?,
154+
arguments: into_arguments(&compiler, source, output, flags)?,
155+
}),
156+
}
157+
})
158+
.collect();
157159

158-
Ok(entries)
159-
}
160-
_ => Ok(vec![]),
161-
}
160+
Ok(entries)
162161
}
163162

164163
fn into_arguments(
@@ -210,10 +209,7 @@ mod test {
210209
fn test_non_compilations() -> Result<()> {
211210
let empty: Vec<Entry> = vec![];
212211

213-
let result: Vec<Entry> = into_entries(semantic::Meaning::Ignored)?;
214-
assert_eq!(empty, result);
215-
216-
let input = semantic::Meaning::Compiler {
212+
let input = semantic::CompilerCall {
217213
compiler: PathBuf::from("/usr/bin/cc"),
218214
working_dir: PathBuf::from("/home/user"),
219215
passes: vec![],
@@ -226,7 +222,7 @@ mod test {
226222

227223
#[test]
228224
fn test_single_source_compilation() -> Result<()> {
229-
let input = semantic::Meaning::Compiler {
225+
let input = semantic::CompilerCall {
230226
compiler: PathBuf::from("clang"),
231227
working_dir: PathBuf::from("/home/user"),
232228
passes: vec![semantic::CompilerPass::Compile {
@@ -252,7 +248,7 @@ mod test {
252248

253249
#[test]
254250
fn test_multiple_sources_compilation() -> Result<()> {
255-
let input = semantic::Meaning::Compiler {
251+
let input = semantic::CompilerCall {
256252
compiler: PathBuf::from("clang"),
257253
working_dir: PathBuf::from("/home/user"),
258254
passes: vec![

rust/bear/src/recognition.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,8 @@ impl TryFrom<&config::Main> for Recognition {
5151
impl Recognition {
5252
/// Simple call the semantic module to recognize the execution.
5353
/// Forward only the compiler calls, and log each recognition result.
54-
pub fn apply(&self, execution: Execution) -> Option<semantic::Meaning> {
54+
pub fn apply(&self, execution: Execution) -> Option<semantic::CompilerCall> {
5555
match self.interpreter.recognize(&execution) {
56-
semantic::Recognition::Success(semantic::Meaning::Ignored) => {
57-
log::debug!("execution recognized, but ignored: {:?}", execution);
58-
None
59-
}
6056
semantic::Recognition::Success(semantic) => {
6157
log::debug!(
6258
"execution recognized as compiler call, {:?} : {:?}",
@@ -65,6 +61,10 @@ impl Recognition {
6561
);
6662
Some(semantic)
6763
}
64+
semantic::Recognition::Ignored => {
65+
log::debug!("execution recognized, but ignored: {:?}", execution);
66+
None
67+
}
6868
semantic::Recognition::Error(reason) => {
6969
log::debug!(
7070
"execution recognized with failure, {:?} : {:?}",

rust/bear/src/semantic/interpreters/combinators.rs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// SPDX-License-Identifier: GPL-3.0-or-later
22

3-
use super::super::{Interpreter, Meaning, Recognition};
3+
use super::super::{CompilerCall, Interpreter, Recognition};
44
use intercept::Execution;
55

66
/// Represents a set of interpreters, where any of them can recognize the semantic.
@@ -19,7 +19,7 @@ impl Any {
1919
}
2020

2121
impl Interpreter for Any {
22-
fn recognize(&self, x: &Execution) -> Recognition<Meaning> {
22+
fn recognize(&self, x: &Execution) -> Recognition<CompilerCall> {
2323
for tool in &self.interpreters {
2424
match tool.recognize(x) {
2525
Recognition::Unknown => continue,
@@ -35,7 +35,7 @@ mod test {
3535
use std::collections::HashMap;
3636
use std::path::PathBuf;
3737

38-
use super::super::super::Meaning;
38+
use super::super::super::CompilerCall;
3939
use super::*;
4040

4141
#[test]
@@ -57,7 +57,7 @@ mod test {
5757
}
5858

5959
#[test]
60-
fn test_any_when_match() {
60+
fn test_any_when_success() {
6161
let sut = Any {
6262
interpreters: vec![
6363
Box::new(MockTool::NotRecognize),
@@ -74,6 +74,24 @@ mod test {
7474
}
7575
}
7676

77+
#[test]
78+
fn test_any_when_ignored() {
79+
let sut = Any {
80+
interpreters: vec![
81+
Box::new(MockTool::NotRecognize),
82+
Box::new(MockTool::RecognizeIgnored),
83+
Box::new(MockTool::Recognize),
84+
],
85+
};
86+
87+
let input = any_execution();
88+
89+
match sut.recognize(&input) {
90+
Recognition::Ignored => assert!(true),
91+
_ => assert!(false),
92+
}
93+
}
94+
7795
#[test]
7896
fn test_any_when_match_fails() {
7997
let sut = Any {
@@ -95,14 +113,16 @@ mod test {
95113

96114
enum MockTool {
97115
Recognize,
116+
RecognizeIgnored,
98117
RecognizeFailed,
99118
NotRecognize,
100119
}
101120

102121
impl Interpreter for MockTool {
103-
fn recognize(&self, _: &Execution) -> Recognition<Meaning> {
122+
fn recognize(&self, _: &Execution) -> Recognition<CompilerCall> {
104123
match self {
105-
MockTool::Recognize => Recognition::Success(Meaning::Ignored),
124+
MockTool::Recognize => Recognition::Success(any_compiler_call()),
125+
MockTool::RecognizeIgnored => Recognition::Ignored,
106126
MockTool::RecognizeFailed => Recognition::Error(String::from("problem")),
107127
MockTool::NotRecognize => Recognition::Unknown,
108128
}
@@ -117,4 +137,12 @@ mod test {
117137
environment: HashMap::new(),
118138
}
119139
}
140+
141+
fn any_compiler_call() -> CompilerCall {
142+
CompilerCall {
143+
compiler: PathBuf::new(),
144+
working_dir: PathBuf::new(),
145+
passes: vec![],
146+
}
147+
}
120148
}

rust/bear/src/semantic/interpreters/gcc.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use nom::branch::alt;
44
use nom::multi::many1;
55
use nom::sequence::preceded;
66

7-
use super::super::{Interpreter, Meaning, Recognition};
7+
use super::super::{CompilerCall, Interpreter, Recognition};
88
use intercept::Execution;
99
use internal::Argument;
1010

@@ -17,7 +17,7 @@ impl Gcc {
1717
}
1818

1919
impl Interpreter for Gcc {
20-
fn recognize(&self, execution: &Execution) -> Recognition<Meaning> {
20+
fn recognize(&self, execution: &Execution) -> Recognition<CompilerCall> {
2121
let mut parser = preceded(
2222
internal::compiler,
2323
many1(alt((internal::flag, internal::source))),
@@ -29,7 +29,7 @@ impl Interpreter for Gcc {
2929
let flags = result.1;
3030
let passes = Argument::passes(flags.as_slice());
3131

32-
Recognition::Success(Meaning::Compiler {
32+
Recognition::Success(CompilerCall {
3333
compiler: execution.executable.clone(),
3434
working_dir: execution.working_dir.clone(),
3535
passes,

rust/bear/src/semantic/interpreters/generic.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::collections::HashSet;
44
use std::path::PathBuf;
55
use std::vec;
66

7-
use super::super::{CompilerPass, Interpreter, Meaning, Recognition};
7+
use super::super::{CompilerCall, CompilerPass, Interpreter, Recognition};
88
use super::matchers::source::looks_like_a_source_file;
99
use intercept::Execution;
1010

@@ -25,7 +25,7 @@ impl Interpreter for Generic {
2525
/// - the executable name,
2626
/// - one of the arguments is a source file,
2727
/// - the rest of the arguments are flags.
28-
fn recognize(&self, x: &Execution) -> Recognition<Meaning> {
28+
fn recognize(&self, x: &Execution) -> Recognition<CompilerCall> {
2929
if self.executables.contains(&x.executable) {
3030
let mut flags = vec![];
3131
let mut sources = vec![];
@@ -42,7 +42,7 @@ impl Interpreter for Generic {
4242
if sources.is_empty() {
4343
Recognition::Error(String::from("source file is not found"))
4444
} else {
45-
Recognition::Success(Meaning::Compiler {
45+
Recognition::Success(CompilerCall {
4646
compiler: x.executable.clone(),
4747
working_dir: x.working_dir.clone(),
4848
passes: sources
@@ -87,7 +87,7 @@ mod test {
8787
environment: HashMap::new(),
8888
};
8989

90-
let expected = Meaning::Compiler {
90+
let expected = CompilerCall {
9191
compiler: PathBuf::from("/usr/bin/something"),
9292
working_dir: PathBuf::from("/home/user"),
9393
passes: vec![CompilerPass::Compile {

rust/bear/src/semantic/interpreters/ignore.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::collections::HashSet;
44
use std::path::PathBuf;
55

6-
use super::super::{Interpreter, Meaning, Recognition};
6+
use super::super::{CompilerCall, Interpreter, Recognition};
77
use intercept::Execution;
88

99
/// A tool to ignore a command execution by executable name.
@@ -25,9 +25,9 @@ impl IgnoreByPath {
2525

2626
/// A tool to ignore a command execution by arguments.
2727
impl Interpreter for IgnoreByPath {
28-
fn recognize(&self, execution: &Execution) -> Recognition<Meaning> {
28+
fn recognize(&self, execution: &Execution) -> Recognition<CompilerCall> {
2929
if self.executables.contains(&execution.executable) {
30-
Recognition::Success(Meaning::Ignored)
30+
Recognition::Ignored
3131
} else {
3232
Recognition::Unknown
3333
}
@@ -162,10 +162,7 @@ mod test {
162162
};
163163
let sut = IgnoreByPath::new();
164164

165-
assert_eq!(
166-
Recognition::Success(Meaning::Ignored),
167-
sut.recognize(&input)
168-
)
165+
assert_eq!(Recognition::Ignored, sut.recognize(&input))
169166
}
170167

171168
#[test]

rust/bear/src/semantic/interpreters/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ mod test {
6464
use std::collections::HashMap;
6565
use std::path::PathBuf;
6666

67-
use super::super::{Meaning, Recognition};
67+
use super::super::{CompilerCall, Recognition};
6868
use super::*;
6969
use crate::{vec_of_pathbuf, vec_of_strings};
7070
use intercept::Execution;
@@ -75,7 +75,7 @@ mod test {
7575

7676
let input = any_execution();
7777
match sut.recognize(&input) {
78-
Recognition::Success(Meaning::Compiler { .. }) => assert!(true),
78+
Recognition::Success(CompilerCall { .. }) => assert!(true),
7979
_ => assert!(false),
8080
}
8181
}
@@ -87,7 +87,7 @@ mod test {
8787

8888
let input = any_execution();
8989
match sut.recognize(&input) {
90-
Recognition::Success(Meaning::Ignored) => assert!(true),
90+
Recognition::Ignored => assert!(true),
9191
_ => assert!(false),
9292
}
9393
}

rust/bear/src/semantic/mod.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,10 @@ use std::path::PathBuf;
1717

1818
/// Represents an executed command semantic.
1919
#[derive(Debug, PartialEq)]
20-
pub enum Meaning {
21-
/// This is a compiler call.
22-
Compiler {
23-
compiler: PathBuf,
24-
working_dir: PathBuf,
25-
passes: Vec<CompilerPass>,
26-
},
27-
/// This is something else we recognised, but not interested to fully specify.
28-
Ignored,
20+
pub struct CompilerCall {
21+
pub compiler: PathBuf,
22+
pub working_dir: PathBuf,
23+
pub passes: Vec<CompilerPass>,
2924
}
3025

3126
/// Represents a compiler call pass.
@@ -49,7 +44,7 @@ pub enum CompilerPass {
4944
/// Or classify the recognition as ignored to not be further processed
5045
/// later on.
5146
pub trait Interpreter: Send {
52-
fn recognize(&self, _: &Execution) -> Recognition<Meaning>;
47+
fn recognize(&self, _: &Execution) -> Recognition<CompilerCall>;
5348
}
5449

5550
/// Represents a semantic recognition result.
@@ -59,7 +54,12 @@ pub trait Interpreter: Send {
5954
/// to continue with the next interpreter.
6055
#[derive(Debug, PartialEq)]
6156
pub enum Recognition<T> {
57+
/// The command was recognized and the semantic was identified.
6258
Success(T),
59+
/// The command was recognized, but the semantic was ignored.
60+
Ignored,
61+
/// The command was recognized, but the semantic was broken.
6362
Error(String),
63+
/// The command was not recognized.
6464
Unknown,
6565
}

0 commit comments

Comments
 (0)