2012-08-23 04:52:52 +08:00
|
|
|
//===--- tools/clang-check/ClangCheck.cpp - Clang check tool --------------===//
|
2012-04-04 20:07:46 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2012-10-03 21:28:43 +08:00
|
|
|
// This file implements a clang-check tool that runs clang based on the info
|
|
|
|
// stored in a compilation database.
|
2012-04-04 20:07:46 +08:00
|
|
|
//
|
2012-07-16 20:46:48 +08:00
|
|
|
// This tool uses the Clang Tooling infrastructure, see
|
|
|
|
// http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html
|
|
|
|
// for details on setting it up with LLVM source tree.
|
|
|
|
//
|
2012-04-04 20:07:46 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-08-13 18:50:08 +08:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
|
|
|
#include "clang/Driver/Options.h"
|
|
|
|
#include "clang/Frontend/ASTConsumers.h"
|
2012-10-03 21:28:43 +08:00
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Rewrite/Frontend/FixItRewriter.h"
|
|
|
|
#include "clang/Rewrite/Frontend/FrontendActions.h"
|
2014-01-07 19:51:46 +08:00
|
|
|
#include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
|
2012-08-23 04:52:52 +08:00
|
|
|
#include "clang/Tooling/CommonOptionsParser.h"
|
2012-04-04 20:07:46 +08:00
|
|
|
#include "clang/Tooling/Tooling.h"
|
2014-01-07 19:51:46 +08:00
|
|
|
#include "llvm/Option/OptTable.h"
|
2012-10-03 21:28:43 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2013-01-18 21:46:48 +08:00
|
|
|
#include "llvm/Support/Signals.h"
|
2012-04-04 20:07:46 +08:00
|
|
|
|
2012-08-13 18:50:08 +08:00
|
|
|
using namespace clang::driver;
|
2012-04-04 20:07:46 +08:00
|
|
|
using namespace clang::tooling;
|
|
|
|
using namespace llvm;
|
|
|
|
|
2012-08-24 08:39:14 +08:00
|
|
|
static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
|
2012-08-23 04:52:52 +08:00
|
|
|
static cl::extrahelp MoreHelp(
|
2012-07-16 20:46:48 +08:00
|
|
|
"\tFor example, to run clang-check on all files in a subtree of the\n"
|
|
|
|
"\tsource tree, use:\n"
|
2012-07-12 22:34:23 +08:00
|
|
|
"\n"
|
2012-07-16 20:46:48 +08:00
|
|
|
"\t find path/in/subtree -name '*.cpp'|xargs clang-check\n"
|
2012-07-12 22:34:23 +08:00
|
|
|
"\n"
|
2012-07-16 20:46:48 +08:00
|
|
|
"\tor using a specific build path:\n"
|
2012-07-12 22:34:23 +08:00
|
|
|
"\n"
|
2012-07-16 20:46:48 +08:00
|
|
|
"\t find path/in/subtree -name '*.cpp'|xargs clang-check -p build/path\n"
|
2012-07-12 22:34:23 +08:00
|
|
|
"\n"
|
2012-07-16 20:46:48 +08:00
|
|
|
"\tNote, that path/in/subtree and current directory should follow the\n"
|
|
|
|
"\trules described above.\n"
|
2012-08-23 04:52:52 +08:00
|
|
|
"\n"
|
|
|
|
);
|
|
|
|
|
Filter-out irrelevant command-line options in CommonOptionsParser.
Summary:
Leave only -help, -version and options from the specified category.
Updated clang-check and clang-tidy. As clang-tidy is in a separate repository,
here's the diff:
Index: tools/extra/clang-tidy/tool/ClangTidyMain.cpp
===================================================================
--- tools/extra/clang-tidy/tool/ClangTidyMain.cpp (revision 197024)
+++ tools/extra/clang-tidy/tool/ClangTidyMain.cpp (working copy)
@@ -39,7 +39,7 @@
// FIXME: Add option to list name/description of all checks.
int main(int argc, const char **argv) {
- CommonOptionsParser OptionsParser(argc, argv);
+ CommonOptionsParser OptionsParser(argc, argv, ClangTidyCategory);
SmallVector<clang::tidy::ClangTidyError, 16> Errors;
clang::tidy::runClangTidy(Checks, OptionsParser.getCompilations(),
Reviewers: djasper, klimek
Reviewed By: djasper
CC: cfe-commits, revane, klimek
Differential Revision: http://llvm-reviews.chandlerc.com/D2379
llvm-svn: 197139
2013-12-12 17:59:42 +08:00
|
|
|
static cl::OptionCategory ClangCheckCategory("clang-check options");
|
2014-03-08 04:03:18 +08:00
|
|
|
static std::unique_ptr<opt::OptTable> Options(createDriverOptTable());
|
Filter-out irrelevant command-line options in CommonOptionsParser.
Summary:
Leave only -help, -version and options from the specified category.
Updated clang-check and clang-tidy. As clang-tidy is in a separate repository,
here's the diff:
Index: tools/extra/clang-tidy/tool/ClangTidyMain.cpp
===================================================================
--- tools/extra/clang-tidy/tool/ClangTidyMain.cpp (revision 197024)
+++ tools/extra/clang-tidy/tool/ClangTidyMain.cpp (working copy)
@@ -39,7 +39,7 @@
// FIXME: Add option to list name/description of all checks.
int main(int argc, const char **argv) {
- CommonOptionsParser OptionsParser(argc, argv);
+ CommonOptionsParser OptionsParser(argc, argv, ClangTidyCategory);
SmallVector<clang::tidy::ClangTidyError, 16> Errors;
clang::tidy::runClangTidy(Checks, OptionsParser.getCompilations(),
Reviewers: djasper, klimek
Reviewed By: djasper
CC: cfe-commits, revane, klimek
Differential Revision: http://llvm-reviews.chandlerc.com/D2379
llvm-svn: 197139
2013-12-12 17:59:42 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
ASTDump("ast-dump", cl::desc(Options->getOptionHelpText(options::OPT_ast_dump)),
|
|
|
|
cl::cat(ClangCheckCategory));
|
|
|
|
static cl::opt<bool>
|
|
|
|
ASTList("ast-list", cl::desc(Options->getOptionHelpText(options::OPT_ast_list)),
|
|
|
|
cl::cat(ClangCheckCategory));
|
|
|
|
static cl::opt<bool>
|
|
|
|
ASTPrint("ast-print",
|
|
|
|
cl::desc(Options->getOptionHelpText(options::OPT_ast_print)),
|
|
|
|
cl::cat(ClangCheckCategory));
|
2012-08-23 04:52:52 +08:00
|
|
|
static cl::opt<std::string> ASTDumpFilter(
|
|
|
|
"ast-dump-filter",
|
Filter-out irrelevant command-line options in CommonOptionsParser.
Summary:
Leave only -help, -version and options from the specified category.
Updated clang-check and clang-tidy. As clang-tidy is in a separate repository,
here's the diff:
Index: tools/extra/clang-tidy/tool/ClangTidyMain.cpp
===================================================================
--- tools/extra/clang-tidy/tool/ClangTidyMain.cpp (revision 197024)
+++ tools/extra/clang-tidy/tool/ClangTidyMain.cpp (working copy)
@@ -39,7 +39,7 @@
// FIXME: Add option to list name/description of all checks.
int main(int argc, const char **argv) {
- CommonOptionsParser OptionsParser(argc, argv);
+ CommonOptionsParser OptionsParser(argc, argv, ClangTidyCategory);
SmallVector<clang::tidy::ClangTidyError, 16> Errors;
clang::tidy::runClangTidy(Checks, OptionsParser.getCompilations(),
Reviewers: djasper, klimek
Reviewed By: djasper
CC: cfe-commits, revane, klimek
Differential Revision: http://llvm-reviews.chandlerc.com/D2379
llvm-svn: 197139
2013-12-12 17:59:42 +08:00
|
|
|
cl::desc(Options->getOptionHelpText(options::OPT_ast_dump_filter)),
|
|
|
|
cl::cat(ClangCheckCategory));
|
|
|
|
static cl::opt<bool>
|
|
|
|
Analyze("analyze", cl::desc(Options->getOptionHelpText(options::OPT_analyze)),
|
|
|
|
cl::cat(ClangCheckCategory));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
Fixit("fixit", cl::desc(Options->getOptionHelpText(options::OPT_fixit)),
|
|
|
|
cl::cat(ClangCheckCategory));
|
2012-10-03 21:28:43 +08:00
|
|
|
static cl::opt<bool> FixWhatYouCan(
|
|
|
|
"fix-what-you-can",
|
Filter-out irrelevant command-line options in CommonOptionsParser.
Summary:
Leave only -help, -version and options from the specified category.
Updated clang-check and clang-tidy. As clang-tidy is in a separate repository,
here's the diff:
Index: tools/extra/clang-tidy/tool/ClangTidyMain.cpp
===================================================================
--- tools/extra/clang-tidy/tool/ClangTidyMain.cpp (revision 197024)
+++ tools/extra/clang-tidy/tool/ClangTidyMain.cpp (working copy)
@@ -39,7 +39,7 @@
// FIXME: Add option to list name/description of all checks.
int main(int argc, const char **argv) {
- CommonOptionsParser OptionsParser(argc, argv);
+ CommonOptionsParser OptionsParser(argc, argv, ClangTidyCategory);
SmallVector<clang::tidy::ClangTidyError, 16> Errors;
clang::tidy::runClangTidy(Checks, OptionsParser.getCompilations(),
Reviewers: djasper, klimek
Reviewed By: djasper
CC: cfe-commits, revane, klimek
Differential Revision: http://llvm-reviews.chandlerc.com/D2379
llvm-svn: 197139
2013-12-12 17:59:42 +08:00
|
|
|
cl::desc(Options->getOptionHelpText(options::OPT_fix_what_you_can)),
|
|
|
|
cl::cat(ClangCheckCategory));
|
|
|
|
|
|
|
|
static cl::list<std::string> ArgsAfter(
|
|
|
|
"extra-arg",
|
|
|
|
cl::desc("Additional argument to append to the compiler command line"),
|
|
|
|
cl::cat(ClangCheckCategory));
|
|
|
|
static cl::list<std::string> ArgsBefore(
|
|
|
|
"extra-arg-before",
|
|
|
|
cl::desc("Additional argument to prepend to the compiler command line"),
|
|
|
|
cl::cat(ClangCheckCategory));
|
2013-06-06 00:23:30 +08:00
|
|
|
|
2012-10-03 21:28:43 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// FIXME: Move FixItRewriteInPlace from lib/Rewrite/Frontend/FrontendActions.cpp
|
|
|
|
// into a header file and reuse that.
|
|
|
|
class FixItOptions : public clang::FixItOptions {
|
|
|
|
public:
|
|
|
|
FixItOptions() {
|
|
|
|
FixWhatYouCan = ::FixWhatYouCan;
|
|
|
|
}
|
|
|
|
|
2014-03-15 15:47:46 +08:00
|
|
|
std::string RewriteFilename(const std::string& filename, int &fd) override {
|
2012-10-03 21:28:43 +08:00
|
|
|
assert(llvm::sys::path::is_absolute(filename) &&
|
|
|
|
"clang-fixit expects absolute paths only.");
|
|
|
|
|
|
|
|
// We don't need to do permission checking here since clang will diagnose
|
|
|
|
// any I/O errors itself.
|
|
|
|
|
|
|
|
fd = -1; // No file descriptor for file.
|
|
|
|
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Subclasses \c clang::FixItRewriter to not count fixed errors/warnings
|
|
|
|
/// in the final error counts.
|
|
|
|
///
|
|
|
|
/// This has the side-effect that clang-check -fixit exits with code 0 on
|
|
|
|
/// successfully fixing all errors.
|
|
|
|
class FixItRewriter : public clang::FixItRewriter {
|
|
|
|
public:
|
|
|
|
FixItRewriter(clang::DiagnosticsEngine& Diags,
|
|
|
|
clang::SourceManager& SourceMgr,
|
|
|
|
const clang::LangOptions& LangOpts,
|
|
|
|
clang::FixItOptions* FixItOpts)
|
|
|
|
: clang::FixItRewriter(Diags, SourceMgr, LangOpts, FixItOpts) {
|
|
|
|
}
|
|
|
|
|
2014-03-15 15:47:46 +08:00
|
|
|
bool IncludeInDiagnosticCounts() const override { return false; }
|
2012-10-03 21:28:43 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Subclasses \c clang::FixItAction so that we can install the custom
|
|
|
|
/// \c FixItRewriter.
|
|
|
|
class FixItAction : public clang::FixItAction {
|
|
|
|
public:
|
2014-03-15 15:47:46 +08:00
|
|
|
bool BeginSourceFileAction(clang::CompilerInstance& CI,
|
|
|
|
StringRef Filename) override {
|
2012-10-03 21:28:43 +08:00
|
|
|
FixItOpts.reset(new FixItOptions);
|
|
|
|
Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(),
|
|
|
|
CI.getLangOpts(), FixItOpts.get()));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-06-06 00:23:30 +08:00
|
|
|
class InsertAdjuster: public clang::tooling::ArgumentsAdjuster {
|
|
|
|
public:
|
|
|
|
enum Position { BEGIN, END };
|
|
|
|
|
|
|
|
InsertAdjuster(const CommandLineArguments &Extra, Position Pos)
|
|
|
|
: Extra(Extra), Pos(Pos) {
|
|
|
|
}
|
|
|
|
|
2013-06-06 20:35:43 +08:00
|
|
|
InsertAdjuster(const char *Extra, Position Pos)
|
|
|
|
: Extra(1, std::string(Extra)), Pos(Pos) {
|
|
|
|
}
|
|
|
|
|
2013-06-06 00:23:30 +08:00
|
|
|
virtual CommandLineArguments
|
2014-03-02 17:32:10 +08:00
|
|
|
Adjust(const CommandLineArguments &Args) override {
|
2013-06-06 00:23:30 +08:00
|
|
|
CommandLineArguments Return(Args);
|
|
|
|
|
|
|
|
CommandLineArguments::iterator I;
|
|
|
|
if (Pos == END) {
|
|
|
|
I = Return.end();
|
|
|
|
} else {
|
|
|
|
I = Return.begin();
|
|
|
|
++I; // To leave the program name in place
|
|
|
|
}
|
|
|
|
|
|
|
|
Return.insert(I, Extra.begin(), Extra.end());
|
|
|
|
return Return;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const CommandLineArguments Extra;
|
|
|
|
const Position Pos;
|
|
|
|
};
|
|
|
|
|
2012-10-03 21:28:43 +08:00
|
|
|
} // namespace
|
|
|
|
|
2012-09-10 22:54:38 +08:00
|
|
|
// Anonymous namespace here causes problems with gcc <= 4.4 on MacOS 10.6.
|
|
|
|
// "Non-global symbol: ... can't be a weak_definition"
|
|
|
|
namespace clang_check {
|
|
|
|
class ClangCheckActionFactory {
|
2012-08-13 18:50:08 +08:00
|
|
|
public:
|
2014-07-18 06:34:12 +08:00
|
|
|
clang::ASTConsumer *newASTConsumer() {
|
2012-08-13 18:50:08 +08:00
|
|
|
if (ASTList)
|
|
|
|
return clang::CreateASTDeclNodeLister();
|
|
|
|
if (ASTDump)
|
|
|
|
return clang::CreateASTDumper(ASTDumpFilter);
|
|
|
|
if (ASTPrint)
|
|
|
|
return clang::CreateASTPrinter(&llvm::outs(), ASTDumpFilter);
|
2014-07-18 06:34:12 +08:00
|
|
|
return new clang::ASTConsumer();
|
2012-08-13 18:50:08 +08:00
|
|
|
}
|
|
|
|
};
|
2012-09-10 22:54:38 +08:00
|
|
|
}
|
2012-08-13 18:50:08 +08:00
|
|
|
|
2012-04-18 15:41:50 +08:00
|
|
|
int main(int argc, const char **argv) {
|
2013-01-18 21:46:48 +08:00
|
|
|
llvm::sys::PrintStackTraceOnErrorSignal();
|
Filter-out irrelevant command-line options in CommonOptionsParser.
Summary:
Leave only -help, -version and options from the specified category.
Updated clang-check and clang-tidy. As clang-tidy is in a separate repository,
here's the diff:
Index: tools/extra/clang-tidy/tool/ClangTidyMain.cpp
===================================================================
--- tools/extra/clang-tidy/tool/ClangTidyMain.cpp (revision 197024)
+++ tools/extra/clang-tidy/tool/ClangTidyMain.cpp (working copy)
@@ -39,7 +39,7 @@
// FIXME: Add option to list name/description of all checks.
int main(int argc, const char **argv) {
- CommonOptionsParser OptionsParser(argc, argv);
+ CommonOptionsParser OptionsParser(argc, argv, ClangTidyCategory);
SmallVector<clang::tidy::ClangTidyError, 16> Errors;
clang::tidy::runClangTidy(Checks, OptionsParser.getCompilations(),
Reviewers: djasper, klimek
Reviewed By: djasper
CC: cfe-commits, revane, klimek
Differential Revision: http://llvm-reviews.chandlerc.com/D2379
llvm-svn: 197139
2013-12-12 17:59:42 +08:00
|
|
|
CommonOptionsParser OptionsParser(argc, argv, ClangCheckCategory);
|
2012-12-15 02:58:25 +08:00
|
|
|
ClangTool Tool(OptionsParser.getCompilations(),
|
|
|
|
OptionsParser.getSourcePathList());
|
2013-06-06 00:23:30 +08:00
|
|
|
|
2013-06-06 20:35:43 +08:00
|
|
|
// Clear adjusters because -fsyntax-only is inserted by the default chain.
|
|
|
|
Tool.clearArgumentsAdjusters();
|
|
|
|
Tool.appendArgumentsAdjuster(new ClangStripOutputAdjuster());
|
|
|
|
if (ArgsAfter.size() > 0) {
|
|
|
|
Tool.appendArgumentsAdjuster(new InsertAdjuster(ArgsAfter,
|
|
|
|
InsertAdjuster::END));
|
|
|
|
}
|
|
|
|
if (ArgsBefore.size() > 0) {
|
|
|
|
Tool.appendArgumentsAdjuster(new InsertAdjuster(ArgsBefore,
|
|
|
|
InsertAdjuster::BEGIN));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Running the analyzer requires --analyze. Other modes can work with the
|
|
|
|
// -fsyntax-only option.
|
|
|
|
Tool.appendArgumentsAdjuster(new InsertAdjuster(
|
|
|
|
Analyze ? "--analyze" : "-fsyntax-only", InsertAdjuster::BEGIN));
|
|
|
|
|
|
|
|
clang_check::ClangCheckActionFactory CheckFactory;
|
2014-04-28 12:57:14 +08:00
|
|
|
std::unique_ptr<FrontendActionFactory> FrontendFactory;
|
2013-06-06 20:35:43 +08:00
|
|
|
|
|
|
|
// Choose the correct factory based on the selected mode.
|
|
|
|
if (Analyze)
|
|
|
|
FrontendFactory = newFrontendActionFactory<clang::ento::AnalysisAction>();
|
|
|
|
else if (Fixit)
|
|
|
|
FrontendFactory = newFrontendActionFactory<FixItAction>();
|
|
|
|
else
|
|
|
|
FrontendFactory = newFrontendActionFactory(&CheckFactory);
|
2013-06-06 00:23:30 +08:00
|
|
|
|
2014-04-28 12:57:14 +08:00
|
|
|
return Tool.run(FrontendFactory.get());
|
2012-04-04 20:07:46 +08:00
|
|
|
}
|