Skip to content

Commit b853c7e

Browse files
committed
conditionally truncate thread comment length
1 parent b4e4b13 commit b853c7e

File tree

10 files changed

+300
-217
lines changed

10 files changed

+300
-217
lines changed

cpp-linter-lib/examples/cli_doc.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,20 @@ pub fn main() -> std::io::Result<()> {
2424
writeln!(&doc_file)?;
2525
}
2626
for group in command.get_groups() {
27-
writeln!(&doc_file, "\n{}", group.get_id().to_string())?;
27+
writeln!(&doc_file, "\n{}", group.get_id())?;
2828
for _ in group.get_id().to_string().chars() {
2929
write!(&doc_file, "-")?;
3030
}
3131
write!(&doc_file, "\n\n")?;
3232
for arg_id in group.get_args() {
3333
let mut arg_match = command.get_arguments().filter(|a| *a.get_id() == *arg_id);
34-
let arg = arg_match.next().expect(
35-
format!(
34+
let arg = arg_match.next().unwrap_or_else(|| {
35+
panic!(
3636
"arg {} expected in group {}",
3737
arg_id.as_str(),
3838
group.get_id().as_str()
3939
)
40-
.as_str(),
41-
);
40+
});
4241
writeln!(
4342
&doc_file,
4443
".. std:option:: -{}, --{}\n",

cpp-linter-lib/src/clang_tools/clang_format.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,26 @@ impl Clone for Replacement {
6060
}
6161
}
6262

63-
/// Get a total count of clang-format advice from the given list of [FormatAdvice].
64-
pub fn tally_format_advice(format_advice: &[FormatAdvice]) -> u64 {
65-
let mut format_checks_failed: u64 = 0;
66-
for fmt_advice in format_advice.iter() {
67-
if !fmt_advice.replacements.is_empty() {
68-
format_checks_failed += 1;
63+
/// Get a total count of clang-format advice from the given list of [FileObj]s.
64+
pub fn tally_format_advice(files: &[FileObj]) -> u64 {
65+
let mut total = 0;
66+
for file in files {
67+
if let Some(advice) = &file.format_advice {
68+
if !advice.replacements.is_empty() {
69+
total += 1;
70+
}
6971
}
7072
}
71-
format_checks_failed
73+
total
7274
}
7375

7476
/// Run clang-tidy for a specific `file`, then parse and return it's XML output.
7577
pub fn run_clang_format(
7678
cmd: &mut Command,
77-
file: &FileObj,
79+
file: &mut FileObj,
7880
style: &str,
7981
lines_changed_only: &LinesChangedOnly,
80-
) -> FormatAdvice {
82+
) {
8183
cmd.args(["--style", style, "--output-replacements-xml"]);
8284
let ranges = file.get_ranges(lines_changed_only);
8385
for range in &ranges {
@@ -104,9 +106,7 @@ pub fn run_clang_format(
104106
// String::from_utf8(output.stdout.clone()).unwrap()
105107
// );
106108
if output.stdout.is_empty() {
107-
return FormatAdvice {
108-
replacements: vec![],
109-
};
109+
return;
110110
}
111111
let xml = String::from_utf8(output.stdout)
112112
.unwrap()
@@ -118,8 +118,8 @@ pub fn run_clang_format(
118118
.whitespace_to_characters(true)
119119
.ignore_root_level_whitespace(true);
120120
let event_reader = serde_xml_rs::EventReader::new_with_config(xml.as_bytes(), config);
121-
let mut format_advice: FormatAdvice =
122-
FormatAdvice::deserialize(&mut Deserializer::new(event_reader)).unwrap_or(FormatAdvice {
121+
let mut format_advice = FormatAdvice::deserialize(&mut Deserializer::new(event_reader))
122+
.unwrap_or(FormatAdvice {
123123
replacements: vec![],
124124
});
125125
if !format_advice.replacements.is_empty() {
@@ -141,7 +141,7 @@ pub fn run_clang_format(
141141
}
142142
format_advice.replacements = filtered_replacements;
143143
}
144-
format_advice
144+
file.format_advice = Some(format_advice)
145145
}
146146

147147
#[cfg(test)]

cpp-linter-lib/src/clang_tools/clang_tidy.rs

+33-9
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ struct CompilationUnit {
4444
}
4545

4646
/// A structure that represents a single notification parsed from clang-tidy's stdout.
47+
#[derive(Debug)]
4748
pub struct TidyNotification {
4849
/// The file's path and name (supposedly relative to the repository root folder).
4950
pub filename: String,
@@ -86,6 +87,7 @@ impl TidyNotification {
8687
}
8788

8889
/// A struct to hold notification from clang-tidy about a single file
90+
#[derive(Debug)]
8991
pub struct TidyAdvice {
9092
/// A list of notifications parsed from clang-tidy stdout.
9193
pub notes: Vec<TidyNotification>,
@@ -98,7 +100,7 @@ pub struct TidyAdvice {
98100
fn parse_tidy_output(
99101
tidy_stdout: &[u8],
100102
database_json: &Option<CompilationDatabase>,
101-
) -> TidyAdvice {
103+
) -> Option<TidyAdvice> {
102104
let note_header = Regex::new(r"^(.+):(\d+):(\d+):\s(\w+):(.*)\[([a-zA-Z\d\-\.]+)\]$").unwrap();
103105
let mut notification = None;
104106
let mut result = Vec::new();
@@ -163,19 +165,39 @@ fn parse_tidy_output(
163165
if let Some(note) = notification {
164166
result.push(note);
165167
}
166-
TidyAdvice { notes: result }
168+
if result.is_empty() {
169+
None
170+
} else {
171+
Some(TidyAdvice { notes: result })
172+
}
173+
}
174+
175+
/// Get a total count of clang-tidy advice from the given list of [FileObj]s.
176+
pub fn tally_tidy_advice(files: &[FileObj]) -> u64 {
177+
let mut total = 0;
178+
for file in files {
179+
if let Some(advice) = &file.tidy_advice {
180+
for tidy_note in &advice.notes {
181+
let file_path = PathBuf::from(&tidy_note.filename);
182+
if file_path == file.name {
183+
total += 1;
184+
}
185+
}
186+
}
187+
}
188+
total
167189
}
168190

169191
/// Run clang-tidy, then parse and return it's output.
170192
pub fn run_clang_tidy(
171193
cmd: &mut Command,
172-
file: &FileObj,
194+
file: &mut FileObj,
173195
checks: &str,
174196
lines_changed_only: &LinesChangedOnly,
175197
database: &Option<PathBuf>,
176198
extra_args: &Option<Vec<&str>>,
177199
database_json: &Option<CompilationDatabase>,
178-
) -> TidyAdvice {
200+
) {
179201
if !checks.is_empty() {
180202
cmd.args(["-checks", checks]);
181203
}
@@ -222,7 +244,7 @@ pub fn run_clang_tidy(
222244
String::from_utf8(output.stderr).unwrap()
223245
);
224246
}
225-
parse_tidy_output(&output.stdout, database_json)
247+
file.tidy_advice = parse_tidy_output(&output.stdout, database_json);
226248
}
227249

228250
#[cfg(test)]
@@ -265,11 +287,11 @@ mod test {
265287
)
266288
.unwrap();
267289
let mut cmd = Command::new(exe_path);
268-
let file = FileObj::new(PathBuf::from("tests/demo/demo.cpp"));
290+
let mut file = FileObj::new(PathBuf::from("tests/demo/demo.cpp"));
269291
let extra_args = vec!["-std=c++17", "-Wall"];
270-
let tidy_advice = run_clang_tidy(
292+
run_clang_tidy(
271293
&mut cmd,
272-
&file,
294+
&mut file,
273295
"", // use .clang-tidy config file
274296
&LinesChangedOnly::Off, // check all lines
275297
&None, // no database path
@@ -286,6 +308,8 @@ mod test {
286308
vec!["--extra-arg", "\"-std=c++17\"", "--extra-arg", "\"-Wall\""],
287309
args
288310
);
289-
assert!(!tidy_advice.notes.is_empty());
311+
assert!(!file
312+
.tidy_advice
313+
.is_some_and(|advice| advice.notes.is_empty()));
290314
}
291315
}

cpp-linter-lib/src/clang_tools/mod.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ use crate::{
1515
logger::{end_log_group, start_log_group},
1616
};
1717
pub mod clang_format;
18-
use clang_format::{run_clang_format, FormatAdvice};
18+
use clang_format::run_clang_format;
1919
pub mod clang_tidy;
20-
use clang_tidy::{run_clang_tidy, CompilationDatabase, TidyAdvice};
20+
use clang_tidy::{run_clang_tidy, CompilationDatabase};
2121

2222
/// Fetch the path to a clang tool by `name` (ie `"clang-tidy"` or `"clang-format"`) and
2323
/// `version`.
@@ -79,14 +79,14 @@ pub fn get_clang_tool_exe(name: &str, version: &str) -> Result<PathBuf, &'static
7979
/// If `tidy_checks` is `"-*"` then clang-tidy is not executed.
8080
/// If `style` is a blank string (`""`), then clang-format is not executed.
8181
pub fn capture_clang_tools_output(
82-
files: &Vec<FileObj>,
82+
files: &mut Vec<FileObj>,
8383
version: &str,
8484
tidy_checks: &str,
8585
style: &str,
8686
lines_changed_only: &LinesChangedOnly,
8787
database: Option<PathBuf>,
8888
extra_args: Option<Vec<&str>>,
89-
) -> (Vec<FormatAdvice>, Vec<TidyAdvice>) {
89+
) {
9090
// find the executable paths for clang-tidy and/or clang-format and show version
9191
// info as debugging output.
9292
let clang_tidy_command = if tidy_checks != "-*" {
@@ -129,32 +129,29 @@ pub fn capture_clang_tools_output(
129129
};
130130

131131
// iterate over the discovered files and run the clang tools
132-
let mut all_format_advice: Vec<FormatAdvice> = Vec::with_capacity(files.len());
133-
let mut all_tidy_advice: Vec<TidyAdvice> = Vec::with_capacity(files.len());
134132
for file in files {
135133
start_log_group(format!("Analyzing {}", file.name.to_string_lossy()));
136134
if let Some(tidy_cmd) = &clang_tidy_command {
137-
all_tidy_advice.push(run_clang_tidy(
135+
run_clang_tidy(
138136
&mut Command::new(tidy_cmd),
139137
file,
140138
tidy_checks,
141139
lines_changed_only,
142140
&database,
143141
&extra_args,
144142
&database_json,
145-
));
143+
);
146144
}
147145
if let Some(format_cmd) = &clang_format_command {
148-
all_format_advice.push(run_clang_format(
146+
run_clang_format(
149147
&mut Command::new(format_cmd),
150148
file,
151149
style,
152150
lines_changed_only,
153-
));
151+
);
154152
}
155153
end_log_group();
156154
}
157-
(all_format_advice, all_tidy_advice)
158155
}
159156

160157
#[cfg(test)]

cpp-linter-lib/src/common_fs.rs

+12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use std::path::{Component, Path};
55
use std::{fs, io};
66
use std::{ops::RangeInclusive, path::PathBuf};
77

8+
use crate::clang_tools::clang_format::FormatAdvice;
9+
use crate::clang_tools::clang_tidy::TidyAdvice;
810
use crate::cli::LinesChangedOnly;
911

1012
/// A structure to represent a file's path and line changes.
@@ -21,6 +23,12 @@ pub struct FileObj {
2123

2224
/// The list of ranges that span the lines present in diff chunks.
2325
pub diff_chunks: Vec<RangeInclusive<u32>>,
26+
27+
/// The collection of clang-format advice for this file.
28+
pub format_advice: Option<FormatAdvice>,
29+
30+
/// The collection of clang-format advice for this file.
31+
pub tidy_advice: Option<TidyAdvice>,
2432
}
2533

2634
impl FileObj {
@@ -33,6 +41,8 @@ impl FileObj {
3341
added_lines: Vec::<u32>::new(),
3442
added_ranges: Vec::<RangeInclusive<u32>>::new(),
3543
diff_chunks: Vec::<RangeInclusive<u32>>::new(),
44+
format_advice: None,
45+
tidy_advice: None,
3646
}
3747
}
3848

@@ -48,6 +58,8 @@ impl FileObj {
4858
added_lines,
4959
added_ranges,
5060
diff_chunks,
61+
format_advice: None,
62+
tidy_advice: None,
5163
}
5264
}
5365

cpp-linter-lib/src/git.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ rename to /tests/demo/some source.cpp
304304
)
305305
}
306306

307-
fn assert_files_eq(files_from_a: &Vec<FileObj>, files_from_b: &Vec<FileObj>) {
307+
fn assert_files_eq(files_from_a: &[FileObj], files_from_b: &[FileObj]) {
308308
assert_eq!(files_from_a.len(), files_from_b.len());
309309
for (a, b) in files_from_a.iter().zip(files_from_b) {
310310
assert_eq!(a.name, b.name);

cpp-linter-lib/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
///! This crate is the binary executable's entrypoint.
1+
/// This crate is the binary executable's entrypoint.
22
use std::env;
33

44
use cpp_linter_lib::run::run_main;

0 commit comments

Comments
 (0)