diff --git a/cpp-linter/src/cli/structs.rs b/cpp-linter/src/cli/structs.rs index 469dca95..5db6839e 100644 --- a/cpp-linter/src/cli/structs.rs +++ b/cpp-linter/src/cli/structs.rs @@ -375,6 +375,9 @@ pub struct FeedbackInput { /// The path of the generated patch file. pub patch_path: PathBuf, + + /// Whether a compilation database was given (see `--database`). + pub has_database: bool, } impl FeedbackInput { @@ -434,6 +437,7 @@ impl From<&Cli> for FeedbackInput { Some(path) => absolute_or_given(path), None => ClangParams::default_patch_path(&args.source_options.repo_root), }, + has_database: args.tidy_options.database.is_some(), } } } @@ -456,6 +460,7 @@ impl Default for FeedbackInput { code_quality_file: None, gen_patch: false, patch_path: ClangParams::default_patch_path(Path::new(".")), + has_database: false, } } } @@ -606,6 +611,8 @@ mod test { let cli = Cli::parse_from(["cpp-linter", "--database", tmp_dir.path().to_str().unwrap()]); let clang_params = ClangParams::from(&cli); assert_eq!(clang_params.database, Some(tmp_dir.path().to_path_buf())); + assert!(FeedbackInput::from(&cli).has_database); + assert!(!FeedbackInput::from(&Cli::parse_from(["cpp-linter"])).has_database); } #[test] diff --git a/cpp-linter/src/rest_client/comment.rs b/cpp-linter/src/rest_client/comment.rs index 91732421..a3801377 100644 --- a/cpp-linter/src/rest_client/comment.rs +++ b/cpp-linter/src/rest_client/comment.rs @@ -49,6 +49,7 @@ impl RestClient { max_len: Option, patch_exists: bool, format_run: FormatRun<'_>, + has_database: bool, ) -> Result { let findings = Findings::collect(files, format_run)?; Ok(findings.to_markdown(&CommentContext { @@ -60,6 +61,7 @@ impl RestClient { .unwrap_or(usize::MAX), patch_exists, format_run, + has_database, })) } } @@ -77,6 +79,8 @@ struct CommentContext<'a> { patch_exists: bool, /// How clang-format ran. format_run: FormatRun<'a>, + /// Whether a compilation database was given (see `database`). + has_database: bool, } /// The findings about one file, for a thread comment or step summary. @@ -278,13 +282,22 @@ impl Findings { let missing = self.missing_includes(); if missing > 0 { - comment.push_str(&format!( - "\nclang-tidy could not find {}, so it likely does not know how this code \ - is built, and its other findings may be wrong too. Generate a compilation \ - database (with CMake: `-DCMAKE_EXPORT_COMPILE_COMMANDS=ON`) and pass its \ - folder as the `database` option.\n", - plural(missing, "included file") - )); + let missing = plural(missing, "included file"); + comment.push_str(&if ctx.has_database { + format!( + "\nclang-tidy could not find {missing} with the given compilation \ + database, so its other findings may be wrong too. The database may be \ + out of date or not match these sources: check that it was generated \ + for this checkout and that the `database` option points to its folder.\n" + ) + } else { + format!( + "\nclang-tidy could not find {missing}, so it likely does not know how \ + this code is built, and its other findings may be wrong too. Generate a \ + compilation database (with CMake: `-DCMAKE_EXPORT_COMPILE_COMMANDS=ON`) \ + and pass its folder as the `database` option.\n" + ) + }); } // "How to fix" matters most when there are many findings, so room for it is kept diff --git a/cpp-linter/src/rest_client/feedback.rs b/cpp-linter/src/rest_client/feedback.rs index 6ec6cb42..7ff67c5a 100644 --- a/cpp-linter/src/rest_client/feedback.rs +++ b/cpp-linter/src/rest_client/feedback.rs @@ -157,6 +157,7 @@ impl RestClient { None, patch_exists, format_run, + feedback_inputs.has_database, )?; if feedback_inputs.step_summary { self.client.append_step_summary(&summary)?; @@ -250,6 +251,7 @@ impl RestClient { Some(u16::MAX as u64), patch_exists, format_run, + feedback_inputs.has_database, )?); } let options = ThreadCommentOptions { diff --git a/cpp-linter/src/rest_client/test.rs b/cpp-linter/src/rest_client/test.rs index d2324900..ce3a31d4 100644 --- a/cpp-linter/src/rest_client/test.rs +++ b/cpp-linter/src/rest_client/test.rs @@ -213,8 +213,17 @@ fn comment_names_files_from_the_repository_root() { lines_only: false, path_prefix: Path::new("member"), }; - let comment = - RestClient::make_comment(&files, 1, 1, &dummy_versions(), None, false, format_run).unwrap(); + let comment = RestClient::make_comment( + &files, + 1, + 1, + &dummy_versions(), + None, + false, + format_run, + false, + ) + .unwrap(); assert!( comment.contains("**`member/tests/demo/demo.cpp`**"), "{comment}" @@ -269,16 +278,32 @@ fn comment_names_files_from_the_repository_root() { #[test] fn missing_includes_get_a_hint() { let files = make_dummy_files("error"); - let comment = - RestClient::make_comment(&files, 0, 1, &dummy_versions(), None, false, "file".into()) - .unwrap(); + let comment = RestClient::make_comment( + &files, + 0, + 1, + &dummy_versions(), + None, + false, + "file".into(), + false, + ) + .unwrap(); assert!(!comment.contains("compilation database"), "{comment}"); files[0].lock().unwrap().tidy_advice.as_mut().unwrap().notes[0].rationale = String::from("'demo.hpp' file not found"); - let comment = - RestClient::make_comment(&files, 0, 1, &dummy_versions(), None, false, "file".into()) - .unwrap(); + let comment = RestClient::make_comment( + &files, + 0, + 1, + &dummy_versions(), + None, + false, + "file".into(), + false, + ) + .unwrap(); assert!( comment.contains("clang-tidy could not find 1 included file, so"), "{comment}" @@ -286,6 +311,27 @@ fn missing_includes_get_a_hint() { // the hint comes before the findings let hint = comment.find("Generate a compilation database").unwrap(); assert!(hint < comment.find("demo.cpp").unwrap(), "{comment}"); + + // with a database, the hint points at the database instead + let comment = RestClient::make_comment( + &files, + 0, + 1, + &dummy_versions(), + None, + false, + "file".into(), + true, + ) + .unwrap(); + assert!( + comment.contains("could not find 1 included file with the given compilation database"), + "{comment}" + ); + assert!( + !comment.contains("Generate a compilation database"), + "{comment}" + ); } #[test] @@ -297,9 +343,17 @@ fn skipped_fix_with_pipe_stays_in_its_cell() { file: "a|b.h".to_string(), reason: "it is not one of the analyzed files".to_string(), }); - let comment = - RestClient::make_comment(&files, 0, 1, &dummy_versions(), None, false, "file".into()) - .unwrap(); + let comment = RestClient::make_comment( + &files, + 0, + 1, + &dummy_versions(), + None, + false, + "file".into(), + false, + ) + .unwrap(); assert!(comment.contains("`a\\|b.h`"), "{comment}"); } @@ -314,9 +368,17 @@ fn tidy_comment_with_skipped_fixes() { reason: "it is not one of the analyzed files".to_string(), }); } - let comment = - RestClient::make_comment(&files, 0, 1, &dummy_versions(), None, true, "file".into()) - .unwrap(); + let comment = RestClient::make_comment( + &files, + 0, + 1, + &dummy_versions(), + None, + true, + "file".into(), + false, + ) + .unwrap(); assert!( comment.contains( " (fixable)
clang-diagnostic-error
\"⚠\"How to fix").unwrap() @@ -803,6 +925,7 @@ fn how_to_fix_outlasts_the_findings() { Some(max_len as u64), false, "llvm".into(), + false, ) .unwrap(); assert!(limited.len() <= max_len); @@ -820,9 +943,17 @@ fn ai_prompt_is_capped() { let note = notes[0].clone(); notes.extend(std::iter::repeat_n(note, 59)); } - let comment = - RestClient::make_comment(&files, 1, 60, &dummy_versions(), None, false, "llvm".into()) - .unwrap(); + let comment = RestClient::make_comment( + &files, + 1, + 60, + &dummy_versions(), + None, + false, + "llvm".into(), + false, + ) + .unwrap(); let prompt = &comment[comment.find("Fix these clang-tidy findings").unwrap()..]; assert_eq!(prompt.matches("A test dummy rationale").count(), 50); assert!( @@ -842,9 +973,17 @@ fn comment_stays_within_its_limit() { let note = notes[0].clone(); notes.extend(std::iter::repeat_n(note, 50)); } - let unlimited = - RestClient::make_comment(&files, 1, 51, &dummy_versions(), None, true, "llvm".into()) - .unwrap(); + let unlimited = RestClient::make_comment( + &files, + 1, + 51, + &dummy_versions(), + None, + true, + "llvm".into(), + false, + ) + .unwrap(); let head = unlimited.find("\n {max_len}", limited.len());