From d7166b01df14173b7f26620c5a89f9934a968f85 Mon Sep 17 00:00:00 2001 From: Alexander Kornienko Date: Wed, 22 Aug 2012 20:52:52 +0000 Subject: [PATCH] Reverted clang-check to fully supported CommandLine Library use-case: global static variables. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@162391 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Tooling/CommandLineClangTool.h | 80 ----------------- include/clang/Tooling/CommonOptionsParser.h | 90 +++++++++++++++++++ lib/Tooling/CMakeLists.txt | 2 +- ...eClangTool.cpp => CommonOptionsParser.cpp} | 61 +++++++------ tools/clang-check/ClangCheck.cpp | 54 +++++------ 5 files changed, 145 insertions(+), 142 deletions(-) delete mode 100644 include/clang/Tooling/CommandLineClangTool.h create mode 100644 include/clang/Tooling/CommonOptionsParser.h rename lib/Tooling/{CommandLineClangTool.cpp => CommonOptionsParser.cpp} (52%) diff --git a/include/clang/Tooling/CommandLineClangTool.h b/include/clang/Tooling/CommandLineClangTool.h deleted file mode 100644 index c29c302364..0000000000 --- a/include/clang/Tooling/CommandLineClangTool.h +++ /dev/null @@ -1,80 +0,0 @@ -//===- CommandLineClangTool.h - command-line clang tools driver -*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file implements the CommandLineClangTool class used to run clang -// tools as separate command-line applications with a consistent common -// interface for handling compilation database and input files. -// -// It provides a common subset of command-line options, common algorithm -// for locating a compilation database and source files, and help messages -// for the basic command-line interface. -// -// It creates a CompilationDatabase, initializes a ClangTool and runs a -// user-specified FrontendAction over all TUs in which the given files are -// compiled. -// -// This class uses the Clang Tooling infrastructure, see -// http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html -// for details on setting it up with LLVM source tree. -// -//===----------------------------------------------------------------------===// - -#ifndef LLVM_TOOLS_CLANG_INCLUDE_CLANG_TOOLING_COMMANDLINECLANGTOOL_H -#define LLVM_TOOLS_CLANG_INCLUDE_CLANG_TOOLING_COMMANDLINECLANGTOOL_H - -#include "llvm/Support/CommandLine.h" -#include "clang/Tooling/CompilationDatabase.h" - -namespace clang { - -namespace tooling { - -class CompilationDatabase; -class FrontendActionFactory; - -/// \brief A common driver for command-line Clang tools. -/// -/// Parses a common subset of command-line arguments, locates and loads a -/// compilation commands database, runs a tool with user-specified action. It -/// also contains a help message for the common command-line options. -/// An example of usage: -/// @code -/// int main(int argc, const char **argv) { -/// CommandLineClangTool Tool; -/// cl::extrahelp MoreHelp("\nMore help text..."); -/// Tool.initialize(argc, argv); -/// return Tool.run(newFrontendActionFactory()); -/// } -/// @endcode -/// -class CommandLineClangTool { -public: - /// Sets up command-line options and help messages. - /// Add your own help messages after constructing this tool. - CommandLineClangTool(); - - /// Parses command-line, initializes a compilation database. - /// This method exits program in case of error. - void initialize(int argc, const char **argv); - - /// Runs a clang tool with an action created by \c ActionFactory. - int run(FrontendActionFactory *ActionFactory); - -private: - llvm::OwningPtr Compilations; - llvm::cl::opt BuildPath; - llvm::cl::list SourcePaths; - llvm::cl::extrahelp MoreHelp; -}; - -} // namespace tooling - -} // namespace clang - -#endif // LLVM_TOOLS_CLANG_INCLUDE_CLANG_TOOLING_COMMANDLINECLANGTOOL_H diff --git a/include/clang/Tooling/CommonOptionsParser.h b/include/clang/Tooling/CommonOptionsParser.h new file mode 100644 index 0000000000..9eed3d3229 --- /dev/null +++ b/include/clang/Tooling/CommonOptionsParser.h @@ -0,0 +1,90 @@ +//===- CommonOptionsParser.h - common options for clang tools -*- C++ -*-=====// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file implements the CommonOptionsParser class used to parse common +// command-line options for clang tools, so that they can be run as separate +// command-line applications with a consistent common interface for handling +// compilation database and input files. +// +// It provides a common subset of command-line options, common algorithm +// for locating a compilation database and source files, and help messages +// for the basic command-line interface. +// +// It creates a CompilationDatabase and reads common command-line options. +// +// This class uses the Clang Tooling infrastructure, see +// http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html +// for details on setting it up with LLVM source tree. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_TOOLS_CLANG_INCLUDE_CLANG_TOOLING_COMMONOPTIONSPARSER_H +#define LLVM_TOOLS_CLANG_INCLUDE_CLANG_TOOLING_COMMONOPTIONSPARSER_H + +#include "clang/Tooling/CompilationDatabase.h" +#include "clang/Frontend/FrontendActions.h" + +namespace clang { +namespace tooling { + +extern const char *const CommonHelpMessage; + +/// \brief A parser for options common to all command-line Clang tools. +/// +/// Parses a common subset of command-line arguments, locates and loads a +/// compilation commands database and runs a tool with user-specified action. It +/// also contains a help message for the common command-line options. +/// +/// An example of usage: +/// \code +/// #include "llvm/Support/CommandLine.h" +/// #include "clang/Tooling/CommonOptionsParser.h" +/// +/// using namespace clang::tooling; +/// using namespace llvm; +/// +/// static cl::extrahelp CommonHelp(CommonHelpMessage); +/// static cl::extrahelp MoreHelp("\nMore help text..."); +/// static cl:opt YourOwnOption(...); +/// ... +/// +/// int main(int argc, const char **argv) { +/// CommonOptionsParser OptionsParser(argc, argv); +/// ClangTool Tool(OptionsParser.GetCompilations(), +/// OptionsParser.GetSourcePathListi()); +/// return Tool.run(newFrontendActionFactory()); +/// } +/// \endcode +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. + /// This constructor exits program in case of error. + CommonOptionsParser(int &argc, const char **argv); + + /// Returns a reference to the loaded compilations database. + CompilationDatabase &GetCompilations() { + return *Compilations; + } + + /// Returns a list of source file paths to process. + std::vector GetSourcePathList() { + return SourcePathList; + } + +private: + llvm::OwningPtr Compilations; + std::vector SourcePathList; +}; + +} // namespace tooling +} // namespace clang + +#endif // LLVM_TOOLS_CLANG_INCLUDE_CLANG_TOOLING_COMMONOPTIONSPARSER_H diff --git a/lib/Tooling/CMakeLists.txt b/lib/Tooling/CMakeLists.txt index 49d3101f0e..750dcc88a8 100644 --- a/lib/Tooling/CMakeLists.txt +++ b/lib/Tooling/CMakeLists.txt @@ -2,7 +2,7 @@ set(LLVM_LINK_COMPONENTS support) add_clang_library(clangTooling ArgumentsAdjusters.cpp - CommandLineClangTool.cpp + CommonOptionsParser.cpp CompilationDatabase.cpp Refactoring.cpp RefactoringCallbacks.cpp diff --git a/lib/Tooling/CommandLineClangTool.cpp b/lib/Tooling/CommonOptionsParser.cpp similarity index 52% rename from lib/Tooling/CommandLineClangTool.cpp rename to lib/Tooling/CommonOptionsParser.cpp index 8da2a335a5..eefd468ac1 100644 --- a/lib/Tooling/CommandLineClangTool.cpp +++ b/lib/Tooling/CommonOptionsParser.cpp @@ -1,4 +1,4 @@ -//===--- CommandLineClangTool.cpp - command-line clang tools driver -------===// +//===--- CommonOptionsParser.cpp - common options for clang tools ---------===// // // The LLVM Compiler Infrastructure // @@ -7,28 +7,31 @@ // //===----------------------------------------------------------------------===// // -// This file implements the CommandLineClangTool class used to run clang -// tools as separate command-line applications with a consistent common -// interface for handling compilation database and input files. +// This file implements the CommonOptionsParser class used to parse common +// command-line options for clang tools, so that they can be run as separate +// command-line applications with a consistent common interface for handling +// compilation database and input files. // // It provides a common subset of command-line options, common algorithm // for locating a compilation database and source files, and help messages // for the basic command-line interface. // -// It creates a CompilationDatabase, initializes a ClangTool and runs a -// user-specified FrontendAction over all TUs in which the given files are -// compiled. +// It creates a CompilationDatabase and reads common command-line options. +// +// This class uses the Clang Tooling infrastructure, see +// http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html +// for details on setting it up with LLVM source tree. // //===----------------------------------------------------------------------===// -#include "clang/Frontend/FrontendActions.h" -#include "clang/Tooling/CommandLineClangTool.h" +#include "llvm/Support/CommandLine.h" +#include "clang/Tooling/CommonOptionsParser.h" #include "clang/Tooling/Tooling.h" using namespace clang::tooling; using namespace llvm; -static const char *MoreHelpText = +const char *const clang::tooling::CommonHelpMessage = "\n" "-p is used to read a compile command database.\n" "\n" @@ -40,26 +43,27 @@ static const char *MoreHelpText = "\thttp://clang.llvm.org/docs/HowToSetupToolingForLLVM.html for an\n" "\texample of setting up Clang Tooling on a source tree.\n" "\n" - " ... specify the paths of source files. These paths are looked\n" - "\tup in the compile command database. If the path of a file is absolute,\n" - "\tit needs to point into CMake's source tree. If the path is relative,\n" - "\tthe current working directory needs to be in the CMake source tree and\n" - "\tthe file must be in a subdirectory of the current working directory.\n" - "\t\"./\" prefixes in the relative files will be automatically removed,\n" - "\tbut the rest of a relative path must be a suffix of a path in the\n" - "\tcompile command database.\n" + " ... specify the paths of source files. These paths are\n" + "\tlooked up in the compile command database. If the path of a file is\n" + "\tabsolute, it needs to point into CMake's source tree. If the path is\n" + "\trelative, the current working directory needs to be in the CMake\n" + "\tsource tree and the file must be in a subdirectory of the current\n" + "\tworking directory. \"./\" prefixes in the relative files will be\n" + "\tautomatically removed, but the rest of a relative path must be a\n" + "\tsuffix of a path in the compile command database.\n" "\n"; -CommandLineClangTool::CommandLineClangTool() : - BuildPath("p", cl::desc("Build path"), cl::Optional), - SourcePaths(cl::Positional, cl::desc(" [... ]"), - cl::OneOrMore), - MoreHelp(MoreHelpText) { -} +static cl::opt BuildPath( + "p", cl::desc("Build path"), cl::Optional); + +static cl::list SourcePaths( + cl::Positional, cl::desc(" [... ]"), cl::OneOrMore); -void CommandLineClangTool::initialize(int argc, const char **argv) { - Compilations.reset(FixedCompilationDatabase::loadFromCommandLine(argc, argv)); +CommonOptionsParser::CommonOptionsParser(int &argc, const char **argv) { + Compilations.reset(FixedCompilationDatabase::loadFromCommandLine(argc, + argv)); cl::ParseCommandLineOptions(argc, argv); + SourcePathList = SourcePaths; if (!Compilations) { std::string ErrorMessage; if (!BuildPath.empty()) { @@ -73,8 +77,3 @@ void CommandLineClangTool::initialize(int argc, const char **argv) { llvm::report_fatal_error(ErrorMessage); } } - -int CommandLineClangTool::run(FrontendActionFactory *ActionFactory) { - ClangTool Tool(*Compilations, SourcePaths); - return Tool.run(ActionFactory); -} diff --git a/tools/clang-check/ClangCheck.cpp b/tools/clang-check/ClangCheck.cpp index 9e58077b5a..c02c23da89 100644 --- a/tools/clang-check/ClangCheck.cpp +++ b/tools/clang-check/ClangCheck.cpp @@ -1,4 +1,4 @@ -//===- tools/clang-check/ClangCheck.cpp - Clang check tool ----------------===// +//===--- tools/clang-check/ClangCheck.cpp - Clang check tool --------------===// // // The LLVM Compiler Infrastructure // @@ -16,20 +16,20 @@ // //===----------------------------------------------------------------------===// -#include "llvm/Support/CommandLine.h" #include "clang/AST/ASTConsumer.h" #include "clang/Driver/OptTable.h" #include "clang/Driver/Options.h" #include "clang/Frontend/ASTConsumers.h" -#include "clang/Frontend/FrontendActions.h" -#include "clang/Tooling/CommandLineClangTool.h" +#include "clang/Tooling/CommonOptionsParser.h" #include "clang/Tooling/Tooling.h" +#include "llvm/Support/CommandLine.h" using namespace clang::driver; using namespace clang::tooling; using namespace llvm; -static const char *MoreHelpText = +static cl::extrahelp CommonHelp(CommonHelpMessage); +static cl::extrahelp MoreHelp( "\tFor example, to run clang-check on all files in a subtree of the\n" "\tsource tree, use:\n" "\n" @@ -41,26 +41,26 @@ static const char *MoreHelpText = "\n" "\tNote, that path/in/subtree and current directory should follow the\n" "\trules described above.\n" - "\n"; + "\n" +); + +static OwningPtr Options(createDriverOptTable()); +static cl::opt ASTDump( + "ast-dump", + cl::desc(Options->getOptionHelpText(options::OPT_ast_dump))); +static cl::opt ASTList( + "ast-list", + cl::desc(Options->getOptionHelpText(options::OPT_ast_list))); +static cl::opt ASTPrint( + "ast-print", + cl::desc(Options->getOptionHelpText(options::OPT_ast_print))); +static cl::opt ASTDumpFilter( + "ast-dump-filter", + cl::desc(Options->getOptionHelpText(options::OPT_ast_dump_filter))); namespace { class ActionFactory { public: - ActionFactory() - : Options(createDriverOptTable()), - ASTDump( - "ast-dump", - cl::desc(Options->getOptionHelpText(options::OPT_ast_dump))), - ASTList( - "ast-list", - cl::desc(Options->getOptionHelpText(options::OPT_ast_list))), - ASTPrint( - "ast-print", - cl::desc(Options->getOptionHelpText(options::OPT_ast_print))), - ASTDumpFilter( - "ast-dump-filter", - cl::desc(Options->getOptionHelpText(options::OPT_ast_dump_filter))) {} - clang::ASTConsumer *newASTConsumer() { if (ASTList) return clang::CreateASTDeclNodeLister(); @@ -70,19 +70,13 @@ public: return clang::CreateASTPrinter(&llvm::outs(), ASTDumpFilter); return new clang::ASTConsumer(); } -private: - OwningPtr Options; - cl::opt ASTDump; - cl::opt ASTList; - cl::opt ASTPrint; - cl::opt ASTDumpFilter; }; } int main(int argc, const char **argv) { ActionFactory Factory; - CommandLineClangTool Tool; - cl::extrahelp MoreHelp(MoreHelpText); - Tool.initialize(argc, argv); + CommonOptionsParser OptionsParser(argc, argv); + ClangTool Tool(OptionsParser.GetCompilations(), + OptionsParser.GetSourcePathList()); return Tool.run(newFrontendActionFactory(&Factory)); } -- 2.40.0