@@ -14,7 +14,7 @@ use serde::Deserialize;
14
14
15
15
// project-specific modules/crates
16
16
use crate :: {
17
- cli:: LinesChangedOnly ,
17
+ cli:: { ClangParams , LinesChangedOnly } ,
18
18
common_fs:: { normalize_path, FileObj } ,
19
19
} ;
20
20
@@ -184,29 +184,25 @@ pub fn tally_tidy_advice(files: &[Arc<Mutex<FileObj>>]) -> u64 {
184
184
185
185
/// Run clang-tidy, then parse and return it's output.
186
186
pub fn run_clang_tidy (
187
- cmd : & mut Command ,
188
187
file : & mut Arc < Mutex < FileObj > > ,
189
- checks : & str ,
190
- lines_changed_only : & LinesChangedOnly ,
191
- database : & Option < PathBuf > ,
192
- extra_args : & Option < Vec < String > > ,
193
- database_json : & Option < Vec < CompilationUnit > > ,
188
+ clang_params : & ClangParams ,
194
189
) -> Vec < ( log:: Level , std:: string:: String ) > {
190
+ let mut cmd = Command :: new ( clang_params. clang_tidy_command . as_ref ( ) . unwrap ( ) ) ;
195
191
let mut logs = vec ! [ ] ;
196
192
let mut file = file. lock ( ) . unwrap ( ) ;
197
- if !checks . is_empty ( ) {
198
- cmd. args ( [ "-checks" , checks ] ) ;
193
+ if !clang_params . tidy_checks . is_empty ( ) {
194
+ cmd. args ( [ "-checks" , & clang_params . tidy_checks ] ) ;
199
195
}
200
- if let Some ( db) = database {
196
+ if let Some ( db) = & clang_params . database {
201
197
cmd. args ( [ "-p" , & db. to_string_lossy ( ) ] ) ;
202
198
}
203
- if let Some ( extras) = extra_args {
199
+ if let Some ( extras) = & clang_params . extra_args {
204
200
for arg in extras {
205
201
cmd. args ( [ "--extra-arg" , format ! ( "\" {}\" " , arg) . as_str ( ) ] ) ;
206
202
}
207
203
}
208
- if * lines_changed_only != LinesChangedOnly :: Off {
209
- let ranges = file. get_ranges ( lines_changed_only) ;
204
+ if clang_params . lines_changed_only != LinesChangedOnly :: Off {
205
+ let ranges = file. get_ranges ( & clang_params . lines_changed_only ) ;
210
206
let filter = format ! (
211
207
"[{{\" name\" :{:?},\" lines\" :{:?}}}]" ,
212
208
& file
@@ -249,7 +245,7 @@ pub fn run_clang_tidy(
249
245
) ,
250
246
) ) ;
251
247
}
252
- file. tidy_advice = parse_tidy_output ( & output. stdout , database_json) ;
248
+ file. tidy_advice = parse_tidy_output ( & output. stdout , & clang_params . database_json ) ;
253
249
logs
254
250
}
255
251
@@ -258,13 +254,16 @@ mod test {
258
254
use std:: {
259
255
env,
260
256
path:: PathBuf ,
261
- process:: Command ,
262
257
sync:: { Arc , Mutex } ,
263
258
} ;
264
259
265
260
use regex:: Regex ;
266
261
267
- use crate :: { clang_tools:: get_clang_tool_exe, cli:: LinesChangedOnly , common_fs:: FileObj } ;
262
+ use crate :: {
263
+ clang_tools:: get_clang_tool_exe,
264
+ cli:: { ClangParams , LinesChangedOnly } ,
265
+ common_fs:: FileObj ,
266
+ } ;
268
267
269
268
use super :: run_clang_tidy;
270
269
@@ -297,33 +296,41 @@ mod test {
297
296
env:: var ( "CLANG_VERSION" ) . unwrap_or ( "" . to_string ( ) ) . as_str ( ) ,
298
297
)
299
298
. unwrap ( ) ;
300
- let mut cmd = Command :: new ( exe_path) ;
301
299
let file = FileObj :: new ( PathBuf :: from ( "tests/demo/demo.cpp" ) ) ;
302
300
let mut arc_ref = Arc :: new ( Mutex :: new ( file) ) ;
303
301
let extra_args = vec ! [ "-std=c++17" . to_string( ) , "-Wall" . to_string( ) ] ;
304
- run_clang_tidy (
305
- & mut cmd,
306
- & mut arc_ref,
307
- "" , // use .clang-tidy config file
308
- & LinesChangedOnly :: Off , // check all lines
309
- & None , // no database path
310
- & Some ( extra_args) , // <---- the reason for this test
311
- & None , // no deserialized database
312
- ) ;
313
- // since `cmd` was passed as a mutable reference, we can inspect the args that were added
314
- let locked_file = arc_ref. lock ( ) . unwrap ( ) ;
315
- let mut args = cmd
316
- . get_args ( )
317
- . map ( |arg| arg. to_str ( ) . unwrap ( ) )
302
+ let clang_params = ClangParams {
303
+ style : "" . to_string ( ) ,
304
+ tidy_checks : "" . to_string ( ) , // use .clang-tidy config file
305
+ lines_changed_only : LinesChangedOnly :: Off ,
306
+ database : None ,
307
+ extra_args : Some ( extra_args. clone ( ) ) , // <---- the reason for this test
308
+ database_json : None ,
309
+ format_filter : None ,
310
+ tidy_filter : None ,
311
+ tidy_review : false ,
312
+ format_review : false ,
313
+ clang_tidy_command : Some ( exe_path) ,
314
+ clang_format_command : None ,
315
+ } ;
316
+ let logs = run_clang_tidy ( & mut arc_ref, & clang_params)
317
+ . into_iter ( )
318
+ . filter_map ( |( _lvl, msg) | {
319
+ if msg. contains ( "Running " ) {
320
+ return Some ( msg) ;
321
+ } else {
322
+ return None ;
323
+ }
324
+ } )
325
+ . collect :: < Vec < String > > ( ) ;
326
+ let args = & logs
327
+ . first ( )
328
+ . expect ( "expected a log message about invoked clang-tidy command" )
329
+ . split ( ' ' )
318
330
. collect :: < Vec < & str > > ( ) ;
319
- assert_eq ! ( locked_file. name. to_string_lossy( ) , args. pop( ) . unwrap( ) ) ;
320
- assert_eq ! (
321
- vec![ "--extra-arg" , "\" -std=c++17\" " , "--extra-arg" , "\" -Wall\" " ] ,
322
- args
323
- ) ;
324
- assert ! ( !locked_file
325
- . tidy_advice
326
- . as_ref( )
327
- . is_some_and( |advice| advice. notes. is_empty( ) ) ) ;
331
+ for arg in & extra_args {
332
+ let extra_arg = format ! ( "\" {arg}\" " ) ;
333
+ assert ! ( args. contains( & extra_arg. as_str( ) ) ) ;
334
+ }
328
335
}
329
336
}
0 commit comments