Clang Project

clang_source_code/include/clang/Tooling/Refactoring/Extract/Extract.h
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
15namespace clang {
16namespace 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.
20class ExtractFunction final : public SourceChangeRefactoringRule {
21public:
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
33private:
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