Skip to content

Commit 603642e

Browse files
committed
rust: semantic transformation implements path transformation based on config
1 parent 122d48a commit 603642e

File tree

4 files changed

+285
-109
lines changed

4 files changed

+285
-109
lines changed

rust/bear/src/config.rs

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@
3535
//! specification: clang
3636
//! compilers:
3737
//! - path: /usr/local/bin/cc
38-
//! ignore: always
39-
//! - path: /usr/bin/cc
4038
//! ignore: never
39+
//! - path: /usr/bin/cc
40+
//! ignore: always
4141
//! - path: /usr/bin/c++
4242
//! ignore: conditional
4343
//! arguments:
@@ -67,8 +67,9 @@
6767
//! - directory
6868
//! format:
6969
//! paths:
70-
//! resolver: original
71-
//! relative: false
70+
//! directory: canonical
71+
//! file: canonical
72+
//! output: canonical
7273
//! ```
7374
//!
7475
//! ```yaml
@@ -318,23 +319,17 @@ pub struct Compiler {
318319
/// - Always: Always ignore the compiler call.
319320
/// - Never: Never ignore the compiler call. (Default)
320321
/// - Conditional: Ignore the compiler call if the arguments match.
321-
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
322+
#[derive(Clone, Debug, Default, PartialEq, Deserialize, Serialize)]
322323
pub enum IgnoreOrConsider {
323324
#[serde(rename = "always", alias = "true")]
324325
Always,
326+
#[default]
325327
#[serde(rename = "never", alias = "false")]
326328
Never,
327329
#[serde(rename = "conditional")]
328330
Conditional,
329331
}
330332

331-
/// The default ignore mode is never ignore.
332-
impl Default for IgnoreOrConsider {
333-
fn default() -> Self {
334-
IgnoreOrConsider::Never
335-
}
336-
}
337-
338333
/// Argument lists to match, add or remove.
339334
///
340335
/// The `match` field is used to specify the arguments to match. Can be used only with the
@@ -415,37 +410,44 @@ pub enum OutputFields {
415410
/// Format configuration of the JSON compilation database.
416411
#[derive(Clone, Debug, Default, PartialEq, Deserialize, Serialize)]
417412
pub struct Format {
413+
#[serde(default)]
418414
pub paths: PathFormat,
419415
}
420416

421417
/// Format configuration of paths in the JSON compilation database.
422418
#[derive(Clone, Debug, Default, PartialEq, Deserialize, Serialize)]
423419
pub struct PathFormat {
424420
#[serde(default)]
425-
pub resolver: PathResolver,
426-
#[serde(default = "default_disabled")]
427-
pub relative: bool,
421+
pub directory: DirectoryPathResolver,
422+
#[serde(default)]
423+
pub file: FilePathResolver,
424+
#[serde(default)]
425+
pub output: FilePathResolver,
428426
}
429427

430-
/// Path resolver configuration.
431-
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
432-
pub enum PathResolver {
433-
/// The original path is the path as it is passed to the compiler.
434-
#[serde(rename = "original", alias = "is")]
435-
Original,
436-
/// The absolute path from the original path. Symlinks are not resolved.
437-
#[serde(rename = "absolute")]
438-
Absolute,
439-
/// The canonical path is the absolute path with the symlinks resolved.
428+
#[derive(Clone, Debug, Default, PartialEq, Deserialize, Serialize)]
429+
pub enum DirectoryPathResolver {
430+
/// The directory path will be resolved to the canonical path. (Default)
431+
#[default]
440432
#[serde(rename = "canonical")]
441433
Canonical,
434+
/// The directory path will be resolved to the relative path to the current working directory.
435+
#[serde(rename = "relative_to_cwd")]
436+
RelativeToCwd,
442437
}
443438

444-
/// The default path format is the original path.
445-
impl Default for PathResolver {
446-
fn default() -> Self {
447-
PathResolver::Original
448-
}
439+
#[derive(Clone, Debug, Default, PartialEq, Deserialize, Serialize)]
440+
pub enum FilePathResolver {
441+
/// The directory path will be resolved to the canonical path. (Default)
442+
#[default]
443+
#[serde(rename = "canonical")]
444+
Canonical,
445+
/// The directory path will be resolved to the relative path to the directory attribute.
446+
#[serde(rename = "relative_to_directory")]
447+
RelativeToDirectory,
448+
/// The directory path will be resolved to the relative path to the current working directory.
449+
#[serde(rename = "relative_to_cwd")]
450+
RelativeToCwd,
449451
}
450452

451453
fn default_disabled() -> bool {
@@ -538,8 +540,9 @@ mod test {
538540
- directory
539541
format:
540542
paths:
541-
resolver: canonical
542-
relative: false
543+
directory: canonical
544+
file: canonical
545+
output: canonical
543546
"#;
544547

545548
let result = Main::from_reader(content).unwrap();
@@ -611,8 +614,9 @@ mod test {
611614
},
612615
format: Format {
613616
paths: PathFormat {
614-
resolver: PathResolver::Canonical,
615-
relative: false,
617+
directory: DirectoryPathResolver::Canonical,
618+
file: FilePathResolver::Canonical,
619+
output: FilePathResolver::Canonical,
616620
},
617621
},
618622
},
@@ -661,8 +665,9 @@ mod test {
661665
},
662666
format: Format {
663667
paths: PathFormat {
664-
resolver: PathResolver::Original,
665-
relative: false,
668+
directory: DirectoryPathResolver::Canonical,
669+
file: FilePathResolver::Canonical,
670+
output: FilePathResolver::Canonical,
666671
},
667672
},
668673
},
@@ -720,8 +725,9 @@ mod test {
720725
- file
721726
format:
722727
paths:
723-
resolver: canonical
724-
relative: true
728+
directory: relative_to_cwd
729+
file: relative_to_directory
730+
output: relative_to_directory
725731
"#;
726732

727733
let result = Main::from_reader(content).unwrap();
@@ -762,8 +768,9 @@ mod test {
762768
},
763769
format: Format {
764770
paths: PathFormat {
765-
resolver: PathResolver::Canonical,
766-
relative: true,
771+
directory: DirectoryPathResolver::RelativeToCwd,
772+
file: FilePathResolver::RelativeToDirectory,
773+
output: FilePathResolver::RelativeToCwd,
767774
},
768775
},
769776
},

rust/bear/src/modes/semantic.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::intercept::Envelope;
44
use crate::output::OutputWriter;
55
use crate::semantic::interpreters::create_interpreter;
6-
use crate::semantic::transformation::Transformation;
6+
use crate::semantic::transformation::create_transformation;
77
use crate::{args, config, output, semantic};
88
use anyhow::Context;
99
use std::fs::{File, OpenOptions};
@@ -13,20 +13,20 @@ use std::path::{Path, PathBuf};
1313
/// The semantic analysis that is independent of the event source.
1414
pub(super) struct SemanticAnalysisPipeline {
1515
interpreter: Box<dyn semantic::Interpreter>,
16-
transform: Box<dyn semantic::Transform>,
16+
transformation: Box<dyn semantic::Transformation>,
1717
output_writer: OutputWriterImpl,
1818
}
1919

2020
impl SemanticAnalysisPipeline {
2121
/// Create a new semantic mode instance.
2222
pub(super) fn from(output: args::BuildSemantic, config: &config::Main) -> anyhow::Result<Self> {
2323
let interpreter = create_interpreter(config);
24-
let transform = Transformation::from(&config.output);
24+
let transformation = create_transformation(&config.output)?;
2525
let output_writer = OutputWriterImpl::create(&output, &config.output)?;
2626

2727
Ok(Self {
2828
interpreter: Box::new(interpreter),
29-
transform: Box::new(transform),
29+
transformation,
3030
output_writer,
3131
})
3232
}
@@ -44,7 +44,7 @@ impl SemanticAnalysisPipeline {
4444
.map(|envelope| envelope.event.execution)
4545
.flat_map(|execution| self.interpreter.recognize(&execution))
4646
.inspect(|semantic| log::debug!("semantic: {:?}", semantic))
47-
.flat_map(|semantic| self.transform.apply(semantic));
47+
.flat_map(|semantic| self.transformation.apply(semantic));
4848
// Consume the entries and write them to the output file.
4949
// The exit code is based on the result of the output writer.
5050
self.output_writer.run(entries)

rust/bear/src/semantic/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl<T> IntoIterator for Recognition<T> {
8484
/// It conditionally removes compiler calls based on compiler names or flags.
8585
/// It can also alter the compiler flags of the compiler calls. The actions
8686
/// are defined in the configuration this module is given.
87-
pub trait Transform: Send {
87+
pub trait Transformation: Send {
8888
fn apply(&self, _: CompilerCall) -> Option<CompilerCall>;
8989
}
9090

0 commit comments

Comments
 (0)