blob: 701db52334c6ea980595cd571edaddd1795ee764 [file] [log] [blame]
Alexander Kornienkod7166b02012-08-22 20:52:52 +00001//===--- tools/clang-check/ClangCheck.cpp - Clang check tool --------------===//
Manuel Klimekcb971c62012-04-04 12:07:46 +00002//
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//
Daniel Jasper5e5f7262012-10-03 13:28:43 +000010// This file implements a clang-check tool that runs clang based on the info
11// stored in a compilation database.
Manuel Klimekcb971c62012-04-04 12:07:46 +000012//
Alexander Kornienkodea8fba2012-07-16 12:46:48 +000013// This tool uses the Clang Tooling infrastructure, see
14// http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html
15// for details on setting it up with LLVM source tree.
16//
Manuel Klimekcb971c62012-04-04 12:07:46 +000017//===----------------------------------------------------------------------===//
18
Alexander Kornienko48464702012-08-13 10:50:08 +000019#include "clang/AST/ASTConsumer.h"
Alexander Kornienko48464702012-08-13 10:50:08 +000020#include "clang/Driver/Options.h"
21#include "clang/Frontend/ASTConsumers.h"
Daniel Jasper5e5f7262012-10-03 13:28:43 +000022#include "clang/Frontend/CompilerInstance.h"
Pavel Labath373b8072013-06-06 12:35:43 +000023#include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
Daniel Jasper5e5f7262012-10-03 13:28:43 +000024#include "clang/Rewrite/Frontend/FixItRewriter.h"
25#include "clang/Rewrite/Frontend/FrontendActions.h"
Alexander Kornienkod7166b02012-08-22 20:52:52 +000026#include "clang/Tooling/CommonOptionsParser.h"
Manuel Klimekcb971c62012-04-04 12:07:46 +000027#include "clang/Tooling/Tooling.h"
Alexander Kornienkod7166b02012-08-22 20:52:52 +000028#include "llvm/Support/CommandLine.h"
Daniel Jasper5e5f7262012-10-03 13:28:43 +000029#include "llvm/Support/Path.h"
NAKAMURA Takumib8238b62013-01-18 13:46:48 +000030#include "llvm/Support/Signals.h"
Reid Klecknerb1e25a12013-06-14 17:17:23 +000031#include "llvm/Option/OptTable.h"
Manuel Klimekcb971c62012-04-04 12:07:46 +000032
Alexander Kornienko48464702012-08-13 10:50:08 +000033using namespace clang::driver;
Manuel Klimekcb971c62012-04-04 12:07:46 +000034using namespace clang::tooling;
35using namespace llvm;
Reid Klecknerb1e25a12013-06-14 17:17:23 +000036using namespace llvm::opt;
Manuel Klimekcb971c62012-04-04 12:07:46 +000037
Alexander Kornienko6fbe9822012-08-24 00:39:14 +000038static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
Alexander Kornienkod7166b02012-08-22 20:52:52 +000039static cl::extrahelp MoreHelp(
Alexander Kornienkodea8fba2012-07-16 12:46:48 +000040 "\tFor example, to run clang-check on all files in a subtree of the\n"
41 "\tsource tree, use:\n"
Alexander Kornienko140d5132012-07-12 14:34:23 +000042 "\n"
Alexander Kornienkodea8fba2012-07-16 12:46:48 +000043 "\t find path/in/subtree -name '*.cpp'|xargs clang-check\n"
Alexander Kornienko140d5132012-07-12 14:34:23 +000044 "\n"
Alexander Kornienkodea8fba2012-07-16 12:46:48 +000045 "\tor using a specific build path:\n"
Alexander Kornienko140d5132012-07-12 14:34:23 +000046 "\n"
Alexander Kornienkodea8fba2012-07-16 12:46:48 +000047 "\t find path/in/subtree -name '*.cpp'|xargs clang-check -p build/path\n"
Alexander Kornienko140d5132012-07-12 14:34:23 +000048 "\n"
Alexander Kornienkodea8fba2012-07-16 12:46:48 +000049 "\tNote, that path/in/subtree and current directory should follow the\n"
50 "\trules described above.\n"
Alexander Kornienkod7166b02012-08-22 20:52:52 +000051 "\n"
52);
53
54static OwningPtr<OptTable> Options(createDriverOptTable());
55static cl::opt<bool> ASTDump(
56 "ast-dump",
57 cl::desc(Options->getOptionHelpText(options::OPT_ast_dump)));
58static cl::opt<bool> ASTList(
59 "ast-list",
60 cl::desc(Options->getOptionHelpText(options::OPT_ast_list)));
61static cl::opt<bool> ASTPrint(
62 "ast-print",
63 cl::desc(Options->getOptionHelpText(options::OPT_ast_print)));
64static cl::opt<std::string> ASTDumpFilter(
65 "ast-dump-filter",
66 cl::desc(Options->getOptionHelpText(options::OPT_ast_dump_filter)));
Pavel Labath373b8072013-06-06 12:35:43 +000067static cl::opt<bool> Analyze(
68 "analyze",
69 cl::desc(Options->getOptionHelpText(options::OPT_analyze)));
Alexander Kornienko140d5132012-07-12 14:34:23 +000070
Daniel Jasper5e5f7262012-10-03 13:28:43 +000071static cl::opt<bool> Fixit(
72 "fixit",
73 cl::desc(Options->getOptionHelpText(options::OPT_fixit)));
74static cl::opt<bool> FixWhatYouCan(
75 "fix-what-you-can",
76 cl::desc(Options->getOptionHelpText(options::OPT_fix_what_you_can)));
77
Pavel Labath88c20042013-06-05 16:23:30 +000078static cl::list<std::string> ArgsAfter("extra-arg",
79 cl::desc("Additional argument to append to the compiler command line"));
80static cl::list<std::string> ArgsBefore("extra-arg-before",
81 cl::desc("Additional argument to prepend to the compiler command line"));
82
Daniel Jasper5e5f7262012-10-03 13:28:43 +000083namespace {
84
85// FIXME: Move FixItRewriteInPlace from lib/Rewrite/Frontend/FrontendActions.cpp
86// into a header file and reuse that.
87class FixItOptions : public clang::FixItOptions {
88public:
89 FixItOptions() {
90 FixWhatYouCan = ::FixWhatYouCan;
91 }
92
93 std::string RewriteFilename(const std::string& filename, int &fd) {
94 assert(llvm::sys::path::is_absolute(filename) &&
95 "clang-fixit expects absolute paths only.");
96
97 // We don't need to do permission checking here since clang will diagnose
98 // any I/O errors itself.
99
100 fd = -1; // No file descriptor for file.
101
102 return filename;
103 }
104};
105
106/// \brief Subclasses \c clang::FixItRewriter to not count fixed errors/warnings
107/// in the final error counts.
108///
109/// This has the side-effect that clang-check -fixit exits with code 0 on
110/// successfully fixing all errors.
111class FixItRewriter : public clang::FixItRewriter {
112public:
113 FixItRewriter(clang::DiagnosticsEngine& Diags,
114 clang::SourceManager& SourceMgr,
115 const clang::LangOptions& LangOpts,
116 clang::FixItOptions* FixItOpts)
117 : clang::FixItRewriter(Diags, SourceMgr, LangOpts, FixItOpts) {
118 }
119
120 virtual bool IncludeInDiagnosticCounts() const { return false; }
121};
122
123/// \brief Subclasses \c clang::FixItAction so that we can install the custom
124/// \c FixItRewriter.
125class FixItAction : public clang::FixItAction {
126public:
127 virtual bool BeginSourceFileAction(clang::CompilerInstance& CI,
128 StringRef Filename) {
129 FixItOpts.reset(new FixItOptions);
130 Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(),
131 CI.getLangOpts(), FixItOpts.get()));
132 return true;
133 }
134};
135
Pavel Labath88c20042013-06-05 16:23:30 +0000136class InsertAdjuster: public clang::tooling::ArgumentsAdjuster {
137public:
138 enum Position { BEGIN, END };
139
140 InsertAdjuster(const CommandLineArguments &Extra, Position Pos)
141 : Extra(Extra), Pos(Pos) {
142 }
143
Pavel Labath373b8072013-06-06 12:35:43 +0000144 InsertAdjuster(const char *Extra, Position Pos)
145 : Extra(1, std::string(Extra)), Pos(Pos) {
146 }
147
Pavel Labath88c20042013-06-05 16:23:30 +0000148 virtual CommandLineArguments
149 Adjust(const CommandLineArguments &Args) LLVM_OVERRIDE {
150 CommandLineArguments Return(Args);
151
152 CommandLineArguments::iterator I;
153 if (Pos == END) {
154 I = Return.end();
155 } else {
156 I = Return.begin();
157 ++I; // To leave the program name in place
158 }
159
160 Return.insert(I, Extra.begin(), Extra.end());
161 return Return;
162 }
163
164private:
165 const CommandLineArguments Extra;
166 const Position Pos;
167};
168
Daniel Jasper5e5f7262012-10-03 13:28:43 +0000169} // namespace
170
Alexander Kornienko18857f72012-09-10 14:54:38 +0000171// Anonymous namespace here causes problems with gcc <= 4.4 on MacOS 10.6.
172// "Non-global symbol: ... can't be a weak_definition"
173namespace clang_check {
174class ClangCheckActionFactory {
Alexander Kornienko48464702012-08-13 10:50:08 +0000175public:
Alexander Kornienko48464702012-08-13 10:50:08 +0000176 clang::ASTConsumer *newASTConsumer() {
177 if (ASTList)
178 return clang::CreateASTDeclNodeLister();
179 if (ASTDump)
180 return clang::CreateASTDumper(ASTDumpFilter);
181 if (ASTPrint)
182 return clang::CreateASTPrinter(&llvm::outs(), ASTDumpFilter);
183 return new clang::ASTConsumer();
184 }
Alexander Kornienko48464702012-08-13 10:50:08 +0000185};
Alexander Kornienko18857f72012-09-10 14:54:38 +0000186}
Alexander Kornienko48464702012-08-13 10:50:08 +0000187
Manuel Klimek30318e62012-04-18 07:41:50 +0000188int main(int argc, const char **argv) {
NAKAMURA Takumib8238b62013-01-18 13:46:48 +0000189 llvm::sys::PrintStackTraceOnErrorSignal();
Alexander Kornienkod7166b02012-08-22 20:52:52 +0000190 CommonOptionsParser OptionsParser(argc, argv);
Edwin Vaneb1f67db2012-12-14 18:58:25 +0000191 ClangTool Tool(OptionsParser.getCompilations(),
192 OptionsParser.getSourcePathList());
Pavel Labath88c20042013-06-05 16:23:30 +0000193
Pavel Labath373b8072013-06-06 12:35:43 +0000194 // Clear adjusters because -fsyntax-only is inserted by the default chain.
195 Tool.clearArgumentsAdjusters();
196 Tool.appendArgumentsAdjuster(new ClangStripOutputAdjuster());
197 if (ArgsAfter.size() > 0) {
198 Tool.appendArgumentsAdjuster(new InsertAdjuster(ArgsAfter,
199 InsertAdjuster::END));
200 }
201 if (ArgsBefore.size() > 0) {
202 Tool.appendArgumentsAdjuster(new InsertAdjuster(ArgsBefore,
203 InsertAdjuster::BEGIN));
204 }
Pavel Labath88c20042013-06-05 16:23:30 +0000205
Pavel Labath373b8072013-06-06 12:35:43 +0000206 // Running the analyzer requires --analyze. Other modes can work with the
207 // -fsyntax-only option.
208 Tool.appendArgumentsAdjuster(new InsertAdjuster(
209 Analyze ? "--analyze" : "-fsyntax-only", InsertAdjuster::BEGIN));
210
211 clang_check::ClangCheckActionFactory CheckFactory;
212 FrontendActionFactory *FrontendFactory;
213
214 // Choose the correct factory based on the selected mode.
215 if (Analyze)
216 FrontendFactory = newFrontendActionFactory<clang::ento::AnalysisAction>();
217 else if (Fixit)
218 FrontendFactory = newFrontendActionFactory<FixItAction>();
219 else
220 FrontendFactory = newFrontendActionFactory(&CheckFactory);
221
222 return Tool.run(FrontendFactory);
Manuel Klimekcb971c62012-04-04 12:07:46 +0000223}