blob: 4e815c0d174e977a19eefc88967e47e23a763e87 [file] [log] [blame]
Daniel Jasperd07c8402013-07-29 08:19:24 +00001//===--- tools/extra/clang-tidy/ClangTidyMain.cpp - Clang tidy tool -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file This file implements a clang-tidy tool.
11///
12/// This tool uses the Clang Tooling infrastructure, see
13/// http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html
14/// for details on setting it up with LLVM source tree.
15///
16//===----------------------------------------------------------------------===//
17
18#include "../ClangTidy.h"
Manuel Klimek814f9bd2013-11-14 15:49:44 +000019#include "clang/Tooling/CommonOptionsParser.h"
Daniel Jasperd07c8402013-07-29 08:19:24 +000020
21using namespace clang::ast_matchers;
22using namespace clang::driver;
23using namespace clang::tooling;
24using namespace llvm;
25
Alexander Kornienko99c9d6a2014-02-05 13:43:27 +000026static cl::OptionCategory ClangTidyCategory("clang-tidy options");
Daniel Jasperd07c8402013-07-29 08:19:24 +000027
Manuel Klimek814f9bd2013-11-14 15:49:44 +000028static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
Daniel Jasperd07c8402013-07-29 08:19:24 +000029
Alexander Kornienko23fe9592014-05-15 14:27:36 +000030const char DefaultChecks[] =
31 "*," // Enable all checks, except these:
32 "-clang-analyzer-alpha*," // Too many false positives.
33 "-llvm-include-order," // Not implemented yet.
34 "-llvm-namespace-comment," // Not complete.
35 "-google-*,"; // Doesn't apply to LLVM.
36static cl::opt<std::string>
37Checks("checks",
38 cl::desc("Comma-separated list of positive and negative globs matching\n"
39 "the names of the checks to be run."),
40 cl::init(""), cl::cat(ClangTidyCategory));
Alexander Kornienko9ff5b6f2014-05-05 14:54:47 +000041static cl::opt<std::string> HeaderFilter(
42 "header-filter",
43 cl::desc("Regular expression matching the names of the headers to output\n"
44 "diagnostics from. Diagnostics from the main file of each\n"
45 "translation unit are always displayed."),
46 cl::init(""), cl::cat(ClangTidyCategory));
Daniel Jasperd07c8402013-07-29 08:19:24 +000047static cl::opt<bool> Fix("fix", cl::desc("Fix detected errors if possible."),
48 cl::init(false), cl::cat(ClangTidyCategory));
49
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +000050static cl::opt<bool> ListChecks("list-checks",
51 cl::desc("List all enabled checks and exit."),
52 cl::init(false), cl::cat(ClangTidyCategory));
Daniel Jasperd07c8402013-07-29 08:19:24 +000053
Alex McCarthyfec08c72014-04-30 14:09:24 +000054static cl::opt<bool> AnalyzeTemporaryDtors(
55 "analyze-temporary-dtors",
56 cl::desc("Enable temporary destructor-aware analysis in clang-analyzer- "
57 "checks."),
58 cl::init(false),
59 cl::cat(ClangTidyCategory));
60
Alexander Kornienko5d174542014-05-07 09:06:53 +000061static void printStats(const clang::tidy::ClangTidyStats &Stats) {
62 unsigned ErrorsIgnored = Stats.ErrorsIgnoredNOLINT +
63 Stats.ErrorsIgnoredCheckFilter +
64 Stats.ErrorsIgnoredNonUserCode;
65 if (ErrorsIgnored) {
66 llvm::errs() << "Suppressed " << ErrorsIgnored << " warnings (";
67 StringRef Separator = "";
68 if (Stats.ErrorsIgnoredNonUserCode) {
69 llvm::errs() << Stats.ErrorsIgnoredNonUserCode << " in non-user code";
70 Separator = ", ";
71 }
72 if (Stats.ErrorsIgnoredNOLINT) {
73 llvm::errs() << Separator << Stats.ErrorsIgnoredNOLINT << " NOLINT";
74 Separator = ", ";
75 }
76 if (Stats.ErrorsIgnoredCheckFilter)
77 llvm::errs() << Separator << Stats.ErrorsIgnoredCheckFilter
78 << " with check filters";
79 llvm::errs() << ").\n";
80 if (Stats.ErrorsIgnoredNonUserCode)
81 llvm::errs() << "Use -header-filter='.*' to display errors from all "
82 "non-system headers.\n";
83 }
84}
85
Daniel Jasperd07c8402013-07-29 08:19:24 +000086int main(int argc, const char **argv) {
Alexander Kornienko06ff5a72013-12-12 10:01:39 +000087 CommonOptionsParser OptionsParser(argc, argv, ClangTidyCategory);
Daniel Jasperd07c8402013-07-29 08:19:24 +000088
Alexander Kornienko33a9bcc2014-04-29 15:20:10 +000089 clang::tidy::ClangTidyOptions Options;
Alexander Kornienko23fe9592014-05-15 14:27:36 +000090 Options.Checks = DefaultChecks + Checks;
Alexander Kornienko9ff5b6f2014-05-05 14:54:47 +000091 Options.HeaderFilterRegex = HeaderFilter;
Alex McCarthyfec08c72014-04-30 14:09:24 +000092 Options.AnalyzeTemporaryDtors = AnalyzeTemporaryDtors;
Alexander Kornienko33a9bcc2014-04-29 15:20:10 +000093
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +000094 // FIXME: Allow using --list-checks without positional arguments.
95 if (ListChecks) {
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +000096 llvm::outs() << "Enabled checks:";
Alexander Kornienko33a9bcc2014-04-29 15:20:10 +000097 for (auto CheckName : clang::tidy::getCheckNames(Options))
Alexander Kornienko16ac6ce2014-03-05 13:14:32 +000098 llvm::outs() << "\n " << CheckName;
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +000099 llvm::outs() << "\n\n";
100 return 0;
101 }
102
Alexander Kornienko826b5ad2014-05-09 12:24:09 +0000103 std::vector<clang::tidy::ClangTidyError> Errors;
Alexander Kornienko5d174542014-05-07 09:06:53 +0000104 clang::tidy::ClangTidyStats Stats =
105 clang::tidy::runClangTidy(Options, OptionsParser.getCompilations(),
106 OptionsParser.getSourcePathList(), &Errors);
Daniel Jasperd07c8402013-07-29 08:19:24 +0000107 clang::tidy::handleErrors(Errors, Fix);
108
Alexander Kornienko5d174542014-05-07 09:06:53 +0000109 printStats(Stats);
Daniel Jasperd07c8402013-07-29 08:19:24 +0000110 return 0;
111}
Daniel Jasper89bbab02013-08-04 15:56:30 +0000112
113namespace clang {
114namespace tidy {
115
116// This anchor is used to force the linker to link the LLVMModule.
117extern volatile int LLVMModuleAnchorSource;
118static int LLVMModuleAnchorDestination = LLVMModuleAnchorSource;
119
120// This anchor is used to force the linker to link the GoogleModule.
121extern volatile int GoogleModuleAnchorSource;
122static int GoogleModuleAnchorDestination = GoogleModuleAnchorSource;
123
Alexander Kornienko16ac6ce2014-03-05 13:14:32 +0000124// This anchor is used to force the linker to link the MiscModule.
125extern volatile int MiscModuleAnchorSource;
126static int MiscModuleAnchorDestination = MiscModuleAnchorSource;
127
Daniel Jasper89bbab02013-08-04 15:56:30 +0000128} // namespace tidy
129} // namespace clang