blob: 9284a8b038a232e78b64cd9d219905338dec479c [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);
Daniel Jasperd07c8402013-07-29 08:19:24 +000029
30static 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 Kornienkofb9e92b2013-12-19 19:57:05 +000034static cl::opt<std::string> DisableChecks(
35 "disable-checks",
36 cl::desc("Regular expression matching the names of the checks to disable."),
Alex McCarthy5bc902e2014-04-29 02:33:58 +000037 cl::init("(clang-analyzer-alpha.*" // Too many false positives.
Daniel Jaspera3b8f0c2014-04-02 08:27:12 +000038 "|llvm-include-order" // Not implemented yet.
39 "|llvm-namespace-comment" // Not complete.
Daniel Jasper7b8d2632014-04-02 08:52:06 +000040 "|google-.*)"), // Doesn't apply to LLVM.
Daniel Jaspera3b8f0c2014-04-02 08:27:12 +000041 cl::cat(ClangTidyCategory));
Daniel Jasperd07c8402013-07-29 08:19:24 +000042static cl::opt<bool> Fix("fix", cl::desc("Fix detected errors if possible."),
43 cl::init(false), cl::cat(ClangTidyCategory));
44
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +000045static cl::opt<bool> ListChecks("list-checks",
46 cl::desc("List all enabled checks and exit."),
47 cl::init(false), cl::cat(ClangTidyCategory));
Daniel Jasperd07c8402013-07-29 08:19:24 +000048
49int main(int argc, const char **argv) {
Alexander Kornienko06ff5a72013-12-12 10:01:39 +000050 CommonOptionsParser OptionsParser(argc, argv, ClangTidyCategory);
Daniel Jasperd07c8402013-07-29 08:19:24 +000051
Alexander Kornienko33a9bcc2014-04-29 15:20:10 +000052 clang::tidy::ClangTidyOptions Options;
53 Options.EnableChecksRegex = Checks;
54 Options.DisableChecksRegex = DisableChecks;
55
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +000056 // FIXME: Allow using --list-checks without positional arguments.
57 if (ListChecks) {
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +000058 llvm::outs() << "Enabled checks:";
Alexander Kornienko33a9bcc2014-04-29 15:20:10 +000059 for (auto CheckName : clang::tidy::getCheckNames(Options))
Alexander Kornienko16ac6ce2014-03-05 13:14:32 +000060 llvm::outs() << "\n " << CheckName;
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +000061 llvm::outs() << "\n\n";
62 return 0;
63 }
64
Daniel Jasperd07c8402013-07-29 08:19:24 +000065 SmallVector<clang::tidy::ClangTidyError, 16> Errors;
Alexander Kornienko33a9bcc2014-04-29 15:20:10 +000066 clang::tidy::runClangTidy(Options, OptionsParser.getCompilations(),
Manuel Klimek814f9bd2013-11-14 15:49:44 +000067 OptionsParser.getSourcePathList(), &Errors);
Daniel Jasperd07c8402013-07-29 08:19:24 +000068 clang::tidy::handleErrors(Errors, Fix);
69
70 return 0;
71}
Daniel Jasper89bbab02013-08-04 15:56:30 +000072
73namespace clang {
74namespace tidy {
75
76// This anchor is used to force the linker to link the LLVMModule.
77extern volatile int LLVMModuleAnchorSource;
78static int LLVMModuleAnchorDestination = LLVMModuleAnchorSource;
79
80// This anchor is used to force the linker to link the GoogleModule.
81extern volatile int GoogleModuleAnchorSource;
82static int GoogleModuleAnchorDestination = GoogleModuleAnchorSource;
83
Alexander Kornienko16ac6ce2014-03-05 13:14:32 +000084// This anchor is used to force the linker to link the MiscModule.
85extern volatile int MiscModuleAnchorSource;
86static int MiscModuleAnchorDestination = MiscModuleAnchorSource;
87
Daniel Jasper89bbab02013-08-04 15:56:30 +000088} // namespace tidy
89} // namespace clang