From 6a56ac5a130cd2e39f326477db555baad5c84306 Mon Sep 17 00:00:00 2001 From: Alex Lorenz Date: Thu, 14 Sep 2017 13:16:14 +0000 Subject: [PATCH] [refactor] Use CommonOptionsParser in clang-refactor This commit ensures that CommonOptionsParser works with subcommands. This allows clang-refactor to use the CommonOptionsParser. Differential Revision: https://reviews.llvm.org/D37618 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@313260 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Tooling/CommonOptionsParser.cpp | 12 +++--- test/Refactor/LocalRename/Field.cpp | 2 +- test/Refactor/tool-common-options.c | 3 -- test/Refactor/tool-test-support.c | 12 +++--- tools/clang-refactor/ClangRefactor.cpp | 58 ++++++-------------------- 5 files changed, 27 insertions(+), 60 deletions(-) diff --git a/lib/Tooling/CommonOptionsParser.cpp b/lib/Tooling/CommonOptionsParser.cpp index 3ef223fa55..edcb7df533 100644 --- a/lib/Tooling/CommonOptionsParser.cpp +++ b/lib/Tooling/CommonOptionsParser.cpp @@ -84,24 +84,26 @@ std::vector ArgumentsAdjustingCompilations::adjustCommands( CommonOptionsParser::CommonOptionsParser( int &argc, const char **argv, cl::OptionCategory &Category, llvm::cl::NumOccurrencesFlag OccurrencesFlag, const char *Overview) { - static cl::opt Help("h", cl::desc("Alias for -help"), cl::Hidden); + static cl::opt Help("h", cl::desc("Alias for -help"), cl::Hidden, + cl::sub(*cl::AllSubCommands)); static cl::opt BuildPath("p", cl::desc("Build path"), - cl::Optional, cl::cat(Category)); + cl::Optional, cl::cat(Category), + cl::sub(*cl::AllSubCommands)); static cl::list SourcePaths( cl::Positional, cl::desc(" [... ]"), OccurrencesFlag, - cl::cat(Category)); + cl::cat(Category), cl::sub(*cl::AllSubCommands)); static cl::list ArgsAfter( "extra-arg", cl::desc("Additional argument to append to the compiler command line"), - cl::cat(Category)); + cl::cat(Category), cl::sub(*cl::AllSubCommands)); static cl::list ArgsBefore( "extra-arg-before", cl::desc("Additional argument to prepend to the compiler command line"), - cl::cat(Category)); + cl::cat(Category), cl::sub(*cl::AllSubCommands)); cl::HideUnrelatedOptions(Category); diff --git a/test/Refactor/LocalRename/Field.cpp b/test/Refactor/LocalRename/Field.cpp index db8ada5fcc..830e91d200 100644 --- a/test/Refactor/LocalRename/Field.cpp +++ b/test/Refactor/LocalRename/Field.cpp @@ -1,4 +1,4 @@ -// RUN: clang-refactor local-rename -selection=test:%s -no-dbs %s | FileCheck %s +// RUN: clang-refactor local-rename -selection=test:%s %s -- | FileCheck %s class Baz { int /*range=*/Foo; // CHECK: int /*range=*/Bar; diff --git a/test/Refactor/tool-common-options.c b/test/Refactor/tool-common-options.c index e20c290ae7..b41c8c701e 100644 --- a/test/Refactor/tool-common-options.c +++ b/test/Refactor/tool-common-options.c @@ -1,6 +1,3 @@ // RUN: not clang-refactor 2>&1 | FileCheck --check-prefix=MISSING_ACTION %s // MISSING_ACTION: error: no refactoring action given // MISSING_ACTION-NEXT: note: the following actions are supported: - -// RUN: not clang-refactor local-rename -no-dbs 2>&1 | FileCheck --check-prefix=MISSING_SOURCES %s -// MISSING_SOURCES: error: must provide paths to the source files when '-no-dbs' is used diff --git a/test/Refactor/tool-test-support.c b/test/Refactor/tool-test-support.c index 3eb8d22f51..877a075579 100644 --- a/test/Refactor/tool-test-support.c +++ b/test/Refactor/tool-test-support.c @@ -1,4 +1,4 @@ -// RUN: clang-refactor local-rename -selection=test:%s -no-dbs -v %s 2>&1 | FileCheck %s +// RUN: clang-refactor local-rename -selection=test:%s -v %s -- 2>&1 | FileCheck %s /*range=*/int test; @@ -11,12 +11,12 @@ /*range named =+0*/int test5; // CHECK: Test selection group '': -// CHECK-NEXT: 100-100 -// CHECK-NEXT: 153-153 -// CHECK-NEXT: 192-192 +// CHECK-NEXT: 95-95 +// CHECK-NEXT: 148-148 +// CHECK-NEXT: 187-187 // CHECK-NEXT: Test selection group 'named': -// CHECK-NEXT: 127-127 -// CHECK-NEXT: 213-213 +// CHECK-NEXT: 122-122 +// CHECK-NEXT: 208-208 // The following invocations are in the default group: diff --git a/tools/clang-refactor/ClangRefactor.cpp b/tools/clang-refactor/ClangRefactor.cpp index 047e276d1b..ff13773072 100644 --- a/tools/clang-refactor/ClangRefactor.cpp +++ b/tools/clang-refactor/ClangRefactor.cpp @@ -15,6 +15,7 @@ #include "TestSupport.h" #include "clang/Rewrite/Core/Rewriter.h" +#include "clang/Tooling/CommonOptionsParser.h" #include "clang/Tooling/Refactoring.h" #include "clang/Tooling/Refactoring/RefactoringAction.h" #include "clang/Tooling/Refactoring/Rename/RenamingAction.h" @@ -33,12 +34,6 @@ namespace opts { static cl::OptionCategory CommonRefactorOptions("Common refactoring options"); -static cl::opt - NoDatabases("no-dbs", - cl::desc("Ignore external databases including Clang's " - "compilation database and indexer stores"), - cl::cat(CommonRefactorOptions), cl::sub(*cl::AllSubCommands)); - static cl::opt Verbose("v", cl::desc("Use verbose output"), cl::cat(CommonRefactorOptions), cl::sub(*cl::AllSubCommands)); @@ -129,10 +124,6 @@ public: cl::OptionCategory &Category) : SubCommand(Action->getCommand(), Action->getDescription()), Action(std::move(Action)), ActionRules(std::move(ActionRules)) { - Sources = llvm::make_unique>( - cl::Positional, cl::ZeroOrMore, cl::desc(" [... ]"), - cl::cat(Category), cl::sub(*this)); - // Check if the selection option is supported. bool HasSelection = false; for (const auto &Rule : this->ActionRules) { @@ -169,13 +160,9 @@ public: assert(Selection && "selection not supported!"); return ParsedSelection.get(); } - - ArrayRef getSources() const { return *Sources; } - private: std::unique_ptr Action; RefactoringActionRules ActionRules; - std::unique_ptr> Sources; std::unique_ptr> Selection; std::unique_ptr ParsedSelection; }; @@ -221,20 +208,9 @@ public: /// Parses the translation units that were given to the subcommand using /// the 'sources' option and invokes the callback for each parsed /// translation unit. - bool foreachTranslationUnit(RefactoringActionSubcommand &Subcommand, + bool foreachTranslationUnit(const CompilationDatabase &DB, + ArrayRef Sources, TUCallbackType Callback) { - std::unique_ptr Compilations; - if (opts::NoDatabases) { - // FIXME (Alex L): Support compilation options. - Compilations = - llvm::make_unique( - ".", std::vector()); - } else { - // FIXME (Alex L): Support compilation database. - llvm::errs() << "compilation databases are not supported yet!\n"; - return true; - } - class ToolASTConsumer : public ASTConsumer { public: TUCallbackType Callback; @@ -254,7 +230,7 @@ public: } }; - ClangTool Tool(*Compilations, Subcommand.getSources()); + ClangTool Tool(DB, Sources); ActionWrapper ToolAction(std::move(Callback)); std::unique_ptr Factory = tooling::newFrontendActionFactory(&ToolAction); @@ -277,7 +253,9 @@ public: } } - bool invokeAction(RefactoringActionSubcommand &Subcommand) { + bool invokeAction(RefactoringActionSubcommand &Subcommand, + const CompilationDatabase &DB, + ArrayRef Sources) { // Find a set of matching rules. SmallVector MatchingRules; llvm::StringSet<> MissingOptions; @@ -303,7 +281,7 @@ public: bool HasFailed = false; ClangRefactorConsumer Consumer; - if (foreachTranslationUnit(Subcommand, [&](ASTContext &AST) { + if (foreachTranslationUnit(DB, Sources, [&](ASTContext &AST) { RefactoringRuleContext Context(AST.getSourceManager()); Context.setASTContext(AST); @@ -347,12 +325,9 @@ public: int main(int argc, const char **argv) { ClangRefactorTool Tool; - // FIXME: Use LibTooling's CommonOptions parser when subcommands are supported - // by it. - cl::HideUnrelatedOptions(opts::CommonRefactorOptions); - cl::ParseCommandLineOptions( - argc, argv, "Clang-based refactoring tool for C, C++ and Objective-C"); - cl::PrintOptionValues(); + CommonOptionsParser Options( + argc, argv, opts::CommonRefactorOptions, cl::ZeroOrMore, + "Clang-based refactoring tool for C, C++ and Objective-C"); // Figure out which action is specified by the user. The user must specify // the action using a command-line subcommand, e.g. the invocation @@ -374,17 +349,10 @@ int main(int argc, const char **argv) { } RefactoringActionSubcommand &ActionCommand = **It; - ArrayRef Sources = ActionCommand.getSources(); - // When -no-dbs is used, at least one file (TU) must be given to any - // subcommand. - if (opts::NoDatabases && Sources.empty()) { - llvm::errs() << "error: must provide paths to the source files when " - "'-no-dbs' is used\n"; - return 1; - } if (ActionCommand.parseArguments()) return 1; - if (Tool.invokeAction(ActionCommand)) + if (Tool.invokeAction(ActionCommand, Options.getCompilations(), + Options.getSourcePathList())) return 1; return 0; -- 2.40.0