Skip to content

Commit 2da6d29

Browse files
committed
rust: semantic module got simplified
1 parent 4452195 commit 2da6d29

File tree

8 files changed

+249
-349
lines changed

8 files changed

+249
-349
lines changed

rust/semantic/src/tools.rs renamed to rust/semantic/src/tools/combinators.rs

Lines changed: 3 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -17,74 +17,15 @@
1717
along with this program. If not, see <http://www.gnu.org/licenses/>.
1818
*/
1919

20-
use std::path::PathBuf;
21-
22-
use super::tools::configured::Configured;
23-
use super::tools::ignore::{IgnoreByArgs, IgnoreByPath};
24-
use super::tools::unix::Unix;
25-
use super::tools::wrapper::Wrapper;
26-
use super::{Meaning, RecognitionResult, Tool};
20+
use crate::{Meaning, RecognitionResult, Tool};
2721
use intercept::ipc::Execution;
2822

29-
mod configured;
30-
mod gcc;
31-
mod ignore;
32-
mod matchers;
33-
mod unix;
34-
mod wrapper;
35-
36-
pub struct Builder {
37-
tools: Vec<Box<dyn Tool>>,
38-
}
39-
40-
// TODO: write unit test for this!!!
41-
impl Builder {
42-
pub fn new() -> Self {
43-
Builder {
44-
tools: vec![Unix::new(), Wrapper::new()],
45-
}
46-
}
47-
48-
pub fn build(self) -> impl Tool {
49-
Any::new(self.tools)
50-
}
51-
52-
pub fn compilers_to_recognize(mut self, compilers: &[PathBuf]) -> Self {
53-
if !compilers.is_empty() {
54-
// Add the new compilers at the end of the tools.
55-
for compiler in compilers {
56-
let tool = Configured::new(compiler);
57-
self.tools.push(tool);
58-
}
59-
}
60-
self
61-
}
62-
63-
pub fn compilers_to_exclude(mut self, compilers: &[PathBuf]) -> Self {
64-
if !compilers.is_empty() {
65-
// Add these new compilers at the front of the tools.
66-
let tool = IgnoreByPath::new(compilers);
67-
self.tools.insert(0, tool);
68-
}
69-
self
70-
}
71-
72-
pub fn compilers_to_exclude_by_arguments(mut self, args: &[String]) -> Self {
73-
if !args.is_empty() {
74-
// Add these new compilers at the front of the tools.
75-
let tool = IgnoreByArgs::new(args);
76-
self.tools.insert(0, tool);
77-
}
78-
self
79-
}
80-
}
81-
82-
struct Any {
23+
pub struct Any {
8324
tools: Vec<Box<dyn Tool>>,
8425
}
8526

8627
impl Any {
87-
fn new(tools: Vec<Box<dyn Tool>>) -> impl Tool {
28+
pub fn new(tools: Vec<Box<dyn Tool>>) -> impl Tool {
8829
Any { tools }
8930
}
9031
}
@@ -109,8 +50,6 @@ mod test {
10950
use std::collections::HashMap;
11051
use std::path::PathBuf;
11152

112-
use crate::vec_of_pathbuf;
113-
11453
use super::*;
11554

11655
#[test]

rust/semantic/src/tools/gcc.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ use nom::branch::alt;
2121
use nom::multi::many1;
2222
use nom::sequence::preceded;
2323

24-
use super::super::{CompilerPass, Meaning, RecognitionResult, Tool};
24+
use super::super::{Meaning, RecognitionResult, Tool};
2525
use super::gcc::internal::Argument;
2626
use intercept::ipc::Execution;
2727

28-
pub(crate) struct Gcc {}
28+
pub struct Gcc {}
2929

3030
impl Gcc {
31-
pub(crate) fn new() -> Box<dyn Tool> {
31+
pub fn new() -> Box<dyn Tool> {
3232
Box::new(Gcc {})
3333
}
3434
}
@@ -42,7 +42,7 @@ impl Tool for Gcc {
4242

4343
match parser(execution.arguments.as_slice()) {
4444
Ok(result) => {
45-
// todo: append flags from environment
45+
// TODO: append flags from environment
4646
let flags = result.1;
4747
let passes = Argument::passes(flags.as_slice());
4848

rust/semantic/src/tools/configured.rs renamed to rust/semantic/src/tools/generic.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@ use super::super::{CompilerPass, Meaning, RecognitionResult, Tool};
2424
use super::matchers::source::looks_like_a_source_file;
2525
use intercept::ipc::Execution;
2626

27-
pub struct Configured {
27+
pub struct Generic {
2828
pub executable: PathBuf,
2929
}
3030

31-
impl Configured {
31+
impl Generic {
3232
pub fn new(compiler: &Path) -> Box<dyn Tool> {
3333
Box::new(Self {
3434
executable: compiler.to_path_buf(),
3535
})
3636
}
3737
}
3838

39-
impl Tool for Configured {
39+
impl Tool for Generic {
4040
/// Any of the tool recognize the semantic, will be returned as result.
4141
fn recognize(&self, x: &Execution) -> RecognitionResult {
4242
if x.executable == self.executable {
@@ -144,7 +144,7 @@ mod test {
144144
}
145145

146146
lazy_static! {
147-
static ref SUT: Configured = Configured {
147+
static ref SUT: Generic = Generic {
148148
executable: PathBuf::from("/usr/bin/something"),
149149
};
150150
}

rust/semantic/src/tools/ignore.rs

Lines changed: 154 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,23 @@
1717
along with this program. If not, see <http://www.gnu.org/licenses/>.
1818
*/
1919

20+
use std::collections::HashSet;
21+
use std::path::PathBuf;
22+
2023
use super::super::{Meaning, RecognitionResult, Tool};
2124
use intercept::ipc::Execution;
22-
use std::path::PathBuf;
2325

2426
pub struct IgnoreByPath {
25-
executables: Vec<PathBuf>,
27+
executables: HashSet<PathBuf>,
2628
}
2729

2830
impl IgnoreByPath {
29-
pub fn new(compilers: &[PathBuf]) -> Box<dyn Tool> {
31+
pub fn new() -> Box<dyn Tool> {
32+
let executables = COREUTILS_FILES.iter().map(PathBuf::from).collect();
33+
Box::new(Self { executables })
34+
}
35+
36+
pub fn from(compilers: &[PathBuf]) -> Box<dyn Tool> {
3037
let executables = compilers.iter().map(|compiler| compiler.clone()).collect();
3138
Box::new(Self { executables })
3239
}
@@ -67,9 +74,152 @@ impl Tool for IgnoreByArgs {
6774
}
6875
}
6976

77+
static COREUTILS_FILES: [&str; 106] = [
78+
"/usr/bin/[",
79+
"/usr/bin/arch",
80+
"/usr/bin/b2sum",
81+
"/usr/bin/base32",
82+
"/usr/bin/base64",
83+
"/usr/bin/basename",
84+
"/usr/bin/basenc",
85+
"/usr/bin/cat",
86+
"/usr/bin/chcon",
87+
"/usr/bin/chgrp",
88+
"/usr/bin/chmod",
89+
"/usr/bin/chown",
90+
"/usr/bin/cksum",
91+
"/usr/bin/comm",
92+
"/usr/bin/cp",
93+
"/usr/bin/csplit",
94+
"/usr/bin/cut",
95+
"/usr/bin/date",
96+
"/usr/bin/dd",
97+
"/usr/bin/df",
98+
"/usr/bin/dir",
99+
"/usr/bin/dircolors",
100+
"/usr/bin/dirname",
101+
"/usr/bin/du",
102+
"/usr/bin/echo",
103+
"/usr/bin/env",
104+
"/usr/bin/expand",
105+
"/usr/bin/expr",
106+
"/usr/bin/factor",
107+
"/usr/bin/false",
108+
"/usr/bin/fmt",
109+
"/usr/bin/fold",
110+
"/usr/bin/groups",
111+
"/usr/bin/head",
112+
"/usr/bin/hostid",
113+
"/usr/bin/id",
114+
"/usr/bin/install",
115+
"/usr/bin/join",
116+
"/usr/bin/link",
117+
"/usr/bin/ln",
118+
"/usr/bin/logname",
119+
"/usr/bin/ls",
120+
"/usr/bin/md5sum",
121+
"/usr/bin/mkdir",
122+
"/usr/bin/mkfifo",
123+
"/usr/bin/mknod",
124+
"/usr/bin/mktemp",
125+
"/usr/bin/mv",
126+
"/usr/bin/nice",
127+
"/usr/bin/nl",
128+
"/usr/bin/nohup",
129+
"/usr/bin/nproc",
130+
"/usr/bin/numfmt",
131+
"/usr/bin/od",
132+
"/usr/bin/paste",
133+
"/usr/bin/pathchk",
134+
"/usr/bin/pinky",
135+
"/usr/bin/pr",
136+
"/usr/bin/printenv",
137+
"/usr/bin/printf",
138+
"/usr/bin/ptx",
139+
"/usr/bin/pwd",
140+
"/usr/bin/readlink",
141+
"/usr/bin/realpath",
142+
"/usr/bin/rm",
143+
"/usr/bin/rmdir",
144+
"/usr/bin/runcon",
145+
"/usr/bin/seq",
146+
"/usr/bin/sha1sum",
147+
"/usr/bin/sha224sum",
148+
"/usr/bin/sha256sum",
149+
"/usr/bin/sha384sum",
150+
"/usr/bin/sha512sum",
151+
"/usr/bin/shred",
152+
"/usr/bin/shuf",
153+
"/usr/bin/sleep",
154+
"/usr/bin/sort",
155+
"/usr/bin/split",
156+
"/usr/bin/stat",
157+
"/usr/bin/stdbuf",
158+
"/usr/bin/stty",
159+
"/usr/bin/sum",
160+
"/usr/bin/sync",
161+
"/usr/bin/tac",
162+
"/usr/bin/tail",
163+
"/usr/bin/tee",
164+
"/usr/bin/test",
165+
"/usr/bin/timeout",
166+
"/usr/bin/touch",
167+
"/usr/bin/tr",
168+
"/usr/bin/true",
169+
"/usr/bin/truncate",
170+
"/usr/bin/tsort",
171+
"/usr/bin/tty",
172+
"/usr/bin/uname",
173+
"/usr/bin/unexpand",
174+
"/usr/bin/uniq",
175+
"/usr/bin/unlink",
176+
"/usr/bin/users",
177+
"/usr/bin/vdir",
178+
"/usr/bin/wc",
179+
"/usr/bin/who",
180+
"/usr/bin/whoami",
181+
"/usr/bin/yes",
182+
"/usr/bin/make",
183+
"/usr/bin/gmake",
184+
];
185+
70186
#[cfg(test)]
71187
mod test {
188+
use std::collections::HashMap;
189+
use std::path::PathBuf;
190+
191+
use crate::vec_of_strings;
192+
72193
use super::*;
73194

74-
// TODO: implement test cases
195+
#[test]
196+
fn test_unix_tools_are_recognized() {
197+
let input = Execution {
198+
executable: PathBuf::from("/usr/bin/ls"),
199+
arguments: vec_of_strings!["ls", "/home/user/build"],
200+
working_dir: PathBuf::from("/home/user"),
201+
environment: HashMap::new(),
202+
};
203+
let sut = IgnoreByPath::new();
204+
205+
assert_eq!(
206+
RecognitionResult::Recognized(Ok(Meaning::Ignored)),
207+
sut.recognize(&input)
208+
)
209+
}
210+
211+
#[test]
212+
fn test_not_known_executables_are_not_recognized() {
213+
let input = Execution {
214+
executable: PathBuf::from("/usr/bin/bear"),
215+
arguments: vec_of_strings!["bear", "--", "make"],
216+
working_dir: PathBuf::from("/home/user"),
217+
environment: HashMap::new(),
218+
};
219+
let sut = IgnoreByPath::new();
220+
221+
assert_eq!(RecognitionResult::NotRecognized, sut.recognize(&input))
222+
}
223+
224+
// TODO: implement test cases for args
75225
}
File renamed without changes.

0 commit comments

Comments
 (0)