From 3f4e0839c48480b0cb3396ddbcadef25e69854ee Mon Sep 17 00:00:00 2001 From: Craig Scott Date: Thu, 6 Oct 2022 16:14:04 +1100 Subject: clang-tidy: Don't append compiler commands if using -p When the -p option is given to clang-tidy, it doesn't need the compile command line to be appended. It can get everything it needs from the compile_commands.json file in the directory specified with the -p option. When the compiler being used is not the system default compiler, clang-tidy has been observed to pick up the wrong headers when the compiler command line is given, but not if only the -p option is used. Therefore, don't append the compiler command line if -p is present in the _CLANG_TIDY target property. Fixes: #24017 --- Tests/RunCMake/ClangTidy/RunCMakeTest.cmake | 1 + Tests/RunCMake/ClangTidy/compdb.cmake | 7 +++++++ Tests/RunCMake/pseudo_tidy.c | 10 ++++++++++ 3 files changed, 18 insertions(+) create mode 100644 Tests/RunCMake/ClangTidy/compdb.cmake (limited to 'Tests') diff --git a/Tests/RunCMake/ClangTidy/RunCMakeTest.cmake b/Tests/RunCMake/ClangTidy/RunCMakeTest.cmake index ee41d94755..5e3fbc4785 100644 --- a/Tests/RunCMake/ClangTidy/RunCMakeTest.cmake +++ b/Tests/RunCMake/ClangTidy/RunCMakeTest.cmake @@ -29,3 +29,4 @@ if (NOT RunCMake_GENERATOR STREQUAL "Watcom WMake") endif() endif() run_tidy(C-bad) +run_tidy(compdb) diff --git a/Tests/RunCMake/ClangTidy/compdb.cmake b/Tests/RunCMake/ClangTidy/compdb.cmake new file mode 100644 index 0000000000..f83e0aee9e --- /dev/null +++ b/Tests/RunCMake/ClangTidy/compdb.cmake @@ -0,0 +1,7 @@ +enable_language(C) + +# Include a --checks option to confirm that we don't match options that start +# with --, only a standalone -- +set(CMAKE_C_CLANG_TIDY "${PSEUDO_TIDY}" -p ${CMAKE_BINARY_DIR} --checks=*) + +add_executable(main main.c) diff --git a/Tests/RunCMake/pseudo_tidy.c b/Tests/RunCMake/pseudo_tidy.c index 2feeb0fc9e..a43133b8da 100644 --- a/Tests/RunCMake/pseudo_tidy.c +++ b/Tests/RunCMake/pseudo_tidy.c @@ -5,6 +5,16 @@ int main(int argc, char* argv[]) { int i; for (i = 1; i < argc; ++i) { + if (strcmp(argv[i], "-p") == 0) { + // Ensure compile commands were not appended after the source file + for (++i; i < argc; ++i) { + if (strcmp(argv[i], "--") == 0) { + fprintf(stderr, "Command line arguments unexpectedly appended\n"); + return 1; + } + } + return 0; + } if (strcmp(argv[i], "-bad") == 0) { fprintf(stdout, "stdout from bad command line arg '-bad'\n"); fprintf(stderr, "stderr from bad command line arg '-bad'\n"); -- cgit v1.2.1