1*0b57cec5SDimitry Andric //===--- Refactoring.h - Framework for clang refactoring tools --*- C++ -*-===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // 9*0b57cec5SDimitry Andric // Interfaces supporting refactorings that span multiple translation units. 10*0b57cec5SDimitry Andric // While single translation unit refactorings are supported via the Rewriter, 11*0b57cec5SDimitry Andric // when refactoring multiple translation units changes must be stored in a 12*0b57cec5SDimitry Andric // SourceManager independent form, duplicate changes need to be removed, and 13*0b57cec5SDimitry Andric // all changes must be applied at once at the end of the refactoring so that 14*0b57cec5SDimitry Andric // the code is always parseable. 15*0b57cec5SDimitry Andric // 16*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 17*0b57cec5SDimitry Andric 18*0b57cec5SDimitry Andric #ifndef LLVM_CLANG_TOOLING_REFACTORING_H 19*0b57cec5SDimitry Andric #define LLVM_CLANG_TOOLING_REFACTORING_H 20*0b57cec5SDimitry Andric 21*0b57cec5SDimitry Andric #include "clang/Tooling/Core/Replacement.h" 22*0b57cec5SDimitry Andric #include "clang/Tooling/Tooling.h" 23*0b57cec5SDimitry Andric #include <map> 24*0b57cec5SDimitry Andric #include <string> 25*0b57cec5SDimitry Andric 26*0b57cec5SDimitry Andric namespace clang { 27*0b57cec5SDimitry Andric 28*0b57cec5SDimitry Andric class Rewriter; 29*0b57cec5SDimitry Andric 30*0b57cec5SDimitry Andric namespace tooling { 31*0b57cec5SDimitry Andric 32*0b57cec5SDimitry Andric /// A tool to run refactorings. 33*0b57cec5SDimitry Andric /// 34*0b57cec5SDimitry Andric /// This is a refactoring specific version of \see ClangTool. FrontendActions 35*0b57cec5SDimitry Andric /// passed to run() and runAndSave() should add replacements to 36*0b57cec5SDimitry Andric /// getReplacements(). 37*0b57cec5SDimitry Andric class RefactoringTool : public ClangTool { 38*0b57cec5SDimitry Andric public: 39*0b57cec5SDimitry Andric /// \see ClangTool::ClangTool. 40*0b57cec5SDimitry Andric RefactoringTool(const CompilationDatabase &Compilations, 41*0b57cec5SDimitry Andric ArrayRef<std::string> SourcePaths, 42*0b57cec5SDimitry Andric std::shared_ptr<PCHContainerOperations> PCHContainerOps = 43*0b57cec5SDimitry Andric std::make_shared<PCHContainerOperations>()); 44*0b57cec5SDimitry Andric 45*0b57cec5SDimitry Andric /// Returns the file path to replacements map to which replacements 46*0b57cec5SDimitry Andric /// should be added during the run of the tool. 47*0b57cec5SDimitry Andric std::map<std::string, Replacements> &getReplacements(); 48*0b57cec5SDimitry Andric 49*0b57cec5SDimitry Andric /// Call run(), apply all generated replacements, and immediately save 50*0b57cec5SDimitry Andric /// the results to disk. 51*0b57cec5SDimitry Andric /// 52*0b57cec5SDimitry Andric /// \returns 0 upon success. Non-zero upon failure. 53*0b57cec5SDimitry Andric int runAndSave(FrontendActionFactory *ActionFactory); 54*0b57cec5SDimitry Andric 55*0b57cec5SDimitry Andric /// Apply all stored replacements to the given Rewriter. 56*0b57cec5SDimitry Andric /// 57*0b57cec5SDimitry Andric /// FileToReplaces will be deduplicated with `groupReplacementsByFile` before 58*0b57cec5SDimitry Andric /// application. 59*0b57cec5SDimitry Andric /// 60*0b57cec5SDimitry Andric /// Replacement applications happen independently of the success of other 61*0b57cec5SDimitry Andric /// applications. 62*0b57cec5SDimitry Andric /// 63*0b57cec5SDimitry Andric /// \returns true if all replacements apply. false otherwise. 64*0b57cec5SDimitry Andric bool applyAllReplacements(Rewriter &Rewrite); 65*0b57cec5SDimitry Andric 66*0b57cec5SDimitry Andric private: 67*0b57cec5SDimitry Andric /// Write all refactored files to disk. 68*0b57cec5SDimitry Andric int saveRewrittenFiles(Rewriter &Rewrite); 69*0b57cec5SDimitry Andric 70*0b57cec5SDimitry Andric private: 71*0b57cec5SDimitry Andric std::map<std::string, Replacements> FileToReplaces; 72*0b57cec5SDimitry Andric }; 73*0b57cec5SDimitry Andric 74*0b57cec5SDimitry Andric /// Groups \p Replaces by the file path and applies each group of 75*0b57cec5SDimitry Andric /// Replacements on the related file in \p Rewriter. In addition to applying 76*0b57cec5SDimitry Andric /// given Replacements, this function also formats the changed code. 77*0b57cec5SDimitry Andric /// 78*0b57cec5SDimitry Andric /// \pre Replacements must be conflict-free. 79*0b57cec5SDimitry Andric /// 80*0b57cec5SDimitry Andric /// FileToReplaces will be deduplicated with `groupReplacementsByFile` before 81*0b57cec5SDimitry Andric /// application. 82*0b57cec5SDimitry Andric /// 83*0b57cec5SDimitry Andric /// Replacement applications happen independently of the success of other 84*0b57cec5SDimitry Andric /// applications. 85*0b57cec5SDimitry Andric /// 86*0b57cec5SDimitry Andric /// \param[in] FileToReplaces Replacements (grouped by files) to apply. 87*0b57cec5SDimitry Andric /// \param[in] Rewrite The `Rewritter` to apply replacements on. 88*0b57cec5SDimitry Andric /// \param[in] Style The style name used for reformatting. See ```getStyle``` in 89*0b57cec5SDimitry Andric /// "include/clang/Format/Format.h" for all possible style forms. 90*0b57cec5SDimitry Andric /// 91*0b57cec5SDimitry Andric /// \returns true if all replacements applied and formatted. false otherwise. 92*0b57cec5SDimitry Andric bool formatAndApplyAllReplacements( 93*0b57cec5SDimitry Andric const std::map<std::string, Replacements> &FileToReplaces, 94*0b57cec5SDimitry Andric Rewriter &Rewrite, StringRef Style = "file"); 95*0b57cec5SDimitry Andric 96*0b57cec5SDimitry Andric } // end namespace tooling 97*0b57cec5SDimitry Andric } // end namespace clang 98*0b57cec5SDimitry Andric 99*0b57cec5SDimitry Andric #endif // LLVM_CLANG_TOOLING_REFACTORING_H 100