summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrad King <brad.king@kitware.com>2022-10-07 15:19:55 (GMT)
committerKitware Robot <kwrobot@kitware.com>2022-10-07 15:20:03 (GMT)
commita07be2ffd18a03cc30153f8bafbe47f8e640eb44 (patch)
tree741906773394f23827506ef5ed656651758c57ce
parent5f9994a6c1abe28522c1fd21783f4d3d60a8f3fb (diff)
parent3f4e0839c48480b0cb3396ddbcadef25e69854ee (diff)
downloadCMake-a07be2ffd18a03cc30153f8bafbe47f8e640eb44.zip
CMake-a07be2ffd18a03cc30153f8bafbe47f8e640eb44.tar.gz
CMake-a07be2ffd18a03cc30153f8bafbe47f8e640eb44.tar.bz2
Merge topic 'clang-tidy-prefer-p-option'
3f4e0839c4 clang-tidy: Don't append compiler commands if using -p Acked-by: Kitware Robot <kwrobot@kitware.com> Merge-request: !7753
-rw-r--r--Help/prop_tgt/LANG_CLANG_TIDY.rst18
-rw-r--r--Help/release/dev/clang-tidy-prefer-p-option.rst5
-rw-r--r--Source/cmcmd.cxx26
-rw-r--r--Tests/RunCMake/ClangTidy/RunCMakeTest.cmake1
-rw-r--r--Tests/RunCMake/ClangTidy/compdb.cmake7
-rw-r--r--Tests/RunCMake/pseudo_tidy.c10
6 files changed, 57 insertions, 10 deletions
diff --git a/Help/prop_tgt/LANG_CLANG_TIDY.rst b/Help/prop_tgt/LANG_CLANG_TIDY.rst
index af16d3c..ffa0b9a 100644
--- a/Help/prop_tgt/LANG_CLANG_TIDY.rst
+++ b/Help/prop_tgt/LANG_CLANG_TIDY.rst
@@ -3,13 +3,25 @@
.. versionadded:: 3.6
-This property is implemented only when ``<LANG>`` is ``C``, ``CXX``, ``OBJC`` or ``OBJCXX``.
+This property is implemented only when ``<LANG>`` is ``C``, ``CXX``, ``OBJC``
+or ``OBJCXX``.
-Specify a :ref:`semicolon-separated list <CMake Language Lists>` containing a command
-line for the ``clang-tidy`` tool. The :ref:`Makefile Generators`
+Specify a :ref:`semicolon-separated list <CMake Language Lists>` containing
+a command line for the ``clang-tidy`` tool. The :ref:`Makefile Generators`
and the :generator:`Ninja` generator will run this tool along with the
compiler and report a warning if the tool reports any problems.
+The specified ``clang-tidy`` command line will be invoked with additional
+arguments specifying the source file and, after ``--``, the full compiler
+command line.
+
+.. versionchanged:: 3.25
+
+ If the specified ``clang-tidy`` command line includes the ``-p`` option,
+ it will invoked without ``--`` and the full compiler command line.
+ ``clang-tidy`` will look up the source file in the specified compiler
+ commands database.
+
This property is initialized by the value of
the :variable:`CMAKE_<LANG>_CLANG_TIDY` variable if it is set
when a target is created.
diff --git a/Help/release/dev/clang-tidy-prefer-p-option.rst b/Help/release/dev/clang-tidy-prefer-p-option.rst
new file mode 100644
index 0000000..816c7dd
--- /dev/null
+++ b/Help/release/dev/clang-tidy-prefer-p-option.rst
@@ -0,0 +1,5 @@
+clang-tidy-prefer-p-option
+--------------------------
+
+* If :prop_tgt:`<LANG>_CLANG_TIDY` includes a ``-p`` argument, the
+ full compiler command line is no longer appended after ``--``.
diff --git a/Source/cmcmd.cxx b/Source/cmcmd.cxx
index 16944e6..67394f9 100644
--- a/Source/cmcmd.cxx
+++ b/Source/cmcmd.cxx
@@ -360,17 +360,29 @@ int HandleIWYU(const std::string& runCmd, const std::string& /* sourceFile */,
int HandleTidy(const std::string& runCmd, const std::string& sourceFile,
const std::vector<std::string>& orig_cmd)
{
- // Construct the clang-tidy command line by taking what was given
- // and adding our compiler command line. The clang-tidy tool will
- // automatically skip over the compiler itself and extract the
- // options.
- int ret;
std::vector<std::string> tidy_cmd = cmExpandedList(runCmd, true);
tidy_cmd.push_back(sourceFile);
- tidy_cmd.emplace_back("--");
- cm::append(tidy_cmd, orig_cmd);
+
+ // clang-tidy supports working out the compile commands from a
+ // compile_commands.json file in a directory given by a "-p" option, or by
+ // passing the compiler command line arguments after --. When the latter
+ // strategy is used and the build is using a compiler other than the system
+ // default, clang-tidy may erroneously use the system default compiler's
+ // headers instead of those from the custom compiler. It doesn't do that if
+ // given a compile_commands.json to work with instead, so prefer to use the
+ // compile_commands.json file when "-p" is present.
+ if (!cm::contains(tidy_cmd.cbegin(), tidy_cmd.cend() - 1, "-p")) {
+ // Construct the clang-tidy command line by taking what was given
+ // and adding our compiler command line. The clang-tidy tool will
+ // automatically skip over the compiler itself and extract the
+ // options. If the compiler is a custom compiler, clang-tidy might
+ // not correctly handle that with this approach.
+ tidy_cmd.emplace_back("--");
+ cm::append(tidy_cmd, orig_cmd);
+ }
// Run the tidy command line. Capture its stdout and hide its stderr.
+ int ret;
std::string stdOut;
std::string stdErr;
if (!cmSystemTools::RunSingleCommand(tidy_cmd, &stdOut, &stdErr, &ret,
diff --git a/Tests/RunCMake/ClangTidy/RunCMakeTest.cmake b/Tests/RunCMake/ClangTidy/RunCMakeTest.cmake
index ee41d94..5e3fbc4 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 0000000..f83e0ae
--- /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 2feeb0f..a43133b 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");