blob: e130d13930d32f2aa63d7b53085e7567e2c4efdf [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."),
37 cl::init("clang-analyzer-alpha.*"), cl::cat(ClangTidyCategory));
Daniel Jasperd07c8402013-07-29 08:19:24 +000038static cl::opt<bool> Fix("fix", cl::desc("Fix detected errors if possible."),
39 cl::init(false), cl::cat(ClangTidyCategory));
40
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +000041static cl::opt<bool> ListChecks("list-checks",
42 cl::desc("List all enabled checks and exit."),
43 cl::init(false), cl::cat(ClangTidyCategory));
Daniel Jasperd07c8402013-07-29 08:19:24 +000044
45int main(int argc, const char **argv) {
Alexander Kornienko06ff5a72013-12-12 10:01:39 +000046 CommonOptionsParser OptionsParser(argc, argv, ClangTidyCategory);
Daniel Jasperd07c8402013-07-29 08:19:24 +000047
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +000048 // FIXME: Allow using --list-checks without positional arguments.
49 if (ListChecks) {
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +000050 llvm::outs() << "Enabled checks:";
Alexander Kornienko16ac6ce2014-03-05 13:14:32 +000051 for (auto CheckName : clang::tidy::getCheckNames(Checks, DisableChecks))
52 llvm::outs() << "\n " << CheckName;
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +000053 llvm::outs() << "\n\n";
54 return 0;
55 }
56
Daniel Jasperd07c8402013-07-29 08:19:24 +000057 SmallVector<clang::tidy::ClangTidyError, 16> Errors;
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +000058 clang::tidy::runClangTidy(Checks, DisableChecks,
59 OptionsParser.getCompilations(),
Manuel Klimek814f9bd2013-11-14 15:49:44 +000060 OptionsParser.getSourcePathList(), &Errors);
Daniel Jasperd07c8402013-07-29 08:19:24 +000061 clang::tidy::handleErrors(Errors, Fix);
62
63 return 0;
64}
Daniel Jasper89bbab02013-08-04 15:56:30 +000065
66namespace clang {
67namespace tidy {
68
69// This anchor is used to force the linker to link the LLVMModule.
70extern volatile int LLVMModuleAnchorSource;
71static int LLVMModuleAnchorDestination = LLVMModuleAnchorSource;
72
73// This anchor is used to force the linker to link the GoogleModule.
74extern volatile int GoogleModuleAnchorSource;
75static int GoogleModuleAnchorDestination = GoogleModuleAnchorSource;
76
Alexander Kornienko16ac6ce2014-03-05 13:14:32 +000077// This anchor is used to force the linker to link the MiscModule.
78extern volatile int MiscModuleAnchorSource;
79static int MiscModuleAnchorDestination = MiscModuleAnchorSource;
80
Daniel Jasper89bbab02013-08-04 15:56:30 +000081} // namespace tidy
82} // namespace clang