Skip to content

Commit

Permalink
rust: semantic module got simplified
Browse files Browse the repository at this point in the history
  • Loading branch information
rizsotto committed Oct 7, 2024
1 parent 4452195 commit 26a016d
Show file tree
Hide file tree
Showing 10 changed files with 282 additions and 382 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,74 +17,15 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

use std::path::PathBuf;

use super::tools::configured::Configured;
use super::tools::ignore::{IgnoreByArgs, IgnoreByPath};
use super::tools::unix::Unix;
use super::tools::wrapper::Wrapper;
use super::{Meaning, RecognitionResult, Tool};
use crate::{Meaning, RecognitionResult, Tool};
use intercept::ipc::Execution;

mod configured;
mod gcc;
mod ignore;
mod matchers;
mod unix;
mod wrapper;

pub struct Builder {
tools: Vec<Box<dyn Tool>>,
}

// TODO: write unit test for this!!!
impl Builder {
pub fn new() -> Self {
Builder {
tools: vec![Unix::new(), Wrapper::new()],
}
}

pub fn build(self) -> impl Tool {
Any::new(self.tools)
}

pub fn compilers_to_recognize(mut self, compilers: &[PathBuf]) -> Self {
if !compilers.is_empty() {
// Add the new compilers at the end of the tools.
for compiler in compilers {
let tool = Configured::new(compiler);
self.tools.push(tool);
}
}
self
}

pub fn compilers_to_exclude(mut self, compilers: &[PathBuf]) -> Self {
if !compilers.is_empty() {
// Add these new compilers at the front of the tools.
let tool = IgnoreByPath::new(compilers);
self.tools.insert(0, tool);
}
self
}

pub fn compilers_to_exclude_by_arguments(mut self, args: &[String]) -> Self {
if !args.is_empty() {
// Add these new compilers at the front of the tools.
let tool = IgnoreByArgs::new(args);
self.tools.insert(0, tool);
}
self
}
}

