1 //===--- Extract.h - Clang refactoring library ----------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLVM_CLANG_TOOLING_REFACTOR_EXTRACT_EXTRACT_H 10 #define LLVM_CLANG_TOOLING_REFACTOR_EXTRACT_EXTRACT_H 11 12 #include "clang/Tooling/Refactoring/ASTSelection.h" 13 #include "clang/Tooling/Refactoring/RefactoringActionRules.h" 14 15 namespace clang { 16 namespace tooling { 17 18 /// An "Extract Function" refactoring moves code into a new function that's 19 /// then called from the place where the original code was. 20 class ExtractFunction final : public SourceChangeRefactoringRule { 21 public: 22 /// Initiates the extract function refactoring operation. 23 /// 24 /// \param Code The selected set of statements. 25 /// \param DeclName The name name of the extract function. If None, 26 /// "extracted" is used. 27 static Expected<ExtractFunction> initiate(RefactoringRuleContext &Context, 28 CodeRangeASTSelection Code, 29 Optional<std::string> DeclName); 30 31 static const RefactoringDescriptor &describe(); 32 33 private: ExtractFunction(CodeRangeASTSelection Code,Optional<std::string> DeclName)34 ExtractFunction(CodeRangeASTSelection Code, Optional<std::string> DeclName) 35 : Code(std::move(Code)), 36 DeclName(DeclName ? std::move(*DeclName) : "extracted") {} 37 38 Expected<AtomicChanges> 39 createSourceReplacements(RefactoringRuleContext &Context) override; 40 41 CodeRangeASTSelection Code; 42 43 // FIXME: Account for naming collisions: 44 // - error when name is specified by user. 45 // - rename to "extractedN" when name is implicit. 46 std::string DeclName; 47 }; 48 49 } // end namespace tooling 50 } // end namespace clang 51 52 #endif // LLVM_CLANG_TOOLING_REFACTOR_EXTRACT_EXTRACT_H 53