summaryrefslogtreecommitdiff
path: root/Source/cmcmd.cxx
diff options
context:
space:
mode:
authorDaniel Pfeifer <daniel@pfeifer-mail.de>2016-04-08 22:09:27 +0200
committerBrad King <brad.king@kitware.com>2016-04-13 09:56:10 -0400
commit5e62444cff5ead280a111c116a3fe810181379cf (patch)
tree83764d54d99f246c011defdd49e0ccfa658f8fd6 /Source/cmcmd.cxx
parent9ac11bc25d6b9f5e7db786034f922d96613e6143 (diff)
downloadcmake-5e62444cff5ead280a111c116a3fe810181379cf.tar.gz
Add options to run clang-tidy with the compiler
Create a <LANG>_CLANG_TIDY target property (initialized by a CMAKE_<LANG>_CLANG_TIDY variable) to specify a clang-tidy command line to be run along with the compiler.
Diffstat (limited to 'Source/cmcmd.cxx')
-rw-r--r--Source/cmcmd.cxx80
1 files changed, 59 insertions, 21 deletions
diff --git a/Source/cmcmd.cxx b/Source/cmcmd.cxx
index e9d77b250b..3c28c35cbc 100644
--- a/Source/cmcmd.cxx
+++ b/Source/cmcmd.cxx
@@ -297,12 +297,14 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string>& args)
if (args.size() < 3)
{
std::cerr << "__run_iwyu Usage: -E __run_iwyu [--iwyu=/path/iwyu]"
- " -- compile command\n";
+ " [--tidy=/path/tidy] -- compile command\n";
return 1;
}
bool doing_options = true;
std::vector<std::string> orig_cmd;
std::string iwyu;
+ std::string tidy;
+ std::string sourceFile;
for (std::string::size_type cc = 2; cc < args.size(); cc ++)
{
std::string const& arg = args[cc];
@@ -314,6 +316,14 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string>& args)
{
iwyu = arg.substr(7);
}
+ else if (doing_options && cmHasLiteralPrefix(arg, "--tidy="))
+ {
+ tidy = arg.substr(7);
+ }
+ else if (doing_options && cmHasLiteralPrefix(arg, "--source="))
+ {
+ sourceFile = arg.substr(9);
+ }
else if (doing_options)
{
std::cerr << "__run_iwyu given unknown argument: " << arg << "\n";
@@ -324,9 +334,14 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string>& args)
orig_cmd.push_back(arg);
}
}
- if (iwyu.empty())
+ if (tidy.empty() && iwyu.empty())
+ {
+ std::cerr << "__run_iwyu missing --tidy= or --iwyu=\n";
+ return 1;
+ }
+ if (!tidy.empty() && sourceFile.empty())
{
- std::cerr << "__run_iwyu missing --iwyu=\n";
+ std::cerr << "__run_iwyu --tidy= requires --source=\n";
return 1;
}
if (orig_cmd.empty())
@@ -335,29 +350,52 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string>& args)
return 1;
}
- // Construct the iwyu command line by taking what was given
- // and adding all the arguments we give to the compiler.
- std::vector<std::string> iwyu_cmd;
- cmSystemTools::ExpandListArgument(iwyu, iwyu_cmd, true);
- 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.
int ret = 0;
- std::string stdErr;
- if(!cmSystemTools::RunSingleCommand(iwyu_cmd, 0, &stdErr, &ret,
- 0, cmSystemTools::OUTPUT_NONE))
+
+ if (!iwyu.empty())
{
- std::cerr << "Error running '" << iwyu_cmd[0] << "': "
- << stdErr << "\n";
- return 1;
+ // Construct the iwyu command line by taking what was given
+ // and adding all the arguments we give to the compiler.
+ std::vector<std::string> iwyu_cmd;
+ cmSystemTools::ExpandListArgument(iwyu, iwyu_cmd, true);
+ 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.
+ std::string stdErr;
+ if(!cmSystemTools::RunSingleCommand(iwyu_cmd, 0, &stdErr, &ret,
+ 0, cmSystemTools::OUTPUT_NONE))
+ {
+ std::cerr << "Error running '" << iwyu_cmd[0] << "': "
+ << stdErr << "\n";
+ return 1;
+ }
+
+ // Warn if iwyu reported anything.
+ if(stdErr.find("should remove these lines:") != stdErr.npos
+ || stdErr.find("should add these lines:") != stdErr.npos)
+ {
+ std::cerr << "Warning: include-what-you-use reported diagnostics:\n"
+ << stdErr << "\n";
+ }
}
- // Warn if iwyu reported anything.
- if(stdErr.find("should remove these lines:") != stdErr.npos
- || stdErr.find("should add these lines:") != stdErr.npos)
+ if (!tidy.empty())
{
- std::cerr << "Warning: include-what-you-use reported diagnostics:\n"
- << stdErr << "\n";
+ // Construct the clang-tidy command line by taking what was given
+ // and adding all the arguments we give to the compiler.
+ std::vector<std::string> tidy_cmd;
+ cmSystemTools::ExpandListArgument(tidy, tidy_cmd, true);
+ tidy_cmd.push_back(sourceFile);
+ tidy_cmd.push_back("--");
+ tidy_cmd.insert(tidy_cmd.end(), orig_cmd.begin()+1, orig_cmd.end());
+
+ // Run the tidy command line.
+ if(!cmSystemTools::RunSingleCommand(tidy_cmd, 0, 0, &ret, 0,
+ cmSystemTools::OUTPUT_PASSTHROUGH))
+ {
+ std::cerr << "Error running '" << tidy_cmd[0] << "'\n";
+ return 1;
+ }
}
// Now run the real compiler command and return its result value.