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