blob: f11cbeec6281151072787de3beaad3e7181821d4 [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);
Alexander Kornienkod53d2682014-09-04 14:23:36 +000029static cl::extrahelp ClangTidyHelp(
30 "Configuration files:\n"
31 " clang-tidy attempts to read configuration for each source file from a\n"
32 " .clang-tidy file located in the closest parent directory of the source\n"
33 " file. If any configuration options have a corresponding command-line\n"
34 " option, command-line option takes precedence. The effective\n"
35 " configuration can be inspected using -dump-config.\n\n");
Daniel Jasperd07c8402013-07-29 08:19:24 +000036
Alexander Kornienko23fe9592014-05-15 14:27:36 +000037const char DefaultChecks[] =
38 "*," // Enable all checks, except these:
39 "-clang-analyzer-alpha*," // Too many false positives.
40 "-llvm-include-order," // Not implemented yet.
Alexander Kornienko23fe9592014-05-15 14:27:36 +000041 "-google-*,"; // Doesn't apply to LLVM.
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000042
Alexander Kornienko23fe9592014-05-15 14:27:36 +000043static cl::opt<std::string>
Alexander Kornienko3ab34672014-05-16 13:07:18 +000044Checks("checks", cl::desc("Comma-separated list of globs with optional '-'\n"
45 "prefix. Globs are processed in order of appearance\n"
46 "in the list. Globs without '-' prefix add checks\n"
47 "with matching names to the set, globs with the '-'\n"
48 "prefix remove checks with matching names from the\n"
Alexander Kornienkod53d2682014-09-04 14:23:36 +000049 "set of enabled checks.\n"
50 "This option's value is appended to the value read\n"
51 "from a .clang-tidy file, if any."),
Alexander Kornienko23fe9592014-05-15 14:27:36 +000052 cl::init(""), cl::cat(ClangTidyCategory));
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000053
Alexander Kornienko3ab34672014-05-16 13:07:18 +000054static cl::opt<std::string>
55HeaderFilter("header-filter",
56 cl::desc("Regular expression matching the names of the\n"
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000057 "headers to output diagnostics from. Diagnostics\n"
58 "from the main file of each translation unit are\n"
59 "always displayed.\n"
Alexander Kornienkod53d2682014-09-04 14:23:36 +000060 "Can be used together with -line-filter.\n"
61 "This option overrides the value read from a\n"
62 ".clang-tidy file."),
Alexander Kornienko3ab34672014-05-16 13:07:18 +000063 cl::init(""), cl::cat(ClangTidyCategory));
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000064
65static cl::opt<std::string>
66LineFilter("line-filter",
67 cl::desc("List of files with line ranges to filter the\n"
68 "warnings. Can be used together with\n"
69 "-header-filter. The format of the list is a JSON\n"
70 "array of objects:\n"
71 " [\n"
72 " {\"name\":\"file1.cpp\",\"lines\":[[1,3],[5,7]]},\n"
73 " {\"name\":\"file2.h\"}\n"
74 " ]"),
75 cl::init(""), cl::cat(ClangTidyCategory));
76
Daniel Jasperd07c8402013-07-29 08:19:24 +000077static cl::opt<bool> Fix("fix", cl::desc("Fix detected errors if possible."),
78 cl::init(false), cl::cat(ClangTidyCategory));
79
Alexander Kornienko3ab34672014-05-16 13:07:18 +000080static cl::opt<bool>
81ListChecks("list-checks",
82 cl::desc("List all enabled checks and exit. Use with\n"
83 "-checks='*' to list all available checks."),
84 cl::init(false), cl::cat(ClangTidyCategory));
Daniel Jasperd07c8402013-07-29 08:19:24 +000085
Alexander Kornienko3ab34672014-05-16 13:07:18 +000086static cl::opt<bool>
Alexander Kornienkod53d2682014-09-04 14:23:36 +000087DumpConfig("dump-config",
88 cl::desc("Dumps configuration in the YAML format to stdout."),
89 cl::init(false), cl::cat(ClangTidyCategory));
90
91static cl::opt<bool> AnalyzeTemporaryDtors(
92 "analyze-temporary-dtors",
93 cl::desc("Enable temporary destructor-aware analysis in\n"
94 "clang-analyzer- checks.\n"
95 "This option overrides the value read from a\n"
96 ".clang-tidy file."),
97 cl::init(false), cl::cat(ClangTidyCategory));
Alex McCarthyfec08c72014-04-30 14:09:24 +000098
Benjamin Kramerfb98b742014-09-04 10:31:23 +000099static cl::opt<std::string> ExportFixes(
100 "export-fixes",
101 cl::desc("YAML file to store suggested fixes in. The\n"
102 "stored fixes can be applied to the input source\n"
103 "code with clang-apply-replacements."),
104 cl::value_desc("filename"), cl::cat(ClangTidyCategory));
105
Alexander Kornienkoc28c32d2014-09-10 11:43:09 +0000106namespace clang {
107namespace tidy {
108
109static void printStats(const ClangTidyStats &Stats) {
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000110 if (Stats.errorsIgnored()) {
111 llvm::errs() << "Suppressed " << Stats.errorsIgnored() << " warnings (";
Alexander Kornienko5d174542014-05-07 09:06:53 +0000112 StringRef Separator = "";
113 if (Stats.ErrorsIgnoredNonUserCode) {
114 llvm::errs() << Stats.ErrorsIgnoredNonUserCode << " in non-user code";
115 Separator = ", ";
116 }
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000117 if (Stats.ErrorsIgnoredLineFilter) {
118 llvm::errs() << Separator << Stats.ErrorsIgnoredLineFilter
119 << " due to line filter";
120 Separator = ", ";
121 }
Alexander Kornienko5d174542014-05-07 09:06:53 +0000122 if (Stats.ErrorsIgnoredNOLINT) {
123 llvm::errs() << Separator << Stats.ErrorsIgnoredNOLINT << " NOLINT";
124 Separator = ", ";
125 }
126 if (Stats.ErrorsIgnoredCheckFilter)
127 llvm::errs() << Separator << Stats.ErrorsIgnoredCheckFilter
128 << " with check filters";
129 llvm::errs() << ").\n";
130 if (Stats.ErrorsIgnoredNonUserCode)
131 llvm::errs() << "Use -header-filter='.*' to display errors from all "
132 "non-system headers.\n";
133 }
134}
135
Alexander Kornienkoc28c32d2014-09-10 11:43:09 +0000136int clangTidyMain(int argc, const char **argv) {
Alexander Kornienko06ff5a72013-12-12 10:01:39 +0000137 CommonOptionsParser OptionsParser(argc, argv, ClangTidyCategory);
Daniel Jasperd07c8402013-07-29 08:19:24 +0000138
Alexander Kornienkoc28c32d2014-09-10 11:43:09 +0000139 ClangTidyGlobalOptions GlobalOptions;
140 if (std::error_code Err = parseLineFilter(LineFilter, GlobalOptions)) {
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000141 llvm::errs() << "Invalid LineFilter: " << Err.message() << "\n\nUsage:\n";
142 llvm::cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true);
143 return 1;
144 }
Alexander Kornienko33a9bcc2014-04-29 15:20:10 +0000145
Alexander Kornienkoc28c32d2014-09-10 11:43:09 +0000146 ClangTidyOptions FallbackOptions;
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000147 FallbackOptions.Checks = DefaultChecks;
148 FallbackOptions.HeaderFilterRegex = HeaderFilter;
149 FallbackOptions.AnalyzeTemporaryDtors = AnalyzeTemporaryDtors;
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000150
Alexander Kornienkoc28c32d2014-09-10 11:43:09 +0000151 ClangTidyOptions OverrideOptions;
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000152 if (Checks.getNumOccurrences() > 0)
153 OverrideOptions.Checks = Checks;
154 if (HeaderFilter.getNumOccurrences() > 0)
155 OverrideOptions.HeaderFilterRegex = HeaderFilter;
156 if (AnalyzeTemporaryDtors.getNumOccurrences() > 0)
157 OverrideOptions.AnalyzeTemporaryDtors = AnalyzeTemporaryDtors;
158
Alexander Kornienkoc28c32d2014-09-10 11:43:09 +0000159 auto OptionsProvider = llvm::make_unique<FileOptionsProvider>(
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000160 GlobalOptions, FallbackOptions, OverrideOptions);
161
162 std::string FileName = OptionsParser.getSourcePathList().front();
Alexander Kornienkoc28c32d2014-09-10 11:43:09 +0000163 ClangTidyOptions EffectiveOptions = OptionsProvider->getOptions(FileName);
164 std::vector<std::string> EnabledChecks = getCheckNames(EffectiveOptions);
Alexander Kornienkofbf92582014-06-02 20:32:06 +0000165
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +0000166 // FIXME: Allow using --list-checks without positional arguments.
167 if (ListChecks) {
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +0000168 llvm::outs() << "Enabled checks:";
Alexander Kornienkofbf92582014-06-02 20:32:06 +0000169 for (auto CheckName : EnabledChecks)
Alexander Kornienko16ac6ce2014-03-05 13:14:32 +0000170 llvm::outs() << "\n " << CheckName;
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +0000171 llvm::outs() << "\n\n";
172 return 0;
173 }
174
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000175 if (DumpConfig) {
Alexander Kornienko6e0cbc82014-09-12 08:53:36 +0000176 EffectiveOptions.CheckOptions = getCheckOptions(EffectiveOptions);
Alexander Kornienkoc28c32d2014-09-10 11:43:09 +0000177 llvm::outs() << configurationAsText(ClangTidyOptions::getDefaults()
178 .mergeWith(EffectiveOptions))
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000179 << "\n";
180 return 0;
181 }
182
Alexander Kornienkofbf92582014-06-02 20:32:06 +0000183 if (EnabledChecks.empty()) {
184 llvm::errs() << "Error: no checks enabled.\n";
185 llvm::cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true);
186 return 1;
187 }
188
Alexander Kornienkoc28c32d2014-09-10 11:43:09 +0000189 std::vector<ClangTidyError> Errors;
190 ClangTidyStats Stats =
191 runClangTidy(std::move(OptionsProvider), OptionsParser.getCompilations(),
192 OptionsParser.getSourcePathList(), &Errors);
193 handleErrors(Errors, Fix);
Daniel Jasperd07c8402013-07-29 08:19:24 +0000194
Alexander Kornienko4153da22014-09-04 15:19:49 +0000195 if (!ExportFixes.empty() && !Errors.empty()) {
Benjamin Kramerfb98b742014-09-04 10:31:23 +0000196 std::error_code EC;
197 llvm::raw_fd_ostream OS(ExportFixes, EC, llvm::sys::fs::F_None);
198 if (EC) {
199 llvm::errs() << "Error opening output file: " << EC.message() << '\n';
200 return 1;
201 }
Alexander Kornienkoc28c32d2014-09-10 11:43:09 +0000202 exportReplacements(Errors, OS);
Benjamin Kramerfb98b742014-09-04 10:31:23 +0000203 }
204
Alexander Kornienko5d174542014-05-07 09:06:53 +0000205 printStats(Stats);
Daniel Jasperd07c8402013-07-29 08:19:24 +0000206 return 0;
207}
Daniel Jasper89bbab02013-08-04 15:56:30 +0000208
Daniel Jasper89bbab02013-08-04 15:56:30 +0000209// This anchor is used to force the linker to link the LLVMModule.
210extern volatile int LLVMModuleAnchorSource;
211static int LLVMModuleAnchorDestination = LLVMModuleAnchorSource;
212
213// This anchor is used to force the linker to link the GoogleModule.
214extern volatile int GoogleModuleAnchorSource;
215static int GoogleModuleAnchorDestination = GoogleModuleAnchorSource;
216
Alexander Kornienko16ac6ce2014-03-05 13:14:32 +0000217// This anchor is used to force the linker to link the MiscModule.
218extern volatile int MiscModuleAnchorSource;
219static int MiscModuleAnchorDestination = MiscModuleAnchorSource;
220
Daniel Jasper89bbab02013-08-04 15:56:30 +0000221} // namespace tidy
222} // namespace clang
Alexander Kornienkoc28c32d2014-09-10 11:43:09 +0000223
224int main(int argc, const char **argv) {
225 return clang::tidy::clangTidyMain(argc, argv);
226}