2013-07-29 16:19:24 +08:00
|
|
|
//===--- tools/extra/clang-tidy/ClangTidyMain.cpp - Clang tidy tool -------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file This file implements a clang-tidy tool.
|
|
|
|
///
|
|
|
|
/// This tool uses the Clang Tooling infrastructure, see
|
|
|
|
/// http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html
|
|
|
|
/// for details on setting it up with LLVM source tree.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "../ClangTidy.h"
|
2013-11-14 23:49:44 +08:00
|
|
|
#include "clang/Tooling/CommonOptionsParser.h"
|
2013-07-29 16:19:24 +08:00
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
using namespace clang::driver;
|
|
|
|
using namespace clang::tooling;
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-02-05 21:43:27 +08:00
|
|
|
static cl::OptionCategory ClangTidyCategory("clang-tidy options");
|
2013-07-29 16:19:24 +08:00
|
|
|
|
2013-11-14 23:49:44 +08:00
|
|
|
static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
|
2013-07-29 16:19:24 +08:00
|
|
|
|
Change the behavior of clang-tidy -checks=, remove -disable-checks.
Summary:
Make checks filtering more intuitive and easy to use. Remove
-disable-checks and change the format of -checks= to a comma-separated list of
globs with optional '-' prefix to denote exclusion. The -checks= option is now
cumulative, so it modifies defaults, not overrides them. Each glob adds or
removes to the current set of checks, so the filter can be refined or overriden
by adding globs.
Example:
The default value for -checks= is
'*,-clang-analyzer-alpha*,-llvm-include-order,-llvm-namespace-comment,-google-*',
which allows all checks except for the ones named clang-analyzer-alpha* and
others specified with the leading '-'. To allow all google-* checks one can
write:
clang-tidy -checks=google-* ...
If one needs only google-* checks, we first need to remove everything (-*):
clang-tidy -checks=-*,google-*
etc.
I'm not sure if we need to change something here, so I didn't touch the docs
yet.
Reviewers: klimek, alexfh
Reviewed By: alexfh
Subscribers: cfe-commits
Differential Revision: http://reviews.llvm.org/D3770
llvm-svn: 208883
2014-05-15 22:27:36 +08:00
|
|
|
const char DefaultChecks[] =
|
|
|
|
"*," // Enable all checks, except these:
|
|
|
|
"-clang-analyzer-alpha*," // Too many false positives.
|
|
|
|
"-llvm-include-order," // Not implemented yet.
|
|
|
|
"-google-*,"; // Doesn't apply to LLVM.
|
2014-05-23 00:07:11 +08:00
|
|
|
|
Change the behavior of clang-tidy -checks=, remove -disable-checks.
Summary:
Make checks filtering more intuitive and easy to use. Remove
-disable-checks and change the format of -checks= to a comma-separated list of
globs with optional '-' prefix to denote exclusion. The -checks= option is now
cumulative, so it modifies defaults, not overrides them. Each glob adds or
removes to the current set of checks, so the filter can be refined or overriden
by adding globs.
Example:
The default value for -checks= is
'*,-clang-analyzer-alpha*,-llvm-include-order,-llvm-namespace-comment,-google-*',
which allows all checks except for the ones named clang-analyzer-alpha* and
others specified with the leading '-'. To allow all google-* checks one can
write:
clang-tidy -checks=google-* ...
If one needs only google-* checks, we first need to remove everything (-*):
clang-tidy -checks=-*,google-*
etc.
I'm not sure if we need to change something here, so I didn't touch the docs
yet.
Reviewers: klimek, alexfh
Reviewed By: alexfh
Subscribers: cfe-commits
Differential Revision: http://reviews.llvm.org/D3770
llvm-svn: 208883
2014-05-15 22:27:36 +08:00
|
|
|
static cl::opt<std::string>
|
2014-05-16 21:07:18 +08:00
|
|
|
Checks("checks", cl::desc("Comma-separated list of globs with optional '-'\n"
|
|
|
|
"prefix. Globs are processed in order of appearance\n"
|
|
|
|
"in the list. Globs without '-' prefix add checks\n"
|
|
|
|
"with matching names to the set, globs with the '-'\n"
|
|
|
|
"prefix remove checks with matching names from the\n"
|
|
|
|
"set of enabled checks."),
|
Change the behavior of clang-tidy -checks=, remove -disable-checks.
Summary:
Make checks filtering more intuitive and easy to use. Remove
-disable-checks and change the format of -checks= to a comma-separated list of
globs with optional '-' prefix to denote exclusion. The -checks= option is now
cumulative, so it modifies defaults, not overrides them. Each glob adds or
removes to the current set of checks, so the filter can be refined or overriden
by adding globs.
Example:
The default value for -checks= is
'*,-clang-analyzer-alpha*,-llvm-include-order,-llvm-namespace-comment,-google-*',
which allows all checks except for the ones named clang-analyzer-alpha* and
others specified with the leading '-'. To allow all google-* checks one can
write:
clang-tidy -checks=google-* ...
If one needs only google-* checks, we first need to remove everything (-*):
clang-tidy -checks=-*,google-*
etc.
I'm not sure if we need to change something here, so I didn't touch the docs
yet.
Reviewers: klimek, alexfh
Reviewed By: alexfh
Subscribers: cfe-commits
Differential Revision: http://reviews.llvm.org/D3770
llvm-svn: 208883
2014-05-15 22:27:36 +08:00
|
|
|
cl::init(""), cl::cat(ClangTidyCategory));
|
2014-05-23 00:07:11 +08:00
|
|
|
|
2014-05-16 21:07:18 +08:00
|
|
|
static cl::opt<std::string>
|
|
|
|
HeaderFilter("header-filter",
|
|
|
|
cl::desc("Regular expression matching the names of the\n"
|
2014-05-23 00:07:11 +08:00
|
|
|
"headers to output diagnostics from. Diagnostics\n"
|
|
|
|
"from the main file of each translation unit are\n"
|
|
|
|
"always displayed.\n"
|
|
|
|
"Can be used together with -line-filter."),
|
2014-05-16 21:07:18 +08:00
|
|
|
cl::init(""), cl::cat(ClangTidyCategory));
|
2014-05-23 00:07:11 +08:00
|
|
|
|
|
|
|
static cl::opt<std::string>
|
|
|
|
LineFilter("line-filter",
|
|
|
|
cl::desc("List of files with line ranges to filter the\n"
|
|
|
|
"warnings. Can be used together with\n"
|
|
|
|
"-header-filter. The format of the list is a JSON\n"
|
|
|
|
"array of objects:\n"
|
|
|
|
" [\n"
|
|
|
|
" {\"name\":\"file1.cpp\",\"lines\":[[1,3],[5,7]]},\n"
|
|
|
|
" {\"name\":\"file2.h\"}\n"
|
|
|
|
" ]"),
|
|
|
|
cl::init(""), cl::cat(ClangTidyCategory));
|
|
|
|
|
2013-07-29 16:19:24 +08:00
|
|
|
static cl::opt<bool> Fix("fix", cl::desc("Fix detected errors if possible."),
|
|
|
|
cl::init(false), cl::cat(ClangTidyCategory));
|
|
|
|
|
2014-05-16 21:07:18 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
ListChecks("list-checks",
|
|
|
|
cl::desc("List all enabled checks and exit. Use with\n"
|
|
|
|
"-checks='*' to list all available checks."),
|
|
|
|
cl::init(false), cl::cat(ClangTidyCategory));
|
2013-07-29 16:19:24 +08:00
|
|
|
|
2014-05-16 21:07:18 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
AnalyzeTemporaryDtors("analyze-temporary-dtors",
|
|
|
|
cl::desc("Enable temporary destructor-aware analysis in\n"
|
|
|
|
"clang-analyzer- checks."),
|
|
|
|
cl::init(false), cl::cat(ClangTidyCategory));
|
2014-04-30 22:09:24 +08:00
|
|
|
|
2014-09-04 18:31:23 +08:00
|
|
|
static cl::opt<std::string> ExportFixes(
|
|
|
|
"export-fixes",
|
|
|
|
cl::desc("YAML file to store suggested fixes in. The\n"
|
|
|
|
"stored fixes can be applied to the input source\n"
|
|
|
|
"code with clang-apply-replacements."),
|
|
|
|
cl::value_desc("filename"), cl::cat(ClangTidyCategory));
|
|
|
|
|
2014-05-07 17:06:53 +08:00
|
|
|
static void printStats(const clang::tidy::ClangTidyStats &Stats) {
|
2014-05-23 00:07:11 +08:00
|
|
|
if (Stats.errorsIgnored()) {
|
|
|
|
llvm::errs() << "Suppressed " << Stats.errorsIgnored() << " warnings (";
|
2014-05-07 17:06:53 +08:00
|
|
|
StringRef Separator = "";
|
|
|
|
if (Stats.ErrorsIgnoredNonUserCode) {
|
|
|
|
llvm::errs() << Stats.ErrorsIgnoredNonUserCode << " in non-user code";
|
|
|
|
Separator = ", ";
|
|
|
|
}
|
2014-05-23 00:07:11 +08:00
|
|
|
if (Stats.ErrorsIgnoredLineFilter) {
|
|
|
|
llvm::errs() << Separator << Stats.ErrorsIgnoredLineFilter
|
|
|
|
<< " due to line filter";
|
|
|
|
Separator = ", ";
|
|
|
|
}
|
2014-05-07 17:06:53 +08:00
|
|
|
if (Stats.ErrorsIgnoredNOLINT) {
|
|
|
|
llvm::errs() << Separator << Stats.ErrorsIgnoredNOLINT << " NOLINT";
|
|
|
|
Separator = ", ";
|
|
|
|
}
|
|
|
|
if (Stats.ErrorsIgnoredCheckFilter)
|
|
|
|
llvm::errs() << Separator << Stats.ErrorsIgnoredCheckFilter
|
|
|
|
<< " with check filters";
|
|
|
|
llvm::errs() << ").\n";
|
|
|
|
if (Stats.ErrorsIgnoredNonUserCode)
|
|
|
|
llvm::errs() << "Use -header-filter='.*' to display errors from all "
|
|
|
|
"non-system headers.\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-29 16:19:24 +08:00
|
|
|
int main(int argc, const char **argv) {
|
2013-12-12 18:01:39 +08:00
|
|
|
CommonOptionsParser OptionsParser(argc, argv, ClangTidyCategory);
|
2013-07-29 16:19:24 +08:00
|
|
|
|
2014-06-05 21:31:45 +08:00
|
|
|
clang::tidy::ClangTidyGlobalOptions GlobalOptions;
|
2014-06-12 21:32:11 +08:00
|
|
|
if (std::error_code Err =
|
2014-06-05 21:31:45 +08:00
|
|
|
clang::tidy::parseLineFilter(LineFilter, GlobalOptions)) {
|
2014-05-23 00:07:11 +08:00
|
|
|
llvm::errs() << "Invalid LineFilter: " << Err.message() << "\n\nUsage:\n";
|
|
|
|
llvm::cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true);
|
|
|
|
return 1;
|
|
|
|
}
|
2014-04-29 23:20:10 +08:00
|
|
|
|
2014-06-05 21:31:45 +08:00
|
|
|
clang::tidy::ClangTidyOptions Options;
|
|
|
|
Options.Checks = DefaultChecks + Checks;
|
|
|
|
Options.HeaderFilterRegex = HeaderFilter;
|
|
|
|
Options.AnalyzeTemporaryDtors = AnalyzeTemporaryDtors;
|
|
|
|
|
2014-06-03 04:32:06 +08:00
|
|
|
std::vector<std::string> EnabledChecks = clang::tidy::getCheckNames(Options);
|
|
|
|
|
Clang-tidy: added --disable-checks, --list-checks options.
Summary:
Allow disabling checks by regex. By default, disable alpha.* checks,
that are not particularly good tested (e.g. IdempotentOperationChecker, see
http://llvm-reviews.chandlerc.com/D2427).
Fixed a bug, that would disable all analyzer checks, when using a regex more
strict, than 'clang-analyzer-', for example --checks='clang-analyzer-deadcode-'.
Added --list-checks to list all enabled checks. This is useful to test specific
values in --checks/--disable-checks.
Reviewers: djasper, klimek
Reviewed By: klimek
CC: cfe-commits, klimek
Differential Revision: http://llvm-reviews.chandlerc.com/D2444
llvm-svn: 197717
2013-12-20 03:57:05 +08:00
|
|
|
// FIXME: Allow using --list-checks without positional arguments.
|
|
|
|
if (ListChecks) {
|
|
|
|
llvm::outs() << "Enabled checks:";
|
2014-06-03 04:32:06 +08:00
|
|
|
for (auto CheckName : EnabledChecks)
|
2014-03-05 21:14:32 +08:00
|
|
|
llvm::outs() << "\n " << CheckName;
|
Clang-tidy: added --disable-checks, --list-checks options.
Summary:
Allow disabling checks by regex. By default, disable alpha.* checks,
that are not particularly good tested (e.g. IdempotentOperationChecker, see
http://llvm-reviews.chandlerc.com/D2427).
Fixed a bug, that would disable all analyzer checks, when using a regex more
strict, than 'clang-analyzer-', for example --checks='clang-analyzer-deadcode-'.
Added --list-checks to list all enabled checks. This is useful to test specific
values in --checks/--disable-checks.
Reviewers: djasper, klimek
Reviewed By: klimek
CC: cfe-commits, klimek
Differential Revision: http://llvm-reviews.chandlerc.com/D2444
llvm-svn: 197717
2013-12-20 03:57:05 +08:00
|
|
|
llvm::outs() << "\n\n";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-03 04:32:06 +08:00
|
|
|
if (EnabledChecks.empty()) {
|
|
|
|
llvm::errs() << "Error: no checks enabled.\n";
|
|
|
|
llvm::cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-06-05 21:31:45 +08:00
|
|
|
// TODO: Implement configuration file reading and a "real" options provider.
|
|
|
|
auto OptionsProvider =
|
|
|
|
new clang::tidy::DefaultOptionsProvider(GlobalOptions, Options);
|
2014-05-09 20:24:09 +08:00
|
|
|
std::vector<clang::tidy::ClangTidyError> Errors;
|
2014-06-05 21:31:45 +08:00
|
|
|
clang::tidy::ClangTidyStats Stats = clang::tidy::runClangTidy(
|
|
|
|
OptionsProvider, OptionsParser.getCompilations(),
|
|
|
|
OptionsParser.getSourcePathList(), &Errors);
|
2013-07-29 16:19:24 +08:00
|
|
|
clang::tidy::handleErrors(Errors, Fix);
|
|
|
|
|
2014-09-04 18:31:23 +08:00
|
|
|
if (!ExportFixes.empty()) {
|
|
|
|
std::error_code EC;
|
|
|
|
llvm::raw_fd_ostream OS(ExportFixes, EC, llvm::sys::fs::F_None);
|
|
|
|
if (EC) {
|
|
|
|
llvm::errs() << "Error opening output file: " << EC.message() << '\n';
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
clang::tidy::exportReplacements(Errors, OS);
|
|
|
|
}
|
|
|
|
|
2014-05-07 17:06:53 +08:00
|
|
|
printStats(Stats);
|
2013-07-29 16:19:24 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2013-08-04 23:56:30 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
|
|
|
|
|
|
|
// This anchor is used to force the linker to link the LLVMModule.
|
|
|
|
extern volatile int LLVMModuleAnchorSource;
|
|
|
|
static int LLVMModuleAnchorDestination = LLVMModuleAnchorSource;
|
|
|
|
|
|
|
|
// This anchor is used to force the linker to link the GoogleModule.
|
|
|
|
extern volatile int GoogleModuleAnchorSource;
|
|
|
|
static int GoogleModuleAnchorDestination = GoogleModuleAnchorSource;
|
|
|
|
|
2014-03-05 21:14:32 +08:00
|
|
|
// This anchor is used to force the linker to link the MiscModule.
|
|
|
|
extern volatile int MiscModuleAnchorSource;
|
|
|
|
static int MiscModuleAnchorDestination = MiscModuleAnchorSource;
|
|
|
|
|
2013-08-04 23:56:30 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|