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" |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace clang::ast_matchers; |
| 22 | using namespace clang::driver; |
| 23 | using namespace clang::tooling; |
| 24 | using namespace llvm; |
| 25 | |
Alexander Kornienko | 99c9d6a | 2014-02-05 13:43:27 +0000 | [diff] [blame] | 26 | static cl::OptionCategory ClangTidyCategory("clang-tidy options"); |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 27 | |
Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 +0000 | [diff] [blame] | 28 | static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage); |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 29 | |
Alexander Kornienko | 23fe959 | 2014-05-15 14:27:36 +0000 | [diff] [blame] | 30 | const char DefaultChecks[] = |
| 31 | "*," // Enable all checks, except these: |
| 32 | "-clang-analyzer-alpha*," // Too many false positives. |
| 33 | "-llvm-include-order," // Not implemented yet. |
Alexander Kornienko | 23fe959 | 2014-05-15 14:27:36 +0000 | [diff] [blame] | 34 | "-google-*,"; // Doesn't apply to LLVM. |
Alexander Kornienko | dad4acb | 2014-05-22 16:07:11 +0000 | [diff] [blame^] | 35 | |
Alexander Kornienko | 23fe959 | 2014-05-15 14:27:36 +0000 | [diff] [blame] | 36 | static cl::opt<std::string> |
Alexander Kornienko | 3ab3467 | 2014-05-16 13:07:18 +0000 | [diff] [blame] | 37 | Checks("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 Kornienko | 23fe959 | 2014-05-15 14:27:36 +0000 | [diff] [blame] | 43 | cl::init(""), cl::cat(ClangTidyCategory)); |
Alexander Kornienko | dad4acb | 2014-05-22 16:07:11 +0000 | [diff] [blame^] | 44 | |
Alexander Kornienko | 3ab3467 | 2014-05-16 13:07:18 +0000 | [diff] [blame] | 45 | static cl::opt<std::string> |
| 46 | HeaderFilter("header-filter", |
| 47 | cl::desc("Regular expression matching the names of the\n" |
Alexander Kornienko | dad4acb | 2014-05-22 16:07:11 +0000 | [diff] [blame^] | 48 | "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 Kornienko | 3ab3467 | 2014-05-16 13:07:18 +0000 | [diff] [blame] | 52 | cl::init(""), cl::cat(ClangTidyCategory)); |
Alexander Kornienko | dad4acb | 2014-05-22 16:07:11 +0000 | [diff] [blame^] | 53 | |
| 54 | static cl::opt<std::string> |
| 55 | LineFilter("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 Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 66 | static cl::opt<bool> Fix("fix", cl::desc("Fix detected errors if possible."), |
| 67 | cl::init(false), cl::cat(ClangTidyCategory)); |
| 68 | |
Alexander Kornienko | 3ab3467 | 2014-05-16 13:07:18 +0000 | [diff] [blame] | 69 | static cl::opt<bool> |
| 70 | ListChecks("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 Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 74 | |
Alexander Kornienko | 3ab3467 | 2014-05-16 13:07:18 +0000 | [diff] [blame] | 75 | static cl::opt<bool> |
| 76 | AnalyzeTemporaryDtors("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 McCarthy | fec08c7 | 2014-04-30 14:09:24 +0000 | [diff] [blame] | 80 | |
Alexander Kornienko | 5d17454 | 2014-05-07 09:06:53 +0000 | [diff] [blame] | 81 | static void printStats(const clang::tidy::ClangTidyStats &Stats) { |
Alexander Kornienko | dad4acb | 2014-05-22 16:07:11 +0000 | [diff] [blame^] | 82 | if (Stats.errorsIgnored()) { |
| 83 | llvm::errs() << "Suppressed " << Stats.errorsIgnored() << " warnings ("; |
Alexander Kornienko | 5d17454 | 2014-05-07 09:06:53 +0000 | [diff] [blame] | 84 | StringRef Separator = ""; |
| 85 | if (Stats.ErrorsIgnoredNonUserCode) { |
| 86 | llvm::errs() << Stats.ErrorsIgnoredNonUserCode << " in non-user code"; |
| 87 | Separator = ", "; |
| 88 | } |
Alexander Kornienko | dad4acb | 2014-05-22 16:07:11 +0000 | [diff] [blame^] | 89 | if (Stats.ErrorsIgnoredLineFilter) { |
| 90 | llvm::errs() << Separator << Stats.ErrorsIgnoredLineFilter |
| 91 | << " due to line filter"; |
| 92 | Separator = ", "; |
| 93 | } |
Alexander Kornienko | 5d17454 | 2014-05-07 09:06:53 +0000 | [diff] [blame] | 94 | if (Stats.ErrorsIgnoredNOLINT) { |
| 95 | llvm::errs() << Separator << Stats.ErrorsIgnoredNOLINT << " NOLINT"; |
| 96 | Separator = ", "; |
| 97 | } |
| 98 | if (Stats.ErrorsIgnoredCheckFilter) |
| 99 | llvm::errs() << Separator << Stats.ErrorsIgnoredCheckFilter |
| 100 | << " with check filters"; |
| 101 | llvm::errs() << ").\n"; |
| 102 | if (Stats.ErrorsIgnoredNonUserCode) |
| 103 | llvm::errs() << "Use -header-filter='.*' to display errors from all " |
| 104 | "non-system headers.\n"; |
| 105 | } |
| 106 | } |
| 107 | |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 108 | int main(int argc, const char **argv) { |
Alexander Kornienko | 06ff5a7 | 2013-12-12 10:01:39 +0000 | [diff] [blame] | 109 | CommonOptionsParser OptionsParser(argc, argv, ClangTidyCategory); |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 110 | |
Alexander Kornienko | 33a9bcc | 2014-04-29 15:20:10 +0000 | [diff] [blame] | 111 | clang::tidy::ClangTidyOptions Options; |
Alexander Kornienko | 23fe959 | 2014-05-15 14:27:36 +0000 | [diff] [blame] | 112 | Options.Checks = DefaultChecks + Checks; |
Alexander Kornienko | 9ff5b6f | 2014-05-05 14:54:47 +0000 | [diff] [blame] | 113 | Options.HeaderFilterRegex = HeaderFilter; |
Alex McCarthy | fec08c7 | 2014-04-30 14:09:24 +0000 | [diff] [blame] | 114 | Options.AnalyzeTemporaryDtors = AnalyzeTemporaryDtors; |
Alexander Kornienko | dad4acb | 2014-05-22 16:07:11 +0000 | [diff] [blame^] | 115 | if (llvm::error_code Err = |
| 116 | clang::tidy::parseLineFilter(LineFilter, Options)) { |
| 117 | llvm::errs() << "Invalid LineFilter: " << Err.message() << "\n\nUsage:\n"; |
| 118 | llvm::cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true); |
| 119 | return 1; |
| 120 | } |
Alexander Kornienko | 33a9bcc | 2014-04-29 15:20:10 +0000 | [diff] [blame] | 121 | |
Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 +0000 | [diff] [blame] | 122 | // FIXME: Allow using --list-checks without positional arguments. |
| 123 | if (ListChecks) { |
Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 +0000 | [diff] [blame] | 124 | llvm::outs() << "Enabled checks:"; |
Alexander Kornienko | 33a9bcc | 2014-04-29 15:20:10 +0000 | [diff] [blame] | 125 | for (auto CheckName : clang::tidy::getCheckNames(Options)) |
Alexander Kornienko | 16ac6ce | 2014-03-05 13:14:32 +0000 | [diff] [blame] | 126 | llvm::outs() << "\n " << CheckName; |
Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 +0000 | [diff] [blame] | 127 | llvm::outs() << "\n\n"; |
| 128 | return 0; |
| 129 | } |
| 130 | |
Alexander Kornienko | 826b5ad | 2014-05-09 12:24:09 +0000 | [diff] [blame] | 131 | std::vector<clang::tidy::ClangTidyError> Errors; |
Alexander Kornienko | 5d17454 | 2014-05-07 09:06:53 +0000 | [diff] [blame] | 132 | clang::tidy::ClangTidyStats Stats = |
| 133 | clang::tidy::runClangTidy(Options, OptionsParser.getCompilations(), |
| 134 | OptionsParser.getSourcePathList(), &Errors); |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 135 | clang::tidy::handleErrors(Errors, Fix); |
| 136 | |
Alexander Kornienko | 5d17454 | 2014-05-07 09:06:53 +0000 | [diff] [blame] | 137 | printStats(Stats); |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 138 | return 0; |
| 139 | } |
Daniel Jasper | 89bbab0 | 2013-08-04 15:56:30 +0000 | [diff] [blame] | 140 | |
| 141 | namespace clang { |
| 142 | namespace tidy { |
| 143 | |
| 144 | // This anchor is used to force the linker to link the LLVMModule. |
| 145 | extern volatile int LLVMModuleAnchorSource; |
| 146 | static int LLVMModuleAnchorDestination = LLVMModuleAnchorSource; |
| 147 | |
| 148 | // This anchor is used to force the linker to link the GoogleModule. |
| 149 | extern volatile int GoogleModuleAnchorSource; |
| 150 | static int GoogleModuleAnchorDestination = GoogleModuleAnchorSource; |
| 151 | |
Alexander Kornienko | 16ac6ce | 2014-03-05 13:14:32 +0000 | [diff] [blame] | 152 | // This anchor is used to force the linker to link the MiscModule. |
| 153 | extern volatile int MiscModuleAnchorSource; |
| 154 | static int MiscModuleAnchorDestination = MiscModuleAnchorSource; |
| 155 | |
Daniel Jasper | 89bbab0 | 2013-08-04 15:56:30 +0000 | [diff] [blame] | 156 | } // namespace tidy |
| 157 | } // namespace clang |