RenamingAction.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- RenamingAction.h - Clang refactoring library ---------------------===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. ///
  14. /// \file
  15. /// Provides an action to rename every symbol at a point.
  16. ///
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CLANG_TOOLING_REFACTORING_RENAME_RENAMINGACTION_H
  19. #define LLVM_CLANG_TOOLING_REFACTORING_RENAME_RENAMINGACTION_H
  20. #include "clang/Tooling/Refactoring.h"
  21. #include "clang/Tooling/Refactoring/AtomicChange.h"
  22. #include "clang/Tooling/Refactoring/RefactoringActionRules.h"
  23. #include "clang/Tooling/Refactoring/RefactoringOptions.h"
  24. #include "clang/Tooling/Refactoring/Rename/SymbolOccurrences.h"
  25. #include "llvm/Support/Error.h"
  26. namespace clang {
  27. class ASTConsumer;
  28. namespace tooling {
  29. class RenamingAction {
  30. public:
  31. RenamingAction(const std::vector<std::string> &NewNames,
  32. const std::vector<std::string> &PrevNames,
  33. const std::vector<std::vector<std::string>> &USRList,
  34. std::map<std::string, tooling::Replacements> &FileToReplaces,
  35. bool PrintLocations = false)
  36. : NewNames(NewNames), PrevNames(PrevNames), USRList(USRList),
  37. FileToReplaces(FileToReplaces), PrintLocations(PrintLocations) {}
  38. std::unique_ptr<ASTConsumer> newASTConsumer();
  39. private:
  40. const std::vector<std::string> &NewNames, &PrevNames;
  41. const std::vector<std::vector<std::string>> &USRList;
  42. std::map<std::string, tooling::Replacements> &FileToReplaces;
  43. bool PrintLocations;
  44. };
  45. class RenameOccurrences final : public SourceChangeRefactoringRule {
  46. public:
  47. static Expected<RenameOccurrences> initiate(RefactoringRuleContext &Context,
  48. SourceRange SelectionRange,
  49. std::string NewName);
  50. static const RefactoringDescriptor &describe();
  51. const NamedDecl *getRenameDecl() const;
  52. private:
  53. RenameOccurrences(const NamedDecl *ND, std::string NewName)
  54. : ND(ND), NewName(std::move(NewName)) {}
  55. Expected<AtomicChanges>
  56. createSourceReplacements(RefactoringRuleContext &Context) override;
  57. const NamedDecl *ND;
  58. std::string NewName;
  59. };
  60. class QualifiedRenameRule final : public SourceChangeRefactoringRule {
  61. public:
  62. static Expected<QualifiedRenameRule> initiate(RefactoringRuleContext &Context,
  63. std::string OldQualifiedName,
  64. std::string NewQualifiedName);
  65. static const RefactoringDescriptor &describe();
  66. private:
  67. QualifiedRenameRule(const NamedDecl *ND,
  68. std::string NewQualifiedName)
  69. : ND(ND), NewQualifiedName(std::move(NewQualifiedName)) {}
  70. Expected<AtomicChanges>
  71. createSourceReplacements(RefactoringRuleContext &Context) override;
  72. // A NamedDecl which identifies the symbol being renamed.
  73. const NamedDecl *ND;
  74. // The new qualified name to change the symbol to.
  75. std::string NewQualifiedName;
  76. };
  77. /// Returns source replacements that correspond to the rename of the given
  78. /// symbol occurrences.
  79. llvm::Expected<std::vector<AtomicChange>>
  80. createRenameReplacements(const SymbolOccurrences &Occurrences,
  81. const SourceManager &SM, const SymbolName &NewName);
  82. /// Rename all symbols identified by the given USRs.
  83. class QualifiedRenamingAction {
  84. public:
  85. QualifiedRenamingAction(
  86. const std::vector<std::string> &NewNames,
  87. const std::vector<std::vector<std::string>> &USRList,
  88. std::map<std::string, tooling::Replacements> &FileToReplaces)
  89. : NewNames(NewNames), USRList(USRList), FileToReplaces(FileToReplaces) {}
  90. std::unique_ptr<ASTConsumer> newASTConsumer();
  91. private:
  92. /// New symbol names.
  93. const std::vector<std::string> &NewNames;
  94. /// A list of USRs. Each element represents USRs of a symbol being renamed.
  95. const std::vector<std::vector<std::string>> &USRList;
  96. /// A file path to replacements map.
  97. std::map<std::string, tooling::Replacements> &FileToReplaces;
  98. };
  99. } // end namespace tooling
  100. } // end namespace clang
  101. #endif // LLVM_CLANG_TOOLING_REFACTORING_RENAME_RENAMINGACTION_H
  102. #ifdef __GNUC__
  103. #pragma GCC diagnostic pop
  104. #endif