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 | |
| 30 | static cl::opt<std::string> Checks( |
| 31 | "checks", |
| 32 | cl::desc("Regular expression matching the names of the checks to be run."), |
| 33 | cl::init(".*"), cl::cat(ClangTidyCategory)); |
Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 +0000 | [diff] [blame] | 34 | static cl::opt<std::string> DisableChecks( |
| 35 | "disable-checks", |
| 36 | cl::desc("Regular expression matching the names of the checks to disable."), |
Alex McCarthy | 5bc902e | 2014-04-29 02:33:58 +0000 | [diff] [blame] | 37 | cl::init("(clang-analyzer-alpha.*" // Too many false positives. |
Daniel Jasper | a3b8f0c | 2014-04-02 08:27:12 +0000 | [diff] [blame] | 38 | "|llvm-include-order" // Not implemented yet. |
| 39 | "|llvm-namespace-comment" // Not complete. |
Daniel Jasper | 7b8d263 | 2014-04-02 08:52:06 +0000 | [diff] [blame] | 40 | "|google-.*)"), // Doesn't apply to LLVM. |
Daniel Jasper | a3b8f0c | 2014-04-02 08:27:12 +0000 | [diff] [blame] | 41 | cl::cat(ClangTidyCategory)); |
Alexander Kornienko | 9ff5b6f | 2014-05-05 14:54:47 +0000 | [diff] [blame^] | 42 | static cl::opt<std::string> HeaderFilter( |
| 43 | "header-filter", |
| 44 | cl::desc("Regular expression matching the names of the headers to output\n" |
| 45 | "diagnostics from. Diagnostics from the main file of each\n" |
| 46 | "translation unit are always displayed."), |
| 47 | cl::init(""), cl::cat(ClangTidyCategory)); |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 48 | static cl::opt<bool> Fix("fix", cl::desc("Fix detected errors if possible."), |
| 49 | cl::init(false), cl::cat(ClangTidyCategory)); |
| 50 | |
Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 +0000 | [diff] [blame] | 51 | static cl::opt<bool> ListChecks("list-checks", |
| 52 | cl::desc("List all enabled checks and exit."), |
| 53 | cl::init(false), cl::cat(ClangTidyCategory)); |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 54 | |
Alex McCarthy | fec08c7 | 2014-04-30 14:09:24 +0000 | [diff] [blame] | 55 | static cl::opt<bool> AnalyzeTemporaryDtors( |
| 56 | "analyze-temporary-dtors", |
| 57 | cl::desc("Enable temporary destructor-aware analysis in clang-analyzer- " |
| 58 | "checks."), |
| 59 | cl::init(false), |
| 60 | cl::cat(ClangTidyCategory)); |
| 61 | |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 62 | int main(int argc, const char **argv) { |
Alexander Kornienko | 06ff5a7 | 2013-12-12 10:01:39 +0000 | [diff] [blame] | 63 | CommonOptionsParser OptionsParser(argc, argv, ClangTidyCategory); |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 64 | |
Alexander Kornienko | 33a9bcc | 2014-04-29 15:20:10 +0000 | [diff] [blame] | 65 | clang::tidy::ClangTidyOptions Options; |
| 66 | Options.EnableChecksRegex = Checks; |
| 67 | Options.DisableChecksRegex = DisableChecks; |
Alexander Kornienko | 9ff5b6f | 2014-05-05 14:54:47 +0000 | [diff] [blame^] | 68 | Options.HeaderFilterRegex = HeaderFilter; |
Alex McCarthy | fec08c7 | 2014-04-30 14:09:24 +0000 | [diff] [blame] | 69 | Options.AnalyzeTemporaryDtors = AnalyzeTemporaryDtors; |
Alexander Kornienko | 33a9bcc | 2014-04-29 15:20:10 +0000 | [diff] [blame] | 70 | |
Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 +0000 | [diff] [blame] | 71 | // FIXME: Allow using --list-checks without positional arguments. |
| 72 | if (ListChecks) { |
Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 +0000 | [diff] [blame] | 73 | llvm::outs() << "Enabled checks:"; |
Alexander Kornienko | 33a9bcc | 2014-04-29 15:20:10 +0000 | [diff] [blame] | 74 | for (auto CheckName : clang::tidy::getCheckNames(Options)) |
Alexander Kornienko | 16ac6ce | 2014-03-05 13:14:32 +0000 | [diff] [blame] | 75 | llvm::outs() << "\n " << CheckName; |
Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 +0000 | [diff] [blame] | 76 | llvm::outs() << "\n\n"; |
| 77 | return 0; |
| 78 | } |
| 79 | |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 80 | SmallVector<clang::tidy::ClangTidyError, 16> Errors; |
Alexander Kornienko | 33a9bcc | 2014-04-29 15:20:10 +0000 | [diff] [blame] | 81 | clang::tidy::runClangTidy(Options, OptionsParser.getCompilations(), |
Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 +0000 | [diff] [blame] | 82 | OptionsParser.getSourcePathList(), &Errors); |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 83 | clang::tidy::handleErrors(Errors, Fix); |
| 84 | |
| 85 | return 0; |
| 86 | } |
Daniel Jasper | 89bbab0 | 2013-08-04 15:56:30 +0000 | [diff] [blame] | 87 | |
| 88 | namespace clang { |
| 89 | namespace tidy { |
| 90 | |
| 91 | // This anchor is used to force the linker to link the LLVMModule. |
| 92 | extern volatile int LLVMModuleAnchorSource; |
| 93 | static int LLVMModuleAnchorDestination = LLVMModuleAnchorSource; |
| 94 | |
| 95 | // This anchor is used to force the linker to link the GoogleModule. |
| 96 | extern volatile int GoogleModuleAnchorSource; |
| 97 | static int GoogleModuleAnchorDestination = GoogleModuleAnchorSource; |
| 98 | |
Alexander Kornienko | 16ac6ce | 2014-03-05 13:14:32 +0000 | [diff] [blame] | 99 | // This anchor is used to force the linker to link the MiscModule. |
| 100 | extern volatile int MiscModuleAnchorSource; |
| 101 | static int MiscModuleAnchorDestination = MiscModuleAnchorSource; |
| 102 | |
Daniel Jasper | 89bbab0 | 2013-08-04 15:56:30 +0000 | [diff] [blame] | 103 | } // namespace tidy |
| 104 | } // namespace clang |