Skip to content

Commit b242cfe

Browse files
committed
remove semantic output format
1 parent f155a7c commit b242cfe

File tree

9 files changed

+72
-303
lines changed

9 files changed

+72
-303
lines changed

bear/src/config.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
//! intercept:
7777
//! mode: preload
7878
//! output:
79-
//! specification: bear
79+
//! specification: clang
8080
//! ```
8181
8282
// Re-Export the types and the loader module content.
@@ -178,11 +178,12 @@ mod types {
178178

179179
/// Output configuration is used to customize the output format.
180180
///
181-
/// Allow customizing the output format of the compiler calls.
181+
/// Configures how the compiler calls are output in the clang project defined
182+
/// "JSON compilation database" format. (The format is used by clang tooling
183+
/// and other tools based on that library.)
182184
///
183-
/// - Clang: Output the compiler calls in the clang project defined "JSON compilation database"
184-
/// format. (The format is used by clang tooling and other tools based on that library.)
185-
/// - Semantic: Output the compiler calls in the semantic format. (The format is not defined yet.)
185+
/// The configuration allows customization of compiler filtering, source filtering,
186+
/// duplicate handling, and output formatting.
186187
#[derive(Debug, PartialEq, Deserialize, Serialize)]
187188
#[serde(tag = "specification")]
188189
pub enum Output {
@@ -197,11 +198,9 @@ mod types {
197198
#[serde(default)]
198199
format: Format,
199200
},
200-
#[serde(rename = "bear")]
201-
Semantic {},
202201
}
203202

204-
/// The default output is the clang format.
203+
/// The default output configuration.
205204
impl Default for Output {
206205
fn default() -> Self {
207206
Output::Clang {
@@ -745,17 +744,17 @@ pub mod loader {
745744
mode: preload
746745
path: /usr/local/lib/libexec.so
747746
output:
748-
specification: bear
747+
specification: clang
749748
"#;
750749

751750
let result = Loader::from_reader(content).unwrap();
752751

753752
let expected = Main {
753+
schema: String::from("4.0"),
754754
intercept: Intercept::Preload {
755755
path: PathBuf::from("/usr/local/lib/libexec.so"),
756756
},
757-
output: Output::Semantic {},
758-
schema: String::from("4.0"),
757+
output: Output::default(),
759758
};
760759

761760
assert_eq!(expected, result);
@@ -794,6 +793,7 @@ pub mod loader {
794793
let result = Loader::from_reader(content).unwrap();
795794

796795
let expected = Main {
796+
schema: String::from("4.0"),
797797
intercept: Intercept::Preload {
798798
path: default_preload_library(),
799799
},
@@ -838,7 +838,6 @@ pub mod loader {
838838
},
839839
},
840840
},
841-
schema: String::from("4.0"),
842841
};
843842

844843
assert_eq!(expected, result);

bear/src/modes/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ mod impls {
276276
///
277277
/// The `output` argument contains the configuration for the output file location,
278278
/// while the `config` argument contains the configuration for the semantic analysis
279-
/// and the output format.
279+
/// and clang compilation database formatting.
280280
pub(super) fn create(
281281
output: args::BuildSemantic,
282282
config: &config::Main,

bear/src/output/formats.rs

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//! - The execution event database format (internal format of this project.)
99
1010
use super::json;
11-
use crate::{intercept, semantic, semantic::clang};
11+
use crate::{intercept, semantic::clang};
1212
use serde_json::de::IoRead;
1313
use serde_json::StreamDeserializer;
1414
use thiserror::Error;
@@ -95,31 +95,6 @@ impl SerializationFormat<clang::Entry> for JsonCompilationDatabase {
9595
}
9696
}
9797

98-
/// The type represents a JSON semantic database format.
99-
///
100-
/// The format is a JSON array format, which is a sequence of JSON objects
101-
/// enclosed in square brackets. Each object represents a semantic analysis
102-
/// result.
103-
///
104-
/// # Note
105-
/// The output format is not stable and may change in future versions.
106-
pub struct JsonSemanticDatabase;
107-
108-
impl SerializationFormat<semantic::Command> for JsonSemanticDatabase {
109-
fn write(
110-
writer: impl std::io::Write,
111-
commands: impl Iterator<Item = semantic::Command>,
112-
) -> Result<(), SerializationError> {
113-
json::serialize_seq(writer, commands).map_err(SerializationError::Syntax)
114-
}
115-
fn read(
116-
_: impl std::io::Read,
117-
) -> impl Iterator<Item = Result<semantic::Command, SerializationError>> {
118-
// Not implemented! (No reader for the semantic output in this project.)
119-
std::iter::empty()
120-
}
121-
}
122-
12398
/// The type represents a database format for execution events.
12499
///
125100
/// The format is a [JSON line format](https://jsonlines.org/), which is a sequence
@@ -153,9 +128,9 @@ impl SerializationFormat<intercept::Event> for ExecutionEventDatabase {
153128
#[cfg(test)]
154129
mod test {
155130
mod compilation_database {
156-
use super::super::semantic::clang::{Entry, EntryError};
157131
use super::super::JsonCompilationDatabase as Sut;
158132
use super::super::{SerializationError, SerializationFormat};
133+
use crate::semantic::clang::{Entry, EntryError};
159134
use serde_json::error::Category;
160135
use serde_json::json;
161136
use std::io::{Cursor, Seek, SeekFrom};

bear/src/output/json.rs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,6 @@ where
4444
Ok(())
4545
}
4646

47-
/// Serialize entries from an iterator into a JSON array.
48-
pub fn serialize_seq<W, T>(
49-
writer: W,
50-
entries: impl Iterator<Item = T>,
51-
) -> Result<(), serde_json::Error>
52-
where
53-
W: io::Write,
54-
T: Serialize,
55-
{
56-
let mut ser = serde_json::Serializer::pretty(writer);
57-
let mut seq = ser.serialize_seq(None)?;
58-
for entry in entries {
59-
seq.serialize_element(&entry)?;
60-
}
61-
seq.end()
62-
}
63-
6447
/// Deserialize entries from a JSON array into an iterator.
6548
///
6649
/// from https://github.com/serde-rs/json/issues/404#issuecomment-892957228
@@ -252,7 +235,11 @@ mod tests {
252235
{
253236
// Create fake "file"
254237
let mut buffer = Cursor::new(Vec::new());
255-
serialize_seq(&mut buffer, input.iter().cloned()).unwrap();
238+
serialize_result_seq(
239+
&mut buffer,
240+
input.iter().cloned().map(Ok::<_, serde_json::Error>),
241+
)
242+
.unwrap();
256243

257244
// Use the fake "file" as input
258245
buffer.seek(SeekFrom::Start(0)).unwrap();

bear/src/output/mod.rs

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22

33
//! This module is responsible for writing the output of semantic analysis.
44
//!
5-
//! The output can be in different formats, such as JSON compilation databases
6-
//! or semantic analysis results in JSON format. The module provides functionality
7-
//! to write these outputs to files, handle duplicates, and format the output
8-
//! as needed.
5+
//! The output is written as JSON compilation databases.
6+
//! The module provides functionality to write these outputs to files,
7+
//! handle duplicates, and format the output as needed.
98
//!
10-
//! The `OutputWriter` enum represents the main entry point for writing output.
9+
//! The `OutputWriter` struct represents the main entry point for writing output.
1110
//! The input to the `OutputWriter` is a stream of `semantic::Command` instances.
1211
1312
mod formats;
@@ -18,7 +17,7 @@ use crate::{args, config, semantic};
1817
use thiserror::Error;
1918
use writers::{
2019
AppendClangOutputWriter, AtomicClangOutputWriter, ClangOutputWriter,
21-
ConverterClangOutputWriter, IteratorWriter, SemanticOutputWriter, UniqueOutputWriter,
20+
ConverterClangOutputWriter, IteratorWriter, UniqueOutputWriter,
2221
};
2322

2423
// Re-export types for convenience.
@@ -29,18 +28,13 @@ type ClangWriterStack = ConverterClangOutputWriter<
2928
AppendClangOutputWriter<AtomicClangOutputWriter<UniqueOutputWriter<ClangOutputWriter>>>,
3029
>;
3130

32-
/// Represents the output writer, which can handle different types of outputs.
31+
/// Represents the output writer for JSON compilation databases.
3332
///
34-
/// This enum provides two variants:
35-
/// - `Clang`: Writes output as a JSON compilation database.
36-
/// - `Semantic`: Writes output as a JSON semantic analysis result.
37-
///
38-
/// The variants are selected at runtime based on the configuration provided.
39-
pub enum OutputWriter {
40-
#[allow(private_interfaces)]
41-
Clang(ClangWriterStack),
33+
/// The writer handles writing semantic analysis results as JSON compilation databases,
34+
/// with support for deduplication, atomic writes, and appending to existing files.
35+
pub struct OutputWriter {
4236
#[allow(private_interfaces)]
43-
Semantic(SemanticOutputWriter),
37+
writer: ClangWriterStack,
4438
}
4539

4640
impl TryFrom<(&args::BuildSemantic, &config::Output)> for OutputWriter {
@@ -64,12 +58,9 @@ impl TryFrom<(&args::BuildSemantic, &config::Output)> for OutputWriter {
6458
let formatted_writer =
6559
ConverterClangOutputWriter::new(append_writer, &format.entry);
6660

67-
Ok(Self::Clang(formatted_writer))
68-
}
69-
config::Output::Semantic { .. } => {
70-
let path = std::path::Path::new(&args.path);
71-
let result = SemanticOutputWriter::try_from(path)?;
72-
Ok(Self::Semantic(result))
61+
Ok(Self {
62+
writer: formatted_writer,
63+
})
7364
}
7465
}
7566
}
@@ -87,10 +78,7 @@ impl OutputWriter {
8778
self,
8879
semantics: impl Iterator<Item = semantic::Command>,
8980
) -> Result<(), WriterError> {
90-
match self {
91-
Self::Clang(writer) => writer.write(semantics),
92-
Self::Semantic(writer) => writer.write(semantics),
93-
}
81+
self.writer.write(semantics)
9482
}
9583
}
9684

bear/src/output/writers.rs

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

3-
use super::formats::{
4-
JsonCompilationDatabase, JsonSemanticDatabase, SerializationError, SerializationFormat,
5-
};
3+
use super::formats::{JsonCompilationDatabase, SerializationError, SerializationFormat};
64
use super::{WriterCreationError, WriterError};
75
use crate::semantic::clang::{DuplicateEntryFilter, Entry};
86
use crate::{config, semantic};
@@ -20,38 +18,6 @@ pub(super) trait IteratorWriter<T> {
2018
fn write(self, items: impl Iterator<Item = T>) -> Result<(), WriterError>;
2119
}
2220

23-
/// The type represents a writer for semantic analysis results to a file.
24-
///
25-
/// # Note
26-
/// The output format is not stable and may change in future versions.
27-
/// It reflects the internal representation of the semantic analysis types.
28-
pub(super) struct SemanticOutputWriter {
29-
output: io::BufWriter<fs::File>,
30-
path: path::PathBuf,
31-
}
32-
33-
impl TryFrom<&path::Path> for SemanticOutputWriter {
34-
type Error = WriterCreationError;
35-
36-
fn try_from(output_path: &path::Path) -> Result<Self, Self::Error> {
37-
let output = fs::File::create(output_path)
38-
.map(io::BufWriter::new)
39-
.map_err(|err| WriterCreationError::Io(output_path.to_path_buf(), err))?;
40-
41-
Ok(Self {
42-
output,
43-
path: output_path.to_path_buf(),
44-
})
45-
}
46-
}
47-
48-
impl IteratorWriter<semantic::Command> for SemanticOutputWriter {
49-
fn write(self, semantics: impl Iterator<Item = semantic::Command>) -> Result<(), WriterError> {
50-
JsonSemanticDatabase::write(self.output, semantics)
51-
.map_err(|err| WriterError::Io(self.path, err))
52-
}
53-
}
54-
5521
/// The type represents a converter that formats `semantic::Command` instances into `Entry` objects.
5622
pub(super) struct ConverterClangOutputWriter<T: IteratorWriter<Entry>> {
5723
converter: semantic::clang::CommandConverter,

0 commit comments

Comments
 (0)