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
This commit is contained in:
Alexander Kornienko 2013-12-12 09:59:42 +00:00
parent 3a33f0292b
commit b5e774eb2c
4 changed files with 69 additions and 32 deletions

View File

@ -60,13 +60,18 @@ and automatic location of the compilation database using source files paths.
.. code-block:: c++
#include "clang/Tooling/CommonOptionsParser.h"
#include "llvm/Support/CommandLine.h"
using namespace clang::tooling;
// Apply a custom category to all command-line options so that they are the
// only ones displayed.
llvm::cl::OptionCategory MyToolCategory("my-tool options");
int main(int argc, const char **argv) {
// CommonOptionsParser constructor will parse arguments and create a
// CompilationDatabase. In case of error it will terminate the program.
CommonOptionsParser OptionsParser(argc, argv);
CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
// Use OptionsParser.getCompilations() and OptionsParser.getSourcePathList()
// to retrieve CompilationDatabase and the list of input file paths.
@ -115,6 +120,10 @@ tool is also checked into the clang tree at
using namespace clang::tooling;
using namespace llvm;
// Apply a custom category to all command-line options so that they are the
// only ones displayed.
cl::MyToolCategory("my-tool options");
// CommonOptionsParser declares HelpMessage with a description of the common
// command-line options related to the compilation database and input files.
// It's nice to have this help message in all tools.
@ -124,7 +133,7 @@ tool is also checked into the clang tree at
static cl::extrahelp MoreHelp("\nMore help text...");
int main(int argc, const char **argv) {
CommonOptionsParser OptionsParser(argc, argv);
CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
ClangTool Tool(OptionsParser.getCompilations(),
OptionsParser.getSourcePathList());
return Tool.run(newFrontendActionFactory<clang::SyntaxOnlyAction>());

View File

@ -28,6 +28,7 @@
#define LLVM_TOOLS_CLANG_INCLUDE_CLANG_TOOLING_COMMONOPTIONSPARSER_H
#include "clang/Tooling/CompilationDatabase.h"
#include "llvm/Support/CommandLine.h"
namespace clang {
namespace tooling {
@ -46,13 +47,14 @@ namespace tooling {
/// using namespace clang::tooling;
/// using namespace llvm;
///
/// static cl::OptionCategory MyToolCategory("My tool options");
/// static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
/// static cl::extrahelp MoreHelp("\nMore help text...");
/// static cl:opt<bool> YourOwnOption(...);
/// ...
///
/// int main(int argc, const char **argv) {
/// CommonOptionsParser OptionsParser(argc, argv);
/// CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
/// ClangTool Tool(OptionsParser.getCompilations(),
/// OptionsParser.getSourcePathListi());
/// return Tool.run(newFrontendActionFactory<clang::SyntaxOnlyAction>());
@ -61,10 +63,16 @@ namespace tooling {
class CommonOptionsParser {
public:
/// \brief Parses command-line, initializes a compilation database.
///
/// This constructor can change argc and argv contents, e.g. consume
/// command-line options used for creating FixedCompilationDatabase.
///
/// All options not belonging to \p Category become hidden.
///
/// This constructor exits program in case of error.
CommonOptionsParser(int &argc, const char **argv, const char *Overview = 0);
CommonOptionsParser(int &argc, const char **argv,
llvm::cl::OptionCategory &Category,
const char *Overview = 0);
/// Returns a reference to the loaded compilations database.
CompilationDatabase &getCompilations() {

View File

@ -54,12 +54,26 @@ const char *const CommonOptionsParser::HelpMessage =
"\n";
CommonOptionsParser::CommonOptionsParser(int &argc, const char **argv,
cl::OptionCategory &Category,
const char *Overview) {
static cl::opt<std::string> BuildPath(
"p", cl::desc("Build path"), cl::Optional);
static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden);
static cl::opt<std::string> BuildPath("p", cl::desc("Build path"),
cl::Optional, cl::cat(Category));
static cl::list<std::string> SourcePaths(
cl::Positional, cl::desc("<source0> [... <sourceN>]"), cl::OneOrMore);
cl::Positional, cl::desc("<source0> [... <sourceN>]"), cl::OneOrMore,
cl::cat(Category));
// Hide unrelated options.
StringMap<cl::Option*> Options;
cl::getRegisteredOptions(Options);
for (StringMap<cl::Option *>::iterator I = Options.begin(), E = Options.end();
I != E; ++I) {
if (I->second->Category != &Category && I->first() != "help" &&
I->first() != "version")
I->second->setHiddenFlag(cl::ReallyHidden);
}
Compilations.reset(FixedCompilationDatabase::loadFromCommandLine(argc,
argv));

View File

@ -25,7 +25,6 @@
#include "clang/Rewrite/Frontend/FrontendActions.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Signals.h"
#include "llvm/Option/OptTable.h"
@ -33,7 +32,6 @@
using namespace clang::driver;
using namespace clang::tooling;
using namespace llvm;
using namespace llvm::opt;
static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
static cl::extrahelp MoreHelp(
@ -51,34 +49,42 @@ static cl::extrahelp MoreHelp(
"\n"
);
static OwningPtr<OptTable> Options(createDriverOptTable());
static cl::opt<bool> ASTDump(
"ast-dump",
cl::desc(Options->getOptionHelpText(options::OPT_ast_dump)));
static cl::opt<bool> ASTList(
"ast-list",
cl::desc(Options->getOptionHelpText(options::OPT_ast_list)));
static cl::opt<bool> ASTPrint(
"ast-print",
cl::desc(Options->getOptionHelpText(options::OPT_ast_print)));
static cl::OptionCategory ClangCheckCategory("clang-check options");
static OwningPtr<opt::OptTable> Options(createDriverOptTable());
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));
static cl::opt<std::string> ASTDumpFilter(
"ast-dump-filter",
cl::desc(Options->getOptionHelpText(options::OPT_ast_dump_filter)));
static cl::opt<bool> Analyze(
"analyze",
cl::desc(Options->getOptionHelpText(options::OPT_analyze)));
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)));
static cl::opt<bool>
Fixit("fixit", cl::desc(Options->getOptionHelpText(options::OPT_fixit)),
cl::cat(ClangCheckCategory));
static cl::opt<bool> FixWhatYouCan(
"fix-what-you-can",
cl::desc(Options->getOptionHelpText(options::OPT_fix_what_you_can)));
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"));
static cl::list<std::string> ArgsBefore("extra-arg-before",
cl::desc("Additional argument to prepend to the compiler command line"));
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));
namespace {
@ -187,7 +193,7 @@ public:
int main(int argc, const char **argv) {
llvm::sys::PrintStackTraceOnErrorSignal();
CommonOptionsParser OptionsParser(argc, argv);
CommonOptionsParser OptionsParser(argc, argv, ClangCheckCategory);
ClangTool Tool(OptionsParser.getCompilations(),
OptionsParser.getSourcePathList());