blob: d49994d2f4179908589addda50f6253dcb9a39ed [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.
Alexander Kornienko23fe9592014-05-15 14:27:36 +000034 "-google-*,"; // Doesn't apply to LLVM.
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000035
Alexander Kornienko23fe9592014-05-15 14:27:36 +000036static cl::opt<std::string>
Alexander Kornienko3ab34672014-05-16 13:07:18 +000037Checks("checks", cl::desc("Comma-separated list of globs with optional '-'\n"
38 "prefix. Globs are processed in order of appearance\n"
39 "in the list. Globs without '-' prefix add checks\n"
40 "with matching names to the set, globs with the '-'\n"
41 "prefix remove checks with matching names from the\n"
42 "set of enabled checks."),
Alexander Kornienko23fe9592014-05-15 14:27:36 +000043 cl::init(""), cl::cat(ClangTidyCategory));
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000044
Alexander Kornienko3ab34672014-05-16 13:07:18 +000045static cl::opt<std::string>
46HeaderFilter("header-filter",
47 cl::desc("Regular expression matching the names of the\n"
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000048 "headers to output diagnostics from. Diagnostics\n"
49 "from the main file of each translation unit are\n"
50 "always displayed.\n"
51 "Can be used together with -line-filter."),
Alexander Kornienko3ab34672014-05-16 13:07:18 +000052 cl::init(""), cl::cat(ClangTidyCategory));
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000053
54static cl::opt<std::string>
55LineFilter("line-filter",
56 cl::desc("List of files with line ranges to filter the\n"
57 "warnings. Can be used together with\n"
58 "-header-filter. The format of the list is a JSON\n"
59 "array of objects:\n"
60 " [\n"
61 " {\"name\":\"file1.cpp\",\"lines\":[[1,3],[5,7]]},\n"
62 " {\"name\":\"file2.h\"}\n"
63 " ]"),
64 cl::init(""), cl::cat(ClangTidyCategory));
65
Daniel Jasperd07c8402013-07-29 08:19:24 +000066static cl::opt<bool> Fix("fix", cl::desc("Fix detected errors if possible."),
67 cl::init(false), cl::cat(ClangTidyCategory));
68
Alexander Kornienko3ab34672014-05-16 13:07:18 +000069static cl::opt<bool>
70ListChecks("list-checks",
71 cl::desc("List all enabled checks and exit. Use with\n"
72 "-checks='*' to list all available checks."),
73 cl::init(false), cl::cat(ClangTidyCategory));
Daniel Jasperd07c8402013-07-29 08:19:24 +000074
Alexander Kornienko3ab34672014-05-16 13:07:18 +000075static cl::opt<bool>
76AnalyzeTemporaryDtors("analyze-temporary-dtors",
77 cl::desc("Enable temporary destructor-aware analysis in\n"
78 "clang-analyzer- checks."),
79 cl::init(false), cl::cat(ClangTidyCategory));
Alex McCarthyfec08c72014-04-30 14:09:24 +000080
Benjamin Kramerfb98b742014-09-04 10:31:23 +000081static cl::opt<std::string> ExportFixes(
82 "export-fixes",
83 cl::desc("YAML file to store suggested fixes in. The\n"
84 "stored fixes can be applied to the input source\n"
85 "code with clang-apply-replacements."),
86 cl::value_desc("filename"), cl::cat(ClangTidyCategory));
87
Alexander Kornienko5d174542014-05-07 09:06:53 +000088static void printStats(const clang::tidy::ClangTidyStats &Stats) {
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000089 if (Stats.errorsIgnored()) {
90 llvm::errs() << "Suppressed " << Stats.errorsIgnored() << " warnings (";
Alexander Kornienko5d174542014-05-07 09:06:53 +000091 StringRef Separator = "";
92 if (Stats.ErrorsIgnoredNonUserCode) {
93 llvm::errs() << Stats.ErrorsIgnoredNonUserCode << " in non-user code";
94 Separator = ", ";
95 }
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000096 if (Stats.ErrorsIgnoredLineFilter) {
97 llvm::errs() << Separator << Stats.ErrorsIgnoredLineFilter
98 << " due to line filter";
99 Separator = ", ";
100 }
Alexander Kornienko5d174542014-05-07 09:06:53 +0000101 if (Stats.ErrorsIgnoredNOLINT) {
102 llvm::errs() << Separator << Stats.ErrorsIgnoredNOLINT << " NOLINT";
103 Separator = ", ";
104 }
105 if (Stats.ErrorsIgnoredCheckFilter)
106 llvm::errs() << Separator << Stats.ErrorsIgnoredCheckFilter
107 << " with check filters";
108 llvm::errs() << ").\n";
109 if (Stats.ErrorsIgnoredNonUserCode)
110 llvm::errs() << "Use -header-filter='.*' to display errors from all "
111 "non-system headers.\n";
112 }
113}
114
Daniel Jasperd07c8402013-07-29 08:19:24 +0000115int main(int argc, const char **argv) {
Alexander Kornienko06ff5a72013-12-12 10:01:39 +0000116 CommonOptionsParser OptionsParser(argc, argv, ClangTidyCategory);
Daniel Jasperd07c8402013-07-29 08:19:24 +0000117
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000118 clang::tidy::ClangTidyGlobalOptions GlobalOptions;
Rafael Espindola15c57842014-06-12 13:32:11 +0000119 if (std::error_code Err =
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000120 clang::tidy::parseLineFilter(LineFilter, GlobalOptions)) {
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000121 llvm::errs() << "Invalid LineFilter: " << Err.message() << "\n\nUsage:\n";
122 llvm::cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true);
123 return 1;
124 }
Alexander Kornienko33a9bcc2014-04-29 15:20:10 +0000125
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000126 clang::tidy::ClangTidyOptions Options;
127 Options.Checks = DefaultChecks + Checks;
128 Options.HeaderFilterRegex = HeaderFilter;
129 Options.AnalyzeTemporaryDtors = AnalyzeTemporaryDtors;
130
Alexander Kornienkofbf92582014-06-02 20:32:06 +0000131 std::vector<std::string> EnabledChecks = clang::tidy::getCheckNames(Options);
132
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +0000133 // FIXME: Allow using --list-checks without positional arguments.
134 if (ListChecks) {
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +0000135 llvm::outs() << "Enabled checks:";
Alexander Kornienkofbf92582014-06-02 20:32:06 +0000136 for (auto CheckName : EnabledChecks)
Alexander Kornienko16ac6ce2014-03-05 13:14:32 +0000137 llvm::outs() << "\n " << CheckName;
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +0000138 llvm::outs() << "\n\n";
139 return 0;
140 }
141
Alexander Kornienkofbf92582014-06-02 20:32:06 +0000142 if (EnabledChecks.empty()) {
143 llvm::errs() << "Error: no checks enabled.\n";
144 llvm::cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true);
145 return 1;
146 }
147
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000148 // TODO: Implement configuration file reading and a "real" options provider.
149 auto OptionsProvider =
150 new clang::tidy::DefaultOptionsProvider(GlobalOptions, Options);
Alexander Kornienko826b5ad2014-05-09 12:24:09 +0000151 std::vector<clang::tidy::ClangTidyError> Errors;
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000152 clang::tidy::ClangTidyStats Stats = clang::tidy::runClangTidy(
153 OptionsProvider, OptionsParser.getCompilations(),
154 OptionsParser.getSourcePathList(), &Errors);
Daniel Jasperd07c8402013-07-29 08:19:24 +0000155 clang::tidy::handleErrors(Errors, Fix);
156
Benjamin Kramerfb98b742014-09-04 10:31:23 +0000157 if (!ExportFixes.empty()) {
158 std::error_code EC;
159 llvm::raw_fd_ostream OS(ExportFixes, EC, llvm::sys::fs::F_None);
160 if (EC) {
161 llvm::errs() << "Error opening output file: " << EC.message() << '\n';
162 return 1;
163 }
164 clang::tidy::exportReplacements(Errors, OS);
165 }
166
Alexander Kornienko5d174542014-05-07 09:06:53 +0000167 printStats(Stats);
Daniel Jasperd07c8402013-07-29 08:19:24 +0000168 return 0;
169}
Daniel Jasper89bbab02013-08-04 15:56:30 +0000170
171namespace clang {
172namespace tidy {
173
174// This anchor is used to force the linker to link the LLVMModule.
175extern volatile int LLVMModuleAnchorSource;
176static int LLVMModuleAnchorDestination = LLVMModuleAnchorSource;
177
178// This anchor is used to force the linker to link the GoogleModule.
179extern volatile int GoogleModuleAnchorSource;
180static int GoogleModuleAnchorDestination = GoogleModuleAnchorSource;
181
Alexander Kornienko16ac6ce2014-03-05 13:14:32 +0000182// This anchor is used to force the linker to link the MiscModule.
183extern volatile int MiscModuleAnchorSource;
184static int MiscModuleAnchorDestination = MiscModuleAnchorSource;
185
Daniel Jasper89bbab02013-08-04 15:56:30 +0000186} // namespace tidy
187} // namespace clang