1320535Sdim//===--- RenamingAction.h - Clang refactoring library ---------------------===// 2320535Sdim// 3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4353358Sdim// See https://llvm.org/LICENSE.txt for license information. 5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6320535Sdim// 7320535Sdim//===----------------------------------------------------------------------===// 8320535Sdim/// 9320535Sdim/// \file 10341825Sdim/// Provides an action to rename every symbol at a point. 11320535Sdim/// 12320535Sdim//===----------------------------------------------------------------------===// 13320535Sdim 14320535Sdim#ifndef LLVM_CLANG_TOOLING_REFACTOR_RENAME_RENAMING_ACTION_H 15320535Sdim#define LLVM_CLANG_TOOLING_REFACTOR_RENAME_RENAMING_ACTION_H 16320535Sdim 17320535Sdim#include "clang/Tooling/Refactoring.h" 18327952Sdim#include "clang/Tooling/Refactoring/AtomicChange.h" 19327952Sdim#include "clang/Tooling/Refactoring/RefactoringActionRules.h" 20327952Sdim#include "clang/Tooling/Refactoring/RefactoringOptions.h" 21327952Sdim#include "clang/Tooling/Refactoring/Rename/SymbolOccurrences.h" 22327952Sdim#include "llvm/Support/Error.h" 23320535Sdim 24320535Sdimnamespace clang { 25320535Sdimclass ASTConsumer; 26320535Sdimclass CompilerInstance; 27320535Sdim 28320535Sdimnamespace tooling { 29320535Sdim 30320535Sdimclass RenamingAction { 31320535Sdimpublic: 32320535Sdim RenamingAction(const std::vector<std::string> &NewNames, 33320535Sdim const std::vector<std::string> &PrevNames, 34320535Sdim const std::vector<std::vector<std::string>> &USRList, 35320535Sdim std::map<std::string, tooling::Replacements> &FileToReplaces, 36320535Sdim bool PrintLocations = false) 37320535Sdim : NewNames(NewNames), PrevNames(PrevNames), USRList(USRList), 38320535Sdim FileToReplaces(FileToReplaces), PrintLocations(PrintLocations) {} 39320535Sdim 40320535Sdim std::unique_ptr<ASTConsumer> newASTConsumer(); 41320535Sdim 42320535Sdimprivate: 43320535Sdim const std::vector<std::string> &NewNames, &PrevNames; 44320535Sdim const std::vector<std::vector<std::string>> &USRList; 45320535Sdim std::map<std::string, tooling::Replacements> &FileToReplaces; 46320535Sdim bool PrintLocations; 47320535Sdim}; 48320535Sdim 49327952Sdimclass RenameOccurrences final : public SourceChangeRefactoringRule { 50327952Sdimpublic: 51327952Sdim static Expected<RenameOccurrences> initiate(RefactoringRuleContext &Context, 52327952Sdim SourceRange SelectionRange, 53327952Sdim std::string NewName); 54327952Sdim 55327952Sdim static const RefactoringDescriptor &describe(); 56327952Sdim 57353358Sdim const NamedDecl *getRenameDecl() const; 58353358Sdim 59327952Sdimprivate: 60327952Sdim RenameOccurrences(const NamedDecl *ND, std::string NewName) 61327952Sdim : ND(ND), NewName(std::move(NewName)) {} 62327952Sdim 63327952Sdim Expected<AtomicChanges> 64327952Sdim createSourceReplacements(RefactoringRuleContext &Context) override; 65327952Sdim 66327952Sdim const NamedDecl *ND; 67327952Sdim std::string NewName; 68327952Sdim}; 69327952Sdim 70327952Sdimclass QualifiedRenameRule final : public SourceChangeRefactoringRule { 71327952Sdimpublic: 72327952Sdim static Expected<QualifiedRenameRule> initiate(RefactoringRuleContext &Context, 73327952Sdim std::string OldQualifiedName, 74327952Sdim std::string NewQualifiedName); 75327952Sdim 76327952Sdim static const RefactoringDescriptor &describe(); 77327952Sdim 78327952Sdimprivate: 79327952Sdim QualifiedRenameRule(const NamedDecl *ND, 80327952Sdim std::string NewQualifiedName) 81327952Sdim : ND(ND), NewQualifiedName(std::move(NewQualifiedName)) {} 82327952Sdim 83327952Sdim Expected<AtomicChanges> 84327952Sdim createSourceReplacements(RefactoringRuleContext &Context) override; 85327952Sdim 86341825Sdim // A NamedDecl which identifies the symbol being renamed. 87327952Sdim const NamedDecl *ND; 88327952Sdim // The new qualified name to change the symbol to. 89327952Sdim std::string NewQualifiedName; 90327952Sdim}; 91327952Sdim 92327952Sdim/// Returns source replacements that correspond to the rename of the given 93327952Sdim/// symbol occurrences. 94327952Sdimllvm::Expected<std::vector<AtomicChange>> 95327952SdimcreateRenameReplacements(const SymbolOccurrences &Occurrences, 96327952Sdim const SourceManager &SM, const SymbolName &NewName); 97327952Sdim 98320535Sdim/// Rename all symbols identified by the given USRs. 99320535Sdimclass QualifiedRenamingAction { 100320535Sdimpublic: 101320535Sdim QualifiedRenamingAction( 102320535Sdim const std::vector<std::string> &NewNames, 103320535Sdim const std::vector<std::vector<std::string>> &USRList, 104320535Sdim std::map<std::string, tooling::Replacements> &FileToReplaces) 105320535Sdim : NewNames(NewNames), USRList(USRList), FileToReplaces(FileToReplaces) {} 106320535Sdim 107320535Sdim std::unique_ptr<ASTConsumer> newASTConsumer(); 108320535Sdim 109320535Sdimprivate: 110320535Sdim /// New symbol names. 111320535Sdim const std::vector<std::string> &NewNames; 112320535Sdim 113320535Sdim /// A list of USRs. Each element represents USRs of a symbol being renamed. 114320535Sdim const std::vector<std::vector<std::string>> &USRList; 115320535Sdim 116320535Sdim /// A file path to replacements map. 117320535Sdim std::map<std::string, tooling::Replacements> &FileToReplaces; 118320535Sdim}; 119320535Sdim 120320535Sdim} // end namespace tooling 121320535Sdim} // end namespace clang 122320535Sdim 123320535Sdim#endif // LLVM_CLANG_TOOLING_REFACTOR_RENAME_RENAMING_ACTION_H 124