소스 검색

Merge topic 'capture-clang-tidy-errors'

80ebc55a cmake: Report if the <LANG>_CLANG_TIDY tool exits with non-zero
ce1abfa4 cmake: If ldd for LINK_WHAT_YOU_USE fails to run then report why
44de6157 cmake: Comment why we ignore the include-what-you-use return code
Brad King 9 년 전
부모
커밋
95cfb54579

+ 6 - 0
Help/release/dev/capture-clang-tidy-errors.rst

@@ -0,0 +1,6 @@
+capture-clang-tidy-errors
+-------------------------
+
+* If a command specified by the :prop_tgt:`<LANG>_CLANG_TIDY` target property
+  returns non-zero at build time this is now treated as an error instead of
+  silently ignored.

+ 18 - 6
Source/cmcmd.cxx

@@ -327,6 +327,7 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string>& args)
         iwyu_cmd.insert(iwyu_cmd.end(), orig_cmd.begin() + 1, orig_cmd.end());
         iwyu_cmd.insert(iwyu_cmd.end(), orig_cmd.begin() + 1, orig_cmd.end());
 
 
         // Run the iwyu command line.  Capture its stderr and hide its stdout.
         // Run the iwyu command line.  Capture its stderr and hide its stdout.
+        // Ignore its return code because the tool always returns non-zero.
         std::string stdErr;
         std::string stdErr;
         if (!cmSystemTools::RunSingleCommand(iwyu_cmd, CM_NULLPTR, &stdErr,
         if (!cmSystemTools::RunSingleCommand(iwyu_cmd, CM_NULLPTR, &stdErr,
                                              &ret, CM_NULLPTR,
                                              &ret, CM_NULLPTR,
@@ -357,14 +358,21 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string>& args)
 
 
         // Run the tidy command line.  Capture its stdout and hide its stderr.
         // Run the tidy command line.  Capture its stdout and hide its stderr.
         std::string stdOut;
         std::string stdOut;
-        if (!cmSystemTools::RunSingleCommand(tidy_cmd, &stdOut, CM_NULLPTR,
-                                             &ret, CM_NULLPTR,
+        std::string stdErr;
+        if (!cmSystemTools::RunSingleCommand(tidy_cmd, &stdOut, &stdErr, &ret,
+                                             CM_NULLPTR,
                                              cmSystemTools::OUTPUT_NONE)) {
                                              cmSystemTools::OUTPUT_NONE)) {
-          std::cerr << "Error running '" << tidy_cmd[0] << "'\n";
+          std::cerr << "Error running '" << tidy_cmd[0] << "': " << stdErr
+                    << "\n";
           return 1;
           return 1;
         }
         }
         // Output the stdout from clang-tidy to stderr
         // Output the stdout from clang-tidy to stderr
         std::cerr << stdOut;
         std::cerr << stdOut;
+        // If clang-tidy exited with an error do the same.
+        if (ret != 0) {
+          std::cerr << stdErr;
+          return ret;
+        }
       }
       }
       if (!lwyu.empty()) {
       if (!lwyu.empty()) {
         // Construct the ldd -r -u (link what you use lwyu) command line
         // Construct the ldd -r -u (link what you use lwyu) command line
@@ -377,11 +385,15 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string>& args)
 
 
         // Run the ldd -u -r command line.
         // Run the ldd -u -r command line.
         // Capture its stdout and hide its stderr.
         // Capture its stdout and hide its stderr.
+        // Ignore its return code because the tool always returns non-zero
+        // if there are any warnings, but we just want to warn.
         std::string stdOut;
         std::string stdOut;
-        if (!cmSystemTools::RunSingleCommand(lwyu_cmd, &stdOut, CM_NULLPTR,
-                                             &ret, CM_NULLPTR,
+        std::string stdErr;
+        if (!cmSystemTools::RunSingleCommand(lwyu_cmd, &stdOut, &stdErr, &ret,
+                                             CM_NULLPTR,
                                              cmSystemTools::OUTPUT_NONE)) {
                                              cmSystemTools::OUTPUT_NONE)) {
-          std::cerr << "Error running '" << lwyu_cmd[0] << "'\n";
+          std::cerr << "Error running '" << lwyu_cmd[0] << "': " << stdErr
+                    << "\n";
           return 1;
           return 1;
         }
         }
 
 

+ 1 - 0
Tests/RunCMake/ClangTidy/C-bad-Build-result.txt

@@ -0,0 +1 @@
+[^0]

+ 2 - 0
Tests/RunCMake/ClangTidy/C-bad-Build-stdout.txt

@@ -0,0 +1,2 @@
+stdout from bad command line arg '-bad'
+stderr from bad command line arg '-bad'

+ 3 - 0
Tests/RunCMake/ClangTidy/C-bad.cmake

@@ -0,0 +1,3 @@
+enable_language(C)
+set(CMAKE_C_CLANG_TIDY "${PSEUDO_TIDY}" -bad)
+add_executable(main main.c)

+ 1 - 0
Tests/RunCMake/ClangTidy/RunCMakeTest.cmake

@@ -20,3 +20,4 @@ if (NOT RunCMake_GENERATOR STREQUAL "Watcom WMake")
   run_tidy(C-launch)
   run_tidy(C-launch)
   run_tidy(CXX-launch)
   run_tidy(CXX-launch)
 endif()
 endif()
+run_tidy(C-bad)

+ 2 - 1
Tests/RunCMake/pseudo_iwyu.c

@@ -3,5 +3,6 @@
 int main(void)
 int main(void)
 {
 {
   fprintf(stderr, "should add these lines:\n#include <...>\n");
   fprintf(stderr, "should add these lines:\n#include <...>\n");
-  return 0;
+  /* include-what-you-use always returns failure */
+  return 1;
 }
 }

+ 6 - 0
Tests/RunCMake/pseudo_tidy.c

@@ -1,9 +1,15 @@
 #include <stdio.h>
 #include <stdio.h>
+#include <string.h>
 
 
 int main(int argc, char* argv[])
 int main(int argc, char* argv[])
 {
 {
   int i;
   int i;
   for (i = 1; i < argc; ++i) {
   for (i = 1; i < argc; ++i) {
+    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");
+      return 1;
+    }
     if (argv[i][0] != '-') {
     if (argv[i][0] != '-') {
       fprintf(stdout, "%s:0:0: warning: message [checker]\n", argv[i]);
       fprintf(stdout, "%s:0:0: warning: message [checker]\n", argv[i]);
       break;
       break;