1f4a2713aSLionel Sambuc //===--- tools/clang-check/ClangCheck.cpp - Clang check tool --------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file implements a clang-check tool that runs clang based on the info
11f4a2713aSLionel Sambuc // stored in a compilation database.
12f4a2713aSLionel Sambuc //
13f4a2713aSLionel Sambuc // This tool uses the Clang Tooling infrastructure, see
14f4a2713aSLionel Sambuc // http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html
15f4a2713aSLionel Sambuc // for details on setting it up with LLVM source tree.
16f4a2713aSLionel Sambuc //
17f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
18f4a2713aSLionel Sambuc
19f4a2713aSLionel Sambuc #include "clang/AST/ASTConsumer.h"
20f4a2713aSLionel Sambuc #include "clang/Driver/Options.h"
21f4a2713aSLionel Sambuc #include "clang/Frontend/ASTConsumers.h"
22f4a2713aSLionel Sambuc #include "clang/Frontend/CompilerInstance.h"
23f4a2713aSLionel Sambuc #include "clang/Rewrite/Frontend/FixItRewriter.h"
24f4a2713aSLionel Sambuc #include "clang/Rewrite/Frontend/FrontendActions.h"
25*0a6a1f1dSLionel Sambuc #include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
26f4a2713aSLionel Sambuc #include "clang/Tooling/CommonOptionsParser.h"
27f4a2713aSLionel Sambuc #include "clang/Tooling/Tooling.h"
28*0a6a1f1dSLionel Sambuc #include "llvm/ADT/STLExtras.h"
29*0a6a1f1dSLionel Sambuc #include "llvm/Option/OptTable.h"
30f4a2713aSLionel Sambuc #include "llvm/Support/Path.h"
31f4a2713aSLionel Sambuc #include "llvm/Support/Signals.h"
32f4a2713aSLionel Sambuc
33f4a2713aSLionel Sambuc using namespace clang::driver;
34f4a2713aSLionel Sambuc using namespace clang::tooling;
35f4a2713aSLionel Sambuc using namespace llvm;
36f4a2713aSLionel Sambuc
37f4a2713aSLionel Sambuc static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
38f4a2713aSLionel Sambuc static cl::extrahelp MoreHelp(
39f4a2713aSLionel Sambuc "\tFor example, to run clang-check on all files in a subtree of the\n"
40f4a2713aSLionel Sambuc "\tsource tree, use:\n"
41f4a2713aSLionel Sambuc "\n"
42f4a2713aSLionel Sambuc "\t find path/in/subtree -name '*.cpp'|xargs clang-check\n"
43f4a2713aSLionel Sambuc "\n"
44f4a2713aSLionel Sambuc "\tor using a specific build path:\n"
45f4a2713aSLionel Sambuc "\n"
46f4a2713aSLionel Sambuc "\t find path/in/subtree -name '*.cpp'|xargs clang-check -p build/path\n"
47f4a2713aSLionel Sambuc "\n"
48f4a2713aSLionel Sambuc "\tNote, that path/in/subtree and current directory should follow the\n"
49f4a2713aSLionel Sambuc "\trules described above.\n"
50f4a2713aSLionel Sambuc "\n"
51f4a2713aSLionel Sambuc );
52f4a2713aSLionel Sambuc
53*0a6a1f1dSLionel Sambuc static cl::OptionCategory ClangCheckCategory("clang-check options");
54*0a6a1f1dSLionel Sambuc static std::unique_ptr<opt::OptTable> Options(createDriverOptTable());
55*0a6a1f1dSLionel Sambuc static cl::opt<bool>
56*0a6a1f1dSLionel Sambuc ASTDump("ast-dump", cl::desc(Options->getOptionHelpText(options::OPT_ast_dump)),
57*0a6a1f1dSLionel Sambuc cl::cat(ClangCheckCategory));
58*0a6a1f1dSLionel Sambuc static cl::opt<bool>
59*0a6a1f1dSLionel Sambuc ASTList("ast-list", cl::desc(Options->getOptionHelpText(options::OPT_ast_list)),
60*0a6a1f1dSLionel Sambuc cl::cat(ClangCheckCategory));
61*0a6a1f1dSLionel Sambuc static cl::opt<bool>
62*0a6a1f1dSLionel Sambuc ASTPrint("ast-print",
63*0a6a1f1dSLionel Sambuc cl::desc(Options->getOptionHelpText(options::OPT_ast_print)),
64*0a6a1f1dSLionel Sambuc cl::cat(ClangCheckCategory));
65f4a2713aSLionel Sambuc static cl::opt<std::string> ASTDumpFilter(
66f4a2713aSLionel Sambuc "ast-dump-filter",
67*0a6a1f1dSLionel Sambuc cl::desc(Options->getOptionHelpText(options::OPT_ast_dump_filter)),
68*0a6a1f1dSLionel Sambuc cl::cat(ClangCheckCategory));
69*0a6a1f1dSLionel Sambuc static cl::opt<bool>
70*0a6a1f1dSLionel Sambuc Analyze("analyze", cl::desc(Options->getOptionHelpText(options::OPT_analyze)),
71*0a6a1f1dSLionel Sambuc cl::cat(ClangCheckCategory));
72f4a2713aSLionel Sambuc
73*0a6a1f1dSLionel Sambuc static cl::opt<bool>
74*0a6a1f1dSLionel Sambuc Fixit("fixit", cl::desc(Options->getOptionHelpText(options::OPT_fixit)),
75*0a6a1f1dSLionel Sambuc cl::cat(ClangCheckCategory));
76f4a2713aSLionel Sambuc static cl::opt<bool> FixWhatYouCan(
77f4a2713aSLionel Sambuc "fix-what-you-can",
78*0a6a1f1dSLionel Sambuc cl::desc(Options->getOptionHelpText(options::OPT_fix_what_you_can)),
79*0a6a1f1dSLionel Sambuc cl::cat(ClangCheckCategory));
80f4a2713aSLionel Sambuc
81f4a2713aSLionel Sambuc namespace {
82f4a2713aSLionel Sambuc
83f4a2713aSLionel Sambuc // FIXME: Move FixItRewriteInPlace from lib/Rewrite/Frontend/FrontendActions.cpp
84f4a2713aSLionel Sambuc // into a header file and reuse that.
85f4a2713aSLionel Sambuc class FixItOptions : public clang::FixItOptions {
86f4a2713aSLionel Sambuc public:
FixItOptions()87f4a2713aSLionel Sambuc FixItOptions() {
88f4a2713aSLionel Sambuc FixWhatYouCan = ::FixWhatYouCan;
89f4a2713aSLionel Sambuc }
90f4a2713aSLionel Sambuc
RewriteFilename(const std::string & filename,int & fd)91*0a6a1f1dSLionel Sambuc std::string RewriteFilename(const std::string& filename, int &fd) override {
92f4a2713aSLionel Sambuc assert(llvm::sys::path::is_absolute(filename) &&
93f4a2713aSLionel Sambuc "clang-fixit expects absolute paths only.");
94f4a2713aSLionel Sambuc
95f4a2713aSLionel Sambuc // We don't need to do permission checking here since clang will diagnose
96f4a2713aSLionel Sambuc // any I/O errors itself.
97f4a2713aSLionel Sambuc
98f4a2713aSLionel Sambuc fd = -1; // No file descriptor for file.
99f4a2713aSLionel Sambuc
100f4a2713aSLionel Sambuc return filename;
101f4a2713aSLionel Sambuc }
102f4a2713aSLionel Sambuc };
103f4a2713aSLionel Sambuc
104f4a2713aSLionel Sambuc /// \brief Subclasses \c clang::FixItRewriter to not count fixed errors/warnings
105f4a2713aSLionel Sambuc /// in the final error counts.
106f4a2713aSLionel Sambuc ///
107f4a2713aSLionel Sambuc /// This has the side-effect that clang-check -fixit exits with code 0 on
108f4a2713aSLionel Sambuc /// successfully fixing all errors.
109f4a2713aSLionel Sambuc class FixItRewriter : public clang::FixItRewriter {
110f4a2713aSLionel Sambuc public:
FixItRewriter(clang::DiagnosticsEngine & Diags,clang::SourceManager & SourceMgr,const clang::LangOptions & LangOpts,clang::FixItOptions * FixItOpts)111f4a2713aSLionel Sambuc FixItRewriter(clang::DiagnosticsEngine& Diags,
112f4a2713aSLionel Sambuc clang::SourceManager& SourceMgr,
113f4a2713aSLionel Sambuc const clang::LangOptions& LangOpts,
114f4a2713aSLionel Sambuc clang::FixItOptions* FixItOpts)
115f4a2713aSLionel Sambuc : clang::FixItRewriter(Diags, SourceMgr, LangOpts, FixItOpts) {
116f4a2713aSLionel Sambuc }
117f4a2713aSLionel Sambuc
IncludeInDiagnosticCounts() const118*0a6a1f1dSLionel Sambuc bool IncludeInDiagnosticCounts() const override { return false; }
119f4a2713aSLionel Sambuc };
120f4a2713aSLionel Sambuc
121f4a2713aSLionel Sambuc /// \brief Subclasses \c clang::FixItAction so that we can install the custom
122f4a2713aSLionel Sambuc /// \c FixItRewriter.
123f4a2713aSLionel Sambuc class FixItAction : public clang::FixItAction {
124f4a2713aSLionel Sambuc public:
BeginSourceFileAction(clang::CompilerInstance & CI,StringRef Filename)125*0a6a1f1dSLionel Sambuc bool BeginSourceFileAction(clang::CompilerInstance& CI,
126*0a6a1f1dSLionel Sambuc StringRef Filename) override {
127f4a2713aSLionel Sambuc FixItOpts.reset(new FixItOptions);
128f4a2713aSLionel Sambuc Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(),
129f4a2713aSLionel Sambuc CI.getLangOpts(), FixItOpts.get()));
130f4a2713aSLionel Sambuc return true;
131f4a2713aSLionel Sambuc }
132f4a2713aSLionel Sambuc };
133f4a2713aSLionel Sambuc
134*0a6a1f1dSLionel Sambuc class ClangCheckActionFactory {
135f4a2713aSLionel Sambuc public:
newASTConsumer()136*0a6a1f1dSLionel Sambuc std::unique_ptr<clang::ASTConsumer> newASTConsumer() {
137*0a6a1f1dSLionel Sambuc if (ASTList)
138*0a6a1f1dSLionel Sambuc return clang::CreateASTDeclNodeLister();
139*0a6a1f1dSLionel Sambuc if (ASTDump)
140*0a6a1f1dSLionel Sambuc return clang::CreateASTDumper(ASTDumpFilter, /*DumpDecls=*/true,
141*0a6a1f1dSLionel Sambuc /*DumpLookups=*/false);
142*0a6a1f1dSLionel Sambuc if (ASTPrint)
143*0a6a1f1dSLionel Sambuc return clang::CreateASTPrinter(&llvm::outs(), ASTDumpFilter);
144*0a6a1f1dSLionel Sambuc return llvm::make_unique<clang::ASTConsumer>();
145f4a2713aSLionel Sambuc }
146f4a2713aSLionel Sambuc };
147f4a2713aSLionel Sambuc
148f4a2713aSLionel Sambuc } // namespace
149f4a2713aSLionel Sambuc
main(int argc,const char ** argv)150f4a2713aSLionel Sambuc int main(int argc, const char **argv) {
151f4a2713aSLionel Sambuc llvm::sys::PrintStackTraceOnErrorSignal();
152*0a6a1f1dSLionel Sambuc CommonOptionsParser OptionsParser(argc, argv, ClangCheckCategory);
153f4a2713aSLionel Sambuc ClangTool Tool(OptionsParser.getCompilations(),
154f4a2713aSLionel Sambuc OptionsParser.getSourcePathList());
155f4a2713aSLionel Sambuc
156f4a2713aSLionel Sambuc // Clear adjusters because -fsyntax-only is inserted by the default chain.
157f4a2713aSLionel Sambuc Tool.clearArgumentsAdjusters();
158*0a6a1f1dSLionel Sambuc Tool.appendArgumentsAdjuster(getClangStripOutputAdjuster());
159f4a2713aSLionel Sambuc
160f4a2713aSLionel Sambuc // Running the analyzer requires --analyze. Other modes can work with the
161f4a2713aSLionel Sambuc // -fsyntax-only option.
162*0a6a1f1dSLionel Sambuc Tool.appendArgumentsAdjuster(getInsertArgumentAdjuster(
163*0a6a1f1dSLionel Sambuc Analyze ? "--analyze" : "-fsyntax-only", ArgumentInsertPosition::BEGIN));
164f4a2713aSLionel Sambuc
165*0a6a1f1dSLionel Sambuc ClangCheckActionFactory CheckFactory;
166*0a6a1f1dSLionel Sambuc std::unique_ptr<FrontendActionFactory> FrontendFactory;
167f4a2713aSLionel Sambuc
168f4a2713aSLionel Sambuc // Choose the correct factory based on the selected mode.
169f4a2713aSLionel Sambuc if (Analyze)
170f4a2713aSLionel Sambuc FrontendFactory = newFrontendActionFactory<clang::ento::AnalysisAction>();
171f4a2713aSLionel Sambuc else if (Fixit)
172f4a2713aSLionel Sambuc FrontendFactory = newFrontendActionFactory<FixItAction>();
173f4a2713aSLionel Sambuc else
174f4a2713aSLionel Sambuc FrontendFactory = newFrontendActionFactory(&CheckFactory);
175f4a2713aSLionel Sambuc
176*0a6a1f1dSLionel Sambuc return Tool.run(FrontendFactory.get());
177f4a2713aSLionel Sambuc }
178