Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 1 | //===--- 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 Klimek | 814f9bd | 2013-11-14 15:49:44 +0000 | [diff] [blame] | 19 | #include "clang/Tooling/CommonOptionsParser.h" |
Alexander Kornienko | e995154 | 2014-09-24 18:36:03 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Process.h" |
Zachary Turner | fbdca1d | 2017-10-20 23:00:51 +0000 | [diff] [blame^] | 21 | #include "llvm/Support/TargetSelect.h" |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace clang::ast_matchers; |
| 24 | using namespace clang::driver; |
| 25 | using namespace clang::tooling; |
| 26 | using namespace llvm; |
| 27 | |
Alexander Kornienko | 99c9d6a | 2014-02-05 13:43:27 +0000 | [diff] [blame] | 28 | static cl::OptionCategory ClangTidyCategory("clang-tidy options"); |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 29 | |
Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 +0000 | [diff] [blame] | 30 | static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage); |
Alexander Kornienko | f1d54eb | 2016-02-08 00:19:29 +0000 | [diff] [blame] | 31 | static cl::extrahelp ClangTidyHelp(R"( |
| 32 | Configuration files: |
| 33 | clang-tidy attempts to read configuration for each source file from a |
| 34 | .clang-tidy file located in the closest parent directory of the source |
| 35 | file. If any configuration options have a corresponding command-line |
| 36 | option, command-line option takes precedence. The effective |
| 37 | configuration can be inspected using -dump-config: |
| 38 | |
Alexander Kornienko | 215604c | 2017-04-06 14:27:00 +0000 | [diff] [blame] | 39 | $ clang-tidy -dump-config |
Alexander Kornienko | f1d54eb | 2016-02-08 00:19:29 +0000 | [diff] [blame] | 40 | --- |
| 41 | Checks: '-*,some-check' |
| 42 | WarningsAsErrors: '' |
| 43 | HeaderFilterRegex: '' |
| 44 | AnalyzeTemporaryDtors: false |
Alexander Kornienko | 215604c | 2017-04-06 14:27:00 +0000 | [diff] [blame] | 45 | FormatStyle: none |
Alexander Kornienko | f1d54eb | 2016-02-08 00:19:29 +0000 | [diff] [blame] | 46 | User: user |
| 47 | CheckOptions: |
| 48 | - key: some-check.SomeOption |
| 49 | value: 'some value' |
| 50 | ... |
| 51 | |
| 52 | )"); |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 53 | |
Jonas Devlieghere | 7319184 | 2016-11-30 18:06:42 +0000 | [diff] [blame] | 54 | const char DefaultChecks[] = // Enable these checks by default: |
| 55 | "clang-diagnostic-*," // * compiler diagnostics |
| 56 | "clang-analyzer-*"; // * Static Analyzer checks |
Alexander Kornienko | dad4acb | 2014-05-22 16:07:11 +0000 | [diff] [blame] | 57 | |
Alexander Kornienko | f1d54eb | 2016-02-08 00:19:29 +0000 | [diff] [blame] | 58 | static cl::opt<std::string> Checks("checks", cl::desc(R"( |
| 59 | Comma-separated list of globs with optional '-' |
| 60 | prefix. Globs are processed in order of |
| 61 | appearance in the list. Globs without '-' |
| 62 | prefix add checks with matching names to the |
| 63 | set, globs with the '-' prefix remove checks |
| 64 | with matching names from the set of enabled |
Alexander Kornienko | 17a4b23 | 2017-03-03 11:16:34 +0000 | [diff] [blame] | 65 | checks. This option's value is appended to the |
Alexander Kornienko | f1d54eb | 2016-02-08 00:19:29 +0000 | [diff] [blame] | 66 | value of the 'Checks' option in .clang-tidy |
| 67 | file, if any. |
| 68 | )"), |
| 69 | cl::init(""), cl::cat(ClangTidyCategory)); |
Alexander Kornienko | dad4acb | 2014-05-22 16:07:11 +0000 | [diff] [blame] | 70 | |
Alexander Kornienko | f1d54eb | 2016-02-08 00:19:29 +0000 | [diff] [blame] | 71 | static cl::opt<std::string> WarningsAsErrors("warnings-as-errors", cl::desc(R"( |
| 72 | Upgrades warnings to errors. Same format as |
| 73 | '-checks'. |
| 74 | This option's value is appended to the value of |
| 75 | the 'WarningsAsErrors' option in .clang-tidy |
| 76 | file, if any. |
| 77 | )"), |
| 78 | cl::init(""), |
| 79 | cl::cat(ClangTidyCategory)); |
Jonathan Roelofs | d60388a | 2016-01-13 17:36:41 +0000 | [diff] [blame] | 80 | |
Alexander Kornienko | f1d54eb | 2016-02-08 00:19:29 +0000 | [diff] [blame] | 81 | static cl::opt<std::string> HeaderFilter("header-filter", cl::desc(R"( |
| 82 | Regular expression matching the names of the |
| 83 | headers to output diagnostics from. Diagnostics |
| 84 | from the main file of each translation unit are |
| 85 | always displayed. |
| 86 | Can be used together with -line-filter. |
| 87 | This option overrides the 'HeaderFilter' option |
| 88 | in .clang-tidy file, if any. |
| 89 | )"), |
| 90 | cl::init(""), |
| 91 | cl::cat(ClangTidyCategory)); |
Alexander Kornienko | dad4acb | 2014-05-22 16:07:11 +0000 | [diff] [blame] | 92 | |
Alexander Kornienko | 37f7abe | 2014-10-28 22:16:13 +0000 | [diff] [blame] | 93 | static cl::opt<bool> |
| 94 | SystemHeaders("system-headers", |
Alexander Kornienko | 5eac3c6 | 2014-11-03 14:06:31 +0000 | [diff] [blame] | 95 | cl::desc("Display the errors from system headers."), |
Alexander Kornienko | 37f7abe | 2014-10-28 22:16:13 +0000 | [diff] [blame] | 96 | cl::init(false), cl::cat(ClangTidyCategory)); |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 97 | static cl::opt<std::string> LineFilter("line-filter", cl::desc(R"( |
Alexander Kornienko | f1d54eb | 2016-02-08 00:19:29 +0000 | [diff] [blame] | 98 | List of files with line ranges to filter the |
| 99 | warnings. Can be used together with |
| 100 | -header-filter. The format of the list is a |
| 101 | JSON array of objects: |
| 102 | [ |
| 103 | {"name":"file1.cpp","lines":[[1,3],[5,7]]}, |
| 104 | {"name":"file2.h"} |
| 105 | ] |
| 106 | )"), |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 107 | cl::init(""), |
| 108 | cl::cat(ClangTidyCategory)); |
Alexander Kornienko | dad4acb | 2014-05-22 16:07:11 +0000 | [diff] [blame] | 109 | |
Alexander Kornienko | f1d54eb | 2016-02-08 00:19:29 +0000 | [diff] [blame] | 110 | static cl::opt<bool> Fix("fix", cl::desc(R"( |
| 111 | Apply suggested fixes. Without -fix-errors |
| 112 | clang-tidy will bail out if any compilation |
| 113 | errors were found. |
| 114 | )"), |
| 115 | cl::init(false), cl::cat(ClangTidyCategory)); |
Alexander Kornienko | 5eac3c6 | 2014-11-03 14:06:31 +0000 | [diff] [blame] | 116 | |
Alexander Kornienko | f1d54eb | 2016-02-08 00:19:29 +0000 | [diff] [blame] | 117 | static cl::opt<bool> FixErrors("fix-errors", cl::desc(R"( |
| 118 | Apply suggested fixes even if compilation |
| 119 | errors were found. If compiler errors have |
| 120 | attached fix-its, clang-tidy will apply them as |
| 121 | well. |
| 122 | )"), |
| 123 | cl::init(false), cl::cat(ClangTidyCategory)); |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 124 | |
Alexander Kornienko | 17a4b23 | 2017-03-03 11:16:34 +0000 | [diff] [blame] | 125 | static cl::opt<std::string> FormatStyle("format-style", cl::desc(R"( |
| 126 | Style for formatting code around applied fixes: |
| 127 | - 'none' (default) turns off formatting |
| 128 | - 'file' (literally 'file', not a placeholder) |
| 129 | uses .clang-format file in the closest parent |
| 130 | directory |
| 131 | - '{ <json> }' specifies options inline, e.g. |
| 132 | -format-style='{BasedOnStyle: llvm, IndentWidth: 8}' |
| 133 | - 'llvm', 'google', 'webkit', 'mozilla' |
| 134 | See clang-format documentation for the up-to-date |
| 135 | information about formatting styles and options. |
Alexander Kornienko | 215604c | 2017-04-06 14:27:00 +0000 | [diff] [blame] | 136 | This option overrides the 'FormatStyle` option in |
| 137 | .clang-tidy file, if any. |
Jonas Devlieghere | 7319184 | 2016-11-30 18:06:42 +0000 | [diff] [blame] | 138 | )"), |
Alexander Kornienko | 17a4b23 | 2017-03-03 11:16:34 +0000 | [diff] [blame] | 139 | cl::init("none"), |
| 140 | cl::cat(ClangTidyCategory)); |
Jonas Devlieghere | 7319184 | 2016-11-30 18:06:42 +0000 | [diff] [blame] | 141 | |
Alexander Kornienko | f1d54eb | 2016-02-08 00:19:29 +0000 | [diff] [blame] | 142 | static cl::opt<bool> ListChecks("list-checks", cl::desc(R"( |
| 143 | List all enabled checks and exit. Use with |
| 144 | -checks=* to list all available checks. |
| 145 | )"), |
| 146 | cl::init(false), cl::cat(ClangTidyCategory)); |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 147 | |
Haojian Wu | 12e6b8f | 2016-04-27 09:15:01 +0000 | [diff] [blame] | 148 | static cl::opt<bool> ExplainConfig("explain-config", cl::desc(R"( |
Haojian Wu | e5555e2 | 2016-09-22 14:36:43 +0000 | [diff] [blame] | 149 | For each enabled check explains, where it is |
| 150 | enabled, i.e. in clang-tidy binary, command |
| 151 | line or a specific configuration file. |
Haojian Wu | 12e6b8f | 2016-04-27 09:15:01 +0000 | [diff] [blame] | 152 | )"), |
| 153 | cl::init(false), cl::cat(ClangTidyCategory)); |
| 154 | |
Alexander Kornienko | f1d54eb | 2016-02-08 00:19:29 +0000 | [diff] [blame] | 155 | static cl::opt<std::string> Config("config", cl::desc(R"( |
| 156 | Specifies a configuration in YAML/JSON format: |
| 157 | -config="{Checks: '*', |
| 158 | CheckOptions: [{key: x, |
| 159 | value: y}]}" |
| 160 | When the value is empty, clang-tidy will |
| 161 | attempt to find a file named .clang-tidy for |
| 162 | each source file in its parent directories. |
| 163 | )"), |
| 164 | cl::init(""), cl::cat(ClangTidyCategory)); |
Alexander Kornienko | eff4e92 | 2014-09-26 11:42:29 +0000 | [diff] [blame] | 165 | |
Alexander Kornienko | f1d54eb | 2016-02-08 00:19:29 +0000 | [diff] [blame] | 166 | static cl::opt<bool> DumpConfig("dump-config", cl::desc(R"( |
| 167 | Dumps configuration in the YAML format to |
| 168 | stdout. This option can be used along with a |
| 169 | file name (and '--' if the file is outside of a |
| 170 | project with configured compilation database). |
| 171 | The configuration used for this file will be |
| 172 | printed. |
| 173 | Use along with -checks=* to include |
| 174 | configuration of all checks. |
| 175 | )"), |
| 176 | cl::init(false), cl::cat(ClangTidyCategory)); |
Alexander Kornienko | d53d268 | 2014-09-04 14:23:36 +0000 | [diff] [blame] | 177 | |
Alexander Kornienko | f1d54eb | 2016-02-08 00:19:29 +0000 | [diff] [blame] | 178 | static cl::opt<bool> EnableCheckProfile("enable-check-profile", cl::desc(R"( |
| 179 | Enable per-check timing profiles, and print a |
| 180 | report to stderr. |
| 181 | )"), |
| 182 | cl::init(false), |
| 183 | cl::cat(ClangTidyCategory)); |
Samuel Benzaquen | aedd994 | 2014-10-23 17:23:20 +0000 | [diff] [blame] | 184 | |
Alexander Kornienko | f1d54eb | 2016-02-08 00:19:29 +0000 | [diff] [blame] | 185 | static cl::opt<bool> AnalyzeTemporaryDtors("analyze-temporary-dtors", |
| 186 | cl::desc(R"( |
| 187 | Enable temporary destructor-aware analysis in |
| 188 | clang-analyzer- checks. |
| 189 | This option overrides the value read from a |
| 190 | .clang-tidy file. |
| 191 | )"), |
| 192 | cl::init(false), |
| 193 | cl::cat(ClangTidyCategory)); |
Alex McCarthy | fec08c7 | 2014-04-30 14:09:24 +0000 | [diff] [blame] | 194 | |
Alexander Kornienko | f1d54eb | 2016-02-08 00:19:29 +0000 | [diff] [blame] | 195 | static cl::opt<std::string> ExportFixes("export-fixes", cl::desc(R"( |
| 196 | YAML file to store suggested fixes in. The |
Kirill Bobyrev | 405699e | 2016-08-19 09:36:14 +0000 | [diff] [blame] | 197 | stored fixes can be applied to the input source |
Alexander Kornienko | f1d54eb | 2016-02-08 00:19:29 +0000 | [diff] [blame] | 198 | code with clang-apply-replacements. |
| 199 | )"), |
| 200 | cl::value_desc("filename"), |
| 201 | cl::cat(ClangTidyCategory)); |
Benjamin Kramer | fb98b74 | 2014-09-04 10:31:23 +0000 | [diff] [blame] | 202 | |
Ehsan Akhgari | b7418d3 | 2017-02-09 18:32:02 +0000 | [diff] [blame] | 203 | static cl::opt<bool> Quiet("quiet", cl::desc(R"( |
Alexander Kornienko | 17a4b23 | 2017-03-03 11:16:34 +0000 | [diff] [blame] | 204 | Run clang-tidy in quiet mode. This suppresses |
Ehsan Akhgari | b7418d3 | 2017-02-09 18:32:02 +0000 | [diff] [blame] | 205 | printing statistics about ignored warnings and |
| 206 | warnings treated as errors if the respective |
| 207 | options are specified. |
| 208 | )"), |
| 209 | cl::init(false), |
| 210 | cl::cat(ClangTidyCategory)); |
| 211 | |
Alexander Kornienko | c28c32d | 2014-09-10 11:43:09 +0000 | [diff] [blame] | 212 | namespace clang { |
| 213 | namespace tidy { |
| 214 | |
| 215 | static void printStats(const ClangTidyStats &Stats) { |
Alexander Kornienko | dad4acb | 2014-05-22 16:07:11 +0000 | [diff] [blame] | 216 | if (Stats.errorsIgnored()) { |
| 217 | llvm::errs() << "Suppressed " << Stats.errorsIgnored() << " warnings ("; |
Alexander Kornienko | 5d17454 | 2014-05-07 09:06:53 +0000 | [diff] [blame] | 218 | StringRef Separator = ""; |
| 219 | if (Stats.ErrorsIgnoredNonUserCode) { |
| 220 | llvm::errs() << Stats.ErrorsIgnoredNonUserCode << " in non-user code"; |
| 221 | Separator = ", "; |
| 222 | } |
Alexander Kornienko | dad4acb | 2014-05-22 16:07:11 +0000 | [diff] [blame] | 223 | if (Stats.ErrorsIgnoredLineFilter) { |
| 224 | llvm::errs() << Separator << Stats.ErrorsIgnoredLineFilter |
| 225 | << " due to line filter"; |
| 226 | Separator = ", "; |
| 227 | } |
Alexander Kornienko | 5d17454 | 2014-05-07 09:06:53 +0000 | [diff] [blame] | 228 | if (Stats.ErrorsIgnoredNOLINT) { |
| 229 | llvm::errs() << Separator << Stats.ErrorsIgnoredNOLINT << " NOLINT"; |
| 230 | Separator = ", "; |
| 231 | } |
| 232 | if (Stats.ErrorsIgnoredCheckFilter) |
| 233 | llvm::errs() << Separator << Stats.ErrorsIgnoredCheckFilter |
| 234 | << " with check filters"; |
| 235 | llvm::errs() << ").\n"; |
| 236 | if (Stats.ErrorsIgnoredNonUserCode) |
Aaron Ballman | 5a4892b | 2015-07-27 13:41:30 +0000 | [diff] [blame] | 237 | llvm::errs() << "Use -header-filter=.* to display errors from all " |
Alexander Kornienko | f1d54eb | 2016-02-08 00:19:29 +0000 | [diff] [blame] | 238 | "non-system headers. Use -system-headers to display " |
| 239 | "errors from system headers as well.\n"; |
Alexander Kornienko | 5d17454 | 2014-05-07 09:06:53 +0000 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
Samuel Benzaquen | aedd994 | 2014-10-23 17:23:20 +0000 | [diff] [blame] | 243 | static void printProfileData(const ProfileData &Profile, |
| 244 | llvm::raw_ostream &OS) { |
| 245 | // Time is first to allow for sorting by it. |
| 246 | std::vector<std::pair<llvm::TimeRecord, StringRef>> Timers; |
| 247 | TimeRecord Total; |
| 248 | |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 249 | for (const auto &P : Profile.Records) { |
Samuel Benzaquen | aedd994 | 2014-10-23 17:23:20 +0000 | [diff] [blame] | 250 | Timers.emplace_back(P.getValue(), P.getKey()); |
| 251 | Total += P.getValue(); |
| 252 | } |
| 253 | |
| 254 | std::sort(Timers.begin(), Timers.end()); |
| 255 | |
| 256 | std::string Line = "===" + std::string(73, '-') + "===\n"; |
| 257 | OS << Line; |
| 258 | |
| 259 | if (Total.getUserTime()) |
| 260 | OS << " ---User Time---"; |
| 261 | if (Total.getSystemTime()) |
| 262 | OS << " --System Time--"; |
| 263 | if (Total.getProcessTime()) |
| 264 | OS << " --User+System--"; |
| 265 | OS << " ---Wall Time---"; |
| 266 | if (Total.getMemUsed()) |
| 267 | OS << " ---Mem---"; |
| 268 | OS << " --- Name ---\n"; |
| 269 | |
| 270 | // Loop through all of the timing data, printing it out. |
| 271 | for (auto I = Timers.rbegin(), E = Timers.rend(); I != E; ++I) { |
| 272 | I->first.print(Total, OS); |
| 273 | OS << I->second << '\n'; |
| 274 | } |
| 275 | |
| 276 | Total.print(Total, OS); |
| 277 | OS << "Total\n"; |
| 278 | OS << Line << "\n"; |
| 279 | OS.flush(); |
| 280 | } |
| 281 | |
Benjamin Kramer | e710371 | 2015-03-23 12:49:15 +0000 | [diff] [blame] | 282 | static std::unique_ptr<ClangTidyOptionsProvider> createOptionsProvider() { |
Alexander Kornienko | c28c32d | 2014-09-10 11:43:09 +0000 | [diff] [blame] | 283 | ClangTidyGlobalOptions GlobalOptions; |
| 284 | if (std::error_code Err = parseLineFilter(LineFilter, GlobalOptions)) { |
Alexander Kornienko | dad4acb | 2014-05-22 16:07:11 +0000 | [diff] [blame] | 285 | llvm::errs() << "Invalid LineFilter: " << Err.message() << "\n\nUsage:\n"; |
| 286 | llvm::cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true); |
Alexander Kornienko | eff4e92 | 2014-09-26 11:42:29 +0000 | [diff] [blame] | 287 | return nullptr; |
Alexander Kornienko | dad4acb | 2014-05-22 16:07:11 +0000 | [diff] [blame] | 288 | } |
Alexander Kornienko | 33a9bcc | 2014-04-29 15:20:10 +0000 | [diff] [blame] | 289 | |
Alexander Kornienko | e995154 | 2014-09-24 18:36:03 +0000 | [diff] [blame] | 290 | ClangTidyOptions DefaultOptions; |
| 291 | DefaultOptions.Checks = DefaultChecks; |
Jonathan Roelofs | d60388a | 2016-01-13 17:36:41 +0000 | [diff] [blame] | 292 | DefaultOptions.WarningsAsErrors = ""; |
Alexander Kornienko | e995154 | 2014-09-24 18:36:03 +0000 | [diff] [blame] | 293 | DefaultOptions.HeaderFilterRegex = HeaderFilter; |
Alexander Kornienko | 37f7abe | 2014-10-28 22:16:13 +0000 | [diff] [blame] | 294 | DefaultOptions.SystemHeaders = SystemHeaders; |
Alexander Kornienko | e995154 | 2014-09-24 18:36:03 +0000 | [diff] [blame] | 295 | DefaultOptions.AnalyzeTemporaryDtors = AnalyzeTemporaryDtors; |
Alexander Kornienko | 2561320 | 2017-04-06 13:41:29 +0000 | [diff] [blame] | 296 | DefaultOptions.FormatStyle = FormatStyle; |
Alexander Kornienko | e995154 | 2014-09-24 18:36:03 +0000 | [diff] [blame] | 297 | DefaultOptions.User = llvm::sys::Process::GetEnv("USER"); |
| 298 | // USERNAME is used on Windows. |
| 299 | if (!DefaultOptions.User) |
| 300 | DefaultOptions.User = llvm::sys::Process::GetEnv("USERNAME"); |
Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 301 | |
Alexander Kornienko | c28c32d | 2014-09-10 11:43:09 +0000 | [diff] [blame] | 302 | ClangTidyOptions OverrideOptions; |
Alexander Kornienko | d53d268 | 2014-09-04 14:23:36 +0000 | [diff] [blame] | 303 | if (Checks.getNumOccurrences() > 0) |
| 304 | OverrideOptions.Checks = Checks; |
Jonathan Roelofs | d60388a | 2016-01-13 17:36:41 +0000 | [diff] [blame] | 305 | if (WarningsAsErrors.getNumOccurrences() > 0) |
| 306 | OverrideOptions.WarningsAsErrors = WarningsAsErrors; |
Alexander Kornienko | d53d268 | 2014-09-04 14:23:36 +0000 | [diff] [blame] | 307 | if (HeaderFilter.getNumOccurrences() > 0) |
| 308 | OverrideOptions.HeaderFilterRegex = HeaderFilter; |
Alexander Kornienko | 37f7abe | 2014-10-28 22:16:13 +0000 | [diff] [blame] | 309 | if (SystemHeaders.getNumOccurrences() > 0) |
| 310 | OverrideOptions.SystemHeaders = SystemHeaders; |
Alexander Kornienko | d53d268 | 2014-09-04 14:23:36 +0000 | [diff] [blame] | 311 | if (AnalyzeTemporaryDtors.getNumOccurrences() > 0) |
| 312 | OverrideOptions.AnalyzeTemporaryDtors = AnalyzeTemporaryDtors; |
Alexander Kornienko | 2561320 | 2017-04-06 13:41:29 +0000 | [diff] [blame] | 313 | if (FormatStyle.getNumOccurrences() > 0) |
| 314 | OverrideOptions.FormatStyle = FormatStyle; |
Alexander Kornienko | d53d268 | 2014-09-04 14:23:36 +0000 | [diff] [blame] | 315 | |
Alexander Kornienko | eff4e92 | 2014-09-26 11:42:29 +0000 | [diff] [blame] | 316 | if (!Config.empty()) { |
| 317 | if (llvm::ErrorOr<ClangTidyOptions> ParsedConfig = |
| 318 | parseConfiguration(Config)) { |
Haojian Wu | 12e6b8f | 2016-04-27 09:15:01 +0000 | [diff] [blame] | 319 | return llvm::make_unique<ConfigOptionsProvider>( |
| 320 | GlobalOptions, |
| 321 | ClangTidyOptions::getDefaults().mergeWith(DefaultOptions), |
| 322 | *ParsedConfig, OverrideOptions); |
Alexander Kornienko | eff4e92 | 2014-09-26 11:42:29 +0000 | [diff] [blame] | 323 | } else { |
| 324 | llvm::errs() << "Error: invalid configuration specified.\n" |
| 325 | << ParsedConfig.getError().message() << "\n"; |
| 326 | return nullptr; |
| 327 | } |
| 328 | } |
| 329 | return llvm::make_unique<FileOptionsProvider>(GlobalOptions, DefaultOptions, |
| 330 | OverrideOptions); |
| 331 | } |
| 332 | |
Benjamin Kramer | e710371 | 2015-03-23 12:49:15 +0000 | [diff] [blame] | 333 | static int clangTidyMain(int argc, const char **argv) { |
Alexander Kornienko | 65eccb4 | 2015-08-17 10:03:27 +0000 | [diff] [blame] | 334 | CommonOptionsParser OptionsParser(argc, argv, ClangTidyCategory, |
| 335 | cl::ZeroOrMore); |
Alexander Kornienko | eff4e92 | 2014-09-26 11:42:29 +0000 | [diff] [blame] | 336 | |
Alexander Kornienko | 2561320 | 2017-04-06 13:41:29 +0000 | [diff] [blame] | 337 | auto OwningOptionsProvider = createOptionsProvider(); |
| 338 | auto *OptionsProvider = OwningOptionsProvider.get(); |
Alexander Kornienko | eff4e92 | 2014-09-26 11:42:29 +0000 | [diff] [blame] | 339 | if (!OptionsProvider) |
| 340 | return 1; |
Alexander Kornienko | d53d268 | 2014-09-04 14:23:36 +0000 | [diff] [blame] | 341 | |
Alexander Kornienko | 65eccb4 | 2015-08-17 10:03:27 +0000 | [diff] [blame] | 342 | StringRef FileName("dummy"); |
| 343 | auto PathList = OptionsParser.getSourcePathList(); |
| 344 | if (!PathList.empty()) { |
Alexander Kornienko | e0c900e | 2015-08-17 11:27:11 +0000 | [diff] [blame] | 345 | FileName = PathList.front(); |
Alexander Kornienko | 65eccb4 | 2015-08-17 10:03:27 +0000 | [diff] [blame] | 346 | } |
Haojian Wu | d121875 | 2016-07-11 07:47:04 +0000 | [diff] [blame] | 347 | |
| 348 | SmallString<256> FilePath(FileName); |
| 349 | if (std::error_code EC = llvm::sys::fs::make_absolute(FilePath)) { |
| 350 | llvm::errs() << "Can't make absolute path from " << FileName << ": " |
| 351 | << EC.message() << "\n"; |
| 352 | } |
| 353 | ClangTidyOptions EffectiveOptions = OptionsProvider->getOptions(FilePath); |
Alexander Kornienko | c28c32d | 2014-09-10 11:43:09 +0000 | [diff] [blame] | 354 | std::vector<std::string> EnabledChecks = getCheckNames(EffectiveOptions); |
Alexander Kornienko | fbf9258 | 2014-06-02 20:32:06 +0000 | [diff] [blame] | 355 | |
Haojian Wu | 12e6b8f | 2016-04-27 09:15:01 +0000 | [diff] [blame] | 356 | if (ExplainConfig) { |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 357 | // FIXME: Show other ClangTidyOptions' fields, like ExtraArg. |
Haojian Wu | 12e6b8f | 2016-04-27 09:15:01 +0000 | [diff] [blame] | 358 | std::vector<clang::tidy::ClangTidyOptionsProvider::OptionsSource> |
Haojian Wu | d121875 | 2016-07-11 07:47:04 +0000 | [diff] [blame] | 359 | RawOptions = OptionsProvider->getRawOptions(FilePath); |
Haojian Wu | 12e6b8f | 2016-04-27 09:15:01 +0000 | [diff] [blame] | 360 | for (const std::string &Check : EnabledChecks) { |
| 361 | for (auto It = RawOptions.rbegin(); It != RawOptions.rend(); ++It) { |
| 362 | if (It->first.Checks && GlobList(*It->first.Checks).contains(Check)) { |
| 363 | llvm::outs() << "'" << Check << "' is enabled in the " << It->second |
| 364 | << ".\n"; |
| 365 | break; |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | return 0; |
| 370 | } |
| 371 | |
Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 +0000 | [diff] [blame] | 372 | if (ListChecks) { |
Alexander Kornienko | 493db09 | 2016-04-27 11:45:14 +0000 | [diff] [blame] | 373 | if (EnabledChecks.empty()) { |
| 374 | llvm::errs() << "No checks enabled.\n"; |
| 375 | return 1; |
| 376 | } |
Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 +0000 | [diff] [blame] | 377 | llvm::outs() << "Enabled checks:"; |
Benjamin Kramer | 51a9cc9 | 2016-06-15 15:46:10 +0000 | [diff] [blame] | 378 | for (const auto &CheckName : EnabledChecks) |
Alexander Kornienko | 16ac6ce | 2014-03-05 13:14:32 +0000 | [diff] [blame] | 379 | llvm::outs() << "\n " << CheckName; |
Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 +0000 | [diff] [blame] | 380 | llvm::outs() << "\n\n"; |
| 381 | return 0; |
| 382 | } |
| 383 | |
Alexander Kornienko | d53d268 | 2014-09-04 14:23:36 +0000 | [diff] [blame] | 384 | if (DumpConfig) { |
Alexander Kornienko | 6e0cbc8 | 2014-09-12 08:53:36 +0000 | [diff] [blame] | 385 | EffectiveOptions.CheckOptions = getCheckOptions(EffectiveOptions); |
Alexander Kornienko | 65eccb4 | 2015-08-17 10:03:27 +0000 | [diff] [blame] | 386 | llvm::outs() << configurationAsText( |
| 387 | ClangTidyOptions::getDefaults().mergeWith( |
| 388 | EffectiveOptions)) |
Alexander Kornienko | d53d268 | 2014-09-04 14:23:36 +0000 | [diff] [blame] | 389 | << "\n"; |
| 390 | return 0; |
| 391 | } |
| 392 | |
Alexander Kornienko | fbf9258 | 2014-06-02 20:32:06 +0000 | [diff] [blame] | 393 | if (EnabledChecks.empty()) { |
| 394 | llvm::errs() << "Error: no checks enabled.\n"; |
| 395 | llvm::cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true); |
Rafael Espindola | d63b2f3 | 2017-09-08 00:33:39 +0000 | [diff] [blame] | 396 | return 0; |
Alexander Kornienko | fbf9258 | 2014-06-02 20:32:06 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Alexander Kornienko | 65eccb4 | 2015-08-17 10:03:27 +0000 | [diff] [blame] | 399 | if (PathList.empty()) { |
| 400 | llvm::errs() << "Error: no input files specified.\n"; |
| 401 | llvm::cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true); |
Rafael Espindola | d63b2f3 | 2017-09-08 00:33:39 +0000 | [diff] [blame] | 402 | return 0; |
Alexander Kornienko | 65eccb4 | 2015-08-17 10:03:27 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Samuel Benzaquen | aedd994 | 2014-10-23 17:23:20 +0000 | [diff] [blame] | 405 | ProfileData Profile; |
| 406 | |
Zachary Turner | fbdca1d | 2017-10-20 23:00:51 +0000 | [diff] [blame^] | 407 | llvm::InitializeAllTargetInfos(); |
| 408 | llvm::InitializeAllTargetMCs(); |
| 409 | llvm::InitializeAllAsmParsers(); |
| 410 | |
Alexander Kornienko | 2561320 | 2017-04-06 13:41:29 +0000 | [diff] [blame] | 411 | ClangTidyContext Context(std::move(OwningOptionsProvider)); |
| 412 | runClangTidy(Context, OptionsParser.getCompilations(), PathList, |
| 413 | EnableCheckProfile ? &Profile : nullptr); |
| 414 | ArrayRef<ClangTidyError> Errors = Context.getErrors(); |
Alexander Kornienko | 5eac3c6 | 2014-11-03 14:06:31 +0000 | [diff] [blame] | 415 | bool FoundErrors = |
| 416 | std::find_if(Errors.begin(), Errors.end(), [](const ClangTidyError &E) { |
| 417 | return E.DiagLevel == ClangTidyError::Error; |
| 418 | }) != Errors.end(); |
| 419 | |
| 420 | const bool DisableFixes = Fix && FoundErrors && !FixErrors; |
| 421 | |
Jonathan Roelofs | d60388a | 2016-01-13 17:36:41 +0000 | [diff] [blame] | 422 | unsigned WErrorCount = 0; |
| 423 | |
Alexander Kornienko | 5eac3c6 | 2014-11-03 14:06:31 +0000 | [diff] [blame] | 424 | // -fix-errors implies -fix. |
Alexander Kornienko | 2561320 | 2017-04-06 13:41:29 +0000 | [diff] [blame] | 425 | handleErrors(Context, (FixErrors || Fix) && !DisableFixes, WErrorCount); |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 426 | |
Alexander Kornienko | 4153da2 | 2014-09-04 15:19:49 +0000 | [diff] [blame] | 427 | if (!ExportFixes.empty() && !Errors.empty()) { |
Benjamin Kramer | fb98b74 | 2014-09-04 10:31:23 +0000 | [diff] [blame] | 428 | std::error_code EC; |
| 429 | llvm::raw_fd_ostream OS(ExportFixes, EC, llvm::sys::fs::F_None); |
| 430 | if (EC) { |
| 431 | llvm::errs() << "Error opening output file: " << EC.message() << '\n'; |
| 432 | return 1; |
| 433 | } |
Alexander Kornienko | 563de79 | 2017-01-03 14:36:13 +0000 | [diff] [blame] | 434 | exportReplacements(FilePath.str(), Errors, OS); |
Benjamin Kramer | fb98b74 | 2014-09-04 10:31:23 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Ehsan Akhgari | b7418d3 | 2017-02-09 18:32:02 +0000 | [diff] [blame] | 437 | if (!Quiet) { |
Alexander Kornienko | 2561320 | 2017-04-06 13:41:29 +0000 | [diff] [blame] | 438 | printStats(Context.getStats()); |
Ehsan Akhgari | b7418d3 | 2017-02-09 18:32:02 +0000 | [diff] [blame] | 439 | if (DisableFixes) |
| 440 | llvm::errs() |
| 441 | << "Found compiler errors, but -fix-errors was not specified.\n" |
| 442 | "Fixes have NOT been applied.\n\n"; |
| 443 | } |
Alexander Kornienko | 5eac3c6 | 2014-11-03 14:06:31 +0000 | [diff] [blame] | 444 | |
Samuel Benzaquen | aedd994 | 2014-10-23 17:23:20 +0000 | [diff] [blame] | 445 | if (EnableCheckProfile) |
| 446 | printProfileData(Profile, llvm::errs()); |
| 447 | |
Jonathan Roelofs | d60388a | 2016-01-13 17:36:41 +0000 | [diff] [blame] | 448 | if (WErrorCount) { |
Ehsan Akhgari | b7418d3 | 2017-02-09 18:32:02 +0000 | [diff] [blame] | 449 | if (!Quiet) { |
| 450 | StringRef Plural = WErrorCount == 1 ? "" : "s"; |
| 451 | llvm::errs() << WErrorCount << " warning" << Plural << " treated as error" |
| 452 | << Plural << "\n"; |
| 453 | } |
Jonathan Roelofs | d60388a | 2016-01-13 17:36:41 +0000 | [diff] [blame] | 454 | return WErrorCount; |
| 455 | } |
| 456 | |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 457 | return 0; |
| 458 | } |
Daniel Jasper | 89bbab0 | 2013-08-04 15:56:30 +0000 | [diff] [blame] | 459 | |
Aaron Ballman | ea2f90c | 2015-10-02 13:27:19 +0000 | [diff] [blame] | 460 | // This anchor is used to force the linker to link the CERTModule. |
| 461 | extern volatile int CERTModuleAnchorSource; |
| 462 | static int LLVM_ATTRIBUTE_UNUSED CERTModuleAnchorDestination = |
| 463 | CERTModuleAnchorSource; |
| 464 | |
Piotr Padlewski | 5625f65 | 2016-04-29 17:58:29 +0000 | [diff] [blame] | 465 | // This anchor is used to force the linker to link the BoostModule. |
| 466 | extern volatile int BoostModuleAnchorSource; |
| 467 | static int LLVM_ATTRIBUTE_UNUSED BoostModuleAnchorDestination = |
| 468 | BoostModuleAnchorSource; |
| 469 | |
Gabor Horvath | 829e75a | 2017-07-14 12:15:55 +0000 | [diff] [blame] | 470 | // This anchor is used to force the linker to link the BugproneModule. |
| 471 | extern volatile int BugproneModuleAnchorSource; |
| 472 | static int LLVM_ATTRIBUTE_UNUSED BugproneModuleAnchorDestination = |
| 473 | BugproneModuleAnchorSource; |
| 474 | |
Daniel Jasper | 89bbab0 | 2013-08-04 15:56:30 +0000 | [diff] [blame] | 475 | // This anchor is used to force the linker to link the LLVMModule. |
| 476 | extern volatile int LLVMModuleAnchorSource; |
Alexander Kornienko | e1292f8 | 2015-08-19 16:54:51 +0000 | [diff] [blame] | 477 | static int LLVM_ATTRIBUTE_UNUSED LLVMModuleAnchorDestination = |
| 478 | LLVMModuleAnchorSource; |
Daniel Jasper | 89bbab0 | 2013-08-04 15:56:30 +0000 | [diff] [blame] | 479 | |
Aaron Ballman | aaa4080 | 2015-10-06 13:31:00 +0000 | [diff] [blame] | 480 | // This anchor is used to force the linker to link the CppCoreGuidelinesModule. |
| 481 | extern volatile int CppCoreGuidelinesModuleAnchorSource; |
| 482 | static int LLVM_ATTRIBUTE_UNUSED CppCoreGuidelinesModuleAnchorDestination = |
| 483 | CppCoreGuidelinesModuleAnchorSource; |
| 484 | |
Daniel Jasper | 89bbab0 | 2013-08-04 15:56:30 +0000 | [diff] [blame] | 485 | // This anchor is used to force the linker to link the GoogleModule. |
| 486 | extern volatile int GoogleModuleAnchorSource; |
Alexander Kornienko | e1292f8 | 2015-08-19 16:54:51 +0000 | [diff] [blame] | 487 | static int LLVM_ATTRIBUTE_UNUSED GoogleModuleAnchorDestination = |
| 488 | GoogleModuleAnchorSource; |
Daniel Jasper | 89bbab0 | 2013-08-04 15:56:30 +0000 | [diff] [blame] | 489 | |
Yan Wang | 3620620 | 2017-06-23 21:37:29 +0000 | [diff] [blame] | 490 | // This anchor is used to force the linker to link the AndroidModule. |
| 491 | extern volatile int AndroidModuleAnchorSource; |
| 492 | static int LLVM_ATTRIBUTE_UNUSED AndroidModuleAnchorDestination = |
| 493 | AndroidModuleAnchorSource; |
| 494 | |
Alexander Kornienko | 16ac6ce | 2014-03-05 13:14:32 +0000 | [diff] [blame] | 495 | // This anchor is used to force the linker to link the MiscModule. |
| 496 | extern volatile int MiscModuleAnchorSource; |
Alexander Kornienko | e1292f8 | 2015-08-19 16:54:51 +0000 | [diff] [blame] | 497 | static int LLVM_ATTRIBUTE_UNUSED MiscModuleAnchorDestination = |
| 498 | MiscModuleAnchorSource; |
Alexander Kornienko | 16ac6ce | 2014-03-05 13:14:32 +0000 | [diff] [blame] | 499 | |
Alexander Kornienko | fc65086 | 2015-08-14 13:17:11 +0000 | [diff] [blame] | 500 | // This anchor is used to force the linker to link the ModernizeModule. |
| 501 | extern volatile int ModernizeModuleAnchorSource; |
Alexander Kornienko | e1292f8 | 2015-08-19 16:54:51 +0000 | [diff] [blame] | 502 | static int LLVM_ATTRIBUTE_UNUSED ModernizeModuleAnchorDestination = |
| 503 | ModernizeModuleAnchorSource; |
Alexander Kornienko | fc65086 | 2015-08-14 13:17:11 +0000 | [diff] [blame] | 504 | |
Alexander Kornienko | 5e0a50c | 2016-08-02 20:29:35 +0000 | [diff] [blame] | 505 | // This anchor is used to force the linker to link the MPIModule. |
| 506 | extern volatile int MPIModuleAnchorSource; |
| 507 | static int LLVM_ATTRIBUTE_UNUSED MPIModuleAnchorDestination = |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 508 | MPIModuleAnchorSource; |
Alexander Kornienko | 5e0a50c | 2016-08-02 20:29:35 +0000 | [diff] [blame] | 509 | |
Alexander Kornienko | b959f4c | 2015-12-30 10:24:40 +0000 | [diff] [blame] | 510 | // This anchor is used to force the linker to link the PerformanceModule. |
| 511 | extern volatile int PerformanceModuleAnchorSource; |
| 512 | static int LLVM_ATTRIBUTE_UNUSED PerformanceModuleAnchorDestination = |
| 513 | PerformanceModuleAnchorSource; |
| 514 | |
Alexander Kornienko | 2192a8e | 2014-10-26 01:41:14 +0000 | [diff] [blame] | 515 | // This anchor is used to force the linker to link the ReadabilityModule. |
| 516 | extern volatile int ReadabilityModuleAnchorSource; |
Alexander Kornienko | e1292f8 | 2015-08-19 16:54:51 +0000 | [diff] [blame] | 517 | static int LLVM_ATTRIBUTE_UNUSED ReadabilityModuleAnchorDestination = |
| 518 | ReadabilityModuleAnchorSource; |
Alexander Kornienko | 2192a8e | 2014-10-26 01:41:14 +0000 | [diff] [blame] | 519 | |
Aaron Ballman | dbdbabf | 2017-03-19 17:23:23 +0000 | [diff] [blame] | 520 | // This anchor is used to force the linker to link the HICPPModule. |
| 521 | extern volatile int HICPPModuleAnchorSource; |
| 522 | static int LLVM_ATTRIBUTE_UNUSED HICPPModuleAnchorDestination = |
| 523 | HICPPModuleAnchorSource; |
Jonathan Coe | 3032d3c | 2017-02-06 22:57:14 +0000 | [diff] [blame] | 524 | |
Daniel Jasper | 89bbab0 | 2013-08-04 15:56:30 +0000 | [diff] [blame] | 525 | } // namespace tidy |
| 526 | } // namespace clang |
Alexander Kornienko | c28c32d | 2014-09-10 11:43:09 +0000 | [diff] [blame] | 527 | |
| 528 | int main(int argc, const char **argv) { |
| 529 | return clang::tidy::clangTidyMain(argc, argv); |
| 530 | } |