22//! output.
33
44use std:: {
5- env:: { consts:: OS , current_dir } ,
5+ env:: consts:: OS ,
66 fs,
7- path:: PathBuf ,
7+ path:: { Path , PathBuf } ,
88 process:: Command ,
99 sync:: { Arc , Mutex , MutexGuard } ,
1010} ;
@@ -144,13 +144,13 @@ const NOTE_HEADER: &str = r"^(.+):(\d+):(\d+):\s(\w+):(.*)\[([a-zA-Z\d\-\.]+),?[
144144fn parse_tidy_output (
145145 tidy_stdout : & [ u8 ] ,
146146 database_json : & Option < Vec < CompilationUnit > > ,
147+ repo_root : & Path ,
147148) -> Result < TidyAdvice , ClangCaptureError > {
148149 let note_header = Regex :: new ( NOTE_HEADER ) ?;
149150 let fixed_note = Regex :: new ( r"^.+:(\d+):\d+:\snote: FIX-IT applied suggested code changes$" ) ?;
150151 let mut found_fix = false ;
151152 let mut notification = None ;
152153 let mut result = Vec :: new ( ) ;
153- let cur_dir = current_dir ( ) . map_err ( ClangCaptureError :: UnknownWorkingDirectory ) ?;
154154 for line in String :: from_utf8 ( tidy_stdout. to_vec ( ) )
155155 . map_err ( |e| ClangCaptureError :: NonUtf8Output {
156156 task : "convert clang-tidy stdout" . to_string ( ) ,
@@ -191,16 +191,16 @@ fn parse_tidy_output(
191191 // file was not a named unit in the database;
192192 // try to normalize path as if relative to working directory.
193193 // NOTE: This shouldn't happen with a properly formed JSON database
194- filename = normalize_path ( & PathBuf :: from_iter ( [ & cur_dir , & filename] ) ) ;
194+ filename = normalize_path ( & PathBuf :: from_iter ( [ repo_root , & filename] ) ) ;
195195 }
196196 } else {
197197 // still need to normalize the relative path despite missing database info.
198198 // let's assume the file is relative to current working directory.
199- filename = normalize_path ( & PathBuf :: from_iter ( [ & cur_dir , & filename] ) ) ;
199+ filename = normalize_path ( & PathBuf :: from_iter ( [ repo_root , & filename] ) ) ;
200200 }
201- debug_assert ! ( filename . is_absolute ( ) ) ;
201+
202202 if filename. is_absolute ( )
203- && let Ok ( file_n) = filename. strip_prefix ( & cur_dir )
203+ && let Ok ( file_n) = filename. strip_prefix ( repo_root )
204204 {
205205 // if this filename can't be made into a relative path, then it is
206206 // likely not a member of the project's sources (ie /usr/include/stdio.h)
@@ -297,16 +297,17 @@ pub fn run_clang_tidy(
297297 ) ;
298298 cmd. args ( [ "--line-filter" , filter. as_str ( ) ] ) ;
299299 }
300+ let repo_file_path = PathBuf :: from_iter ( [ & clang_params. repo_root , & file. name ] ) ;
300301 let original_content = if !clang_params. tidy_review {
301302 None
302303 } else {
303304 cmd. arg ( "--fix-errors" ) ;
304- Some (
305- fs :: read_to_string ( & file . name ) . map_err ( |e| ClangCaptureError :: ReadFileFailed {
305+ Some ( fs :: read_to_string ( & repo_file_path ) . map_err ( |e| {
306+ ClangCaptureError :: ReadFileFailed {
306307 file_name : file_name. clone ( ) ,
307308 source : e,
308- } ) ? ,
309- )
309+ }
310+ } ) ? )
310311 } ;
311312 if !clang_params. style . is_empty ( ) {
312313 cmd. args ( [ "--format-style" , clang_params. style . as_str ( ) ] ) ;
@@ -348,6 +349,7 @@ pub fn run_clang_tidy(
348349 file. tidy_advice = Some ( parse_tidy_output (
349350 & output. stdout ,
350351 & clang_params. database_json ,
352+ & clang_params. repo_root ,
351353 ) ?) ;
352354 if clang_params. tidy_review
353355 && let Some ( original_content) = & original_content
@@ -356,14 +358,14 @@ pub fn run_clang_tidy(
356358 // cache file changes in a buffer and restore the original contents for further analysis
357359 tidy_advice. patched =
358360 Some (
359- fs:: read ( & file_name ) . map_err ( |e| ClangCaptureError :: ReadFileFailed {
361+ fs:: read ( & repo_file_path ) . map_err ( |e| ClangCaptureError :: ReadFileFailed {
360362 file_name : file_name. clone ( ) ,
361363 source : e,
362364 } ) ?,
363365 ) ;
364366 }
365367 // original_content is guaranteed to be Some() value at this point
366- fs:: write ( & file_name , original_content) . map_err ( |e| {
368+ fs:: write ( & repo_file_path , original_content) . map_err ( |e| {
367369 ClangCaptureError :: WriteFileFailed {
368370 file_name,
369371 source : e,
@@ -565,7 +567,7 @@ TrenchBroom/TrenchBroom/common/test/src/mdl/tst_ReadFreeImageTexture.cpp:44:48:
565567 44 | return diskFS.openFile(name) | kdl::and_then([](const auto& file) {
566568 | ^
567569"# ;
568- let advice = parse_tidy_output ( tidy_out. as_bytes ( ) , & None ) . unwrap ( ) ;
570+ let advice = parse_tidy_output ( tidy_out. as_bytes ( ) , & None , & PathBuf :: from ( "." ) ) . unwrap ( ) ;
569571 assert_eq ! ( advice. notes. len( ) , 4 ) ;
570572 for note in advice. notes {
571573 assert ! ( note. diagnostic. contains( '-' ) ) ;
0 commit comments