struct Any {
pub struct Any {
tools: Vec<Box<dyn Tool>>,
}

impl Any {
fn new(tools: Vec<Box<dyn Tool>>) -> impl Tool {
pub fn new(tools: Vec<Box<dyn Tool>>) -> impl Tool {
Any { tools }
}
}
Expand All @@ -109,8 +50,6 @@ mod test {
use std::collections::HashMap;
use std::path::PathBuf;

use crate::vec_of_pathbuf;

use super::*;

#[test]
Expand Down
8 changes: 4 additions & 4 deletions rust/semantic/src/tools/gcc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ use nom::branch::alt;
use nom::multi::many1;
use nom::sequence::preceded;

use super::super::{CompilerPass, Meaning, RecognitionResult, Tool};
use super::super::{Meaning, RecognitionResult, Tool};
use super::gcc::internal::Argument;
use intercept::ipc::Execution;

pub(crate) struct Gcc {}
pub struct Gcc {}

impl Gcc {
pub(crate) fn new() -> Box<dyn Tool> {
pub fn new() -> Box<dyn Tool> {
Box::new(Gcc {})
}
}
Expand All @@ -42,7 +42,7 @@ impl Tool for Gcc {

match parser(execution.arguments.as_slice()) {
Ok(result) => {
// todo: append flags from environment
// TODO: append flags from environment
let flags = result.1;
let passes = Argument::passes(flags.as_slice());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ use super::super::{CompilerPass, Meaning, RecognitionResult, Tool};
use super::matchers::source::looks_like_a_source_file;
use intercept::ipc::Execution;

pub struct Configured {
pub struct Generic {
pub executable: PathBuf,
}

impl Configured {
impl Generic {
pub fn new(compiler: &Path) -> Box<dyn Tool> {
Box::new(Self {
executable: compiler.to_path_buf(),
})
}
}

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

lazy_static! {
static ref SUT: Configured = Configured {
static ref SUT: Generic = Generic {
executable: PathBuf::from("/usr/bin/something"),
};
}
Expand Down
158 changes: 154 additions & 4 deletions rust/semantic/src/tools/ignore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,23 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

use std::collections::HashSet;
use std::path::PathBuf;

use super::super::{Meaning, RecognitionResult, Tool};
use intercept::ipc::Execution;
use std::path::PathBuf;

pub struct IgnoreByPath {
executables: Vec<PathBuf>,
executables: HashSet<PathBuf>,
}

impl IgnoreByPath {
pub fn new(compilers: &[PathBuf]) -> Box<dyn Tool> {
pub fn new() -> Box<dyn Tool> {
let executables = COREUTILS_FILES.iter().map(PathBuf::from).collect();
Box::new(Self { executables })
}

pub fn from(compilers: &[PathBuf]) -> Box<dyn Tool> {
let executables = compilers.iter().map(|compiler| compiler.clone()).collect();
Box::new(Self { executables })
}
Expand Down Expand Up @@ -67,9 +74,152 @@ impl Tool for IgnoreByArgs {
}
}

static COREUTILS_FILES: [&str; 106] = [
"/usr/bin/[",
"/usr/bin/arch",
"/usr/bin/b2sum",
"/usr/bin/base32",
"/usr/bin/base64",
"/usr/bin/basename",
"/usr/bin/basenc",
"/usr/bin/cat",
"/usr/bin/chcon",
"/usr/bin/chgrp",
"/usr/bin/chmod",
"/usr/bin/chown",
"/usr/bin/cksum",
"/usr/bin/comm",
"/usr/bin/cp",
"/usr/bin/csplit",
"/usr/bin/cut",
"/usr/bin/date",
"/usr/bin/dd",
"/usr/bin/df",
"/usr/bin/dir",
"/usr/bin/dircolors",
"/usr/bin/dirname",
"/usr/bin/du",
"/usr/bin/echo",
"/usr/bin/env",
"/usr/bin/expand",
"/usr/bin/expr",
"/usr/bin/factor",
"/usr/bin/false",
"/usr/bin/fmt",
"/usr/bin/fold",
"/usr/bin/groups",
"/usr/bin/head",
"/usr/bin/hostid",
"/usr/bin/id",
"/usr/bin/install",
"/usr/bin/join",
"/usr/bin/link",
"/usr/bin/ln",
"/usr/bin/logname",
"/usr/bin/ls",
"/usr/bin/md5sum",
"/usr/bin/mkdir",
"/usr/bin/mkfifo",
"/usr/bin/mknod",
"/usr/bin/mktemp",
"/usr/bin/mv",
"/usr/bin/nice",
"/usr/bin/nl",
"/usr/bin/nohup",
"/usr/bin/nproc",
"/usr/bin/numfmt",
"/usr/bin/od",
"/usr/bin/paste",
"/usr/bin/pathchk",
"/usr/bin/pinky",
"/usr/bin/pr",
"/usr/bin/printenv",
"/usr/bin/printf",
"/usr/bin/ptx",
"/usr/bin/pwd",
"/usr/bin/readlink",
"/usr/bin/realpath",
"/usr/bin/rm",
"/usr/bin/rmdir",
"/usr/bin/runcon",
"/usr/bin/seq",
"/usr/bin/sha1sum",
"/usr/bin/sha224sum",
"/usr/bin/sha256sum",
"/usr/bin/sha384sum",
"/usr/bin/sha512sum",
"/usr/bin/shred",
"/usr/bin/shuf",
"/usr/bin/sleep",
"/usr/bin/sort",
"/usr/bin/split",
"/usr/bin/stat",
"/usr/bin/stdbuf",
"/usr/bin/stty",
"/usr/bin/sum",
"/usr/bin/sync",
"/usr/bin/tac",
"/usr/bin/tail",
"/usr/bin/tee",
"/usr/bin/test",
"/usr/bin/timeout",
"/usr/bin/touch",
"/usr/bin/tr",
"/usr/bin/true",
"/usr/bin/truncate",
"/usr/bin/tsort",
"/usr/bin/tty",
"/usr/bin/uname",
"/usr/bin/unexpand",
"/usr/bin/uniq",
"/usr/bin/unlink",
"/usr/bin/users",
"/usr/bin/vdir",
"/usr/bin/wc",
"/usr/bin/who",
"/usr/bin/whoami",
"/usr/bin/yes",
"/usr/bin/make",
"/usr/bin/gmake",
];

#[cfg(test)]
mod test {
use std::collections::HashMap;
use std::path::PathBuf;

use crate::vec_of_strings;

use super::*;

// TODO: implement test cases
#[test]
fn test_unix_tools_are_recognized() {
let input = Execution {
executable: PathBuf::from("/usr/bin/ls"),
arguments: vec_of_strings!["ls", "/home/user/build"],
working_dir: PathBuf::from("/home/user"),
environment: HashMap::new(),
};
let sut = IgnoreByPath::new();

assert_eq!(
RecognitionResult::Recognized(Ok(Meaning::Ignored)),
sut.recognize(&input)
)
}

#[test]
fn test_not_known_executables_are_not_recognized() {
let input = Execution {
executable: PathBuf::from("/usr/bin/bear"),
arguments: vec_of_strings!["bear", "--", "make"],
working_dir: PathBuf::from("/home/user"),
environment: HashMap::new(),
};
let sut = IgnoreByPath::new();

assert_eq!(RecognitionResult::NotRecognized, sut.recognize(&input))
}

// TODO: implement test cases for args
}
20 changes: 0 additions & 20 deletions rust/semantic/src/tools/matchers.rs

This file was deleted.

20 changes: 20 additions & 0 deletions rust/semantic/src/tools/matchers/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* Copyright (C) 2012-2024 by László Nagy
This file is part of Bear.
Bear is a tool to generate compilation database for clang tooling.
Bear is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Bear is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

pub(crate) mod source;
Loading

0 comments on commit 26a016d

Please sign in to comment.